-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.py
79 lines (60 loc) · 1.68 KB
/
func.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
import numpy as np
from scipy.stats import norm, truncnorm
### fix the number of different populations
n_pop = 4
def indicate(M, trans, i):
'''
indicate which M belongs to population i given transition parameter
'''
ts = np.insert(np.insert(trans, n_pop-1, np.inf), 0, -np.inf)
ind = (M>=ts[i]) & (M<ts[i+1])
return ind
def split_hyper_linear(hyper):
'''
split hyper and derive c
'''
c0, slope,sigma, trans = \
hyper[0], hyper[1:1+n_pop], hyper[1+n_pop:1+2*n_pop], hyper[1+2*n_pop:]
c = np.zeros_like(slope)
c[0] = c0
for i in range(1,n_pop):
c[i] = c[i-1] + trans[i-1]*(slope[i-1]-slope[i])
return c, slope, sigma, trans
def piece_linear(hyper, M, prob_R):
'''
model: straight line
'''
c, slope, sigma, trans = split_hyper_linear(hyper)
R = np.zeros_like(M)
for i in range(4):
ind = indicate(M, trans, i)
mu = c[i] + M[ind]*slope[i]
R[ind] = norm.ppf(prob_R[ind], mu, sigma[i])
return R
def ProbRGivenM(radii, M, hyper):
'''
p(radii|M)
'''
c, slope, sigma, trans = split_hyper_linear(hyper)
prob = np.zeros_like(M)
for i in range(4):
ind = indicate(M, trans, i)
mu = c[i] + M[ind]*slope[i]
sig = sigma[i]
prob[ind] = norm.pdf(radii, mu, sig)
prob = prob/np.sum(prob)
return prob
def classification( logm, trans ):
'''
classify as four worlds
'''
count = np.zeros(4)
sample_size = len(logm)
for iclass in range(4):
for isample in range(sample_size):
ind = indicate( logm[isample], trans[isample], iclass)
count[iclass] = count[iclass] + ind
prob = count / np.sum(count) * 100.
print('Terran %(T).1f %%, Neptunian %(N).1f %%, Jovian %(J).1f %%, Star %(S).1f %%' \
% {'T': prob[0], 'N': prob[1], 'J': prob[2], 'S': prob[3]})
return None