-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_chain.py
139 lines (112 loc) · 4.03 KB
/
read_chain.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
#!/usr/bin/env python
# coding: utf-8
import math
import os, sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
'''
argvs = sys.argv
argc = len(argvs)
if (argc != 3):
print('Usage: # python %s indir output' % argvs[0])
quit()
dirname=str(argvs[1])
ofname=str(argvs[2])
'''
dirname="/home/ethlau/projects/Power_Spectrum/halo_model_Flender/MCMC/test/2019-01-03_10"
ofname='test.pdf'
# chain will be saved every nstep. In total nbunch * nstep samplings.
nbunch = 2
nstep = 1000
#nwalkers = (size-1)*2 # (total_number_of_cores - 1)*2
# read MCMC information
ndim = 5
try :
chains_npy="chains_"+str(nbunch-1)+".npy"
filename_bunch_chains = os.path.join(dirname, chains_npy)
X = np.load(filename_bunch_chains)
lnp_npy="lnp_"+str(nbunch-1)+".npy"
filename_bunch_lnp = os.path.join(dirname, lnp_npy)
Y = np.load(filename_bunch_lnp)
except IOError:
print("Files not found!")
exit(1)
print(X.shape)
print(Y.shape)
#print(Y)
#quit()
labels=[r"$10^{6}\epsilon_{f}$", r"$f_{*}$", r"$S_{*}$", r"$C_0$", r"$\log P{\rm SN}$"]
fig, axes = plt.subplots(ndim, figsize=(10, 7), sharex=True)
for i in range(ndim):
ax = axes[i]
ax.plot(X[:, :, i].T, "k", alpha=0.3)
ax.set_xlim(0, nstep*nbunch)
ax.set_ylabel(labels[i])
ax.yaxis.set_label_coords(-0.1, 0.5)
axes[-1].set_xlabel("step number")
fig.savefig("chains.png")
# set burn-in
skip = 200
samples = X[:, skip:, :].reshape((-1, ndim))
lnp = Y[skip:,:].reshape(-1,)
#for i in range(lnp.size):
# samples[i,0] = samples[i,0] * 1e6
min_lnp = lnp.max()
print(lnp, samples.shape, lnp.shape, min_lnp)
weights = np.array(lnp) - np.full(lnp.shape, min_lnp)
weights = np.exp(weights)
print (weights)
norm = weights.sum()
mean = []
for i in range(ndim):
mean.append(np.dot(samples[:,i], weights))
mean = mean / norm
print(mean)
var = []
for i in range(ndim):
var.append(np.dot((samples[:,i]-mean[i])*(samples[:,i]-mean[i]), weights))
var = var / norm
print(np.sqrt(var))
import corner
#fig_c = corner.corner(samples, labels=[r"$10^{6}\epsilon_{f}$", r"$\epsilon_{\rm DM}$", r"$f_{*}$", r"$S_{*}$", r"$A_C$", r"$\tilde{\Gamma}$", r"$\gamma$", r"$x_{\rm break}$"],
# weights=weights, quantiles=[0.16, 0.50, 0.84], levels=[0.68, 0.95],
# range=[(0.1,2.0), (0.00,0.010), (0.020,0.032), (0.01, 0.30), (0.5,1.5), (0.01,0.30), (0.10,3.0), (0.10,0.25)],
# show_titles=True, plot_datapoints=False, title_kwargs={"fontsize": 14}, smooth=1.0)
fig_c = corner.corner(samples,labels=[r"$10^{6}\epsilon_{f}$", r"$f_{*}$", r"$S_{*}$", r"$C_0$", r"$\log P{\rm SN}$"], weights=weights, quantiles=[0.16, 0.50, 0.84], levels=[0.68, 0.95], range=[(0.1,2.0), (0.020,0.032), (0.01, 0.30), (0.01,2.0), (-23,-21)], show_titles=True, plot_datapoints=False, title_kwargs={"fontsize": 14}, smooth=1.0)
fig_c.savefig(ofname)
quit()
# test whether all chain converge to the same target distribution.
# for detail, see e.g.
# http://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_introbayes_sect008.htm#statug.introbayes.bayesgelman
# or http://joergdietrich.github.io/emcee-convergence.html
def gelman_rubin(chain):
ssq = np.var(chain, axis=1, ddof=1)
W = np.mean(ssq, axis=0)
thetab = np.mean(chain, axis=1)
thetabb = np.mean(thetab, axis=0)
m = chain.shape[0]
n = chain.shape[1]
B = n / (m-1) * np.sum((thetabb-thetab)**2, axis=0)
var_theta = (n-1)/(n) * W + 1./n * B
Rhat = np.sqrt(var_theta / W)
return Rhat
chain = X[:, skip:, :]
fig = plt.figure(figsize=(8.9, 5.5))
xmin = 1000
chain_length = chain.shape[1]
step_sampling = np.arange(xmin, chain_length, 50)
for i in range(ndim):
rhat = np.array([gelman_rubin(chain[:, :steps, :])[i] for steps in step_sampling])
plt.plot(step_sampling, rhat, label="param{:d}".format(i), linewidth=2)
ax = plt.gca()
xmax = ax.get_xlim()[1]
plt.hlines(1.01, xmin, xmax, linestyles="--")
plt.ylabel("$\hat R$")
plt.xlabel("chain length")
plt.ylim(1.00, 1.10)
legend = plt.legend(loc='best')
plt.draw()
plt.pause(20.0)
'''
'''