-
Notifications
You must be signed in to change notification settings - Fork 5
/
likelihood.py
278 lines (229 loc) · 7.8 KB
/
likelihood.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
#!/usr/bin/env python
"""
Module to implement likelihood function for quantifying the mass fraction and mass function of dark matter substructure from strongly lensed systems.
"""
__author__ = "Alex Drlica-Wagner"
import logging
import numpy as np
import pylab as plt
from scipy import stats
from scipy import integrate
from scipy.integrate import simps,trapz,quad,nquad
from scipy.interpolate import interp1d
from scipy.misc import factorial
MMIN=4e6
MMAX=4e9
MLOW=0.3e8
MHIGH=4e9
MHIGH=MMAX
NSTEPS=(1500,1300)
#SIGMA=1e7
SIGMA = 0
ALPHA=1.9
FRAC=0.025
MHALO=1e11
def create_mass_array(log=True,nsteps=NSTEPS):
""" Create an array spanning the true and observable mass ranges.
Parameters:
-----------
p : Tuple of the range of masses (MMIN,MMAX,MHIGH,MLOW)
nsteps : Number of steps to span the ranges (NTRUE, NCONV)
log : Sample in log or linear space
Returns:
--------
m,mp,mm,mmp : The
"""
if log:
m = np.logspace(np.log10(MMIN),np.log10(MMAX),nsteps[0])
mp = np.logspace(np.log10(MLOW),np.log10(MHIGH),nsteps[1])
else:
m = np.linspace(MMIN,MMAX,nsteps[0])
mp = np.linspace(MLOW,MHIGH,nsteps[1])
mm,mmp = np.meshgrid(m,mp)
return m,mp,mm,mmp
def mhalo(radius=None):
""" Return the halo mass as a function of maximum radius.
WARNING: Returns constant MHALO independent of R!
Parameters:
-----------
radius : Maximum radius for inclused halo mass
Returns:
--------
mhalo : Enclosed halo mass
"""
return MHALO
def dP_dm_true(m,alpha):
""" True mass function (Eqn. 6) normalized over full mass range [MMIN,MMAX].
Parameters:
----------
m : True mass of subhalo
alpha : Power-law index of subhalo mass function
Returns:
--------
dP_dm_true : Normalized pdf
"""
m = np.atleast_1d(m)
ret = ((1-alpha)*m**(-alpha))/(MMAX**(1-alpha)-MMIN**(1-alpha))
ret = np.where(alpha==1,(m**-alpha)/np.log(MMAX/MMIN),ret)
return np.where(np.isfinite(ret),ret,np.nan)
def dP_dm_conv(m,mp,alpha,sigma=SIGMA):
""" The convolved mass function.
Parameters:
-----------
m : The range of true masses
mp : The range of observed masses
Returns:
--------
dP_dm_conv : The integrated convolved mass function
"""
if sigma == 0:
return dP_dm_true(np.atleast_2d(mp.T)[0],alpha)
else:
return simps(dP_dm_true(m,alpha)*stats.norm.pdf(m,loc=mp,scale=sigma),m)
def mu0(alpha, frac, radius=None):
""" Expected number of substructures from the true mass function (Eq. 5).
Parameters:
-----------
alpha : Slope of the substructure mass function
frac : Substructure mass fraction
radius: Enclosed radius
Returns:
--------
mu0 : Predicted number of substructures for the true mass function
"""
alpha = np.atleast_1d(alpha)
integral = ( (2-alpha)*(MMAX**(1-alpha) - MMIN**(1-alpha))) / \
( (1-alpha)*(MMAX**(2-alpha) - MMIN**(2-alpha)))
integral = np.where(alpha==2,-(MMAX**-1 - MMIN**-1)/np.log(MMAX/MMIN),integral)
integral = np.where(alpha==1,np.log(MMAX/MMIN)/(MMAX - MMIN),integral)
return frac * mhalo(radius) * integral
def mu(alpha, frac, radius=None, sigma=SIGMA):
""" Expected number of substructures from the observable mass function (Eq. 4)
Parameters:
-----------
alpha : Slope of the substructure mass function
frac : Substructure mass fraction
radius: Enclosed radius
sigma : Substructure mass error
Returns:
--------
mu : Predicted number of substructures for the observable mass function
"""
m,mp,mm,mmp = create_mass_array()
_mu0 = mu0(alpha, frac, radius)
_integral = simps(dP_dm_conv(mm,mmp,alpha,sigma=sigma),mp)
return _mu0 * _integral
def LogProbNumber(data, alpha, frac, R=None, sigma=SIGMA):
""" Logarithm of the joint probability for the number of substructures.
Parameters:
-----------
data : Input data
alpha: Index of the mass function
frac : Substructure mass fraction
Returns:
--------
prob : Logarithm of the joint Poission probability
"""
logging.debug(' LogProbNumber: %s'%len(data))
nsrc = data['nsrc']
_mu = mu(alpha,frac,R,sigma=sigma)
return np.sum(stats.poisson.logpmf(nsrc[:,np.newaxis],_mu),axis=0)
def LogProbMass(data, alpha, sigma=SIGMA):
""" Logarithm of the joint probability for mass of substructures.
Parameters:
-----------
data : Input data
alpha: Index of the mass function
Returns:
--------
prob: Logarithm of the joint spectral probability
"""
logging.debug(' LogProbMass: %s'%len(data))
m,mp,mm,mmp = create_mass_array()
masses = np.concatenate(data['mass'])
top = np.sum(np.log([dP_dm_conv(m,mi,alpha,sigma=sigma) for mi in masses]))
bottom = len(masses)*np.log(simps(dP_dm_conv(mm,mmp,alpha,sigma=sigma),mp))
return top - bottom
def LogLike(data, alpha, frac, sigma=SIGMA):
""" Logarithm of the joint likelihood over all lens systems.
"""
logging.debug('LogLike: %s'%len(data))
logpois = LogProbNumber(data, alpha, frac, sigma=sigma)
logprob = LogProbMass(data, alpha, sigma=sigma)
return logpois + logprob
def logprior():
return 1
def logprob():
pass
# The following methods are for simulating data
def sample(size,alpha=ALPHA):
""" Random samples of the mass function.
Parameters:
-----------
size : Number of smaples to make
alpha : Index of the mass function
Returns:
--------
mass : Random samples of the mass function
"""
x = create_mass_array(log=False,nsteps=(1e4,1e1))[0]
pdf = dP_dm_true(x,alpha)
size = int(size)
cdf = np.cumsum(pdf)
cdf = np.insert(cdf, 0, 0.)
cdf /= cdf[-1]
icdf = interp1d(cdf, range(0, len(cdf)), bounds_error=False, fill_value=-1)
u = np.random.uniform(size=size)
index = np.floor(icdf(u)).astype(int)
index = index[index >= 0]
masses = x[index]
return masses
def simulate(nlens=1, alpha=ALPHA, frac=FRAC, R=None, sigma=SIGMA):
"""Generate the simulated data set of lens, sources, and masses.
Parameters:
-----------
nlens: Number of lenses to generate.
alpha: Index of the substructure mass function
frac: Substructure mass fraction
Returns:
--------
data : Array of output lenses and substructures
"""
# First, figure out how many lenses we are sampling
m,mp,mm,mmp = create_mass_array()
pdf = dP_dm_true(m,alpha)
_mu = mu0(alpha,frac,R)
lenses = stats.poisson.rvs(_mu,size=nlens)
out = []
for i,l in enumerate(lenses):
masses = sample(l,alpha=alpha)
if sigma != 0:
masses += stats.norm.rvs(size=len(masses),scale=sigma)
sel = (masses > MLOW) & (masses < MHIGH)
mass = masses[sel]
out += [(i,len(mass),mass)]
names = ['lens','nsrc','mass']
return np.rec.fromrecords(out,names=names)
# Note the typo in VK09 for the 3 sigma p-value
levels = -stats.chi2.isf([0.0028,0.05,0.32,1.0],2)/2.
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description=__doc__)
args = parser.parse_args()
FRAC=0.005; ALPHA=1.9; MLOW=0.3e8; SIGMA=0
nlens=10; seed = 1
np.random.seed(seed)
fracs = np.linspace(0.001,0.03,151)
alphas = np.linspace(1.0,3.0,51)
data = simulate(nlens,alpha=ALPHA, frac=FRAC, sigma=SIGMA)
loglikes = np.array([LogLike(data,a,fracs) for a in alphas])
loglikes -= loglikes.max()
loglikes = loglikes.T
levels = -stats.chi2.isf([0.0028,0.05,0.32,1.0],2)/2.
plt.contourf(alphas,fracs,loglikes,levels=levels,cmap='binary')
plt.axvline(ALPHA,ls='--',c='dodgerblue')
plt.axhline(FRAC,ls='--',c='dodgerblue')
plt.colorbar(label=r'$\Delta \log {\cal L}$')
plt.xlabel(r'Slope ($\alpha$)')
plt.ylabel(r'Mass Fraction ($f$)')
plt.show()