-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhod.py
More file actions
299 lines (251 loc) · 10.5 KB
/
hod.py
File metadata and controls
299 lines (251 loc) · 10.5 KB
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
import defaults
import numpy
from scipy import special
"""Classes for implementing a halo occupation distribution.
A halo occupation distribution describes the population of a given dark matter
halo. In principle, this distribution can be a function of any number of halo
and galaxy variables. For the purposes of this implementation, we allow for
the HOD to vary as a function of halo mass and redshift. Any HOD-derived
object should be capable of returning the first and second moments of the
distribution as a function of mass and redshift.
"""
__author__ = "Chris Morrison <morrison.chrisb@gmail.com>"
class HOD(object):
"""Base class for a halo occupation distribution.
The exact form of an HOD will vary depending on the galaxy type and
redshift. Our base class defines the API that all of the derived class
instances need to implement.
"""
def __init__(self, hod_dict):
self.hod_dict = hod_dict
self._hod = {}
self._hod[1] = self.first_moment
self._hod[2] = self.second_moment
### These variables are useful for focusing the halo model mass integral
### on non-zero ranges of the integrand. Default -1 forces code to
### integrate over the whole mass range.
self.first_moment_zero = -1
self.second_moment_zero = -1
### Optional value to declare at which mass value the hod is guaranteed
### to evaluate to positive definite.
self._safe_norm = -1
def first_moment(self, mass, z=None):
"""
Expected number of galaxies per halo, <N> as a function of mass and
redshift.
Args:
mass: float array Halo mass in M_Solar/h^2
z: float redshift to evalute the first moment if redshift
dependent
Returns:
float array of <N>
"""
return 1.0
def second_moment(self, mass, z=None):
"""
Expected number of galaxy pairs per halo, <N(N-1)> as a function of
mass and redshift.
Args:
mass: float array Halo mass in M_Solar/h^2
redshift: float redshift to evalute the first moment if redshift
dependent
Returns:
float array of <N(N-1)>
"""
return 1.0
def nth_moment(self, mass, n=3, z=None):
"""
Expected number galaxy moment, <N(N-1)...(N-n+1)> as a function of
mass and redshift.
Args:
mass: float array Halo mass in M_Solar/h^2
redshift: float redshift to evalute the first moment if redshift
dependent
n: integer moment to compute
Returns:
float array of <N(N-1)...(N-n+1)>
"""
exp_nth = 0.0
if n in self._hod:
exp_nth = self._hod[n](mass, z)
else:
first_mom = self.first_moment(mass, z)
exp_nth = first_mom**n
alpha_m2 = numpy.where(
first_mom != 0.0,
self.second_moment(mass, z)/first_mom**2, 0.0)
for j in xrange(n):
exp_nth *= (j*alpha_m2 - j + 1)
return exp_nth
def get_hod(self):
"""
Return the interanl dictionary defining an HOD.
"""
return self.hod_dict
def set_hod(self, hod_dict):
"""
Set the internal valeus of the hod.
Args:
hod_dict: a dictionary defining an HOD.
"""
self.__init__(hod_dict)
def set_halo(self, halo_dict):
pass
def write(self, output_file_name):
mass_max = 1.0e16
mass_min = 1.0e9
dln_mass = (numpy.log(mass_max) - numpy.log(mass_min))/200
ln_mass_max = numpy.log(mass_max) + dln_mass
ln_mass_min = numpy.log(mass_min) - dln_mass
ln_mass_array = numpy.arange(
ln_mass_min, ln_mass_max + dln_mass, dln_mass)
f = open(output_file_name, "w")
for ln_mass in ln_mass_array:
mass = numpy.exp(ln_mass)
f.write("%1.10f %1.10f %1.10f %1.10f\n" % (
mass, self.first_moment(mass), self.second_moment(mass),
self.nth_moment(mass,None,3)))
f.close()
class HODPoisson(HOD):
def __init__(self):
pass
class HODBinomial(HOD):
def __init__(self, n_max, min_mass, mass_max, p_m_spline):
pass
class HODZheng(HOD):
"""
HOD object describing the model from Zheng2007. Input is a dictionary
of variable names and values. Names for these variables are listed below.
Attributes:
M_min: Minimum mass for a halo to to contain a central galaxy
sigma: with of central the central galaxy turn on
M_0: minimum mass for a halo to contain satellite galaxies. Note:
Wake et al. 2011 and Zehavi et al. 2011 show show that M_0~M_min
M_1p: Mass differential at which a halo contains one satellite (M-M_0).
alpha: slope of the satellite number counts. Note: This is motivated to
be 1. (see references above)
"""
def __init__(self, hod_dict=None):
if hod_dict is None:
### HOD parameters from Zehavi et al. 2011 with parameter
### assumptions from Wake et al. 2011.
self.log_M_min = 12.14
self.sigma = 0.15
self.log_M_0 = 12.14
self.log_M_1p = 13.43
self.alpha = 1.0
else:
self.log_M_min = hod_dict['log_M_min']
self.sigma = hod_dict['sigma']
self.log_M_0 = hod_dict['log_M_0']
self.log_M_1p = hod_dict['log_M_1p']
self.alpha = hod_dict['alpha']
HOD.__init__(self, hod_dict)
### These variables are useful for focusing the halo model mass integral
### on non-zero ranges of the integrand. Default -1 forces code to
### integrate over the whole mass range.
self.first_moment_zero = numpy.power(
10.0, self.log_M_min +
self.sigma*special.erfinv(
2.*defaults.default_precision['halo_precision'] - 1.0))
# self.first_moment_zero = numpy.power(10, self.log_M_min -
# 6*self.sigma)
self.second_moment_zero = 10.0**self.log_M_0
if self.second_moment_zero < self.first_moment_zero:
self.secon_moment_zero = self.first_moment_zero
self._safe_norm = 10.0**(self.log_M_min + 1.0*self.sigma)
def first_moment(self, mass, z=None):
return (self.central_first_moment(mass) +
self.satellite_first_moment(mass))
def second_moment(self, mass, z=None):
n_sat = self.satellite_first_moment(mass)
return (2 + n_sat)*n_sat
def central_first_moment(self, mass):
"""
Expected number of central galaxies in a halo, <N> as a function of
mass and redshift.
Args:
mass: float array Halo mass in M_Solar/h^2
redshift: float redshift to evalute the first moment if redshift
dependent
Returns:
float array of <N_c>
"""
if self.sigma <= 0.0:
log_mass = numpy.log10(mass)
return numpy.where(log_mass > self.log_M_min, 1.0, 0.0)
return 0.5*(1+special.erf((numpy.log10(mass) -
self.log_M_min)/self.sigma))
def satellite_first_moment(self, mass):
"""
Expected number of satellite galaxies in a halo, <N> as a function of
mass and redshift.
Args:
mass: float array Halo mass in M_Solar/h^2
redshift: float redshift to evalute the first moment if redshift
dependent
Returns:
float array of <N_s>
"""
diff = mass - numpy.power(10, self.log_M_0)
return numpy.where(diff > 0.0,
self.central_first_moment(mass)*
numpy.power(diff/(10**self.log_M_1p), self.alpha),
0.0)
class HODMandelbaum(HOD):
"""
HOD object describing the model from Mandelbaum2005. Input is a dictionary
of variable names and values. Names for these variables are listed below.
Attributes:
log_M_0: Minimum mass for a halo to contain a central galaxy. Also
defines the transition of the satellite galaxy profile from ~M**2 at
<3*M_0 to ~M at >3*M_0
w: Normalization of the satellite galaxy profile. Halos will contain
1 satellite galaxy at a mass of 3*M0/w. This quantity is related
to the satellite fraction alpha of Mand2005 where alpha is
M_total_sat/M_total where M_total mass of the halos studied and
M_total_sat is the total mass in satellites.
"""
def __init__(self, hod_dict = None):
if hod_dict is None:
self.log_M_0 = 12.14
self.log_M_min = numpy.log10(3.0) + 12.14
self.w = 1.0
else:
self.log_M_0 = hod_dict["log_M_0"]
self.log_M_min = numpy.log10(3.0) + hod_dict["log_M_0"]
self.w = hod_dict["w"]
HOD.__init__(self, hod_dict)
def first_moment(self, mass, z=None):
return (self.central_first_moment(mass) +
self.satellite_first_moment(mass))
def second_moment(self, mass, z=None):
n_sat = self.satellite_first_moment(mass)
return (2 + n_sat)*n_sat
def central_first_moment(self, mass, z=None):
"""
Expected number of central galaxies in a halo, <N> as a function of
mass and redshift.
Args:
mass: float array Halo mass in M_Solar/h^2
redshift: float redshift to evalute the first moment if redshift
dependent
Returns:
float array of <N>
"""
return numpy.where(numpy.log10(mass) >= self.log_M_0,
1.0, 0.0)
def satellite_first_moment(self, mass, z=None):
"""
Expected number of satellite galaxies in a halo, <N> as a function of
mass and redshift.
Args:
mass: float array Halo mass in M_Solar/h^2
redshift: float redshift to evalute the first moment if redshift
dependent
Returns:
float array of <N>
"""
return numpy.where(numpy.log10(mass) < self.log_M_min,
(mass/10**self.log_M_min)**2*self.w,
mass/10**self.log_M_min*self.w)