def test_run(): from ultranest import NestedSampler def loglike(y): z = np.log10(y) a = np.array([-0.5 * sum([((xi - 0.83456 + i*0.1)/0.5)**2 for i, xi in enumerate(x)]) for x in z]) b = np.array([-0.5 * sum([((xi - 0.43456 - i*0.1)/0.5)**2 for i, xi in enumerate(x)]) for x in z]) loglike.ncalls += len(a) return np.logaddexp(a, b) loglike.ncalls = 0 def transform(x): return 10**(10. * x - 5.) paramnames = ['Hinz', 'Kunz'] sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=400, vectorized=True) r = sampler.run(log_interval=50) ncalls = loglike.ncalls if sampler.mpi_size > 1: ncalls = sampler.comm.gather(ncalls, root=0) if sampler.mpi_rank == 0: print("ncalls on the different MPI ranks:", ncalls) ncalls = sum(sampler.comm.bcast(ncalls, root=0)) assert abs(r['ncall'] - ncalls) <= 2 * sampler.mpi_size, (r['ncall'], ncalls) open('nestedsampling_results.txt', 'a').write("%.3f\n" % r['logz']) sampler.plot()
def main(args): np.random.seed(2) ndim = args.x_dim sigma = args.sigma centers = np.sin(np.arange(ndim) / 2.) data = np.random.normal(centers, sigma).reshape((1, -1)) def loglike(theta): sigma = 10**theta[:,0] like = -0.5 * (((theta[:,1:] - data)/sigma.reshape((-1, 1)))**2).sum(axis=1) - 0.5 * log(2 * np.pi * sigma**2) * ndim return like def transform(x): z = x * 20 - 10 z[:,0] = x[:,0] * 6 - 3 return z import string paramnames = ['sigma'] + list(string.ascii_lowercase)[:ndim] if args.reactive: from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir=args.log_dir + 'RNS-%dd' % ndim, vectorized=True, resume=True, viz_callback = False, show_status = False) sampler.run(log_interval=20, min_num_live_points=args.num_live_points) sampler.plot() else: from ultranest import NestedSampler sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=args.num_live_points, vectorized=True, log_dir=args.log_dir + '-%dd' % ndim, resume=True) sampler.run() sampler.plot()
def main(args): from ultranest import NestedSampler #def loglike(z): # return np.array([-sum(100.0 * (x[1:] - x[:-1] ** 2.0) ** 2.0 + (1 - x[:-1]) ** 2.0) for x in z]) def loglike_(z): return np.array([ -sum(100.0 * (x[1::2] - x[::2]**2.0)**2.0 + (1 - x[::2])**2.0) for x in z ]) def loglike(z): a = np.array([ -0.5 * sum([((xi - 0.83456 + i * 0.1) / 0.01)**2 for i, xi in enumerate(x)]) for x in z ]) b = np.array([ -0.5 * sum([((xi - 0.43456 - i * 0.1) / 0.01)**2 for i, xi in enumerate(x)]) for x in z ]) return np.logaddexp(a, b) def transform(x): return 10. * x - 5. import string paramnames = list(string.ascii_lowercase)[:args.x_dim] sampler = NestedSampler(paramnames, loglike, transform=transform, vectorized=True, log_dir=args.log_dir) sampler.run(log_interval=20, num_live_points=args.num_live_points) sampler.plot()
def main(args): def loglike(z): chi = (cos(z / 2.)).prod(axis=1) return (2. + chi)**5 def transform(x): return x * 10 * pi import string paramnames = list(string.ascii_lowercase)[:args.x_dim] if args.reactive: from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler( paramnames, loglike, transform=transform, log_dir=args.log_dir, resume='overwrite', draw_multiple=False, vectorized=True, ) sampler.run( log_interval=20, max_num_improvement_loops=10, min_num_live_points=args.num_live_points, ) sampler.print_results() sampler.plot() else: from ultranest import NestedSampler sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=args.num_live_points, vectorized=True, log_dir=args.log_dir, resume='overwrite') #log_dir=None) sampler.run(log_interval=20) sampler.print_results() sampler.plot()
def main(args): def loglike(z): chi = (2. + (cos(z[:, :2] / 2.)).prod(axis=1))**5 chi2 = -np.abs((z - 5 * pi) / 0.5).sum(axis=1) return chi + chi2 def transform(x): return x * 100 import string paramnames = list(string.ascii_lowercase)[:args.x_dim] if args.reactive: from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir=args.log_dir + 'RNS-%dd' % args.x_dim, resume=True, vectorized=True) #log_dir=None) sampler.run(log_interval=20, min_num_live_points=args.num_live_points) sampler.plot() else: from ultranest import NestedSampler sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=args.num_live_points, vectorized=True, log_dir=args.log_dir + '%dd' % args.x_dim, resume=True) #log_dir=None) sampler.run(log_interval=20) sampler.plot()
def main(args): ndim = args.x_dim adaptive_nsteps = args.adapt_steps if adaptive_nsteps is None: adaptive_nsteps = False def flat_loglike(theta): delta = np.max(np.abs(theta - 0.5)) volume_enclosed = ndim * np.log(delta + 1e-15) if volume_enclosed > -100: return -volume_enclosed else: return +100 def loglike(theta): delta = np.max(np.abs(theta - 0.5), axis=1) volume_enclosed = ndim * np.log(delta + 1e-15) like = -volume_enclosed like[~(like < +100)] = 100 return like def flat_transform(x): return x def transform(x): return x paramnames = ['param%d' % (i+1) for i in range(ndim)] if args.pymultinest: from pymultinest.solve import solve result = solve(LogLikelihood=flat_loglike, Prior=flat_transform, n_dims=ndim, outputfiles_basename=args.log_dir + 'MN-%dd' % ndim, verbose=True, resume=True, importance_nested_sampling=False) print() print('evidence: %(logZ).1f +- %(logZerr).1f' % result) print() print('parameter values:') for name, col in zip(paramnames, result['samples'].transpose()): print('%15s : %.3f +- %.3f' % (name, col.mean(), col.std())) elif args.reactive: if args.slice: log_dir = args.log_dir + 'RNS-%dd-slice%d' % (ndim, args.slice_steps) elif args.harm: log_dir = args.log_dir + 'RNS-%dd-harm%d' % (ndim, args.slice_steps) elif args.dyhmc: log_dir = args.log_dir + 'RNS-%dd-dyhmc%d' % (ndim, args.slice_steps) elif args.dychmc: log_dir = args.log_dir + 'RNS-%dd-dychmc%d' % (ndim, args.slice_steps) else: log_dir = args.log_dir + 'RNS-%dd' % (ndim) if adaptive_nsteps: log_dir = log_dir + '-adapt%s' % (adaptive_nsteps) from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir=log_dir, resume=True, vectorized=True) if args.slice: import ultranest.stepsampler sampler.stepsampler = ultranest.stepsampler.RegionSliceSampler(nsteps=args.slice_steps, adaptive_nsteps=adaptive_nsteps, log=open(log_dir + '/stepsampler.log', 'w')) if args.harm: import ultranest.stepsampler sampler.stepsampler = ultranest.stepsampler.RegionBallSliceSampler(nsteps=args.slice_steps, adaptive_nsteps=adaptive_nsteps, log=open(log_dir + '/stepsampler.log', 'w')) """ if args.dyhmc: import ultranest.dyhmc from ultranest.utils import verify_gradient verify_gradient(ndim, transform, loglike, transform_loglike_gradient, combination=True) sampler.stepsampler = ultranest.dyhmc.DynamicHMCSampler(ndim=ndim, nsteps=args.slice_steps, transform_loglike_gradient=transform_loglike_gradient) if args.dychmc: import ultranest.dychmc from ultranest.utils import verify_gradient verify_gradient(ndim, transform, loglike, gradient, verbose=True) sampler.stepsampler = ultranest.dychmc.DynamicCHMCSampler(ndim=ndim, nsteps=args.slice_steps, transform=transform, loglike=loglike, gradient=gradient) """ sampler.run(frac_remain=0.5, min_num_live_points=args.num_live_points, max_num_improvement_loops=1) sampler.print_results() if sampler.stepsampler is not None: sampler.stepsampler.plot(filename = log_dir + '/stepsampler_stats_region.pdf') if ndim <= 20: sampler.plot() else: from ultranest import NestedSampler sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=args.num_live_points, vectorized=True, log_dir=args.log_dir + '-%dd' % ndim, resume=True) sampler.run() sampler.print_results() sampler.plot()
def main(args): ndim = args.x_dim adaptive_nsteps = args.adapt_steps if adaptive_nsteps is None: adaptive_nsteps = False rv1a = scipy.stats.loggamma(1, loc=2. / 3, scale=1. / 30) rv1b = scipy.stats.loggamma(1, loc=1. / 3, scale=1. / 30) rv2a = scipy.stats.norm(2. / 3, 1. / 30) rv2b = scipy.stats.norm(1. / 3, 1. / 30) rv_rest = [] for i in range(2, ndim): if i <= (ndim + 2) / 2: rv = scipy.stats.loggamma(1, loc=2. / 3., scale=1. / 30) else: rv = scipy.stats.norm(2. / 3, 1. / 30) rv_rest.append(rv) del rv def loglike(theta): L1 = log(0.5 * rv1a.pdf(theta[:, 0]) + 0.5 * rv1b.pdf(theta[:, 0]) + 1e-300) L2 = log(0.5 * rv2a.pdf(theta[:, 1]) + 0.5 * rv2b.pdf(theta[:, 1]) + 1e-300) Lrest = np.sum( [rv.logpdf(t) for rv, t in zip(rv_rest, theta[:, 2:].transpose())], axis=0) #assert L1.shape == (len(theta),) #assert L2.shape == (len(theta),) #assert Lrest.shape == (len(theta),), Lrest.shape like = L1 + L2 + Lrest like = np.where(like < -1e300, -1e300 - ((np.asarray(theta) - 0.5)**2).sum(), like) assert like.shape == (len(theta), ), (like.shape, theta.shape) return like def transform(x): return x paramnames = ['param%d' % (i + 1) for i in range(ndim)] if args.pymultinest: from pymultinest.solve import solve def flat_loglike(theta): return loglike(theta.reshape((1, -1))) result = solve(LogLikelihood=flat_loglike, Prior=transform, n_dims=ndim, outputfiles_basename=args.log_dir + 'MN-%dd' % ndim, verbose=True, resume=True, importance_nested_sampling=False) print() print('evidence: %(logZ).1f +- %(logZerr).1f' % result) print() print('parameter values:') for name, col in zip(paramnames, result['samples'].transpose()): print('%15s : %.3f +- %.3f' % (name, col.mean(), col.std())) elif args.reactive: if args.slice: log_dir = args.log_dir + 'RNS-%dd-slice%d' % (ndim, args.slice_steps) elif args.harm: log_dir = args.log_dir + 'RNS-%dd-harm%d' % (ndim, args.slice_steps) elif args.dyhmc: log_dir = args.log_dir + 'RNS-%dd-dyhmc%d' % (ndim, args.slice_steps) elif args.dychmc: log_dir = args.log_dir + 'RNS-%dd-dychmc%d' % (ndim, args.slice_steps) else: log_dir = args.log_dir + 'RNS-%dd' % (ndim) if adaptive_nsteps: log_dir = log_dir + '-adapt%s' % (adaptive_nsteps) from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir=log_dir, resume=True, vectorized=True) if args.slice: import ultranest.stepsampler sampler.stepsampler = ultranest.stepsampler.RegionSliceSampler( nsteps=args.slice_steps, adaptive_nsteps=adaptive_nsteps, log=open(log_dir + '/stepsampler.log', 'w') if sampler.mpi_rank == 0 else False) if args.harm: import ultranest.stepsampler sampler.stepsampler = ultranest.stepsampler.RegionBallSliceSampler( nsteps=args.slice_steps, adaptive_nsteps=adaptive_nsteps, log=open(log_dir + '/stepsampler.log', 'w') if sampler.mpi_rank == 0 else False) #if args.dyhmc: # import ultranest.dyhmc # verify_gradient(ndim, transform, loglike, transform_loglike_gradient, combination=True) # sampler.stepsampler = ultranest.dyhmc.DynamicHMCSampler(ndim=ndim, nsteps=args.slice_steps, # transform_loglike_gradient=transform_loglike_gradient, adaptive_nsteps=adaptive_nsteps) #if args.dychmc: # import ultranest.dychmc # verify_gradient(ndim, transform, loglike, gradient) # sampler.stepsampler = ultranest.dychmc.DynamicCHMCSampler(ndim=ndim, nsteps=args.slice_steps, # transform=transform, loglike=loglike, gradient=gradient, adaptive_nsteps=adaptive_nsteps) sampler.run(frac_remain=0.5, min_num_live_points=args.num_live_points, max_num_improvement_loops=1) sampler.print_results() if sampler.stepsampler is not None: sampler.stepsampler.plot(filename=log_dir + '/stepsampler_stats_region.pdf') if ndim <= 20: sampler.plot() else: from ultranest import NestedSampler sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=args.num_live_points, vectorized=True, log_dir=args.log_dir + '-%dd' % ndim, resume=True) sampler.run() sampler.print_results() sampler.plot()
def main(args): ndim = args.x_dim adaptive_nsteps = args.adapt_steps if adaptive_nsteps is None: adaptive_nsteps = False def loglike(theta): a = theta[:, :-1] b = theta[:, 1:] return -2 * (100 * (b - a**2)**2 + (1 - a)**2).sum(axis=1) def transform(u): return u * 20 - 10 def transform_loglike_gradient(u): theta = u * 20 - 10 a = theta[:-1] b = theta[1:] grad = theta.copy() L = -2 * (100 * (b - a**2)**2 + (1 - a)**2).sum() for i in range(ndim): a = theta[i] if i < ndim - 1: b = theta[i + 1] grad[i] = -2 * (-400 * a * (b - a**2) - 2 * (1 - a)) if i > 0: c = theta[i - 1] grad[i] += -400 * (a - c**2) prior_factor = 20 return theta, L, grad * prior_factor def gradient(u): theta = u * 20 - 10 grad = theta.copy() for i in range(ndim): a = theta[i] if i < ndim - 1: b = theta[i + 1] grad[i] = -2 * (-400 * a * (b - a**2) - 2 * (1 - a)) if i > 0: c = theta[i - 1] grad[i] += -400 * (a - c**2) prior_factor = 20 return grad * prior_factor paramnames = ['param%d' % (i + 1) for i in range(ndim)] if args.pymultinest: from pymultinest.solve import solve def flat_loglike(theta): return loglike(theta.reshape((1, -1))) result = solve(LogLikelihood=flat_loglike, Prior=transform, n_dims=ndim, outputfiles_basename=args.log_dir + 'MN-%dd' % ndim, verbose=True, resume=True, importance_nested_sampling=False) print() print('evidence: %(logZ).1f +- %(logZerr).1f' % result) print() print('parameter values:') for name, col in zip(paramnames, result['samples'].transpose()): print('%15s : %.3f +- %.3f' % (name, col.mean(), col.std())) elif args.reactive: if args.slice: log_dir = args.log_dir + 'RNS-%dd-slice%d' % (ndim, args.slice_steps) elif args.harm: log_dir = args.log_dir + 'RNS-%dd-harm%d' % (ndim, args.slice_steps) elif args.dyhmc: log_dir = args.log_dir + 'RNS-%dd-dyhmc%d' % (ndim, args.slice_steps) elif args.dychmc: log_dir = args.log_dir + 'RNS-%dd-dychmc%d' % (ndim, args.slice_steps) else: log_dir = args.log_dir + 'RNS-%dd' % (ndim) if adaptive_nsteps: log_dir = log_dir + '-adapt%s' % (adaptive_nsteps) from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir=log_dir, resume=True, vectorized=True) if args.slice: import ultranest.stepsampler sampler.stepsampler = ultranest.stepsampler.RegionSliceSampler( nsteps=args.slice_steps, adaptive_nsteps=adaptive_nsteps, log=open(log_dir + '/stepsampler.log', 'w')) if args.harm: import ultranest.stepsampler sampler.stepsampler = ultranest.stepsampler.RegionBallSliceSampler( nsteps=args.slice_steps, adaptive_nsteps=adaptive_nsteps, log=open(log_dir + '/stepsampler.log', 'w')) if args.dyhmc: import ultranest.dyhmc from ultranest.utils import verify_gradient verify_gradient(ndim, transform, loglike, transform_loglike_gradient, combination=True) sampler.stepsampler = ultranest.dyhmc.DynamicHMCSampler( ndim=ndim, nsteps=args.slice_steps, transform_loglike_gradient=transform_loglike_gradient, adaptive_nsteps=adaptive_nsteps) if args.dychmc: import ultranest.dychmc from ultranest.utils import verify_gradient verify_gradient(ndim, transform, loglike, gradient) sampler.stepsampler = ultranest.dychmc.DynamicCHMCSampler( ndim=ndim, nsteps=args.slice_steps, transform=transform, loglike=loglike, gradient=gradient, adaptive_nsteps=adaptive_nsteps) sampler.run(frac_remain=0.5, min_num_live_points=args.num_live_points, max_num_improvement_loops=1) sampler.print_results() if sampler.stepsampler is not None: sampler.stepsampler.plot(filename=log_dir + '/stepsampler_stats_region.pdf') if ndim <= 20: sampler.plot() else: from ultranest import NestedSampler sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=args.num_live_points, vectorized=True, log_dir=args.log_dir + '-%dd' % ndim, resume=True) sampler.run() sampler.print_results() sampler.plot()
def main(args): np.random.seed(2) Ndata = args.ndata jitter_true = 0.1 phase_true = 0. period_true = 180 amplitude_true = args.contrast / Ndata * jitter_true paramnames = ['amplitude', 'jitter', 'phase', 'period'] ndim = 4 derivednames = [] #'frequency'] wrapped_params = [False, False, True, False] #wrapped_params = None x = np.linspace(0, 360, 1000) y = amplitude_true * sin(x / period_true * 2 * pi + phase_true) if True: plt.plot(x, y) x = np.random.uniform(0, 360, Ndata) y = np.random.normal(amplitude_true * sin(x / period_true * 2 * pi + phase_true), jitter_true) plt.errorbar(x, y, yerr=jitter_true, marker='x', ls=' ') plt.savefig('testsine.pdf', bbox_inches='tight') plt.close() def loglike(params): amplitude, jitter, phase, period = params.transpose()[:4] predicty = amplitude * sin(x.reshape((-1,1)) / period * 2 * pi + phase) logl = (-0.5 * log(2 * pi * jitter**2) - 0.5 * ((predicty - y.reshape((-1,1))) / jitter)**2).sum(axis=0) assert logl.shape == jitter.shape return logl def transform(x): z = np.empty((len(x), 4)) z[:,0] = 10**(x[:,0] * 4 - 2) z[:,1] = 10**(x[:,1] * 1 - 1.5) z[:,2] = 2 * pi * x[:,2] z[:,3] = 10**(x[:,3] * 4 - 1) #z[:,4] = 2 * pi / x[:,3] return z loglike(transform(np.ones((2, ndim))*0.5)) if args.pymultinest: from pymultinest.solve import solve global Lmax Lmax = -np.inf def flat_loglike(theta): L = loglike(theta.reshape((1, -1)))[0] global Lmax if L > Lmax: print("Like: %.2f" % L) Lmax = L return L def flat_transform(cube): return transform(cube.reshape((1, -1)))[0] result = solve(LogLikelihood=flat_loglike, Prior=flat_transform, n_dims=ndim, outputfiles_basename=args.log_dir + 'MN-%dd' % ndim, n_live_points=args.num_live_points, verbose=True, resume=False, importance_nested_sampling=False) print() print('evidence: %(logZ).1f +- %(logZerr).1f' % result) print() print('parameter values:') for name, col in zip(paramnames, result['samples'].transpose()): print('%15s : %.3f +- %.3f' % (name, col.mean(), col.std())) return elif args.reactive: from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir=args.log_dir, vectorized=True, derived_param_names=derivednames, wrapped_params=wrapped_params, resume='overwrite') else: from ultranest import NestedSampler sampler = NestedSampler(paramnames, loglike, transform=transform, log_dir=args.log_dir, vectorized=True, derived_param_names=derivednames, wrapped_params=wrapped_params, resume='overwrite') sampler.run(min_num_live_points=args.num_live_points) print() sampler.plot() for i, p in enumerate(paramnames + derivednames): v = sampler.results['samples'][:,i] print('%20s: %5.3f +- %5.3f' % (p, v.mean(), v.std()))
def main(args): ndim = args.x_dim sigmas = 10**(-2.0 + 2.0 * np.cos(np.arange(ndim) - 2)) / (np.arange(ndim) - 2) sigmas[:2] = 1.0 def transform(x): y = x #.copy() #y[:,1::3] = 10**-y[:,1::3] #y[:,::3] *= x[:,2::3] return y centers = transform(np.ones((1, ndim)) * 0.2).flatten() degsigmas = 0.01 crosssigmas = args.sigma # * sigmas[3:-1:] * sigmas[4::] def loglike(theta): # gaussian like = -0.5 * (np.abs( (theta[:, 1:] - centers[1:]) / sigmas[1:])**2).sum(axis=1) # non-linear degeneracy correlation like2 = -0.5 * (np.abs( (theta[:, 1] * theta[:, 0] - centers[1] * centers[0]) / degsigmas) **2) #.sum(axis=1) # pair-wise correlation a = (theta[:, 3:-1:] - centers[3:-1:]) / sigmas[3:-1:] b = (theta[:, 4::] - centers[4::]) / sigmas[4::] like3 = -0.5 * (np.abs((a - b) / crosssigmas)**2).sum(axis=1) return like + like2 + like3 print(centers, crosssigmas, sigmas) import string paramnames = list(string.ascii_lowercase)[:ndim] if args.pymultinest: from pymultinest.solve import solve import json def flat_loglike(theta): return loglike(theta.reshape((1, -1))).flatten() def flat_transform(cube): return transform(cube.reshape((1, -1))).flatten() result = solve(LogLikelihood=flat_loglike, Prior=flat_transform, n_dims=ndim, outputfiles_basename=args.log_dir + 'MN-%dd' % ndim, verbose=True, resume=True, n_live_points=args.num_live_points, importance_nested_sampling=False) json.dump(paramnames, open(args.log_dir + 'MN-%ddparams.json' % ndim, 'w')) print() print('evidence: %(logZ).1f +- %(logZerr).1f' % result) print() print('parameter values:') for name, col in zip(paramnames, result['samples'].transpose()): print('%15s : %.3f +- %.3f' % (name, col.mean(), col.std())) elif args.reactive: from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir=args.log_dir + 'RNS-%dd' % ndim, vectorized=True) sampler.run(frac_remain=0.5, min_ess=400, min_num_live_points=args.num_live_points) sampler.print_results() sampler.plot() else: from ultranest import NestedSampler sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=args.num_live_points, vectorized=True, log_dir=args.log_dir + '-%dd' % ndim) sampler.run() sampler.print_results() sampler.plot()
def main(args): ndim = args.x_dim sigma = args.sigma sigma = np.logspace(-1, np.log10(args.sigma), ndim) width = 1 - 5 * sigma width[width < 1e-20] = 1e-20 centers = (np.sin(np.arange(ndim) / 2.) * width + 1.) / 2. #centers = np.ones(ndim) * 0.5 adaptive_nsteps = args.adapt_steps if adaptive_nsteps is None: adaptive_nsteps = False def loglike(theta): like = -0.5 * (((theta - centers) / sigma)**2).sum( axis=1) - 0.5 * np.log(2 * np.pi * sigma**2).sum() return like def transform(x): return x def transform_loglike_gradient(u): theta = u like = -0.5 * (((theta - centers) / sigma)**2).sum( axis=1) - 0.5 * np.log(2 * np.pi * sigma**2).sum() grad = (theta - centers) / sigma return u, like, grad def gradient(theta): return (theta - centers) / sigma paramnames = ['param%d' % (i + 1) for i in range(ndim)] if args.pymultinest: from pymultinest.solve import solve def flat_loglike(theta): return loglike(theta.reshape((1, -1))) result = solve(LogLikelihood=flat_loglike, Prior=transform, n_dims=ndim, outputfiles_basename=args.log_dir + 'MN-%dd' % ndim, verbose=True, resume=True, importance_nested_sampling=False) print() print('evidence: %(logZ).1f +- %(logZerr).1f' % result) print() print('parameter values:') for name, col in zip(paramnames, result['samples'].transpose()): print('%15s : %.3f +- %.3f' % (name, col.mean(), col.std())) elif args.reactive: if args.slice: log_dir = args.log_dir + 'RNS-%dd-slice%d' % (ndim, args.slice_steps) elif args.harm: log_dir = args.log_dir + 'RNS-%dd-harm%d' % (ndim, args.slice_steps) elif args.dyhmc: log_dir = args.log_dir + 'RNS-%dd-dyhmc%d' % (ndim, args.slice_steps) elif args.dychmc: log_dir = args.log_dir + 'RNS-%dd-dychmc%d' % (ndim, args.slice_steps) else: log_dir = args.log_dir + 'RNS-%dd' % (ndim) if adaptive_nsteps: log_dir = log_dir + '-adapt%s' % (adaptive_nsteps) from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir=log_dir, resume=True, vectorized=True) if args.slice: import ultranest.stepsampler sampler.stepsampler = ultranest.stepsampler.RegionSliceSampler( nsteps=args.slice_steps, adaptive_nsteps=adaptive_nsteps, log=open(log_dir + '/stepsampler.log', 'w')) if args.harm: import ultranest.stepsampler sampler.stepsampler = ultranest.stepsampler.RegionBallSliceSampler( nsteps=args.slice_steps, adaptive_nsteps=adaptive_nsteps, log=open(log_dir + '/stepsampler.log', 'w')) if args.dyhmc: import ultranest.dyhmc from ultranest.utils import verify_gradient verify_gradient(ndim, transform, loglike, transform_loglike_gradient, combination=True) sampler.stepsampler = ultranest.dyhmc.DynamicHMCSampler( ndim=ndim, nsteps=args.slice_steps, transform_loglike_gradient=transform_loglike_gradient, adaptive_nsteps=adaptive_nsteps) if args.dychmc: import ultranest.dychmc from ultranest.utils import verify_gradient verify_gradient(ndim, transform, loglike, gradient) sampler.stepsampler = ultranest.dychmc.DynamicCHMCSampler( ndim=ndim, nsteps=args.slice_steps, transform=transform, loglike=loglike, gradient=gradient, adaptive_nsteps=adaptive_nsteps) sampler.run(frac_remain=0.5, min_num_live_points=args.num_live_points, max_num_improvement_loops=1) sampler.print_results() if sampler.stepsampler is not None: sampler.stepsampler.plot(filename=log_dir + '/stepsampler_stats_region.pdf') if ndim <= 20: sampler.plot() else: from ultranest import NestedSampler sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=args.num_live_points, vectorized=True, log_dir=args.log_dir + '-%dd' % ndim, resume=True) sampler.run() sampler.print_results() sampler.plot()
def main(args): ndim = args.x_dim adaptive_nsteps = args.adapt_steps if adaptive_nsteps is None: adaptive_nsteps = False #C = 0.01 r = 0.2 # the shell thickness is #w = (r**(ndim+1) + C * scipy.special.gamma((ndim+3)/2)*ndim*pi**(-(ndim+1)/2) / ( # scipy.special.gamma((ndim+2)/2) * pi**(-ndim/2)))**(1 / (ndim+1)) - r w = 0.001 / ndim r1, r2 = r, r w1, w2 = w, w c1, c2 = np.zeros(ndim) + 0.5, np.zeros(ndim) + 0.5 c1[0] -= r1 / 2 c2[0] += r2 / 2 N1 = -0.5 * log(2 * pi * w1**2) N2 = -0.5 * log(2 * pi * w2**2) Z_analytic = log(shell_vol(ndim, r1, w1) + shell_vol(ndim, r2, w2)) def loglike(theta): d1 = ((theta - c1)**2).sum(axis=1)**0.5 d2 = ((theta - c2)**2).sum(axis=1)**0.5 L1 = -0.5 * ((d1 - r1)**2) / w1**2 + N1 L2 = -0.5 * ((d2 - r2)**2) / w2**2 + N2 return np.logaddexp(L1, L2) def transform(x): return x def gradient(theta): delta1 = theta - c1 delta2 = theta - c1 d1 = (delta1**2).sum()**0.5 d2 = (delta2**2).sum()**0.5 g1 = -delta1 * (1 - r1 / d1) / w1**2 g2 = -delta2 * (1 - r2 / d2) / w2**2 return np.logaddexp(g1, g2) """ N = 10000 x = np.random.normal(size=(N, ndim)) x *= (np.random.uniform(size=N)**(1./ndim) / (x**2).sum(axis=1)**0.5).reshape((-1, 1)) x = x * r1 + c1 print(loglike(x) - N1) print('%.3f%%' % ((loglike(x) - N1 > -ndim*2).mean() * 100)) import sys; sys.exit() """ paramnames = ['param%d' % (i+1) for i in range(ndim)] if args.pymultinest: from pymultinest.solve import solve def flat_loglike(theta): return loglike(theta.reshape((1, -1))) result = solve(LogLikelihood=flat_loglike, Prior=transform, n_dims=ndim, outputfiles_basename=args.log_dir + 'MN-%dd' % ndim, verbose=True, resume=True, importance_nested_sampling=False) print() print('evidence: %(logZ).1f +- %(logZerr).1f' % result) print() print('parameter values:') for name, col in zip(paramnames, result['samples'].transpose()): print('%15s : %.3f +- %.3f' % (name, col.mean(), col.std())) elif args.reactive: if args.slice: log_dir = args.log_dir + 'RNS-%dd-slice%d' % (ndim, args.slice_steps) elif args.harm: log_dir = args.log_dir + 'RNS-%dd-harm%d' % (ndim, args.slice_steps) elif args.dyhmc: log_dir = args.log_dir + 'RNS-%dd-dyhmc%d' % (ndim, args.slice_steps) elif args.dychmc: log_dir = args.log_dir + 'RNS-%dd-dychmc%d' % (ndim, args.slice_steps) else: log_dir = args.log_dir + 'RNS-%dd' % (ndim) if adaptive_nsteps: log_dir = log_dir + '-adapt%s' % (adaptive_nsteps) from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir=log_dir, resume=True, vectorized=True) if args.slice: import ultranest.stepsampler sampler.stepsampler = ultranest.stepsampler.RegionSliceSampler(nsteps=args.slice_steps, adaptive_nsteps=adaptive_nsteps, log=open(log_dir + '/stepsampler.log', 'w')) if args.harm: import ultranest.stepsampler sampler.stepsampler = ultranest.stepsampler.RegionBallSliceSampler(nsteps=args.slice_steps, adaptive_nsteps=adaptive_nsteps, log=open(log_dir + '/stepsampler.log', 'w')) #if args.dyhmc: # import ultranest.dyhmc # from ultranest.utils import verify_gradient # verify_gradient(ndim, transform, loglike, transform_loglike_gradient, combination=True) # sampler.stepsampler = ultranest.dyhmc.DynamicHMCSampler(ndim=ndim, nsteps=args.slice_steps, # transform_loglike_gradient=transform_loglike_gradient) if args.dychmc: import ultranest.dychmc from ultranest.utils import verify_gradient verify_gradient(ndim, transform, loglike, gradient, verbose=True) sampler.stepsampler = ultranest.dychmc.DynamicCHMCSampler(ndim=ndim, nsteps=args.slice_steps, transform=transform, loglike=loglike, gradient=gradient) sampler.run(frac_remain=0.5, min_num_live_points=args.num_live_points, max_num_improvement_loops=1) sampler.print_results() if sampler.stepsampler is not None: sampler.stepsampler.plot(filename = log_dir + '/stepsampler_stats_region.pdf') if ndim <= 20: sampler.plot() else: from ultranest import NestedSampler sampler = NestedSampler(paramnames, loglike, transform=transform, num_live_points=args.num_live_points, vectorized=True, log_dir=args.log_dir + '-%dd' % ndim, resume=True) sampler.run() sampler.print_results() sampler.plot() print("expected Z=%.3f (analytic solution)" % Z_analytic)