-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsampler.py
195 lines (172 loc) · 6.94 KB
/
sampler.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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 9 11:57:16 2015
@author: landman
The 2nd Driver routine
"""
import traceback
import time
import numpy as np
from Copernicus.Master import SSU
def sampler(*args, **kwargs):
"""
This here is to get the stack trace when an exception is thrown
"""
try:
return sampler_impl(*args, **kwargs)
except Exception as e:
traceback_str = traceback.format_exc(e)
raise StandardError("Error occurred. Original traceback "
"is\n%s\n" % traceback_str)
def sampler_impl(zmax,Np,Nret,Nsamp,Nburn,tmin,data_prior,data_lik,DoPLCF,DoTransform,err,j,fname,beta,Hz,rhoz,Lam,
use_meanf,sigma_lower=np.array([1.0e-5, 1.0e-5])):
#Set sparams
Xrho = np.array([0.8, 3.5])
XH = np.array([0.6, 3.5])
#set characteristic variance of Lambda prior (here 60%)
sigmaLam = 0.1*3*0.7*(70.0/299.79)**2
#Set z domain
zp = np.linspace(0.0, zmax, Np)
#Instantiate universe object
if use_meanf:
U = SSU(zmax, tmin, Np, err, XH, Xrho, sigmaLam, Nret, data_prior, data_lik, fname, False,
beta=beta, Hz=Hz, rhoz=rhoz, Lam=Lam, sigma_lower=sigma_lower)
else:
U = SSU(zmax, tmin, Np, err, XH, Xrho, sigmaLam, Nret, data_prior, data_lik, fname, False, beta=beta, sigma_lower=sigma_lower)
#Get starting sample
Hz = U.Hz
rhoz = U.rhoz
Lam = U.Lam
logLik = U.logLik
#Array storage for posterior samples
Dzsamps = np.zeros([Np, Nsamp])
Hzsamps = np.zeros([Np,Nsamp])
rhozsamps = np.zeros([Np,Nsamp])
dzdwzsamps = np.zeros([Np, Nsamp])
Lamsamps = np.zeros(Nsamp)
t0samps = np.zeros(Nsamp)
# musamps = np.zeros([Np,Nsamp])
Di = np.zeros([Nret,Nsamp])
Si = np.zeros([Nret,Nsamp])
Qi = np.zeros([Nret,Nsamp])
Ai = np.zeros([Nret,Nsamp])
Zi = np.zeros([Nret,Nsamp])
Spi = np.zeros([Nret,Nsamp])
Qpi = np.zeros([Nret, Nsamp])
Zpi = np.zeros([Nret, Nsamp])
ui = np.zeros([Nret,Nsamp])
upi = np.zeros([Nret,Nsamp])
uppi = np.zeros([Nret,Nsamp])
udoti = np.zeros([Nret,Nsamp])
rhoi = np.zeros([Nret,Nsamp])
rhopi = np.zeros([Nret,Nsamp])
rhodoti = np.zeros([Nret,Nsamp])
T2i = np.zeros([Nret,Nsamp])
T1i = np.zeros([Nret,Nsamp])
sigmasqi = np.zeros([Nret,Nsamp])
LLTBConsi = np.zeros([Nret,Nsamp])
if DoPLCF:
Df = np.zeros([Nret, Nsamp])
Sf = np.zeros([Nret, Nsamp])
Qf = np.zeros([Nret, Nsamp])
Af = np.zeros([Nret, Nsamp])
Zf = np.zeros([Nret, Nsamp])
Spf = np.zeros([Nret, Nsamp])
Qpf = np.zeros([Nret, Nsamp])
Zpf = np.zeros([Nret, Nsamp])
uf = np.zeros([Nret, Nsamp])
upf = np.zeros([Nret, Nsamp])
uppf = np.zeros([Nret, Nsamp])
udotf = np.zeros([Nret, Nsamp])
rhof = np.zeros([Nret, Nsamp])
rhopf = np.zeros([Nret, Nsamp])
rhodotf = np.zeros([Nret, Nsamp])
T2f = np.zeros([Nret, Nsamp])
T1f = np.zeros([Nret, Nsamp])
sigmasqf = np.zeros([Nret, Nsamp])
LLTBConsf = np.zeros([Nret, Nsamp])
I = []
# rhostar = np.zeros([Nret,Nsamp])
# Dstar = np.zeros([Nret,Nsamp])
# Xstar = np.zeros([Nret,Nsamp])
# Hperpstar = np.zeros([Nret,Nsamp])
# rmax = np.zeros([Nsamp])
# Omsamps = np.zeros([Nsamp])
# OLsamps = np.zeros([Nsamp])
# t0samps = np.zeros([Nsamp])
accrate = np.zeros(2)
#Do the burnin period
print "Sampler ",j, "started burnin"
#U.set_DoPLCF(False) # don't need to compute the PLCF during burnin
t1 = time.time()
interval = Nburn / 5
for i in range(Nburn):
Hz, rhoz, Lam, logLik, F, a = U.MCMCstep(logLik, Hz, rhoz, Lam)
U.track_max_lik(logLik, Hz, rhoz, Lam)
accrate += np.array([a, 1])
if i % interval == 0 and i != 0:
# Check acceptance rate
arate = accrate[0] / accrate[1]
if arate < 0.2:
beta /= 1.3
U.reset_beta(beta)
print "Acceptance rate of ", arate, " is too low. Resetting beta to ", beta, i, j
elif arate > 0.5:
beta *= 1.3
U.reset_beta(beta)
print " Acceptance rate of ", arate, " is too high. Resetting beta to ", beta, i, j
print 'It took sampler'+str(j),(time.time() - t1)/60.0,'min to draw ',Nburn,' samples with an accrate of ', accrate[0]/accrate[1]
# Reset the lambda prior
U.set_Lambda_Prior(U.Hz, U.rhoz)
U.set_DoPLCF(DoPLCF)
accrate = np.zeros(2)
interval = Nsamp / 10
t1 = time.time()
for i in range(Nsamp):
if i%interval == 0:
print "Sampler ",j," is at sample ",i
Hz,rhoz,Lam,logLik,F,a = U.MCMCstep(logLik,Hz,rhoz,Lam)
accrate += np.array([a,1])
Hzsamps[:,i] = Hz
rhozsamps[:,i] = rhoz
Lamsamps[i] = Lam
T1i[:, i], T2i[:, i], LLTBConsi[:, i], Di[:, i], Si[:, i], Qi[:, i], Ai[:, i], Zi[:, i], Spi[:, i], \
Qpi[:, i], Zpi[:, i], ui[:, i], upi[:, i], uppi[:, i], udoti[:, i], rhoi[:, i], rhopi[:, i], rhodoti[:, i], \
Dzsamps[:, i], dzdwzsamps[:, i], sigmasqi[:, i], t0samps[i] = U.get_funcsi()
if t0samps[i] > U.tmin and U.NI > 1 and DoPLCF:
T1f[:, i], T2f[:, i], LLTBConsf[:, i], Df[:, i], Sf[:, i], Qf[:, i], Af[:, i], Zf[:, i], Spf[:, i], \
Qpf[:, i], Zpf[:, i], uf[:, i], upf[:, i], uppf[:, i], udotf[:, i], rhof[:, i], rhopf[:, i], rhodotf[:, i],\
sigmasqf[:, i] = U.get_funcsf()
else:
I.append(i)
if DoPLCF:
# Delete the empty columns in PLCF quantities that result when t0 < tfind
np.delete(T1f, I, axis=1)
np.delete(T2f, I, axis=1)
np.delete(LLTBConsf, I, axis=1)
np.delete(Df, I, axis=1)
np.delete(Sf, I, axis=1)
np.delete(Qf, I, axis=1)
np.delete(Af, I, axis=1)
np.delete(Zf, I, axis=1)
np.delete(Spf, I, axis=1)
np.delete(Qpf, I, axis=1)
np.delete(Zpf, I, axis=1)
np.delete(uf, I, axis=1)
np.delete(upf, I, axis=1)
np.delete(uppf, I, axis=1)
np.delete(udotf, I, axis=1)
np.delete(rhof, I, axis=1)
np.delete(rhopf, I, axis=1)
np.delete(rhodotf, I, axis=1)
np.delete(sigmasqf, I, axis=1)
# Report
print 'It took sampler' + str(j), (time.time() - t1) / 60.0, 'min to draw ', Nsamp, \
' samples with an acceptance rate of ', accrate[0]/accrate[1]
if DoPLCF:
return Hzsamps, rhozsamps, Lamsamps, T1i, T1f, T2i, T2f, LLTBConsi, LLTBConsf, Di, Df, Si, Sf, Qi, Qf, Ai, Af, Zi, \
Zf, Spi, Spf, Qpi, Qpf, Zpi, Zpf, ui, uf, upi, upf, uppi, uppf, udoti, udotf, rhoi, rhof, rhopi, rhopf, \
rhodoti, rhodotf, Dzsamps, dzdwzsamps, sigmasqi, sigmasqf, t0samps
else:
return Hzsamps, rhozsamps, Lamsamps, T1i, T2i, LLTBConsi, Di, Si, Qi, Ai, Zi, Spi, Qpi, Zpi, ui, upi, uppi, \
udoti, rhoi, rhopi, rhodoti, Dzsamps, dzdwzsamps, sigmasqi, t0samps