def main(): print( gv.ranseed( (2050203335594632366, 8881439510219835677, 2605204918634240925))) log_stdout('eg3a.out') integ = vegas.Integrator(4 * [[0, 1]]) # adapt grid training = integ(f(), nitn=10, neval=1000) # evaluate multi-integrands result = integ(f(), nitn=10, neval=5000) print('I[0] =', result[0], ' I[1] =', result[1], ' I[2] =', result[2]) print('Q = %.2f\n' % result.Q) print('<x> =', result[1] / result[0]) print('sigma_x**2 = <x**2> - <x>**2 =', result[2] / result[0] - (result[1] / result[0])**2) print('\ncorrelation matrix:\n', gv.evalcorr(result)) unlog_stdout() r = gv.gvar(gv.mean(result), gv.sdev(result)) print(r[1] / r[0]) print((r[1] / r[0]).sdev / (result[1] / result[0]).sdev) print(r[2] / r[0] - (r[1] / r[0])**2) print(result.summary())
def main(): print(gv.ranseed((1814855126, 100213625, 262796317))) log_stdout('eg3a.out') integ = vegas.Integrator(4 * [[0, 1]]) # adapt grid training = integ(f(), nitn=10, neval=2000) # evaluate multi-integrands result = integ(f(), nitn=10, neval=10000) print('I[0] =', result[0], ' I[1] =', result[1], ' I[2] =', result[2]) print('Q = %.2f\n' % result.Q) print('<x> =', result[1] / result[0]) print('sigma_x**2 = <x**2> - <x>**2 =', result[2] / result[0] - (result[1] / result[0])**2) print('\ncorrelation matrix:\n', gv.evalcorr(result)) unlog_stdout() r = gv.gvar(gv.mean(result), gv.sdev(result)) print(r[1] / r[0]) print((r[1] / r[0]).sdev / (result[1] / result[0]).sdev) print(r[2] / r[0] - (r[1] / r[0])**2) print((r[2] / r[0] - (r[1] / r[0])**2).sdev / (result[2] / result[0] - (result[1] / result[0])**2).sdev) print(result.summary()) # do it again for a dictionary print(gv.ranseed((1814855126, 100213625, 262796317))) integ = vegas.Integrator(4 * [[0, 1]]) # adapt grid training = integ(f(), nitn=10, neval=2000) # evaluate the integrals result = integ(fdict(), nitn=10, neval=10000) log_stdout('eg3b.out') print(result) print('Q = %.2f\n' % result.Q) print('<x> =', result['x'] / result['1']) print('sigma_x**2 = <x**2> - <x>**2 =', result['x**2'] / result['1'] - (result['x'] / result['1'])**2) unlog_stdout()
def main(): np.random.seed(12) log_stdout('eg6a.out') itg = vegas.Integrator(dim * [[0, 1]], alpha=0.25) nstrat = 5 * [12] + (dim - 5) * [1] itg(f, nitn=15, nstrat=nstrat) r = itg(f, nitn=5, nstrat=nstrat) print(r.summary()) print('nstrat =', np.array(itg.nstrat)) unlog_stdout() print() log_stdout('eg6b.out') itg = vegas.Integrator(dim * [[0, 1]], alpha=0.25) neval = 2e6 itg(f, nitn=15, neval=neval) r = itg(f, nitn=5, neval=neval) print(r.summary()) print('nstrat =', np.array(itg.nstrat)) unlog_stdout() print()
def main(): dim = 5 log_stdout('eg5a.out') np.random.seed(123) map = vegas.AdaptiveMap(dim * [(0, 1)]) itg = vegas.Integrator(map, alpha=0.1) r = itg(f, neval=1e4, nitn=5) print(r.summary()) unlog_stdout() log_stdout('eg5b.out') np.random.seed(1234567) map = vegas.AdaptiveMap(dim * [(0, 1)]) x = np.concatenate([ np.random.normal(loc=0.45, scale=3 / 50, size=(1000, dim)), np.random.normal(loc=0.7, scale=3 / 50, size=(1000, dim)), ]) map.adapt_to_samples(x, f, nitn=5) itg = vegas.Integrator(map, alpha=0.1) r = itg(f, neval=1e4, nitn=5) print(r.summary()) unlog_stdout() log_stdout('eg5c.out') np.random.seed(123) def smc(f, neval, dim): " integrates f(y) over dim-dimensional unit hypercube " y = np.random.uniform(0, 1, (neval, dim)) fy = f(y) return (np.average(fy), np.std(fy) / neval**0.5) def g(y): jac = np.empty(y.shape[0], float) x = np.empty(y.shape, float) map.map(y, x, jac) return jac * f(x) # with map r = smc(g, 50_000, dim) print(' SMC + map:', f'{r[0]:.3f} +- {r[1]:.3f}') # without map r = smc(f, 50_000, dim) print('SMC (no map):', f'{r[0]:.3f} +- {r[1]:.3f}')
]) # 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)) unif_prior = BufferDict() unif_prior['ga(a)'] = BufferDict.uniform('ga', 0, 0.04) for p in [prior, log_prior, sqrt_prior, unif_prior]: key = list(p.keys())[0].replace('(a)','_a') log_stdout("eg6-{}.out".format(key)) def fcn(p, N=len(y)): return N*[p['a']] fit = nonlinear_fit(prior=p, fcn=fcn, data=(y)) print (fit) print ("a =", fit.p['a']) if DO_PLOT: log_stdout('eg6-hist.out') print(fit) a = fit.p['a'] print('a =', a) import matplotlib.pyplot as plt fs = plt.rcParams['figure.figsize']
def main(): print( gv.ranseed( (5751754790502652836, 7676372623888309739, 7570829798026950508))) integ = vegas.Integrator([[-1., 1.], [0., 1.], [0., 1.], [0., 1.]], # analyzer=vegas.reporter(), ) if SAVE_OUTPUT: log_stdout('eg1a.out') result = integ(f, nitn=10, neval=1000) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) integ.map.show_grid(30, axes=[(0, 1), (2, 3), (0, None), (None, 1), (2, 0), (3, 0)], shrink=False) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1b.out') result = integ(f, nitn=100, neval=1000) print('larger nitn => %s Q = %.2f' % (result, result.Q)) result = integ(f, nitn=10, neval=1e4) print('larger neval => %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1c.out') # integ.set(map=[[-2., .4, .6, 2.], [0, .4, .6, 2.], [0,.4, .6, 2.], [0.,.4, .6, 2.]]) # integ.set(map=[[-2., 2.], [0, 2.], [0, 2.], [0., 2.]]) integ = vegas.Integrator([[-2., 2.], [0, 2.], [0, 2.], [0., 2.]], ) result = integ(f, nitn=10, neval=1000) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1c1.out') if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1d.out') integ(f, nitn=7, neval=1000) result = integ(f, nitn=10, neval=1000) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1e.out') integ = vegas.Integrator([[-1, 1]] + 3 * [[0, 1]]) integ(f, nitn=10, neval=1000) def g(x): return x[0] * f(x) result = integ(g, nitn=10, neval=1000) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1f.out') # integ(f_sphere, nitn=10, neval=400000, alpha=0.25) # result = integ(f_sphere, nitn=10, neval=400000, alpha=0.25, beta=0.75)#, analyzer=vegas.reporter(5)) integ(f_sphere, nitn=10, neval=1000, alpha=0.5) result = integ(f_sphere, nitn=10, neval=1000, alpha=0.5) #, analyzer=vegas.reporter(5)) # print(integ.settings()) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1g.out') integ(f_sphere, nitn=10, neval=1000, alpha=0.1) result = integ(f_sphere, nitn=10, neval=1000, adapt=False) # alpha=0.1) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1h.out') integ(f_sphere, nitn=10, neval=1000, alpha=0.1) result = integ(f_sphere, nitn=10, neval=1000, alpha=0.1) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() # log_stdout('eg1h.out') integ = vegas.Integrator(4 * [[0, 1]]) integ(f2, nitn=10, neval=4e4) result = integ(f2, nitn=10, neval=4e4, beta=0.75) # , analyzer=vegas.reporter()) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) integ.map.show_grid(70) print(integ(f2, nitn=10, neval=4e4, beta=0.).summary())
def main(): ### 1) least-squares fit to the data x = np.array([ 0.2, 0.4, 0.6, 0.8, 1., 1.2, 1.4, 1.6, 1.8, 2., 2.2, 2.4, 2.6, 2.8, 3., 3.2, 3.4, 3.6, 3.8 ]) y = gv.gvar([ '0.38(20)', '2.89(20)', '0.85(20)', '0.59(20)', '2.88(20)', '1.44(20)', '0.73(20)', '1.23(20)', '1.68(20)', '1.36(20)', '1.51(20)', '1.73(20)', '2.16(20)', '1.85(20)', '2.00(20)', '2.11(20)', '2.75(20)', '0.86(20)', '2.73(20)' ]) prior = make_prior() fit = lsqfit.nonlinear_fit(data=(x, y), prior=prior, fcn=fitfcn) if LSQFIT_ONLY: log_stdout('case-outliers-lsq.out') elif not MULTI_W: log_stdout('case-outliers.out') print(fit) # plot data plt.errorbar(x, gv.mean(y), gv.sdev(y), fmt='o', c='b') # plot fit function xline = np.linspace(x[0], x[-1], 100) yline = fitfcn(xline, fit.p) plt.plot(xline, gv.mean(yline), 'k:') # yp = gv.mean(yline) + gv.sdev(yline) # ym = gv.mean(yline) - gv.sdev(yline) # plt.fill_between(xline, yp, ym, color='0.8') plt.xlabel('x') plt.ylabel('y') plt.savefig('case-outliers1.png', bbox_inches='tight') if LSQFIT_ONLY: return ### 2) Bayesian integral with modified PDF pdf = ModifiedPDF(data=(x, y), fcn=fitfcn, prior=prior) # integrator for expectation values with modified PDF expval = vegas.PDFIntegrator(fit.p, pdf=pdf) # adapt integrator to pdf expval(neval=1000, nitn=15) # evaluate expectation value of g(p) def g(p): w = p['w'] c = p['c'] return dict(w=[w, w**2], mean=c, outer=np.outer(c,c)) results = expval(g, neval=1000, nitn=15, adapt=False) print(results.summary()) # expval.map.show_grid(15) if MULTI_W: log_stdout('case-outliers-multi.out') # parameters c[i] mean = results['mean'] cov = results['outer'] - np.outer(mean, mean) c = mean + gv.gvar(np.zeros(mean.shape), gv.mean(cov)) print('c =', c) print( 'corr(c) =', np.array2string(gv.evalcorr(c), prefix=10 * ' '), '\n', ) # parameter w wmean, w2mean = results['w'] wsdev = gv.mean(w2mean - wmean ** 2) ** 0.5 w = wmean + gv.gvar(np.zeros(np.shape(wmean)), wsdev) print('w =', w, '\n') # Bayes Factor print('logBF =', np.log(results.pdfnorm)) sys.stdout = STDOUT if MULTI_W: return # add new fit to plot yline = fitfcn(xline, dict(c=c)) plt.plot(xline, gv.mean(yline), 'r--') yp = gv.mean(yline) + gv.sdev(yline) ym = gv.mean(yline) - gv.sdev(yline) plt.fill_between(xline, yp, ym, color='r', alpha=0.2) plt.savefig('case-outliers2.png', bbox_inches='tight')
import numpy as np import gvar RMAX = (2 * 0.5**2)**0.5 def fcn(x): dx2 = 0.0 for d in range(2): dx2 += (x[d] - 0.5)**2 I = np.exp(-dx2) # add I to appropriate bin in dI dI = np.zeros(5, dtype=float) dr = RMAX / len(dI) j = int(dx2**0.5 / dr) dI[j] = I return dict(I=I, dI=dI) log_stdout('eg4.out') integ = vegas.Integrator(2 * [(0, 1)]) # results returned in a dictionary result = integ(fcn) print(result.summary()) print(' I =', result['I']) print('dI/I =', result['dI'] / result['I']) print('sum(dI/I) =', sum(result['dI']) / result['I']) # print(gvar.evalcorr(result['dI'] / result['I']))
import numpy as np from outputsplitter import log_stdout def f(x): return x[0] * x[1] ** 2 m = vegas.AdaptiveMap([[0, 1], [0, 1]], ninc=5) ny = 1000 y = np.random.uniform(0., 1., (ny, 2)) # 1000 random y's x = np.empty(y.shape, float) # work space jac = np.empty(y.shape[0], float) f2 = np.empty(y.shape[0], float) log_stdout('eg2a.out') print('initial grid:') print m.settings() for itn in range(5): # 20 iterations to adapt m.map(y, x, jac) # compute x's and jac for j in range(ny): # compute training data f2[j] = (jac[j] * f(x[j])) ** 2 m.add_training_data(y, f2) # adapt m.adapt(alpha=1.5) print('iteration %d:' % itn) print(m.settings())
def main(): print(gv.ranseed( (5751754790502652836, 7676372623888309739, 7570829798026950508) )) integ = vegas.Integrator( [[-1., 1.], [0., 1.], [0., 1.], [0., 1.]], # analyzer=vegas.reporter(), ) if SAVE_OUTPUT: log_stdout('eg1a.out') result = integ(f, nitn=10, neval=1000) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) integ.map.show_grid( 30, axes=[(0, 1), (2, 3), (0, None), (None, 1), (2, 0), (3, 0)], shrink=False ) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1b.out') result = integ(f, nitn=100, neval=1000 ) print('larger nitn => %s Q = %.2f' % (result, result.Q)) result = integ(f, nitn=10, neval=1e4) print('larger neval => %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1c.out') # integ.set(map=[[-2., .4, .6, 2.], [0, .4, .6, 2.], [0,.4, .6, 2.], [0.,.4, .6, 2.]]) # integ.set(map=[[-2., 2.], [0, 2.], [0, 2.], [0., 2.]]) integ = vegas.Integrator( [[-2., 2.], [0, 2.], [0, 2.], [0., 2.]], ) result = integ(f, nitn=10, neval=1000) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1c1.out') if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1d.out') integ(f, nitn=7, neval=1000) result = integ(f, nitn=10, neval=1000) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1e.out') integ = vegas.Integrator([[-1,1]] + 3 * [[0, 1]]) integ(f, nitn=10, neval=1000) def g(x): return x[0] * f(x) result = integ(g, nitn=10, neval=1000) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1f.out') # integ(f_sphere, nitn=10, neval=400000, alpha=0.25) # result = integ(f_sphere, nitn=10, neval=400000, alpha=0.25, beta=0.75)#, analyzer=vegas.reporter(5)) integ(f_sphere, nitn=10, neval=1000, alpha=0.5) result = integ(f_sphere, nitn=10, neval=1000, alpha=0.5)#, analyzer=vegas.reporter(5)) # print(integ.settings()) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1g.out') integ(f_sphere, nitn=10, neval=1000, alpha=0.1) result = integ(f_sphere, nitn=10, neval=1000, adapt=False) # alpha=0.1) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() log_stdout('eg1h.out') integ(f_sphere, nitn=10, neval=1000, alpha=0.1) result = integ(f_sphere, nitn=10, neval=1000, alpha=0.1) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) if SAVE_OUTPUT: unlog_stdout() # log_stdout('eg1h.out') integ = vegas.Integrator(4 * [[0, 1]]) integ(f2, nitn=10, neval=4e4) result = integ(f2, nitn=10, neval=4e4, beta=0.75) # , analyzer=vegas.reporter()) print(result.summary()) print('result = %s Q = %.2f' % (result, result.Q)) integ.map.show_grid(70) print(integ(f2, nitn=10, neval=4e4, beta=0.).summary())