def test_dataset_bootstrap_iter(self): """ bootstrap_iter(data_dict) """ # make data N = 100 a0 = dict(n=gvar(1,1),a=[gvar(2,2),gvar(100,100)]) dset = Dataset() for ai in raniter(a0,30): dset.append(ai) a = avg_data(dset) # do bootstrap -- calculate means bs_mean = Dataset() for ai in bootstrap_iter(dset,N): for k in ai: bs_mean.append(k,np.average(ai[k],axis=0)) for x in ai[k]: self.assertTrue( # x in numpy.asarray(dset[k]), "Bootstrap element not in original dataset.") a_bs = avg_data(bs_mean,bstrap=True) # 6 sigma tests an_mean = a['n'].mean an_sdev = a['n'].sdev self.assertGreater(6*an_sdev/N**0.5,abs(an_mean-a_bs['n'].mean)) self.assertGreater(6*an_sdev/N**0.5,abs(an_sdev-a_bs['n'].sdev))
def test_dataset_bootstrap_iter(self): """ bootstrap_iter(data_dict) """ # make data N = 100 a0 = dict(n=gvar(1, 1), a=[gvar(2, 2), gvar(100, 100)]) dset = Dataset() for ai in raniter(a0, 30): dset.append(ai) a = avg_data(dset) # do bootstrap -- calculate means bs_mean = Dataset() for ai in bootstrap_iter(dset, N): for k in ai: bs_mean.append(k, np.average(ai[k], axis=0)) for x in ai[k]: self.assertTrue( # x in numpy.asarray(dset[k]), "Bootstrap element not in original dataset.") a_bs = avg_data(bs_mean, bstrap=True) # 6 sigma tests an_mean = a['n'].mean an_sdev = a['n'].sdev self.assertGreater(6 * an_sdev / N**0.5, abs(an_mean - a_bs['n'].mean)) self.assertGreater(6 * an_sdev / N**0.5, abs(an_sdev - a_bs['n'].sdev))
def test_pade_gvar(self): " pade_gvar(tayl, m, n) and Pade(tayl, order=(m,n))" optprint('\n=========== Test pade_gvar') e_exp = [ 1.0, 1.0, 1.0 / 2.0, 1.0 / 6.0, 1.0 / 24.0, 1.0 / 120.0, 1.0 / 720. ] def _scipy_pade(m, n): return Pade._scipy_pade(e_exp[:m + n + 1], n) def print_result(p, q): optprint('num =', p) optprint('den =', q) def test_result(p, q, e_exp): m = len(p) - 1 n = len(q) - 1 # test against scipy p0, q0 = _scipy_pade(m, n) try: assert numpy.allclose(mean(p), p0) except: print(m, n, p0, p, q0, q) assert numpy.allclose(mean(q), q0) # test that errors correlate with input coefficients num = powerseries.PowerSeries(p, order=m + n) den = powerseries.PowerSeries(q, order=m + n) ratio = (num / den).c / e_exp[:m + n + 1] assert numpy.allclose(mean(ratio), 1.) assert numpy.allclose(sdev(ratio), 0.0) # print('scipy', _scipy_pade(1,1), pade_svd(e_exp, 3,2, rtol=0.01)) # 1% noise --- automatically reduces to (2,1) e_exp_noise = [x * gvar('1.0(1)') for x in e_exp] p, q = pade_gvar(e_exp_noise, 3, 2) print_result(p, q) self.assertEqual(len(p), 3) self.assertEqual(len(q), 2) test_result(p, q, e_exp_noise) # 30% noise --- automatically reduces to (1,1) e_exp_noise = [x * gvar('1.0(3)') for x in e_exp] p, q = pade_gvar(e_exp_noise, 3, 2) self.assertEqual(len(p), 2) self.assertEqual(len(q), 2) test_result(p, q, e_exp_noise) # 30% noise, rtol=None --- no reduction e_exp_noise = [x * gvar('1.0(3)') for x in e_exp] p, q = pade_gvar(e_exp_noise, 3, 2, rtol=None) self.assertEqual(len(p), 4) self.assertEqual(len(q), 3) test_result(p, q, e_exp_noise)
def test_integral_gvar(self): def f(x, a=gvar(1, 1)): return a * 3 * x**2 ans = ode.integral(f, (1, 2), tol=1e-10) self.assertAlmostEqual(ans.mean, 7) self.assertAlmostEqual(ans.sdev, 7) a = gvar(1, 1) def f(x, a=a): return a * numpy.array([[1.], [2 * x], [3 * x**2]]) ans = ode.integral(f, (1, 2)) self.assert_arraysclose(mean(ans), [[1], [3], [7]]) self.assert_arraysclose(sdev(ans), [[1], [3], [7]]) def f(x, a=a): return dict(a=a, b=[2 * a * x, 3 * a * x**2]) ans = ode.integral(f, (1, 2)) self.assertAlmostEqual(ans['a'].mean, 1.) self.assert_arraysclose(mean(ans['b']), [3, 7]) self.assertAlmostEqual(ans['a'].sdev, 1.) self.assert_arraysclose(sdev(ans['b']), [3, 7]) self.assertTrue( gv.equivalent(ans, dict(a=a, b=[3 * a, 7 * a]), rtol=1e-6, atol=1e-6))
def test_autocorr(self): """ dataset.autocorr """ N = 10000 eps = 10. / float(N)**0.5 x = gvar(2, 0.1) a = np.array([x() for i in range(N)]) a = (a[:-2] + a[1:-1] + a[2:]) / 3. ac_ex = np.zeros(a.shape, float) ac_ex[:3] = np.array([1., 0.66667, 0.33333]) ac_a = autocorr(a) self.assertLess(numpy.std(ac_a - ac_ex) * 2, eps) b = np.array([[x(), x()] for i in range(N)]) b = (b[:-2] + b[1:-1] + b[2:]) / 3. ac_ex = np.array(list(zip(ac_ex, ac_ex))) ac_b = autocorr(b) self.assertLess(numpy.std(ac_b - ac_ex), eps) c = dict(a=a, b=b) ac_c = autocorr(c) self.assert_arraysequal(ac_c['a'], ac_a) self.assert_arraysequal(ac_c['b'], ac_b)
def test_gvar(self): x = gvar(['0(1)', '1(1)', '3(1)']) x0 = x[0] y = self.f(x) yp= self.Df(x) s = cspline.CSpline(x, y, deriv=[yp[0], yp[-1]], warn=False) for xi in x: self.assert_gvclose(self.f(xi), s(xi)) self.assert_gvclose(self.Df(xi), s.D(xi)) self.assert_gvclose(self.integf(xi, x0), s.integ(xi)) x = np.arange(0.4, 3., 0.4) for xi in x: self.assertAlmostEqual(self.f(xi), mean(s(xi))) self.assertGreater(1e-9, sdev(s(xi))) self.assertAlmostEqual(self.Df(xi), mean(s.D(xi))) self.assertGreater(1e-9, sdev(s.D(xi))) self.assertAlmostEqual(self.D2f(xi), mean(s.D2(xi))) self.assertGreater(1e-9, sdev(s.D2(xi))) self.assertAlmostEqual(mean(self.integf(xi, x0.mean)), mean(s.integ(xi))) self.assert_gvclose(s.integ(xi), self.integf(xi, x0))
def test_autocorr(self): """ dataset.autocorr """ N = 10000 eps = 10./float(N)**0.5 x = gvar(2,0.1) a = np.array([x() for i in range(N)]) a = (a[:-2]+a[1:-1]+a[2:])/3. ac_ex = np.zeros(a.shape,float) ac_ex[:3] = np.array([1.,0.66667,0.33333]) ac_a = autocorr(a) self.assertLess(numpy.std(ac_a-ac_ex)*2,eps) b = np.array([[x(),x()] for i in range(N)]) b = (b[:-2]+b[1:-1]+b[2:])/3. ac_ex = np.array(list(zip(ac_ex,ac_ex))) ac_b = autocorr(b) self.assertLess(numpy.std(ac_b-ac_ex),eps) c = dict(a=a,b=b) ac_c = autocorr(c) self.assert_arraysequal(ac_c['a'],ac_a) self.assert_arraysequal(ac_c['b'],ac_b)
def test_integral_gvar(self): def f(x, a=gvar(1,1)): return a * 3 * x**2 ans = ode.integral(f, (1,2), tol=1e-10) self.assertAlmostEqual(ans.mean, 7) self.assertAlmostEqual(ans.sdev, 7) a = gvar(1, 1) def f(x, a=a): return a * numpy.array([[1.], [2 * x], [3 * x**2]]) ans = ode.integral(f, (1, 2)) self.assert_arraysclose(mean(ans), [[1], [3], [7]]) self.assert_arraysclose(sdev(ans), [[1], [3], [7]]) def f(x, a=a): return dict(a=a, b=[2 * a * x, 3 * a * x**2]) ans = ode.integral(f, (1, 2)) self.assertAlmostEqual(ans['a'].mean, 1.) self.assert_arraysclose(mean(ans['b']), [3, 7]) self.assertAlmostEqual(ans['a'].sdev, 1.) self.assert_arraysclose(sdev(ans['b']), [3, 7]) self.assertTrue(gv.equivalent( ans, dict(a=a, b=[3 * a, 7 * a]), rtol=1e-6, atol=1e-6) )
def test_avg_data(self): """ avg_data """ self.assertTrue(avg_data([]) is None) self.assertEqual(avg_data(dict()), BufferDict()) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") avg_data(dict(s=[1.], v=[1., 2.])) self.assertEqual(len(w), 1) with self.assertRaises(ValueError): avg_data(dict(s=[], v=[1., 2.]), warn=False) with self.assertRaises(ValueError): avg_data(dict(s=[], v=[])) with self.assertRaises(ValueError): avg_data([1, 2, "s"]) mean = avg_data([1]) self.assertEqual(mean.mean, 1.) self.assertEqual(mean.sdev, 0.) # mean = avg_data([1, 2]) self.assertAlmostEqual(mean.mean, 1.5) self.assertAlmostEqual(mean.var, sum((vi - 1.5)**2 for vi in [1, 2]) / 4.) mean2 = avg_data(np.array([1., 2.])) self.assertEqual(mean.mean, mean2.mean) self.assertEqual(mean.sdev, mean2.sdev) # mean = avg_data([1, 2], spread=True) self.assertAlmostEqual(mean.mean, 1.5) self.assertAlmostEqual(mean.var, sum((vi - 1.5)**2 for vi in [1, 2]) / 2.) # mean = avg_data([1, 2], median=True) self.assertAlmostEqual(mean.mean, 1.5) self.assertAlmostEqual(mean.var, 0.5**2 / 2.) # mean = avg_data([1, 2], median=True, spread=True) self.assertAlmostEqual(mean.mean, 1.5) self.assertAlmostEqual(mean.var, 0.5**2) # mean = avg_data([1, 2, 3]) self.assertAlmostEqual(mean.mean, 2.0) self.assertAlmostEqual(mean.var, sum((vi - 2.)**2 for vi in [1, 2, 3]) / 9.) # mean = avg_data([1, 2, 3], noerror=True) self.assertAlmostEqual(mean, 2.0) # mean = avg_data([[1], [2], [3]]) self.assertAlmostEqual(mean[0].mean, 2.0) self.assertAlmostEqual(mean[0].var, sum((vi - 2.)**2 for vi in [1, 2, 3]) / 9.) mean = avg_data([[1], [2], [3]], noerror=True) self.assertAlmostEqual(mean[0], 2.0) mean = avg_data([1, 2, 3], spread=True) self.assertAlmostEqual(mean.mean, 2.0) self.assertAlmostEqual(mean.var, sum((vi - 2.)**2 for vi in [1, 2, 3]) / 3.) # mean = avg_data([1, 2, 3], median=True) self.assertAlmostEqual(mean.mean, 2.0) self.assertAlmostEqual(mean.var, 1. / 3.) # mean = avg_data([[1], [2], [3]], median=True) self.assertAlmostEqual(mean[0].mean, 2.0) self.assertAlmostEqual(mean[0].var, 1. / 3.) # mean = avg_data([1, 2, 3], median=True, spread=True) self.assertAlmostEqual(mean.mean, 2.0) self.assertAlmostEqual(mean.var, 1.) # mean = avg_data([1, 2, 3, 4, 5, 6, 7, 8, 9], median=True) self.assertAlmostEqual(mean.mean, 5) self.assertAlmostEqual(mean.var, 3.**2 / 9.) # mean = avg_data([1, 2, 3, 4, 5, 6, 7, 8, 9], median=True, spread=True) self.assertAlmostEqual(mean.mean, 5.) self.assertAlmostEqual(mean.var, 3.**2) # mean = avg_data([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], median=True) self.assertAlmostEqual(mean.mean, 5.5) self.assertAlmostEqual(mean.var, 3.5**2 / 10.) # mean = avg_data([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], median=True, spread=True) self.assertAlmostEqual(mean.mean, 5.5) self.assertAlmostEqual(mean.var, 3.5**2) # data = dict(s=[1, 2, 3], v=[[1, 1], [2, 2], [3, 3]]) mean = avg_data(data, median=True, spread=True) self.assertAlmostEqual(mean['s'].mean, 2.0) self.assertAlmostEqual(mean['s'].var, 1.0) self.assertEqual(mean['v'].shape, (2, )) self.assert_gvclose(mean['v'], [gvar(2, 1), gvar(2, 1)]) mean = avg_data(data, median=True, noerror=True) self.assertAlmostEqual(mean['s'], 2.0) self.assertEqual(mean['v'].shape, (2, )) self.assert_arraysclose(mean['v'], [2, 2]) mean = avg_data(data, noerror=True) self.assertAlmostEqual(mean['s'], 2.0) self.assertEqual(mean['v'].shape, (2, )) self.assert_arraysclose(mean['v'], [2, 2])
""" eg5.py - Code for "y has No Error Bars" and "SVD Cuts and Roundoff Error" Created by Peter Lepage on 2013-02-12. Copyright (c) 2013 G. Peter Lepage. All rights reserved. """ from __future__ import print_function # makes this work for python2 and 3 from gvar import * from lsqfit import nonlinear_fit import functools import inspect import sys ranseed([123]) ygen = gvar(0.02, 0.2) - 0.005 print (ygen) y = [gvar(ygen(), ygen.sdev) for i in range(20)] ystr = [yi.fmt(2) for yi in y] y = gvar(ystr) print (ystr[:]) print log_prior = BufferDict(loga = log(gvar(0.02, 0.02))) sqrt_prior = BufferDict(sqrta = sqrt(gvar(0.02, 0.02))) prior = BufferDict(a = gvar(0.02, 0.02)) stdout = sys.stdout for p in [prior, log_prior, sqrt_prior]: key = list(p.keys())[0]
from __future__ import print_function # makes this work for python2 and 3 from gvar import * from lsqfit import nonlinear_fit import lsqfit import functools import inspect import sys import vegas from outputsplitter import log_stdout DO_PLOT = True if False: ranseed(12345) ygen = gvar(0.015, 0.2) print(ygen) # y = [gi for gi in bootstrap_iter(ygen, 20)] y = [gvar(ygen(), ygen.sdev) for i in range(16000)] ystr = [yi.fmt(2) for yi in y] y = gvar(fmt(y, 2)) else: y = gvar([ '-0.17(20)', '-0.03(20)', '-0.39(20)', '0.10(20)', '-0.03(20)', '0.06(20)', '-0.23(20)', '-0.23(20)', '-0.15(20)', '-0.01(20)', '-0.12(20)', '0.05(20)', '-0.09(20)', '-0.36(20)', '0.09(20)', '-0.07(20)', '-0.31(20)', '0.12(20)', '0.11(20)', '0.13(20)' ]) # print (y)
def make_random(self, a, g='1.0(1)'): a = numpy.asarray(a) ans = numpy.empty(a.shape, object) for i in numpy.ndindex(a.shape): ans[i] = a[i] * gvar(g) return ans
def f(x, a=gvar(1, 1)): return a * 3 * x**2
def f(x, a=gvar(1,1)): return a * 3 * x**2
""" eg5.py - Code for "y has No Error Bars" and "SVD Cuts and Roundoff Error" Created by Peter Lepage on 2013-02-12. Copyright (c) 2013 G. Peter Lepage. All rights reserved. """ from __future__ import print_function # makes this work for python2 and 3 from gvar import * from lsqfit import nonlinear_fit import functools import inspect import sys ranseed([123]) ygen = gvar(0.02, 0.2) - 0.005 print (ygen) y = [gvar(ygen(), ygen.sdev) for i in range(20)] ystr = [yi.fmt(2) for yi in y] y = gvar(ystr) print (ystr[:]) print log_prior = BufferDict() log_prior['log(a)'] = log(gvar(0.02, 0.02)) sqrt_prior = BufferDict() sqrt_prior['sqrt(a)'] = sqrt(gvar(0.02, 0.02)) prior = BufferDict(a = gvar(0.02, 0.02)) stdout = sys.stdout
def test_avg_data(self): """ avg_data """ self.assertTrue(avg_data([]) is None) self.assertEqual(avg_data(dict()),BufferDict()) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") avg_data(dict(s=[1.],v=[1.,2.])) self.assertEqual(len(w), 1) with self.assertRaises(ValueError): avg_data(dict(s=[],v=[1.,2.]), warn=False) with self.assertRaises(ValueError): avg_data(dict(s=[], v=[])) with self.assertRaises(ValueError): avg_data([1,2,"s"]) mean = avg_data([1]) self.assertEqual(mean.mean,1.) self.assertEqual(mean.sdev,0.) # mean = avg_data([1,2]) self.assertAlmostEqual(mean.mean,1.5) self.assertAlmostEqual(mean.var,sum((vi-1.5)**2 for vi in [1,2])/4.) mean2 = avg_data(np.array([1.,2.])) self.assertEqual(mean.mean,mean2.mean) self.assertEqual(mean.sdev,mean2.sdev) # mean = avg_data([1,2],spread=True) self.assertAlmostEqual(mean.mean,1.5) self.assertAlmostEqual(mean.var,sum((vi-1.5)**2 for vi in [1,2])/2.) # mean = avg_data([1,2],median=True) self.assertAlmostEqual(mean.mean,1.5) self.assertAlmostEqual(mean.var,0.5**2/2.) # mean = avg_data([1,2],median=True,spread=True) self.assertAlmostEqual(mean.mean,1.5) self.assertAlmostEqual(mean.var,0.5**2) # mean = avg_data([1,2,3]) self.assertAlmostEqual(mean.mean,2.0) self.assertAlmostEqual(mean.var,sum((vi-2.)**2 for vi in [1,2,3])/9.) # mean = avg_data([1,2,3], noerror=True) self.assertAlmostEqual(mean, 2.0) # mean = avg_data([[1],[2],[3]]) self.assertAlmostEqual(mean[0].mean,2.0) self.assertAlmostEqual(mean[0].var,sum((vi-2.)**2 for vi in [1,2,3])/9.) mean = avg_data([[1],[2],[3]], noerror=True) self.assertAlmostEqual(mean[0], 2.0) mean = avg_data([1,2,3],spread=True) self.assertAlmostEqual(mean.mean,2.0) self.assertAlmostEqual(mean.var,sum((vi-2.)**2 for vi in [1,2,3])/3.) # mean = avg_data([1,2,3],median=True) self.assertAlmostEqual(mean.mean,2.0) self.assertAlmostEqual(mean.var,1./3.) # mean = avg_data([[1],[2],[3]],median=True) self.assertAlmostEqual(mean[0].mean,2.0) self.assertAlmostEqual(mean[0].var,1./3.) # mean = avg_data([1,2,3],median=True,spread=True) self.assertAlmostEqual(mean.mean,2.0) self.assertAlmostEqual(mean.var,1.) # mean = avg_data([1,2,3,4,5,6,7,8,9],median=True) self.assertAlmostEqual(mean.mean,5) self.assertAlmostEqual(mean.var,3.**2/9.) # mean = avg_data([1,2,3,4,5,6,7,8,9],median=True,spread=True) self.assertAlmostEqual(mean.mean,5.) self.assertAlmostEqual(mean.var,3.**2) # mean = avg_data([1,2,3,4,5,6,7,8,9,10],median=True) self.assertAlmostEqual(mean.mean,5.5) self.assertAlmostEqual(mean.var,3.5**2/10.) # mean = avg_data([1,2,3,4,5,6,7,8,9,10],median=True,spread=True) self.assertAlmostEqual(mean.mean,5.5) self.assertAlmostEqual(mean.var,3.5**2) # data = dict(s=[1,2,3],v=[[1,1],[2,2],[3,3]]) mean = avg_data(data,median=True,spread=True) self.assertAlmostEqual(mean['s'].mean,2.0) self.assertAlmostEqual(mean['s'].var,1.0) self.assertEqual(mean['v'].shape,(2,)) self.assert_gvclose(mean['v'],[gvar(2,1),gvar(2,1)]) mean = avg_data(data, median=True, noerror=True) self.assertAlmostEqual(mean['s'],2.0) self.assertEqual(mean['v'].shape,(2,)) self.assert_arraysclose(mean['v'], [2,2]) mean = avg_data(data, noerror=True) self.assertAlmostEqual(mean['s'],2.0) self.assertEqual(mean['v'].shape,(2,)) self.assert_arraysclose(mean['v'], [2,2])
import tee DO_PLOT = True sys_stdout = sys.stdout # ranseed(12345) # ygen = gvar(0.015, 0.2) # print(ygen) # y = [gi for gi in bootstrap_iter(ygen, 20)] # y = [gvar(ygen(), ygen.sdev) for i in range(20)] # ystr = [yi.fmt(2) for yi in y] # y = gvar(fmt(y, 2)) y = gvar([ '-0.17(20)', '-0.03(20)', '-0.39(20)', '0.10(20)', '-0.03(20)', '0.06(20)', '-0.23(20)', '-0.23(20)', '-0.15(20)', '-0.01(20)', '-0.12(20)', '0.05(20)', '-0.09(20)', '-0.36(20)', '0.09(20)', '-0.07(20)', '-0.31(20)', '0.12(20)', '0.11(20)', '0.13(20)' ]) # print (y) print log_prior = BufferDict() log_prior['log(a)'] = log(gvar(0.02, 0.02)) sqrt_prior = BufferDict() sqrt_prior['sqrt(a)'] = sqrt(gvar(0.02, 0.02)) prior = BufferDict(a=gvar(0.02, 0.02)) stdout = sys.stdout for p in [prior, log_prior, sqrt_prior]: key = list(p.keys())[0]