def test_chained_lsqfit_keyword(self): " MultiFitter.chained_lsqfit(...) " # sequential fit fitter = MultiFitter(models=self.make_models(ncg=1)) fit1 = fitter.lsqfit(data=self.data, prior=self.prior, chained=True) self.assertTrue(self.agree_ref(fit1.p)) self.assertEqual(list(fit1.chained_fits.keys()), ['l', 'c1', 'c2'])
def test_bootstrap_lsqfit(self): fitter = MultiFitter(models=self.make_models(ncg=1)) fit = fitter.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) self.assertEqual(ds.samplesize, 10) pdatalist = (fitter.process_data(d, fitter.models) for d in gv.bootstrap_iter(self.data, n=10)) ds = gv.dataset.Dataset() for bf in fit.bootstrapped_fit_iter(pdatalist=pdatalist): 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) self.assertEqual(ds.samplesize, 10) ds = gv.dataset.Dataset() for bf in fit.bootstrapped_fit_iter(n=10): 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) self.assertEqual(ds.samplesize, 10)
def test_marginalization(self): " MultiFitter.lsqfit(..., mopt=...) " fitter = MultiFitter(models=self.make_models(ncg=1)) fit4 = fitter.lsqfit(data=self.data, prior=self.prior, mopt=True) self.assertEqual(str(fit4.p['a']), str(self.ref_fit.p['a'])) self.assertEqual(gv.fmt_chi2(fit4), gv.fmt_chi2(self.ref_fit)) self.assertTrue('b' not in fit4.p)
def test_fast(self): " MultiFitter.lsqfit(..., fast=False) " # with fast=False self.prior['aa'] = self.prior['a'] + gv.gvar('0(1)') * 1e-6 fitter = MultiFitter(models=self.make_models(ncg=1)) fit6 = fitter.lsqfit(data=self.data, prior=self.prior, fast=False) self.assertTrue(self.agree_ref(fit6.p)) self.assertEqual((fit6.p['a'] / fit6.p['aa']).fmt(ndecimal=5), '1.00000(0)') # with fast=True (default) self.prior['aa'] = self.prior['a'] + gv.gvar('0(1)') * 1e-6 fitter = MultiFitter(models=self.make_models(ncg=1)) fit7 = fitter.lsqfit(data=self.data, prior=self.prior) self.assertTrue(self.agree_ref(fit7.p)) self.assertTrue('aa' not in fit7.p)
def test_dump_lsqfit(self): " MultiFitter.lsqfit " fitter = MultiFitter(models=self.make_models(ncg=1)) dfit = gv.loads( gv.dumps(fitter.lsqfit(data=self.data, prior=self.prior))) self.assertEqual( self.ref_fit.format(True)[:-2], dfit.format(True)[:-2])
def test_extend(self): " MultiFitter.lsqfit(..., extend=True) " fitter = MultiFitter(models=self.make_models(ncg=1)) prior = gv.BufferDict([('log(a)', gv.log(self.prior['a'])), ('b', self.prior['b'])]) fit5 = fitter.lsqfit(data=self.data, prior=prior, extend=True) self.assertEqual(str(fit5.p['a']), str(self.ref_fit.p['a'])) self.assertEqual(gv.fmt_chi2(fit5), gv.fmt_chi2(self.ref_fit)) self.assertTrue('log(a)' in fit5.p)
def test_extend(self): " MultiFitter.lsqfit(...) " fitter = MultiFitter(models=self.make_models(ncg=1)) prior = gv.BufferDict([('log(a)', gv.log(self.prior['a'])), ('b', self.prior['b'])]) fit5 = fitter.lsqfit(data=self.data, prior=prior) self.assertTrue(self.agree_ref(fit5.p)) self.assertTrue( abs(fit5.chi2 - self.ref_fit.chi2) / 0.1 / self.ref_fit.chi2) self.assertTrue('log(a)' in fit5.p)
def test_fast(self): " MultiFitter.lsqfit(..., fast=False) " # with fast=False self.prior['aa'] = self.prior['a'] + gv.gvar('0(1)') * 1e-6 fitter = MultiFitter(models=self.make_models(ncg=1)) fit6 = fitter.lsqfit(data=self.data, prior=self.prior, fast=False) self.assertEqual( str(fit6.p), "{'a': 0.99938(46),'b': 0.49985(78),'aa': 0.99938(46)}", ) # with fast=True (default) self.prior['aa'] = self.prior['a'] + gv.gvar('0(1)') * 1e-6 fitter = MultiFitter(models=self.make_models(ncg=1)) fit7 = fitter.lsqfit(data=self.data, prior=self.prior) self.assertEqual( str(fit7.p), "{'a': 0.99938(46),'b': 0.49985(78)}", )
def test_lsqfit_p0(self): " MultiFitter.lsqfit with p0 " fitter = MultiFitter(models=self.make_models(ncg=1)) p0 = {'a': 0.9992476083689589, 'b': 0.4996757090188109} fit2 = fitter.lsqfit(data=self.data, prior=self.prior, p0=p0) self.assertEqual(self.ref_fit.format()[:-22], fit2.format()[:-22]) self.assertEqual(p0, fit2.p0) fit2 = fitter.lsqfit(data=self.data, prior=self.prior, p0=3 * [p0]) self.assertEqual(self.ref_fit.format()[:-20], fit2.format()[:-20]) self.assertEqual(p0, fit2.p0) fn = 'test_multifitter.p' fit1 = fitter.lsqfit(data=self.data, prior=self.prior, p0=fn) fit1 = fitter.lsqfit(data=self.data, prior=self.prior, p0=fn) # should be converged fit2 = fitter.lsqfit(data=self.data, prior=self.prior, p0=fn) self.assertTrue(self.agree_ref(fit2.p)) self.assertEqual(fit1.format()[:-70], fit2.format()[:len(fit1.format()) - 70]) self.assertEqual(fit1.pmean, fit2.p0) os.unlink(fn)
def test_lsqfit_pdata_coarse_grain(self): " MultiFitter.lsqfit(pdata=..., ..., ncg=2) " fitter = MultiFitter(models=self.make_models(ncg=2)) pdata = MultiFitter.process_data(data=self.data, models=fitter.models) fit3 = fitter.lsqfit(pdata=pdata, prior=self.prior) self.assertTrue(self.agree_ref(fit3.p))
def test_lsqfit_coarse_grain(self): " MultiFitter.lsqfit(..., ncg=2) " fitter = MultiFitter(models=self.make_models(ncg=2)) fit3 = fitter.lsqfit(data=self.data, prior=self.prior) self.assertTrue(self.agree_ref(fit3.p))
def test_lsqfit(self): " MultiFitter.lsqfit " fitter = MultiFitter(models=self.make_models(ncg=1)) fit2 = fitter.lsqfit(data=self.data, prior=self.prior) self.assertEqual(self.ref_fit.format(), fit2.format())
def test_lsqfit_pdata_coarse_grain(self): " MultiFitter.lsqfit(pdata=..., ..., ncg=2) " fitter = MultiFitter(models=self.make_models(ncg=2)) pdata = MultiFitter.process_data(data=self.data, models=fitter.models) fit3 = fitter.lsqfit(pdata=pdata, prior=self.prior) self.assertEqual(str(fit3.p), "{'a': 0.99937(46),'b': 0.49994(78)}")