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)}", )
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)
def test_buildprior_fast(self): " MultiFitter.buildprior with fast=False " prior = gv.BufferDict(self.prior) prior['dummy'] = gv.gvar('12(12)') fitter = MultiFitter(models=self.make_models(ncg=1), fast=False) newprior = fitter.buildprior(prior=prior) self.assertEqual(str(prior), str(newprior))
def test_buildprior(self): " MultiFitter.buildprior " prior = gv.BufferDict(self.prior) prior['dummy'] = gv.gvar('12(12)') fitter = MultiFitter(models=self.make_models(ncg=1)) prior = fitter.buildprior(prior=prior) self.assertEqual(str(prior), str(self.prior))
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'])
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_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_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_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'])
def test_buildprior_marginalized(self): " MultiFitter.buildprior with marginalization" prior = gv.BufferDict(self.prior) prior['dummy'] = gv.gvar('12(12)') fitter = MultiFitter(models=self.make_models(ncg=1), mopt=True) prior = fitter.buildprior(prior=prior, mopt=fitter.mopt) del self.prior['b'] self.assertEqual(str(prior), str(self.prior))
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))
def test_builddata_pdata(self): " MultiFitter.builddata(pdata=..., ...) " # ncg=1 fitter = MultiFitter(models=self.make_models(ncg=1)) data = fitter.builddata(data=self.data, mopt=fitter.mopt) data['extra'] = np.array([1.]) ndata = fitter.builddata(pdata=data, mopt=fitter.mopt) del data['extra'] self.assertEqual(str(data), str(ndata)) # ncg=2 fitter = MultiFitter(models=self.make_models(ncg=2)) data = fitter.builddata(data=self.data, mopt=fitter.mopt) data['extra'] = np.array([1.]) ndata = fitter.builddata(pdata=data, mopt=fitter.mopt) del data['extra'] self.assertEqual(str(data), str(ndata))
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))
def test_builddata(self): " MultiFitter.builddata " # ncg=1 fitter = MultiFitter(models=self.make_models(ncg=1)) data = fitter.builddata(data=self.data, mopt=fitter.mopt) del self.data['dummy'] self.assertEqual(str(data), str(self.data)) # ncg=2 simpledata = gv.BufferDict() simpledata['l'] = ['1.0(3)', '2.0(4)', '3.0(3)', '4.0(4)'] simpledata['c1'] = ['1.0(3)', '2.0(4)', '3.0(3)', '4.0(4)'] simpledata['c2'] = ['1.0(3)', '2.0(4)', '3.0(3)', '4.0(4)'] simpledata = gv.gvar(simpledata) fitter = MultiFitter(models=self.make_models(ncg=2)) data = fitter.builddata(data=simpledata, mopt=fitter.mopt) for k in simpledata: self.assertEqual(str(data[k]), '[1.50(25) 3.50(25)]')
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_builddata_marginalized(self): " MultiFitter.builddata with marginalization " fitter = MultiFitter(models=self.make_models(ncg=1), mopt=True) data = fitter.builddata(self.data, prior=self.prior) self.assertEqual( str(self.data['l'] - self.prior['b'] * self.x), str(data['l']), ) self.assertEqual(str(self.data['c1']), str(data['c1'])) self.assertEqual(str(self.data['c2']), str(data['c2']))
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)
def test_empbayes_fit(self): fitter = MultiFitter(models=self.make_models(ncg=1)) def fitargs(z): prior = dict(a=gv.gvar(1.0, z), b=gv.gvar(0.5, z)) return dict(prior=prior, data=self.data, chained=False) fit, z = fitter.empbayes_fit(0.1, fitargs, step=0.01) self.assertNotAlmostEqual(z, 0.1) self.assertGreater(fit.logGBF, self.ref_fit.logGBF)
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)}")
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)}")
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))
def test_set(self): " fitter.set(...) " keys = [ 'fast', 'mopt', 'ratio', 'wavg_kargs', 'wavg_all', 'fitterargs', 'fitname' ] def collect_args(f): return {k: getattr(f, k) for k in keys} # 0 fitter = MultiFitter(models=self.make_models(ncg=1), fast=True, mopt=True, alg='dogleg', ratio=True) args0 = collect_args(fitter) # 1 kargs, oldkargs = fitter.set(fast=False, mopt=False, alg='lm', maxit=10) args1 = collect_args(fitter) self.assertEqual(args1, kargs) self.assertEqual(set(kargs.keys()), set(keys)) self.assertEqual( oldkargs, dict(fast=True, mopt=True, fitterargs=dict(alg='dogleg'))) self.assertEqual(args1['fitterargs'], dict(alg='lm', maxit=10)) # 2 fitter.set(**oldkargs) args2 = collect_args(fitter) self.assertEqual(args0, args2) # 3 kargs, oldkargs = fitter.set(fitterargs=dict(alg='subspace2D'), maxit=100) args3 = collect_args(fitter) self.assertEqual(kargs, args3) self.assertEqual(set(kargs.keys()), set(keys)) self.assertEqual(oldkargs, dict(fitterargs=dict(alg='dogleg'))) self.assertEqual(args3['fitterargs'], dict(alg='subspace2D', maxit=100)) # 4 fitter.set(**oldkargs) args4 = collect_args(fitter) self.assertEqual(args0, args4)
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(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_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))