forked from agabrown/AstroStats-II
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextrastochastics.py
87 lines (70 loc) · 2.26 KB
/
extrastochastics.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
"""
extrastochastics.py
Custom stochastic variables for use with pymc.
"""
from pymc import stochastic_from_dist
import numpy as np
def oneOverXFourth_like(x, lower, upper):
R"""
Log-likelihood for stochastic variable with 1/x^4 distribution
.. math::
f(x \mid lower, upper) = \frac{x^{-4}}{lower^{-3}-upper^{-3}}
:Parameters
x : float
:math`lower \leq x \leq upper`
lower : float
Lower limit
upper : float
Upper limit
"""
if np.any(x < lower) or np.any(x > upper):
return -np.Inf
else:
return -4.0*np.sum(np.log(x))
def random_oneOverXFourth(lower, upper, size):
lowerMinThird=np.power(lower,-3.0)
upperMinThird=np.power(upper,-3.0)
return np.power(lowerMinThird-np.random.random_sample(size)*(lowerMinThird-upperMinThird),-1.0/3.0)
OneOverXFourth=stochastic_from_dist('OneOverXFourth', oneOverXFourth_like, random_oneOverXFourth, dtype=np.float)
def oneOverXSecond_like(x, lower, upper):
R"""
Log-likelihood for stochastic variable with 1/x^2 distribution
.. math::
f(x \mid lower, upper) = \frac{x^{-2}}{lower^{-1}-upper^{-1}}
:Parameters
x : float
:math`lower \leq x \leq upper`
lower : float
Lower limit
upper : float
Upper limit
"""
if np.any(x < lower) or np.any(x > upper):
return -np.Inf
else:
return -2.0*np.sum(np.log(x))
def random_oneOverXSecond(lower, upper, size):
oneOverLower=1.0/lower
oneOverUpper=1.0/upper
return 1.0/(oneOverLower-np.random.random_sample(size)*(oneOverLower-oneOverUpper))
OneOverXSecond=stochastic_from_dist('OneOverXSecond', oneOverXSecond_like, random_oneOverXSecond, dtype=np.float)
def oneOverX_like(x, lower, upper):
R"""
Log-likelihood for stochastic variable with 1/x distribution
.. math::
f(x \mid lower, upper) = \frac{x^{-1}}{\ln(upper)-\ln(lower)}
:Parameters
x : float
:math`lower \leq x \leq upper`
lower : float
Lower limit
upper : float
Upper limit
"""
if np.any(x < lower) or np.any(x > upper):
return -np.Inf
else:
return -np.sum(np.log(x))
def random_oneOverX(lower, upper, size):
return np.exp(np.log(lower)+np.random.random_sample(size)*(np.log(upper)-np.log(lower)))
OneOverX=stochastic_from_dist('OneOverX', oneOverX_like, random_oneOverX, dtype=np.float)