-
Notifications
You must be signed in to change notification settings - Fork 0
/
schechter_functions.py
308 lines (244 loc) · 7.48 KB
/
schechter_functions.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
"""
schechter function classes
"""
import numpy as np
from warnings import warn
try:
from mpmath import gammainc
no_mpmath = False
except ImportError:
from scipy.special import gammainc
no_mpmath = True
__all__=['Schechter', 'MagSchechter', 'LogSchechter']
class Schechter():
"""
Schechter function class
"""
def __init__(self, phi0, x0, alpha):
"""
"""
self.phi0 = phi0
self.x0 = x0
self.alpha = alpha
def __call__(self, x):
"""
"""
x = np.atleast_1d(x)
norm = self.phi0/self.x0
val = norm * (x/self.x0)**self.alpha * np.exp(-x)
return val
def rvs(self, x_min, size=100, max_iter=100):
"""
Parameters
----------
size : int
number of random variates to return
max_iter : int
maximum number of iterations to preform when calculating random variates
Returns
-------
x : numpy.array
array of random variates sampled from the Schechter function
Notes
-----
"""
return _sample_schechter(self.x0, self.alpha, x_min, size=size, max_iter=max_iter)
def number_density(self, a, b):
"""
Intgrate the Schechter function over the bounds [a,b].
Parameters
----------
a : float
faint limit
b : float
bright limit
Returns
-------
N : numpy.array
Notes
-----
"""
if no_mpmath & (self.alpha <= 0):
msg = ('mpmath packlage must be installed in order',
'to perform this calculation for alpha<=0.')
raise ValueError(msg)
if not isinstance(a, float):
msg = ('`a` argument must be a float.')
raise ValueError(msg)
if not isinstance(b, float):
msg = ('`b` argument must be a float.')
raise ValueError(msg)
a = a / self.x0
b = b / self.x0
l = float(gammainc(self.alpha + 1, a))
r = float(gammainc(self.alpha + 1, b))
return (l - r)*self.phi0
class MagSchechter(object):
"""
Magnitudes Schechter function class
"""
def __init__(self, phi0, M0, alpha):
"""
"""
self.phi0 = phi0
self.M0 = M0
self.alpha = alpha
def __call__(self, m):
"""
"""
m = np.atleast_1d(m)
norm = (2.0/5.0)*self.phi0*np.log(10.0)
val = norm*(10.0**(0.4*(self.M0-m)))**(self.alpha+1.0)*np.exp(-10.0**(0.4*(self.M0-m)))
return val
def rvs(self, m_max, size=100, max_iter=100):
"""
Parameters
----------
size : int
number of random variates to return
max_iter : int
maximum number of iterations to preform when calculating random variates
Returns
-------
x : numpy.array
array of random variates sampled from the Schechter function
Notes
-----
"""
x_min = 10**(-0.4*m_max)
x0 = 10**(-0.4*self.M0)
x = _sample_schechter(x0, self.alpha, x_min, size=size, max_iter=max_iter)
return -2.5*np.log10(x)
def number_density(self, a, b):
"""
Intgrate the Schechter function over the bounds [a,b].
Parameters
----------
a : float
faint limit
b : float
bright limit
Returns
-------
N : numpy.array
Notes
-----
"""
if no_mpmath & (self.alpha <= 0):
msg = ('mpmath packlage must be installed in order',
'to perform this calculation for alpha<=0.')
raise ValueError(msg)
if not isinstance(a, float):
msg = ('`a` argument must be a float.')
raise ValueError(msg)
if not isinstance(b, float):
msg = ('`b` argument must be a float.')
raise ValueError(msg)
x0 = 10**(-0.4*self.M0)
a = 10**(-0.4*a) / x0
b = 10**(-0.4*b) / x0
l = float(gammainc(self.alpha + 1, a))
r = float(gammainc(self.alpha + 1, b))
return (l - r)*self.phi0
class LogSchechter():
"""
Log Schecter function class
"""
def __init__(self, phi0, x0, alpha):
"""
"""
self.phi0 = phi0
self.x0 = x0
self.alpha = alpha
def __call__(self, x):
"""
"""
x = np.atleast_1d(x)
norm = np.log(10.0)*self.phi0
val = norm*(10.0**((x-self.x0)*(1.0+self.alpha)))*np.exp(-10.0**(x-self.x0))
return val
def rvs(self, x_min, size=100, max_iter=100):
"""
Parameters
----------
size : int
number of random variates to return
max_iter : int
maximum number of iterations to preform when calculating random variates
Returns
-------
x : numpy.array
array of random variates sampled from the Schechter function
Notes
-----
"""
x_min = 10**(x_min)
x0 = 10**(self.x0)
x = _sample_schechter(x0, self.alpha, x_min, size=size, max_iter=max_iter)
return np.log10(x)
def number_density(self, a, b):
"""
Intgrate the Schechter function over the bounds [a,b].
Parameters
----------
a : float
faint limit
b : float
bright limit
Returns
-------
N : numpy.array
Notes
-----
"""
if no_mpmath & (self.alpha <= 0):
msg = ('mpmath packlage must be installed in order',
'to perform this calculation for alpha<=0.')
raise ValueError(msg)
if not isinstance(a, float):
msg = ('`a` argument must be a float.')
raise ValueError(msg)
if not isinstance(b, float):
msg = ('`b` argument must be a float.')
raise ValueError(msg)
x0 = 10**(self.x0)
a = 10**(a) / x0
b = 10**(b) / x0
l = float(gammainc(self.alpha + 1, a))
r = float(gammainc(self.alpha + 1, b))
return (l - r)*self.phi0
def _sample_schechter(x0, alpha, x_min, size=100, max_iter=1000):
"""
Return random samples from a schechter function.
This method follows Richard Gill's method given here:
http://www.math.leidenuniv.nl/~gill/teaching/astro/stanSchechter.pdf
Parameters
----------
size : int
number of random variates to return
max_iter : int
maximum number of iterations to preform when calculating random variates
Returns
-------
x : numpy.array
array of random variates sampled from the Schechter function
Notes
-----
"""
out = []
n = 0
num_iter = 0
while (n<size) & (num_iter<max_iter):
x = np.random.gamma(scale=x0, shape=alpha+2, size=size)
x = x[x>x_min]
u = np.random.uniform(size=x.size)
x = x[u<x_min/x]
out.append(x)
n+=x.size
num_iter += 1
if num_iter >= max_iter:
msg = ("The maximum number of iterations reached.",
"Random variates may not be representitive.",
"Try increasing `max_iter`.")
print(msg)
return np.concatenate(out)[:size]