-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhds_bilby_ml.py
465 lines (363 loc) · 15 KB
/
hds_bilby_ml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python
"""
All log-likelihoods for bilby runs
"""
import os
import bilby
import math
import numpy as np
import matplotlib.pyplot as plt
# To run GPU trained model with CPU device
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
try:
from tensorflow.keras.models import load_model
except ImportError:
pass
# Output folders
label = 'cmb'
outdir = 'outdir'
class MLFunction():
""" Base class for functions that use a machine learning algorithm to
provide function values """
def __init__(self, *args, **kwargs):
# Check if TF is installed
try:
load_model
except NameError:
raise ImportError(
"The `tensorflow` package is not installed. This is needed in "
"order to run `MLFunction`s. See the wiki on our GitHub "
"project for installation instructions.")
# Define object properties
self.packageloc = self._get_package_location()
self.model = None
# Check definitions in parent class
if not hasattr(self, 'modelname'):
self.modelname = []
raise Exception("MLFunction should define modelname.")
is_standardised = hasattr(self, 'x_mean') and hasattr(self, 'x_stdev')
if not is_standardised:
self.x_mean, self.x_stdev = None, None
raise Exception(
"MLFunctions should either define x_mean and x_stdev or "
"x_min and x_max.")
is_standardised = hasattr(self, 'y_mean') and hasattr(self, 'y_stdev')
if not is_standardised:
self.y_mean, self.y_stdev = None, None
raise Exception(
"MLFunctions should either define y_mean and y_stdev or "
"y_min and y_max.")
super(MLFunction, self).__init__(*args, **kwargs)
def _get_package_location(self):
""" Get location in which the package was installed """
this_dir, _ = os.path.split(__file__)
return this_dir
def _load_model(self):
""" Load ML model from package """
model_path = "ml_functions/{}/{}".format(self.modelname,
"model.hdf5")
self.model = load_model(model_path)
def _normalise(self, x, mu, sigma):
return (x - mu) / sigma
def _unnormalise(self, x, mu, sigma):
return x * sigma + mu
def _evaluate(self, x):
# Load model is not already done
if self.model is None:
self._load_model()
# Correct shape of x is needed
if len(x.shape) == 1:
x = x.reshape(1, -1)
# Query to model
x = self._normalise(x, self.x_mean, self.x_stdev)
y = self.model.predict(x)
y = self._unnormalise(y, self.y_mean, self.y_stdev)
return y
class CMB(MLFunction, bilby.Likelihood):
"""
N-dimensional Rosenbrock as defined in
https://en.wikipedia.org/wiki/Rosenbrock_function
Args:
dimensionality: Number of input dimensions the function should take.
"""
def __init__(self):
self.modelname = 'lcdm'
self.dim = 6
self.x_mean = np.array([
0.95985349, 1.04158987, 0.02235954, 0.11994063, 0.05296935,
3.06873361], np.float64)
self.x_stdev = np.array([
0.00836984, 0.00168727, 0.00043045, 0.00470686, 0.00899083,
0.02362278], np.float64)
self.y_mean = 381.7929565376543
self.y_stdev = 1133.7707883293974
# Limit sampling between these hard borders, in order to ALWAYS remain
# within the training box.
ranges = []
x_min = [0.92, 1.037, 0.02, 0.1, 0.0100002, 2.98
]
x_max = [0.999999, 1.05, 0.024, 0.14, 0.097, 3.16
]
for i in range(len(x_min)):
ranges.append([x_min[i], x_max[i]])
self.ranges = ranges
parameters = {'omega_b':0, 'omega_cdm':0, 'theta_s':0, 'ln[A_s]':0, 'n_s':0, 'tau_r':0}
labels = np.array(['$\\omega_b$', '$\\omega_{cdm}$', '$\\theta_s$', '$ln[A_s]$', '$n_s$', '$\\tau_r$'])
self.names = np.array(['omega_b', 'omega_cdm', 'theta_s', 'ln[A_s]', 'n_s', 'tau_r'])
#Uniform priors are assumed
priors = dict()
i = 0
for key in parameters:
priors[key] = bilby.core.prior.Uniform(ranges[i][0], ranges[i][1], labels[i])
i += 1
self.priors = priors
super(CMB, self).__init__(parameters=parameters)
def log_likelihood(self):
if self.model is None:
self._load_model()
x = np.array([self.parameters[self.names[i]] for i in range(self.dim)])
if len(x.shape) == 1:
x = x.reshape(1, -1)
x = self._normalise(x, self.x_mean, self.x_stdev)
y = self.model.predict(x)
y = self._unnormalise(y, self.y_mean, self.y_stdev)
return -y[0][0]
class MSSM7(MLFunction, bilby.Likelihood):
"""
N-dimensional Rosenbrock as defined in
https://en.wikipedia.org/wiki/Rosenbrock_function
Args:
dimensionality: Number of input dimensions the function should take.
"""
def __init__(self):
self.modelname = 'mssm7'
self.dim = 12
self.x_mean = np.array([
-1.65550622e+02, 6.53242357e+07, -6.04267288e+06, 1.15227686e+07,
-8.96546390e+02, 1.20880748e+03, 3.65456629e+01, 1.73423279e+02,
1.18539912e-01, 4.00306869e-01, 4.31081695e+01, 5.80441328e+01
], np.float64)
self.x_stdev = np.array([
3.13242671e+03, 2.64037878e+07, 4.32735828e+06, 2.22328069e+07,
2.33891832e+03, 6.20060930e+03, 1.40566829e+01, 3.83628710e-01,
2.51362291e-04, 4.59609868e-02, 3.09244370e+00, 3.24780776e+00
], np.float64)
self.y_mean = -262.5887645450105
self.y_stdev = 7.461633956842537
# Limit sampling between these hard borders, in order to ALWAYS remain
# within the training box.
ranges = []
x_min = [
-7.16775760e+03, 4.27547804e+05, -9.98192815e+07, -6.81824964e+07,
-9.99995488e+03, -9.99999903e+03, 3.00597043e+00, 1.71060011e+02,
1.16700013e-01, 2.00000156e-01, 1.90001455e+01, 3.10001673e+01
]
x_max = [
7.18253463e+03, 9.99999857e+07, 4.56142832e+05, 9.99999734e+07,
9.99987623e+03, 9.99999881e+03, 6.99999394e+01, 1.75619963e+02,
1.20299997e-01, 7.99999435e-01, 6.69997800e+01, 8.49983345e+01
]
for i in range(len(x_min)):
ranges.append([x_min[i], x_max[i]])
self.ranges = ranges
parameters = {'M2':0, 'mf2':0, 'mHu2':0, 'mHd2':0, 'Au':0, 'Ad':0, 'tanb':0, 'mt':0, 'alphas':0, 'rho0':0, 'sigmas':0, 'sigmal':0}
labels = np.array(['$M_2$', '$m_f^2$', '$m_{H_u}^2$', '$m_{H_d}^2$', '$A_u$', '$A_d$', 'tan \\beta', '$m_t$', '$\\alpha_s$', '$\\rho_0$', '$\\sigma_s$', '$\\sigma_l$'])
self.names = np.array(['M2', 'mf2', 'mHu2', 'mHd2', 'Au', 'Ad', 'tanb', 'mt', 'alphas', 'rho0', 'sigmas', 'sigmal'])
#Uniform priors are assumed
priors = dict()
i = 0
for key in parameters:
priors[key] = bilby.core.prior.Uniform(ranges[i][0], ranges[i][1], labels[i])
i += 1
self.priors = priors
super(MSSM7, self).__init__(parameters=parameters)
def log_likelihood(self):
if self.model is None:
self._load_model()
x = np.array([self.parameters[self.names[i]] for i in range(self.dim)])
if len(x.shape) == 1:
x = x.reshape(1, -1)
x = self._normalise(x, self.x_mean, self.x_stdev)
y = self.model.predict(x)
y = self._unnormalise(y, self.y_mean, self.y_stdev)
return y[0][0]
class Rosenbrock(bilby.Likelihood):
"""
N-dimensional Rosenbrock as defined in
https://en.wikipedia.org/wiki/Rosenbrock_function
We take a = 1, b = 100
Args:
dimensionality: Number of input dimensions the function should take.
"""
def __init__(self, dimensionality=2):
if dimensionality < 2:
raise Exception("""Dimensionality of Rosenbrock function has to
be >=2.""")
self.dim = dimensionality
x_min = -3
x_max = 3
ranges = []
for i in range(self.dim):
ranges.append([x_min, x_max])
self.ranges = ranges
parameters = {"x{0}".format(i): 0 for i in range(self.dim)}
#Uniform priors are assumed
priors = bilby.core.prior.PriorDict()
priors.update({"x{0}".format(i): bilby.core.prior.Uniform(ranges[i][0], ranges[i][1], "x{0}".format(i)) for i in range(dim)})
self.priors = priors
super(Rosenbrock, self).__init__(parameters=parameters)
def log_likelihood(self):
x = np.array([self.parameters["x{0}".format(i)] for i in range(self.dim)])
y = 0
for i in range(self.dim - 1):
y += (100 * np.power(x[i + 1] - np.power(x[i], 2), 2) +
np.power(1 - x[i], 2))
return -y
class Rastrigin(bilby.Likelihood):
"""
N-dimensional Rastrigin function as defined in
https://en.wikipedia.org/wiki/Rastrigin_function
We take A = 100
Args:
dimensionality: Number of input dimensions the function should take.
"""
def __init__(self, dimensionality=2):
if dimensionality < 2:
raise Exception("""Dimensionality of Rastrigin function has to
be >=2.""")
self.a = 10
self.dim = dimensionality
x_min = -5.12
x_max = 5.12
ranges = []
for i in range(self.dim):
ranges.append([x_min, x_max])
self.ranges = ranges
parameters = {"x{0}".format(i): 0 for i in range(self.dim)}
#Uniform priors are assumed
priors = bilby.core.prior.PriorDict()
priors.update({"x{0}".format(i): bilby.core.prior.Uniform(ranges[i][0], ranges[i][1], "x{0}".format(i)) for i in range(dim)})
self.priors = priors
super(Rastrigin, self).__init__(parameters=parameters)
def log_likelihood(self):
x = np.array([self.parameters["x{0}".format(i)] for i in range(self.dim)])
y = self.a * self.dim
for i in range(self.dim):
y += np.power(x[i], 2) - self.a * np.cos(2 * np.pi * x[i])
return -y
class Himmelblau(bilby.Likelihood):
"""
Himmelblau function as defined in
https://en.wikipedia.org/wiki/Himmelblau%27s_function
This is a 2-dimensional function with an application range bounded by -5
and 5 for both input variables.
"""
def __init__(self):
self.dim = 2
x_min = -5
x_max = 5
ranges = []
for i in range(self.dim):
ranges.append([x_min, x_max])
self.ranges = ranges
parameters = {"x{0}".format(i): 0 for i in range(self.dim)}
#Uniform priors are assumed
priors = bilby.core.prior.PriorDict()
priors.update({"x{0}".format(i): bilby.core.prior.Uniform(ranges[i][0], ranges[i][1], "x{0}".format(i)) for i in range(dim)})
self.priors = priors
super(Himmelblau, self).__init__(parameters=parameters)
def log_likelihood(self):
x = np.array([self.parameters["x{0}".format(i)] for i in range(self.dim)])
y = np.power(np.power(x[0], 2) + x[1] - 11, 2) + np.power(x[0] + np.power(x[1], 2) - 7, 2)
return -y
class EggBox(bilby.Likelihood):
"""
N-dimensional EggBox as defined in arXiv:0809.3437.
Testfunction as defined in arXiv:0809.3437
Args:
dimensionality: Number of input dimensions the function should take.
"""
def __init__(self, dimensionality=2):
if dimensionality < 2:
raise Exception("""Dimensionality of EggBox function has to
be >=2.""")
self.dim = dimensionality
self.tmax = 5.0 * np.pi
x_min = 0.
x_max = 10.*np.pi
ranges = []
for i in range(self.dim):
ranges.append([x_min, x_max])
self.ranges = ranges
parameters = {"x{0}".format(i): 0 for i in range(self.dim)}
#Uniform priors are assumed
priors = bilby.core.prior.PriorDict()
priors.update({"x{0}".format(i): bilby.core.prior.Uniform(ranges[i][0], ranges[i][1], "x{0}".format(i)) for i in range(dim)})
self.priors = priors
super(EggBox, self).__init__(parameters=parameters)
def log_likelihood(self):
x = np.array([self.parameters["x{0}".format(i)] for i in range(self.dim)])
y = 1
for i in range(self.dim):
y *= math.cos(x[i]/2)
y = math.pow(2. + y, 5)
return y
class GaussianShells(bilby.Likelihood):
"""
N-dimensional GaussianShells as defined in arXiv:0809.3437.
Both number of dimensions and number of rings are arbitrary but we assume that have
an equal radius and widths
Args:
dimensionality: Number of input dimensions the function should take.
modes: number of rings
r: radius of rings
w: width of rings
c: is a n-rings x n-dims list with centers coordinates
"""
def __init__(self, dimensionality=2, modes=2, r=2, w=0.1, c=[[-3.5, 0.],[3.5, 0.]]):
if dimensionality < 2:
raise Exception("""Dimensionality of GaussianShells function has to
be >=2.""")
if modes != len(c):
raise Exception("""Number of rings (modes) must be equal to number of centers (c""")
self.dim = dimensionality
self.modes = modes
self.r = r
self.w = w
self.c = np.array(c)
self.const = math.log(1. / math.sqrt(2. * math.pi * w**2)) # normalization constant
x_min = -6
x_max = 6
ranges = []
for i in range(self.dim):
ranges.append([x_min, x_max])
self.ranges = ranges
parameters = {"x{0}".format(i): 0 for i in range(self.dim)}
#Uniform priors are assumed
priors = bilby.core.prior.PriorDict()
priors.update({"x{0}".format(i): bilby.core.prior.Uniform(ranges[i][0], ranges[i][1], "x{0}".format(i)) for i in range(dim)})
self.priors = priors
super(GaussianShells, self).__init__(parameters=parameters)
# log-likelihood of a single shell
def logcirc(self, theta, c):
d = np.sqrt(np.sum((theta - c)**2, axis=-1)) # |theta - c|
return self.const - (d - self.r)**2 / (2. * self.w**2)
def log_likelihood(self):
x = np.array([self.parameters["x{0}".format(i)] for i in range(self.dim)])
y = -240.
for i in range(self.modes):
y = np.logaddexp(y, self.logcirc(x, self.c[i]))
return y
#Functions are:
#6D-CMB, 12D-MSSM7, nD-Rosenbrock, nD-Rastrigin, 2D-Himmelblau, nD-EggBox, nD-GaussianShells
#Input is the number of dimensions for nD functions
dim = 2
# likelihood = Rosenbrock(dim)
likelihood = MSSM7()
priors = likelihood.priors
# And run sampler
result = bilby.run_sampler(
likelihood=likelihood, priors=priors, sampler='dynamic_dynesty', npoints=1000,
outdir=outdir, label=label, bound='multi', sample='unif')
result.plot_corner()
plt.show()