Skip to content

Commit ded1d61

Browse files
use new methods in test suite; added a few new test problems
1 parent 630583e commit ded1d61

File tree

11 files changed

+805
-58
lines changed

11 files changed

+805
-58
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
*.pdf
44
[_]build/
55
*.egg-info/
6+
*.so

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
long_description = f.read()
88

99
setup(name='nested_sampling',
10-
version='0.1',
10+
version='0.2',
1111
author='Johannes Buchner',
1212
url='https://bitbucket.org/JohannesBuchner/nested-sampling',
1313
author_email='[email protected]',

testsuite/algorithms/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
import multinest
55
import cuba
66
import nest
7-
#import runnestle
7+
import runnestle
88

99
algorithms = \
1010
multinest.configs + \
11+
runnestle.configs + \
1112
cuba.configs + \
1213
nest.configs
13-
# + \
14-
#runnestle.configs
1514

1615

testsuite/algorithms/multinest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def myloglike(cube, ndim, nparams):
2525
sys.exit(-127)
2626
return l
2727
nlive_points = config['nlive_points']
28+
if config.get('unlimited_sampling', False):
29+
max_samples = 0
30+
else:
31+
max_samples = 2000000
2832
mn_args = dict(
2933
importance_nested_sampling = config['importance_nested_sampling'],
3034
outputfiles_basename = output_basename + 'out_',
@@ -36,7 +40,7 @@ def myloglike(cube, ndim, nparams):
3640
const_efficiency_mode = False,
3741
evidence_tolerance = 0.5,
3842
seed = config['seed'],
39-
max_iter = 2000000,
43+
max_iter = max_samples,
4044
)
4145
starttime = time.time()
4246
pymultinest.run(myloglike, myprior, mn_args['n_params'], **mn_args)

testsuite/algorithms/nest.py

Lines changed: 231 additions & 14 deletions
Large diffs are not rendered by default.

testsuite/algorithms/runnestle.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import nestle
66
import numpy
77
from numpy import log, exp
8-
8+
import sys
99
import time
1010

1111
def run_nestle(**config):
@@ -14,21 +14,38 @@ def run_nestle(**config):
1414
def priortransform(u):
1515
assert len(u) == ndim, u
1616
return u
17+
def dump_callback(info):
18+
sys.stderr.write("\r%d|%d|logz=%.4f|eff=%f%% " % (info['it'], info['ncall'], info['logz'], info['it']*100./info['ncall']))
19+
20+
#if info['it'] % 50 != 0: return
21+
#print "Replacements: %d" % (info['ncall'])
22+
#print "Samples: %d" % (info['it'])
23+
#print "Efficiency: %f" % (info['ncall']/info['it'])
24+
#print "Nested Sampling ln(Z): %f" % (info['logz'])
1725
if 'seed' in config:
1826
numpy.random.seed(config['seed'])
1927

2028
# can use directly
2129
loglikelihood = config['loglikelihood']
2230
nlive_points = config['nlive_points']
2331
method = config['method']
24-
32+
if config.get('unlimited_sampling', False):
33+
max_samples = None
34+
else:
35+
max_samples = 2000000
36+
print
2537
print 'running nestle ...'
38+
options = dict()
39+
#if 'enlarge' in config:
40+
# options['enlarge'] = config['enlarge']
2641
starttime = time.time()
2742
result = nestle.sample(loglikelihood=loglikelihood, prior_transform=priortransform, ndim=ndim, npoints=nlive_points,
28-
method=method, update_interval=None, maxcall=2000000, dlogz=0.5, rstate=numpy.random)
43+
method=method, update_interval=None, maxcall=max_samples, dlogz=0.5, rstate=numpy.random, callback=dump_callback,
44+
**options)
2945
endtime = time.time()
3046
output_basename = config['output_basename']
31-
print 'nestle done lnZ = %(logz).1f +- %(logzerr).1f' % (result)
47+
print
48+
print 'nestle done lnZ = %(logz).1f +- %(logzerr).1f' % (result)
3249

3350
if config.get('seed', 0) == 0:
3451
import matplotlib.pyplot as plt
@@ -64,18 +81,26 @@ def priortransform(u):
6481
return dict(
6582
Z_computed = float(result['logz']),
6683
Z_computed_err = float(result['logzerr']),
67-
niterations = result['ncall'],
84+
niterations = result['niter'],
6885
duration = endtime - starttime,
6986
)
7087

7188
configs = [
7289
[
73-
#dict(nlive_points=100),
90+
dict(nlive_points=100),
7491
dict(nlive_points=400),
7592
dict(nlive_points=1000),
7693
], [
94+
dict(method='classic'),
7795
dict(method='single'),
7896
dict(method='multi'),
97+
#dict(method='single-robust'),
98+
#dict(method='multi-robust'),
99+
#dict(method='single-veryrobust'),
100+
#dict(method='multi-veryrobust'),
101+
#dict(method='multi-limitedrobust'),
102+
#dict(method='multi-simplelimitedrobust'),
103+
dict(method='multi-rememberingrobust'),
79104
]
80105
]
81106
configs = [dict([[k, v] for d in config for k, v in d.iteritems()]) for config in itertools.product(*configs)]

testsuite/problems/__init__.py

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,77 @@
33
"""
44
import gauss
55
import loggamma
6-
import real.timeseries
6+
#import real.timeseries
7+
import real.bexvar
78

89
problems = [
910
#gauss.create_problem_gauss(ndim=1, problem_name='gauss1d'),
1011
gauss.create_problem_gauss(ndim=3, problem_name='gauss3d'),
1112
gauss.create_problem_gauss(ndim=10, problem_name='gauss10d'),
12-
gauss.create_problem_vargauss(ndim=10, problem_name='vargauss10d'),
13-
gauss.create_problem_vargauss(ndim=20, problem_name='vargauss20d'),
13+
#gauss.create_problem_gauss_i(ndim=5, problem_name='gauss_i_5d'),
14+
#gauss.create_problem_gauss_i(ndim=10, problem_name='gauss_i_10d'),
15+
#gauss.create_problem_vargauss(ndim=5, problem_name='vargauss5d'),
16+
#gauss.create_problem_vargauss(ndim=10, problem_name='vargauss10d'),
17+
#gauss.create_problem_corrgauss(ndim=5, problem_name='corrgauss5d'),
18+
gauss.create_problem_corrgauss(ndim=10, problem_name='corrgauss10d'),
19+
#gauss.create_problem_corrgauss(ndim=10, problem_name='corrgauss10d-2', difficulty=2),
20+
#loggamma.create_problem_bananas(ndim=6, problem_name='bananas6d'),
21+
#loggamma.create_problem_bananas(ndim=10, problem_name='bananas10d'),
22+
#loggamma.create_problem_poisson_counts(ndim=3, problem_name='poissoncounts3d'),
23+
#loggamma.create_problem_poisson_counts(ndim=10, problem_name='poissoncounts10d'),
24+
#gauss.create_problem_vargauss(ndim=20, problem_name='vargauss20d'),
1425
#gauss.create_problem_gauss(ndim=50, problem_name='vargauss50d'),
26+
gauss.create_problem_gauss_i(ndim=2, problem_name='gauss_i_2d'),
27+
gauss.create_problem_gauss_i(ndim=3, problem_name='gauss_i_3d'),
28+
gauss.create_problem_gauss_i(ndim=4, problem_name='gauss_i_4d'),
1529
gauss.create_problem_shell(ndim=2, problem_name='shell2d'),
1630
#gauss.create_problem_shell(ndim=2, problem_name='shell2dtilt10', tilt=10),
1731
gauss.create_problem_shell(ndim=10, problem_name='shell10d'),
32+
#gauss.create_problem_shell(ndim=10, problem_name='shell10d-thin', width=[0.001/12]*2),
1833
gauss.create_problem_gen_gauss_sequence(ndim=7, problem_name='norm_sequence'),
1934
gauss.create_problem_complement_gen_gauss_sequence(ndim=7, problem_name='norm_sequence_complement'),
2035
gauss.create_problem_eggbox(problem_name='eggbox'),
2136
loggamma.create_problem_rosenbrock(ndim=2, problem_name='rosenbrock2d'),
2237
loggamma.create_problem_loggamma(ndim=2, problem_name='loggamma2d'),
23-
loggamma.create_problem_loggamma(ndim=2, problem_name='loggamma10d'),
24-
loggamma.create_problem_funnel(ndim=2, problem_name='funnel2d'),
25-
loggamma.create_problem_funnel(ndim=5, problem_name='funnel5d'),
38+
loggamma.create_problem_loggamma(ndim=10, problem_name='loggamma10d'),
39+
#loggamma.create_problem_funnel(ndim=2, problem_name='funnel2d'),
40+
#loggamma.create_problem_funnel(ndim=2, difficulty=0, problem_name='funnel2d-0'),
41+
#loggamma.create_problem_funnel(ndim=2, difficulty=1, problem_name='funnel2d-1'),
42+
#loggamma.create_problem_funnel(ndim=2, difficulty=2, problem_name='funnel2d-2'),
43+
#loggamma.create_problem_funnel(ndim=5, problem_name='funnel5d'),
44+
#loggamma.create_problem_funnel(ndim=5, difficulty=0, problem_name='funnel5d-0'),
45+
#loggamma.create_problem_funnel(ndim=5, difficulty=1, problem_name='funnel5d-1'),
46+
#loggamma.create_problem_funnel(ndim=5, difficulty=2, problem_name='funnel5d-2'),
47+
#loggamma.create_problem_funnel(ndim=10, problem_name='funnel10d'),
48+
#loggamma.create_problem_funnel(ndim=20, problem_name='funnel20d'),
49+
loggamma.create_problem_ffunnel(ndim=2, problem_name='ffunnel2d-3', difficulty=3),
50+
loggamma.create_problem_ffunnel(ndim=2, problem_name='ffunnel2d-5', difficulty=5),
51+
loggamma.create_problem_ffunnel(ndim=5, problem_name='ffunnel5d-3', difficulty=3),
52+
loggamma.create_problem_ffunnel(ndim=5, problem_name='ffunnel5d-5', difficulty=5),
53+
loggamma.create_problem_ffunnel(ndim=10, problem_name='ffunnel10d-5', difficulty=5),
54+
#real.exvar.create_problem_exvar(ndim=5, problem_name='exvar5d'),
55+
real.bexvar.create_problem_bexvar(ndim=10, problem_name='bexvar10d', variance=1),
56+
#real.bexvar.create_problem_bexvar(ndim=10, problem_name='bexvar10d-1', variance=-1),
57+
#real.exvar.create_problem_exvar(ndim=20, problem_name='exvar20d'),
58+
#loggamma.create_problem_ffunnel(ndim=20, problem_name='ffunnel20d', difficulty=5),
2659
loggamma.create_problem_spikeslab(problem_name='spikeslab2d_difficulty3', ndim=2, difficulty=3),
2760
loggamma.create_problem_spikeslab(problem_name='spikeslab2d_difficulty4', ndim=2, difficulty=4),
2861
#loggamma.create_problem_spikeslab(problem_name='spikeslab2d_difficulty6', ndim=2, difficulty=6),
2962
#loggamma.create_problem_spikeslab(problem_name='spikeslab5d_difficulty2', ndim=5, difficulty=2),
3063
#loggamma.create_problem_spikeslab(problem_name='spikeslab5d_difficulty6', ndim=5, difficulty=6),
3164
# the slowest last
32-
loggamma.create_problem_loggamma_multimodal(ndim=2, problem_name='loggamma_multimodal2d'),
33-
loggamma.create_problem_loggamma_multimodal(ndim=5, problem_name='loggamma_multimodal5d'),
34-
loggamma.create_problem_loggamma_multimodal(ndim=10, problem_name='loggamma_multimodal10d'),
35-
loggamma.create_problem_eyes(ndim=5, hardness=1, problem_name='eyes5d'),
36-
loggamma.create_problem_eyes(ndim=10, hardness=1, problem_name='eyes10d'),
37-
loggamma.create_problem_eyes(ndim=5, hardness=5, problem_name='eyes5d-5'),
38-
loggamma.create_problem_eyes(ndim=10, hardness=5, problem_name='eyes10d-5'),
39-
real.timeseries.create_problem_RVexoplanet(nplanets=0, problem_name='RVexoplanet-0'),
40-
real.timeseries.create_problem_RVexoplanet(nplanets=1, problem_name='RVexoplanet-1'),
65+
loggamma.create_problem_loggammaI_multimodal(ndim=2, problem_name='loggammaI_multimodal2d'),
66+
#loggamma.create_problem_loggammaI_multimodal(ndim=5, problem_name='loggammaI_multimodal5d'),
67+
loggamma.create_problem_loggammaI_multimodal(ndim=10, problem_name='loggammaI_multimodal10d'),
68+
#loggamma.create_problem_loggammaI_multimodal(ndim=20, problem_name='loggammaI_multimodal20d'),
69+
#loggamma.create_problem_eyes(ndim=5, hardness=1, problem_name='eyes5d'),
70+
#loggamma.create_problem_eyes(ndim=10, hardness=1, problem_name='eyes10d'),
71+
#loggamma.create_problem_eyes(ndim=5, hardness=5, problem_name='eyes5d-5'),
72+
#loggamma.create_problem_eyes(ndim=10, hardness=5, problem_name='eyes10d-5'),
73+
#real.timeseries.create_problem_RVexoplanet(nplanets=0, problem_name='RVexoplanet-0'),
74+
#real.timeseries.create_problem_RVexoplanet(nplanets=1, problem_name='RVexoplanet-1'),
4175
#loggamma.create_problem_eyes(ndim=20, hardness=5, problem_name='eyes20d-5'),
42-
real.timeseries.create_problem_RVexoplanet(nplanets=2, problem_name='RVexoplanet-2'),
76+
#real.timeseries.create_problem_RVexoplanet(nplanets=2, problem_name='RVexoplanet-2'),
4377
]
4478

4579

testsuite/problems/gauss.py

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def draw_constrained(x, Lmin):
7474

7575
def create_problem_gauss_multimodal(**config):
7676
ndim = config.get('ndim', 2)
77-
assert ndim >= 2
77+
assert ndim >= 2, ('ndim must be at least 2')
7878

7979
def loglikelihood(y):
8080
x = numpy.asarray(y)
@@ -109,25 +109,98 @@ def volume_computation(u=None, x=None, L=None):
109109

110110
def create_problem_vargauss(**config):
111111
ndim = config.get('ndim', 10)
112-
i = numpy.arange(ndim) + 3.0
112+
assert ndim > 3, ('ndim must be at least 3')
113+
i = numpy.arange(ndim - 3)
113114
mu = 0.54321
114-
s = 6./((3.*i)**1.4)
115+
s = 10**(-((i + 2))*0.5)
115116
def loglikelihood(x):
116-
z = numpy.asarray(x)
117+
z = numpy.asarray(x[3:])
117118
return -0.5 * (((z - mu)/s)**2).sum()
118119

119120
Z_analytic = 0.5 * log(2*pi * s**2).sum()
120121
config['loglikelihood'] = loglikelihood
121122
config['Z_analytic'] = Z_analytic
122123
config['description'] = """Sequence of independent gaussians which become
123-
narrower and narrower with the dimension number. Symmetric, %d dimensions.
124+
narrower and narrower with the dimension number.
125+
The first three dimensions are unconstrained.
126+
Difficult because the parameter sizes are orders of magnitudes different,
127+
and region of interest is tiny.
128+
Symmetric, %d dimensions.
129+
""" % ndim
130+
return config
131+
132+
133+
def create_problem_corrgauss(**config):
134+
ndim = config.get('ndim', 10)
135+
difficulty = config.get('difficulty', 1)
136+
137+
logmatrix = numpy.zeros((ndim, ndim), dtype=int)
138+
matrix = numpy.zeros((ndim, ndim))
139+
140+
for i in range(ndim):
141+
for j in range(i+1):
142+
logmatrix[i,j] = (((j*i) % (5+i)) // 2) % 5
143+
logmatrix[j,i] = logmatrix[i,j]
144+
145+
for i in range(ndim):
146+
for j in range(i+1):
147+
scalei = numpy.exp(-logmatrix[i,i] - 2)
148+
scalej = numpy.exp(-logmatrix[j,j] - 2)
149+
if i == j:
150+
matrix[i,i] = scalei*scalej
151+
else:
152+
matrix[i,j] = (1 - numpy.exp(-logmatrix[i,j]*difficulty))*scalei*scalej
153+
matrix[j,i] = (1 - numpy.exp(-logmatrix[j,i]*difficulty))*scalei*scalej
154+
# ensure positive semi-definite
155+
_, matrix = scipy.linalg.polar(matrix)
156+
matrix = numpy.matrix(matrix)
157+
invmatrix = numpy.linalg.inv(matrix)
158+
mu = 0.5 + numpy.zeros(ndim)
159+
assert numpy.linalg.det(matrix) > 0, numpy.linalg.det(matrix)
160+
norm = -0.5 * log(numpy.linalg.det(2 * pi * matrix))
161+
def loglikelihood(x):
162+
z = numpy.asarray(x) - mu
163+
return -0.5 * numpy.dot(numpy.dot(z, invmatrix), z.T) + norm
164+
165+
Z_analytic = 0
166+
config['loglikelihood'] = loglikelihood
167+
config['Z_analytic'] = Z_analytic
168+
config['description'] = """Correlated gaussian likelihood in %d dimensions.
169+
-log(1 - correlation matrix) is: <pre>%s</pre>
170+
Centred at 0.5.
171+
""" % (ndim, logmatrix*difficulty)
172+
return config
173+
174+
175+
176+
def create_problem_gauss_i(**config):
177+
ndim = config.get('ndim', 10)
178+
mu1 = 0.654321
179+
mu2 = 0.123456
180+
s1 = 0.1
181+
s2 = 0.01
182+
norm1 = -0.5 * log(2*pi*s1**2) * ndim
183+
norm2 = -0.5 * log(2*pi*s2**2) * ndim + log(100)
184+
def loglikelihood(x):
185+
z = numpy.asarray(x)
186+
return numpy.logaddexp(norm1 - 0.5 * ((((z - mu1)/s1)**2)).sum(),
187+
norm2 - 0.5 * (((z - mu2)/s2)**2).sum())
188+
189+
Z_analytic = log(101)
190+
config['loglikelihood'] = loglikelihood
191+
config['Z_analytic'] = Z_analytic
192+
config['description'] = """Two gaussian solutions, one large 0.654321+-0.1, one small 0.123456+-0.01, in %d dimensions.
193+
194+
<p>This function may be difficult if the smaller solution is lost,
195+
because by area it is small; but later it turns out to be the important one.
124196
""" % ndim
125197
return config
126198

127199

200+
128201
def create_problem_halfgauss(**config):
129202
ndim = config.get('ndim', 2)
130-
assert ndim >= 2
203+
assert ndim >= 2, ('ndim must be at least 2')
131204

132205
def loglikelihood(y):
133206
x = numpy.asarray(y)
@@ -295,7 +368,7 @@ def loglikelihood(x):
295368

296369
def create_problem_pyramid(**config):
297370
ndim = config.get('ndim', 20)
298-
assert ndim >= 2
371+
assert ndim >= 2, ('ndim must be at least 2')
299372

300373
def loglikelihood(x):
301374
x = numpy.asarray(x)

0 commit comments

Comments
 (0)