Example #1
0
 def test_chained_lsqfit_p0(self):
     " MultiFitter.chained_lsqfit(...) "
     # sequential fit
     fitter = MultiFitter(models=self.make_models(ncg=1))
     p0 = gv.BufferDict({'a': 0.9991638707908023, 'b': 0.4995927960301173})
     p0list = [
         p0,
         gv.BufferDict(a=0.9991638707908023),
         gv.BufferDict(a=0.9991638707908023)
     ]
     fit1 = fitter.chained_lsqfit(data=self.data, prior=self.prior, p0=p0)
     self.assertTrue(self.agree_ref(fit1.p))
     self.assertEqual(list(fit1.chained_fits.keys()), ['l', 'c1', 'c2'])
     self.assertEqual(fit1.p0, p0list)
     fit1 = fitter.chained_lsqfit(data=self.data,
                                  prior=self.prior,
                                  p0=3 * [p0])
     self.assertTrue(self.agree_ref(fit1.p))
     self.assertEqual(list(fit1.chained_fits.keys()), ['l', 'c1', 'c2'])
     self.assertEqual(fit1.p0, p0list)
     fn = 'test_multifitter.p'
     fit1 = fitter.chained_lsqfit(data=self.data, prior=self.prior, p0=fn)
     fit2 = fitter.chained_lsqfit(data=self.data, prior=self.prior, p0=fn)
     self.assertTrue(self.agree_ref(fit1.p))
     self.assertEqual(list(fit1.chained_fits.keys()), ['l', 'c1', 'c2'])
     self.assertEqual([f.pmean for f in fit1.chained_fits.values()],
                      fit2.p0)
     os.unlink(fn)
Example #2
0
    def test_chained_lsqfit(self):
        " MultiFitter.chained_lsqfit(models=[m1, m2, m3], ...) "
        # sequential fit
        fitter = MultiFitter(models=self.make_models(ncg=1))
        fit1 = fitter.chained_lsqfit(data=self.data, prior=self.prior)
        self.assertEqual(str(fit1.p), "{'a': 0.99929(48),'b': 0.50004(81)}")
        self.assertEqual(list(fit1.chained_fits.keys()), ['l', 'c1', 'c2'])

        # with coarse grain, marginalization and extend, and with fast=False
        prior = gv.BufferDict([
            ('log(a)', gv.log(self.prior['a'])),
            ('b', self.prior['b']),
        ])
        prior['log(aa)'] = prior['log(a)'] + gv.gvar('0(1)') * 1e-6
        fitter = MultiFitter(models=self.make_models(ncg=2), fast=False)
        fit2 = fitter.chained_lsqfit(data=self.data,
                                     prior=prior,
                                     mopt=True,
                                     extend=True)
        self.assertEqual(
            str(fit2.p),
            "{'log(a)': -0.00073(48),'b': 0.50015(82),"
            "'log(aa)': -0.00073(48),'a': 0.99927(48),"
            "'aa': 0.99927(48)}",
        )
Example #3
0
 def test_chained_fit_kargs(self):
     " MultiFitter(models=[m1, dict(...), m2, ...]) "
     models = self.make_models(ncg=1)
     models = [models[2], dict(mopt=True), models[1], models[0]]
     fitter = MultiFitter(models=models, mopt=None)
     fit = fitter.chained_lsqfit(data=self.data,
                                 prior=self.prior,
                                 fast=True,
                                 wavg_all=False)
     self.assertTrue(fit.p['b'] is self.prior['b'])
     self.assertEqual(fitter.mopt, None)
     fitter = MultiFitter(models=fitter.flatten_models(models), mopt=None)
     fit = fitter.chained_lsqfit(data=self.data,
                                 prior=self.prior,
                                 fast=True)
     self.assertTrue(fit.p['b'] is not self.prior['b'])
Example #4
0
    def test_chained_fit_simul(self):
        " MultiFitter(models=[m1, (m2,m3)], ...) "
        models = self.make_models(ncg=1)
        models = [models[0], tuple(models[1:])]
        fitter = MultiFitter(models=models)
        fit3 = fitter.chained_lsqfit(data=self.data, prior=self.prior)
        self.assertTrue(self.agree_ref(fit3.p))
        self.assertEqual(list(fit3.chained_fits.keys()), ['l', '(c1,c2)'])

        # with coarse grain, marginalization and extend
        models = self.make_models(ncg=2)
        models = [models[0], tuple(models[1:])]
        prior = gv.BufferDict([('log(a)', gv.log(self.prior['a'])),
                               ('b', self.prior['b'])])
        fitter = MultiFitter(models=self.make_models(ncg=2))
        fit4 = fitter.chained_lsqfit(data=self.data, prior=prior, mopt=True)
        self.assertTrue(self.agree_ref(fit4.p))
Example #5
0
 def test_dump_chained_lsqfit(self):
     " MultiFitter.chained_lsqfit(...) "
     # sequential fit
     fitter = MultiFitter(models=self.make_models(ncg=1))
     fit1 = gv.loads(
         gv.dumps(fitter.chained_lsqfit(data=self.data, prior=self.prior)))
     self.assertTrue(self.agree_ref(fit1.p))
     self.assertEqual(list(fit1.chained_fits.keys()), ['l', 'c1', 'c2'])
Example #6
0
    def test_chained_lsqfit(self):
        " MultiFitter.chained_lsqfit(...) "
        # sequential fit
        fitter = MultiFitter(models=self.make_models(ncg=1))
        fit1 = fitter.chained_lsqfit(data=self.data, prior=self.prior)
        self.assertTrue(self.agree_ref(fit1.p))
        self.assertEqual(list(fit1.chained_fits.keys()), ['l', 'c1', 'c2'])

        # with coarse grain, marginalization and extend, and with fast=False
        prior = gv.BufferDict([
            ('log(a)', gv.log(self.prior['a'])),
            ('b', self.prior['b']),
        ])
        prior['log(aa)'] = prior['log(a)'] + gv.gvar('0(1)') * 1e-6
        fitter = MultiFitter(models=self.make_models(ncg=2), fast=False)
        fit2 = fitter.chained_lsqfit(data=self.data, prior=prior, mopt=True)
        self.assertTrue(self.agree_ref(fit2.p))
Example #7
0
 def test_bootstrap_chained_lsqfit(self):
     fitter = MultiFitter(models=self.make_models(ncg=1))
     fit = fitter.chained_lsqfit(data=self.data, prior=self.prior)
     datalist = gv.bootstrap_iter(self.data, n=10)
     ds = gv.dataset.Dataset()
     for bf in fit.bootstrapped_fit_iter(datalist=datalist):
         ds.append(bf.pmean)
     p = gv.dataset.avg_data(ds, bstrap=True)
     self.assertTrue(abs(p['a'].mean - 1.) < 5 * p['a'].sdev)
     self.assertTrue(abs(p['b'].mean - 0.5) < 5 * p['b'].sdev)
Example #8
0
    def test_chained_fit_seq_simul(self):
        " MultiFitter.chained_lsqfit(models=[m1, (m2,m3)], ...) "
        models = self.make_models(ncg=1)
        models = [models[0], tuple(models[1:])]
        fitter = MultiFitter(models=models)
        fit3 = fitter.chained_lsqfit(data=self.data, prior=self.prior)
        self.assertEqual(str(fit3.p), "{'a': 0.99931(48),'b': 0.50000(81)}")
        self.assertEqual(list(fit3.chained_fits.keys()), ['l', '(c1,c2)'])

        # with coarse grain, marginalization and extend
        models = self.make_models(ncg=2)
        models = [models[0], tuple(models[1:])]
        prior = gv.BufferDict([('log(a)', gv.log(self.prior['a'])),
                               ('b', self.prior['b'])])
        fitter = MultiFitter(models=self.make_models(ncg=2))
        fit4 = fitter.chained_lsqfit(data=self.data,
                                     prior=prior,
                                     mopt=True,
                                     extend=True)
        self.assertEqual(str(fit4.p),
                         "{'log(a)': -0.00073(48),'a': 0.99927(48)}")
Example #9
0
    def test_chained_fit_seq_parallel(self):
        " MultiFitter.chained_lsqfit(models=[m1, [m2,m3]], ...) "
        models = self.make_models(ncg=1)
        models = [models[0], models[1:]]
        fitter = MultiFitter(models=models)
        fit5 = fitter.chained_lsqfit(data=self.data, prior=self.prior)
        self.assertEqual(str(fit5.p), "{'a': 0.99932(48),'b': 0.49998(81)}")
        self.assertEqual(list(fit5.chained_fits.keys()), ['l', '[c1,c2]'])
        self.assertEqual(list(fit5.chained_fits['[c1,c2]'].sub_fits.keys()),
                         ['c1', 'c2'])

        # with coarse grain, marginalization and extend
        models = self.make_models(ncg=2)
        models = [models[0], models[1:]]
        prior = gv.BufferDict([('log(a)', gv.log(self.prior['a'])),
                               ('b', self.prior['b'])])
        fitter = MultiFitter(models=self.make_models(ncg=2))
        fit6 = fitter.chained_lsqfit(data=self.data,
                                     prior=prior,
                                     mopt=True,
                                     extend=True)
        self.assertEqual(str(fit6.p),
                         "{'log(a)': -0.00073(48),'a': 0.99927(48)}")
Example #10
0
    def test_chained_fit_parallel(self):
        " MultiFitter(models=[m1, [m2,m3]], ...) "
        models = self.make_models(ncg=1)
        models = [models[0], models[1:]]
        fitter = MultiFitter(models=models)
        fit5 = fitter.chained_lsqfit(data=self.data, prior=self.prior)
        self.assertTrue(self.agree_ref(fit5.p))
        self.assertEqual(list(fit5.chained_fits.keys()),
                         ['l', 'c1', 'c2', 'wavg(c1,c2)'])

        # degenerate parallel fit (nfit=1)
        models = self.make_models(ncg=1)
        models = [[models[0]], models[1:]]
        fitter = MultiFitter(models=models)
        fit5 = fitter.chained_lsqfit(data=self.data, prior=self.prior)
        self.assertTrue(self.agree_ref(fit5.p))
        self.assertEqual(list(fit5.chained_fits.keys()),
                         ['l', 'c1', 'c2', 'wavg(c1,c2)'])

        # dictionaries in parallel fits
        models = self.make_models(ncg=1)
        models = [[dict(svdcut=1e-12), models[0]],
                  [dict(svdcut=1e-12)] + models[1:]]
        fitter = MultiFitter(models=models)
        fit5 = fitter.chained_lsqfit(data=self.data, prior=self.prior)
        self.assertTrue(self.agree_ref(fit5.p))
        self.assertEqual(list(fit5.chained_fits.keys()),
                         ['l', 'c1', 'c2', 'wavg(c1,c2)'])

        # with coarse grain, marginalization
        models = self.make_models(ncg=2)
        models = [models[0], models[1:]]
        prior = gv.BufferDict([('log(a)', gv.log(self.prior['a'])),
                               ('b', self.prior['b'])])
        fitter = MultiFitter(models=self.make_models(ncg=2))
        fit6 = fitter.chained_lsqfit(data=self.data, prior=prior, mopt=True)
        self.assertTrue(self.agree_ref(fit6.p))