-
Notifications
You must be signed in to change notification settings - Fork 1
/
BFisherutils.py
344 lines (277 loc) · 10.4 KB
/
BFisherutils.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
import scipy.interpolate as itp
import numpy as np
from interpolation import interp
import pdb
PS = np.loadtxt('test_matterpower.dat')
Pbao = np.loadtxt('Pbao.txt')
Psmooth = np.loadtxt('Psmooth.txt')
Piso = itp.interp1d(PS[:,0],PS[:,1], kind='cubic', fill_value=0)
Pbao_itp = itp.interp1d(Pbao[:,0], Pbao[:,1], kind='cubic', fill_value=0)
Psmooth_itp = itp.interp1d(Psmooth[:,0], Psmooth[:,1], kind='cubic', fill_value=0)
def Pk_new(var, parc):
k, mu = var
A, am1, am2, a0, a1, a2, apar, aper = parc
ak = k*np.sqrt(aper**2+mu**2*(apar**2-aper**2))
return A*(0.64*Psmooth_itp(k) + am2/k**2 + am1/k + a0 + a1*k + a2*k**2)*Pbao_itp(ak)
def Pk_vec(var, parc):
K, Mu = var
A, am1, am2, a0, a1, a2, apar, aper = parc
K = K.reshape(len(K),1)
Mu = Mu.reshape(len(Mu),1)
aK = np.dot(K, np.sqrt(aper**2+Mu.T**2*(apar**2-aper**2)))
Pbao_vec = Pbao_itp(aK)
Psmooth_vec = Psmooth_itp(K)
return A*(0.64*Psmooth_vec + am2/K**2 + am1/K + a0 + a1*K + a2*K**2)*Pbao_vec
def Pk_aniso(var,parc):
'''
Anisotropic power spectrum.
var = k, mu
parc = f, b1
'''
k, mu = var
apar, aper, f, b1, b2 = parc
k = k.reshape(len(k),1)
mu = mu.reshape(len(mu),1)
k = np.dot(k, np.sqrt(aper**2+mu.T**2*(apar**2-aper**2)))
mu = mu*apar/np.sqrt(aper**2+mu**2*(apar**2-aper**2))
return (b1 + f*mu.T)**2*Piso(k)
def Pk(var,parc):
'''
Anisotropic power spectrum.
var = k, mu
parc = f, b1
'''
k, mu = var
apar, aper, f, b1, b2 = parc
k = k*np.sqrt(aper**2+mu**2*(apar**2-aper**2))
mu = mu*apar/np.sqrt(aper**2+mu**2*(apar**2-aper**2))
return (b1 + f*mu)**2*Piso(k)
def Bisp_vec(var,parc,pars):
'''
Bispectrum as a function of 5 variables - k1,k2,k3,mu1,phi12 and cosmological
parameters parc.
This doesn't check for triangular condition.
'''
k1_v, k2_v, k3_v, mu1_v, phi12_v = var
apar, aper, f, b1, b2 = parc
nave, Vs = pars
lk1, lk2, lk3, lmu, lphi = len(k1_v), len(k2_v), len(k3_v), len(mu1_v), len(phi12_v)
k1 = k1_v.reshape(lk1,1,1,1,1) * np.ones((lk1,lk2,lk3,lmu,lphi))
k2 = k2_v.reshape(1,lk2,1,1,1) * np.ones((lk1,lk2,lk3,lmu,lphi))
k3 = k3_v.reshape(1,1,lk3,1,1) * np.ones((lk1,lk2,lk3,lmu,lphi))
mu1 = mu1_v.reshape(1,1,1,lmu,1) * np.ones((lk1, lk2, lk3, lmu, lphi))
phi12 = phi12_v.reshape(1,1,1,1,lphi) * np.ones((lk1, lk2, lk3, lmu, lphi))
mu12 = (k3**2 - k1**2 - k2**2)/2/k1/k2
mu2 = mu1*mu12 - np.sqrt(1 - mu1**2)*np.sqrt(1 - mu12**2)*np.cos(phi12)
mu3 = -(mu1*k1 + mu2*k2)/k3
# rescale for AP
k1 = k1*np.sqrt(aper**2+mu1**2*(apar**2-aper**2))
k2 = k2*np.sqrt(aper**2+mu2**2*(apar**2-aper**2))
k3 = k3*np.sqrt(aper**2+mu3**2*(apar**2-aper**2))
mu1 = mu1*apar/np.sqrt(aper**2+mu1**2*(apar**2-aper**2))
mu2 = mu2*apar/np.sqrt(aper**2+mu2**2*(apar**2-aper**2))
mu3 = mu3*apar/np.sqrt(aper**2+mu3**2*(apar**2-aper**2))
mu12 = (k3**2 - k1**2 - k2**2)/2/k1/k2
mu31 = -(k1 + k2*mu12)/k3
mu23 = -(k1*mu12 + k2)/k3
k12 = np.sqrt(k1**2 + k2**2 + 2*k1*k2*mu12)
k23 = np.sqrt(k2**2 + k3**2 + 2*k2*k3*mu23)
k31 = np.sqrt(k3**2 + k1**2 + 2*k3*k1*mu31)
Z1k1 = b1 + f*mu1**2
Z1k2 = b1 + f*mu2**2
Z1k3 = b1 + f*mu3**2
F12 = 5./7. + mu12/2*(k1/k2 + k2/k1) + 2./7.*mu12**2
F23 = 5./7. + mu23/2*(k2/k3 + k3/k2) + 2./7.*mu23**2
F31 = 5./7. + mu31/2*(k3/k1 + k1/k3) + 2./7.*mu31**2
G12 = 3./7. + mu12/2*(k1/k2 + k2/k1) + 4./7.*mu12**2
G23 = 3./7. + mu23/2*(k2/k3 + k3/k2) + 4./7.*mu23**2
G31 = 3./7. + mu31/2*(k3/k1 + k1/k3) + 4./7.*mu31**2
mu1p2 = (mu1*k1 + mu2*k2)/k12
mu2p3 = (mu2*k2 + mu3*k3)/k23
mu3p1 = (mu3*k3 + mu1*k1)/k31
Z2k12 = b2/2. + b1*F12 + f*mu1p2**2*G12
Z2k12 += f*mu1p2*k12/2.*(mu1/k1*Z1k2 + mu2/k2*Z1k1)
Z2k23 = b2/2. + b1*F23 + f*mu2p3**2*G23
Z2k23 += f*mu2p3*k23/2.*(mu2/k2*Z1k3 + mu3/k3*Z1k2)
Z2k31 = b2/2. + b1*F31 + f*mu3p1**2*G31
Z2k31 += f*mu3p1*k31/2.*(mu3/k3*Z1k1 + mu1/k1*Z1k3)
Bi = 2*Z2k12*Z1k1*Z1k2*Piso(k1)*Piso(k2)
Bi += 2*Z2k23*Z1k2*Z1k3*Piso(k2)*Piso(k3)
Bi += 2*Z2k31*Z1k3*Z1k1*Piso(k3)*Piso(k1)
'''
I think I should add this.
'''
Bi += (Pk([k1, mu1],parc) + Pk([k2,mu2], parc) + Pk([k3,mu3], parc))/nave
return Bi
def Bisp(var,parc,pars):
'''
Bispectrum as a function of 5 variables - k1,k2,k3,mu1,phi12 and cosmological
parameters parc.
This doesn't check for triangular condition.
'''
k1, k2, k3, mu1, phi12 = var
apar, aper, f, b1, b2 = parc
nave, Vs = pars
mu12 = (k3**2 - k1**2 - k2**2)/2/k1/k2
mu2 = mu1*mu12 - np.sqrt(1 - mu1**2)*np.sqrt(1 - mu12**2)*np.cos(phi12)
mu3 = -(mu1*k1 + mu2*k2)/k3
# rescale for AP
k1 = k1*np.sqrt(aper**2+mu1**2*(apar**2-aper**2))
k2 = k2*np.sqrt(aper**2+mu2**2*(apar**2-aper**2))
k3 = k3*np.sqrt(aper**2+mu3**2*(apar**2-aper**2))
mu1 = mu1*apar/np.sqrt(aper**2+mu1**2*(apar**2-aper**2))
mu2 = mu2*apar/np.sqrt(aper**2+mu2**2*(apar**2-aper**2))
mu3 = mu3*apar/np.sqrt(aper**2+mu3**2*(apar**2-aper**2))
mu12 = (k3**2 - k1**2 - k2**2)/2/k1/k2
mu31 = -(k1 + k2*mu12)/k3
mu23 = -(k1*mu12 + k2)/k3
k12 = np.sqrt(k1**2 + k2**2 + 2*k1*k2*mu12)
k23 = np.sqrt(k2**2 + k3**2 + 2*k2*k3*mu23)
k31 = np.sqrt(k3**2 + k1**2 + 2*k3*k1*mu31)
Z1k1 = b1 + f*mu1**2
Z1k2 = b1 + f*mu2**2
Z1k3 = b1 + f*mu3**2
F12 = 5./7. + mu12/2*(k1/k2 + k2/k1) + 2./7.*mu12**2
F23 = 5./7. + mu23/2*(k2/k3 + k3/k2) + 2./7.*mu23**2
F31 = 5./7. + mu31/2*(k3/k1 + k1/k3) + 2./7.*mu31**2
G12 = 3./7. + mu12/2*(k1/k2 + k2/k1) + 4./7.*mu12**2
G23 = 3./7. + mu23/2*(k2/k3 + k3/k2) + 4./7.*mu23**2
G31 = 3./7. + mu31/2*(k3/k1 + k1/k3) + 4./7.*mu31**2
mu1p2 = (mu1*k1 + mu2*k2)/k12
mu2p3 = (mu2*k2 + mu3*k3)/k23
mu3p1 = (mu3*k3 + mu1*k1)/k31
Z2k12 = b2/2. + b1*F12 + f*mu1p2**2*G12
Z2k12 += f*mu1p2*k12/2.*(mu1/k1*Z1k2 + mu2/k2*Z1k1)
Z2k23 = b2/2. + b1*F23 + f*mu2p3**2*G23
Z2k23 += f*mu2p3*k23/2.*(mu2/k2*Z1k3 + mu3/k3*Z1k2)
Z2k31 = b2/2. + b1*F31 + f*mu3p1**2*G31
Z2k31 += f*mu3p1*k31/2.*(mu3/k3*Z1k1 + mu1/k1*Z1k3)
Bi = 2*Z2k12*Z1k1*Z1k2*Piso(k1)*Piso(k2)
Bi += 2*Z2k23*Z1k2*Z1k3*Piso(k2)*Piso(k3)
Bi += 2*Z2k31*Z1k3*Z1k1*Piso(k3)*Piso(k1)
'''
I think I should add this.
'''
Bi += (Pk([k1,mu1],parc) + Pk([k2,mu2],parc) + Pk([k3,mu3],parc))/nave
return Bi
def dP(var,parc):
'''
Derivative of Power spectrum with respect to cosmological parameters - parc.
var are the 5D variables k1,k2,k3,mu1,phi12 k1, k2, k3, mu1, phi12 = var
'''
eps = 1e-6
derP = np.zeros(np.shape(parc))
P0 = Pk(var,parc)
for i in range(np.shape(parc)[0]):
parc1 = np.copy(parc)
parc1[i,:] += eps
P1 = Pk(var,parc1)
derP[i,:] = (P1 - P0)/eps
return derP
def dB(var,parc,pars):
'''
Derivative of Bispectrum with respect to cosmological parameters - parc. var are
the 5D variables k1,k2,k3,mu1,phi12
k1, k2, k3, mu1, phi12 = var
'''
eps = 1e-6
derB = np.zeros(np.shape(parc))
B0 = Bisp(var,parc,pars)
for i in range(np.shape(parc)[0]):
parc1 = np.copy(parc)
parc1[i,:] += eps
B1 = Bisp(var,parc1,pars)
derB[i,:] = (B1 - B0)/eps
return derB
def CovP(var,parc,pars):
navg, Vs = pars
C = (Pk(var,parc) + 1/navg)**2
return C
def CovP_new(var,parc,pars):
navg, Vs = pars
C = (Pk_new(var,parc) + 1/navg)**2
return C
def CovP_vec(var, parc, pars):
navg, Vs = pars
C = (Pk_vec(var,parc) + 1/navg)**2
return C
def CovB(var,parc,pars):
'''
Covariance of Bispectrum at k1,k2,k3,mu1,phi12 for fiducial cosmological
parameters - parc, and survey parameters - pars.
var = k1,k2,k3,mu1,phi12
pars = navg, V
parc = whatever goes into Pk
This doesn't check for triangular condition.
'''
k1, k2, k3, mu1, phi12 = var
navg, Vs = pars
mu12 = (k3**2 - k1**2 - k2**2)/2/k1/k2
mu2 = mu1*mu12 - np.sqrt(1 - mu1**2)*np.sqrt(1 - mu12**2)*np.cos(phi12)
mu3 = -(mu1*k1 + mu2*k2)/k3
C = Pk([k1,mu1],parc) + 1/navg
C *= Pk([k2,mu2],parc) + 1/navg
C *= Pk([k3,mu3],parc) + 1/navg
C *= Vs
return C
def CovB_vec(var,parc,pars):
'''
Covariance of Bispectrum at k1,k2,k3,mu1,phi12 for fiducial cosmological
parameters - parc, and survey parameters - pars.
var = k1,k2,k3,mu1,phi12
pars = navg, V
parc = whatever goes into Pk
This doesn't check for triangular condition.
'''
k1_v, k2_v, k3_v, mu1_v, phi12_v = var
navg, Vs = pars
lk1, lk2, lk3, lmu, lphi = len(k1_v), len(k2_v), len(k3_v), len(mu1_v), len(phi12_v)
k1 = k1_v.reshape(lk1,1,1,1,1) * np.ones((lk1,lk2,lk3,lmu,lphi))
k2 = k2_v.reshape(1,lk2,1,1,1) * np.ones((lk1,lk2,lk3,lmu,lphi))
k3 = k3_v.reshape(1,1,lk3,1,1) * np.ones((lk1,lk2,lk3,lmu,lphi))
mu1 = mu1_v.reshape(1,1,1,lmu,1) * np.ones((lk1, lk2, lk3, lmu, lphi))
phi12 = phi12_v.reshape(1,1,1,1,lphi) * np.ones((lk1, lk2, lk3, lmu, lphi))
mu12 = (k3**2 - k1**2 - k2**2)/2/k1/k2
mu2 = mu1*mu12 - np.sqrt(1 - mu1**2)*np.sqrt(1 - mu12**2)*np.cos(phi12)
mu3 = -(mu1*k1 + mu2*k2)/k3
C = Pk([k1,mu1],parc) + 1/navg
C *= Pk([k2,mu2],parc) + 1/navg
C *= Pk([k3,mu3],parc) + 1/navg
C *= Vs
return C
def CrossPB(var1,var2,parc,pars):
k1, k2, k3, mu1, phi12 = var2
navg, Vs = pars
mu12 = (k3**2 - k1**2 - k2**2)/2/k1/k2
mu2 = mu1*mu12 - np.sqrt(1 - mu1**2)*np.sqrt(1 - mu12**2)*np.cos(phi12)
mu3 = -(mu1*k1 + mu2*k2)/k3
C = Bisp(var2,parc,pars)
C += Pk([k1,mu1],parc)/navg
C += Pk([k2,mu2],parc)/navg
C += Pk([k2,mu2],parc)/navg
C += 1/navg**2
return C
def Fishz(pars,parc):
'''
Compute Bispectrum Fisher Matrix at a fixed redshift for survey parameters -
pars (that are known/fixed), and cosmological parameters - parc (that are
unknown).
pars = [nbar, Volume]
parc = [Omega_m, sigma_8, n_s, w_0, w_a, Omega_b, h0]
'''
# Bispectrum in redshift space is a 5D function. Integrate over 5D with MC.
RR = np.random.rand(NMC,5)
for i in range(NMC):
eta1 = etamax*RR[i,0]
eta2 = etamax*RR[i,1]
eta3 = etamax*RR[i,2]
k1 = np.sqrt(2*eta1)
k2 = np.sqrt(2*eta2)
k3 = np.sqrt(2*eta3)
mu1 = 2*mumax*RR[i,3] - 1
phi12 = 2*np.pi*RR[i,4]
var = [k1, k2, k3, mu1, phi12]
CB = CovB()
derB = dB()
FM += np.outer(dB,dB)/CB
return FM