-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatistics.py
98 lines (84 loc) · 2.71 KB
/
statistics.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
"""
Post distibution statistics
Chi2 and Jensen-Shanon Divergence caluclators
"""
import numpy as np
def calculateChiCrysol(weightedIns, expIns, expErr):
"""
Calculates chis same way as it is done in crysol
"""
#Calculate scaling factor
chi2_=0.0
mixed_term_ = 0.0
square_calc_ = 0.0
Sindex = 0
for ins in expIns:
Iobs=ins
square_err_ = expErr[Sindex]*expErr[Sindex]
mixed_term_ += Iobs*weightedIns[Sindex]/square_err_
square_calc_ += weightedIns[Sindex]*weightedIns[Sindex]/square_err_
Sindex+=1
scale_factor = mixed_term_/square_calc_
Sindex = 0
for ins in expIns:
Iobs=ins
square_err_ = expErr[Sindex]*expErr[Sindex]
chi2_+=(Iobs-scale_factor*weightedIns[Sindex])*(Iobs-scale_factor*weightedIns[Sindex])/square_err_
Sindex+=1
chi2_=chi2_/Sindex
return chi2_
def calculateChemShiftsChi(weightedIns, expIns, expErr, teoErr):
"""
Calculates chis for chemical shifts
"""
#Calculate scaling factor
chi2_ = 0.0
square_calc_ = 0.0
#scale_factor = 1
Sindex = 0
for ins in expIns:
Iobs=ins
et_err_ = np.sqrt(expErr[Sindex]*expErr[Sindex]+teoErr[Sindex]*teoErr[Sindex])
square_calc_ += (Iobs-weightedIns[Sindex])*(Iobs-weightedIns[Sindex])
chi2_+=(Iobs-weightedIns[Sindex])*(Iobs-weightedIns[Sindex])/(et_err_*et_err_)
Sindex+=1
return chi2_/Sindex
def InfEntropy(weight):
S = 0.0
for wi in weight:
if wi<0.0000001:
wi = 0.0000001
S-=wi*np.log2(wi)
return S
def JensenShannonDiv(weights_a, weights_b):
jsd = InfEntropy((weights_a+weights_b)*0.5)-0.5*InfEntropy(weights_a)-0.5*InfEntropy(weights_b)
return jsd
def mean_for_weights(fit):
"""
Calculation mean weight from stan fitting object
:param fit: stan fit model
:return:
"""
stan_chain = fit.extract()
weights = stan_chain["weights"]
vlen = np.shape(weights)
mean_weights = []
for ind in range(vlen[1]):
mean_weights.append(np.mean(weights[:,ind]))
return mean_weights
def me_log_lik(fit):
"""
Calculates model evidence from log likelihood calculations
:param log_lik:
:return:
"""
stan_chain = fit.extract()
log_lik = stan_chain["loglikes"]
return np.sum(np.mean(log_lik, axis=1))
def waic(log_lik):
"""
Calculate the widely available information criterion
"""
lppd = np.sum(np.log(np.mean(np.exp(log_lik), axis=0)))
p_waic = np.sum(np.var(log_lik, axis=0))
return -2 * lppd + 2 * p_waic