-
Notifications
You must be signed in to change notification settings - Fork 1
/
likelihood_tools.py
377 lines (269 loc) · 12.1 KB
/
likelihood_tools.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) Michele Mancarella <michele.mancarella@unimib.it>
#
# All rights reserved. Use of this source code is governed by the
# license that can be found in the LICENSE file.
import os
import copy
from background import BackgroundGammac
from EFTfunctions import EFTfunGammac
from perturbations import Perturbations
from multipole_signal import Signal
from mul_nu1 import interpolate_mul_nu1
mul_nu1=interpolate_mul_nu1(base_dir='..')
import numpy as np
import sys
import corner
import matplotlib.pyplot as plt
import shutil
from abc import ABC, abstractmethod
#####################################################################################
# Planck values
#####################################################################################
# Values From Planck 18, https://arxiv.org/pdf/1807.06209.pdf TT,TE,EE+lowE+lensing+BAO table 2 last column
# angular scale of the first peak
THETA_STAR = 1.04101/100
SIG_THETA_STAR = 0.00029/100
# recombination redshift
Z_STAR = 1089.80
X_STAR = -np.log(1+Z_STAR)
# sound horizon at recombination
R_STAR = 144.57 #Mpc
SIG_R_STAR = 0.22
# comoving distance to last scattering (Mpc)
DM_LS = R_STAR/THETA_STAR
SIG_DM = (THETA_STAR*SIG_R_STAR+R_STAR*SIG_THETA_STAR)/THETA_STAR**2
# % constraint ( Delta_d /d )
perc_constraint_dM_Planck = SIG_DM/DM_LS
# baryon density and h
Ombh2 = 0.02242
h_planck = 0.6766
Om_b = Ombh2/( (h_planck)**2)
Omr0hsq = 4.18343*1e-05
Omr0 = 4.18343*1e-05/h_planck**2
H0_Planck=h_planck*100
clight = 299792.458
# dimensioneless comoving distance to last scattering (Mpc)
DM_LS_DIMLS = DM_LS*H0_Planck/clight
SIG_DM_LS_DIMLS = SIG_DM*H0_Planck/clight
perc_constraint_dM_Planck = SIG_DM/DM_LS
#####################################################################################
# AUXILIARY STUFF
#####################################################################################
# Writes output both on std output and on log file
class Logger(object):
def __init__(self, fname):
self.terminal = sys.__stdout__
self.log = open(fname, "w+")
self.log.write('--------- LOG FILE ---------\n')
print('Logger created log file: %s' %fname)
#self.write('Logger')
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
#this flush method is needed for python 3 compatibility.
#this handles the flush command by doing nothing.
#you might want to specify some extra behavior here.
pass
def close(self):
self.log.close
sys.stdout = sys.__stdout__
def isatty(self):
return False
covariance_lines = { 'monopole': (0, 108),
'dipole': (108, 144),
'quadrupole': (144, 252),
'hexadecapole': (252,288) }
# import covariance at a" given redshift bin
def covariance(bin_number, which_multipoles=['monopole','dipole','quadrupole','hexadecapole'], verbose=False): #bin_number goes from 1 to 15
cov_full = np.loadtxt('Covariance_files/Cov_bin'+str(bin_number)+'_mon_dip_quad_hexa.dat')
#covs_blocks = {}
for i,m in enumerate(which_multipoles):
if i==0:
if verbose:
print('Starting from %s' %m)
all_idxs = np.arange( covariance_lines[m][0], covariance_lines[m][1] )
ndim = covariance_lines[m][1]-covariance_lines[m][0]
#for j,m1 in which_multipoles[i:]:
#idx_name = m+'_'+m1
#print('Block %s, lines/rows %s' %(idx_name, ))
else:
if verbose:
print('Adding %s ' %m)
all_idxs = np.append(all_idxs, np.arange( covariance_lines[m][0], covariance_lines[m][1] ) )
ndim+=(covariance_lines[m][1]-covariance_lines[m][0])
cov = cov_full[ np.ix_( all_idxs, all_idxs ) ]
print('Cov shape is %s' %str(cov.shape))
assert ndim==cov.shape[0]
return cov
class Distribution(ABC):
def __init__(self,):
pass
@abstractmethod
def sample(self, nsamples):
pass
@abstractmethod
def logpdf(self, x):
pass
def pdf(self, x):
return np.exp(self.logpdf(x))
class Uniform(Distribution):
'''
Uncorrelated uniform distribution in N dimensions
'''
def __init__(self, lims = [ (0,1), (0,1), ]):
self.lims=lims
self.lows = [ l[0] for l in lims]
self.highs = [ l[1] for l in lims]
self.D = len(lims)
Distribution.__init__(self)
def sample(self, nsamples):
return np.random.uniform( low=self.lows, high=self.highs, size=[nsamples, self.D]).T
def logpdf(self, x, return_sum=True):
'''
x has shape ( D, n_samples)
works also for x of shape (n_samples) (n points in 1D distribution) and (D, 1) (1 point in D dimensional distribution)
'''
x=np.array(x)
more_points_D_dim=False
one_point_D_dim=False
more_points_1D=False
if not np.isscalar(x) and x.shape[0]==self.D and np.ndim(x)>1:
more_points_D_dim = True
elif x.shape[0]==self.D and np.ndim(x)==1:
one_point_D_dim = True
elif x.shape[0]!=self.D and self.D==1:
more_points_1D = True
lp = np.empty(x.shape)
if more_points_1D:
# samples are 1D, x has more than one point
lp = np.where( (x<=self.highs[0]) & (x>=self.lows[0]), 0 , np.NINF )
elif more_points_D_dim:
lp = np.array([ np.where( (x[d, :]<=self.highs[d]) & (x[d, :]>=self.lows[d]), 0 , np.NINF ) for d in range(self.D)])
elif one_point_D_dim:
lp = np.array([ 0 if ( x[d]<=np.nan_to_num(self.highs[d])) & (x[d]>=np.nan_to_num(self.lows[d])) else np.NINF for d in range(self.D)])
if return_sum and not more_points_1D:
return lp.sum(axis=0)
else:
return lp
def get_theta(x, pinf, pfix, allpars, alB_alM_constraint=False):
# pfix is a dictionary {parameter_name: parameter_value}
# pinf is a list of parameters names
# allpars is a list with names of all parameters in the correct order (i.e. the same order as theta)
# alB_alM_constraint; if True, we will fix alphaB = alphaM/2 . In this case alB has to be
# included in pfix
allp = copy.deepcopy(pfix)
for i,p in enumerate(pinf):
allp[p] = x[i]
if alB_alM_constraint and p=='alphaM':
allp['alphaB'] = x[i]/2
return np.array([ allp[pname] for pname in allpars ])
def get_init_point_flat(prior, expected_vals, nwalkers, ndim, eps, verbose=False):
allinit = np.empty( (nwalkers, ndim))
for i in range(ndim):
if expected_vals[i]!=0:
linf = expected_vals[i]*np.abs( (1-eps) )
lsup = expected_vals[i]*np.abs( (1+eps) )
else:
linf = -eps
lsup = eps
pinf = max( linf, prior.lims[i][0] )
psup = min( lsup, prior.lims[i][1])
if verbose:
print('For param %s, eps=%s, min=%s, max=%s, central value=%s' %(i, eps, pinf, psup, expected_vals[i]))
for k in range(nwalkers):
allinit[k, i] = np.random.uniform( low= pinf, high=psup, size=1)
assert np.all(~np.isnan(allinit))
return allinit
def plot_corner(samples, settings, fiducials, myPrior, out_path, nsteps):
try:
print('Plotting corner...')
eps=0.0005
myrange=[ ( samples[:, i].min()*(1-eps), samples[:, i].max()*(1+eps)) for i in range(samples.shape[1])]
_ = corner.corner(samples, labels=settings["params_inference"],
range=myrange,
truths=[fiducials[settings["which_fid"]][p] for p in settings["params_inference"]],
quantiles=[0.05, 0.95],
show_titles=True, title_kwargs={"fontsize": 12},
smooth=0.5, color='darkred',
levels=[0.68, 0.90],
density=True,
verbose=False,
plot_datapoints=True,
fill_contours=True,
)
plt.savefig(os.path.join(out_path, 'corner_%s.png'%nsteps) )
plt.close('all')
print("Corner ok")
except Exception as e:
print(e)
print('No corner for this event!')
#####################################################################################
# solvers and likelihoods
#####################################################################################
def solve_bg(aM, aB, gamma_par, w, settings, verbose=False, is_beta_gamma=True, H_int='backward'):
# gamma_par is either beta_gamma or gamma_c0 depending on the bg paramterization
if verbose:
print('Solving background from z_in=%s...'%settings['zin'])
if is_beta_gamma:
if verbose:
print('Solving background using beta_gamma parametrization')
#EFT = EFTfunBetaGamma(aM, aB, gamma_par, tol=0.0)
#BG = BackgroundBetaGamma( settings['Om0Pl'], settings['Ob0Pl'], w, EFT, zin=settings['zin'], tol=0)
raise ValueError("beta_gamma parametrization still not supported for this version !")
else:
if verbose:
print('Solving background using gammaC parametrization')
print('am, ab, g, w = %s, %s, %s, %s' %(aM, aB, gamma_par, w))
EFT = EFTfunGammac(aM, aB, gamma_par, tol=0.)
xincs = -np.log(1+settings['zin_pert'])
BG = BackgroundGammac( settings['Om0Pl'], settings['Ob0Pl'], w, EFT, zin=settings['zin'], tol=1e-15, Or0=Omr0, xin_cs=xincs)
BG.solve( method='DOP853', res=100, rtol=settings['tolBG'], atol=settings['tolBG']**2, verbose=verbose, H_int = H_int)
if not BG.is_stable():
if verbose:
print('Unstable background.')
#return np.NINF
else:
if verbose:
print('Stable background.')
return BG
def signal(aM, aB, gamma_par, w, X, b1B, b1F, b2B, b2F, sB1, sB2, sB3, sF1, sF2, sF3, settings, is_beta_gamma=True, H_int='backward', verbose=False, return_object=False): # compute the signal
myBG = solve_bg(aM, aB, gamma_par, w, settings, is_beta_gamma=is_beta_gamma, verbose=verbose, H_int=H_int)
if (not myBG.is_stable()) or ( not myBG.is_X_physical(X) ):
#zgrid = np.exp(-myBG.xpoints)-1
#plt.plot(zgrid, myBG.cs2al(myBG.xpoints), label='cs2_al', ls='--', lw=3, )
#plt.savefig(os.path.join(FLAGS.fout, 'cs2al.png'))
if not return_object:
return np.NINF, 1.
else:
# DEBUGGING ONLY
return myBG, 1.
if verbose:
print('Solving perturbations from z_in=%s...'%settings['zin_pert'])
myPert = Perturbations(myBG, zin=settings['zin_pert'], tol=1e-15, X=X)
myPert.solve(method='DOP853', res=100, rtol=settings['tolPert'], atol=settings['tolPert']**2) # the value of atol is not a typo!!!
if verbose:
print('Computing signal...')
mySignal = Signal(myBG, myPert, mul_nu1, X=X, H0=1/2997.9, b1 = [b1B, b1F], b2 = [b2B, b2F], sB_fit = [sB1, sB2, sB3], sF_fit = [sF1, sF2, sF3], which_multipoles=settings["which_multipoles"], zin=settings["zin_pert"], dipole_boost=settings["dipole_boost"] )
sig = []
for z in settings['z']:
s_ = np.array( mySignal.calculate_signal( np.array(settings['d']), z)).flatten()
sig.append(s_)
#print('signal at z= %s' %z)
#print(s_)
# Compute distance to last scattering
if settings["use_CMB"]:
zz = np.geomspace( 1e-10, Z_STAR, 500) # from today to last scattering
Evals_z = myBG.h( -np.log(1+zz) )
d_M = np.trapz(1/Evals_z, zz)
else:
d_M=0.
if verbose:
print('Done.')
if not return_object:
return sig, d_M
else:
# just for debugging
return mySignal, d_M