-
Notifications
You must be signed in to change notification settings - Fork 28
/
MCMC.py
290 lines (263 loc) · 11.9 KB
/
MCMC.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import numpy as np
from scipy.stats import norm
import model_surrogate as models
import os, math, random
import matplotlib
matplotlib.use('Agg')
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from optparse import OptionParser
parser = OptionParser()
parser.add_option("--case", dest="casename", default="", \
help="Name of case")
parser.add_option("--nevals", dest="nevals", default="200000", \
help="Number of model evaluations")
parser.add_option("--burnsteps", dest="burnsteps", default="10", \
help="Number burn steps")
parser.add_option("--parm_list", dest="parm_list", default='parm_list', \
help = 'File containing list of parameters to vary')
parser.add_option("--parm_default", dest="parm_default", default='' \
,help = 'File containing list of parameters to vary')
(options, args) = parser.parse_args()
UQ_output = 'UQ_output/'+options.casename
os.system('mkdir -p '+UQ_output+'/MCMC_output')
def posterior(parms):
#Calculate the posterior (prior and log likelihood)
line = 0
#Uniform priors
prior = 1.0
for j in range(0,model.nparms):
if (parms[j] < model.pmin[j] or parms[j] > model.pmax[j]):
prior = 0.0
post = prior
if (prior > 0.0):
model.run(parms)
myoutput = model.output.flatten()
myobs = model.obs.flatten()
myerr = model.obs_err.flatten()
for v in range(0,len(myoutput)):
if (abs(myobs[v]) < 1e5 and myerr[v] > 0):
resid = (myoutput[v] - myobs[v])
ri = (resid/myerr[v])**2
li = -1.0 * np.log(2.0*np.pi)/2.0 - \
np.log(myerr[v]) - ri/2.0
post = post + li
else:
post = -9999999
return(post)
#-------------------------------- MCMC ------------------------------------------------------
def MCMC(parms, nevals, type='uniform', nburn=1000, burnsteps=10, default_output=[]):
#Metropolis-Hastings Markov Chain Monte Carlo with adaptive sampling
post_best = -99999
post_last = -99999
accepted_step = 0
accepted_tot = 0
nparms = model.nparms
#parms = np.zeros(nparms)
parm_step = np.zeros(nparms)
chain = np.zeros((nparms+1,nevals))
chain_prop = np.zeros((nparms,nevals))
chain_burn = np.zeros((nparms,nevals))
output = np.zeros((model.nobs,nevals))
mycov = np.zeros((nparms,nparms))
for p in range(0,nparms):
#Starting step size = 1% of prior range
#parm_step[p] = 2.4**2/nparms * (model.pmax[p]-model.pmin[p])
parm_step[p] = 0.05 * (model.pmax[p]-model.pmin[p])
#parms[p] = np.random.uniform(parms[p]-parm_step[p],parms[p]+parm_step[p],1)
parms[p] = model.pdef[p]
#parms_sens = np.copy(parms)
#vary this parameter by one step
#parms_sens[p] = parms_sens[p]+parm_step[p]
#post_sens = posterior(parms_sens)
#use 1D sensitivities to decrease the step sizes accordingly
#print p, np.absolute(post_def - post_sens)
#if (np.absolute(post_def - post_sens) > 1.0):
# parm_step[p] = parm_step[p]/(np.absolute(post_def - post_sens))
for i in range(0,nparms):
mycov[i,i] = parm_step[i]**2
parm_last = parms
scalefac = 1.0
for i in range(0,nevals):
#update proposal step size
if (i > 0 and (i % nburn) == 0 and i < burnsteps*nburn):
acc_ratio = float(accepted_step) / nburn
mycov_step = np.cov(chain_prop[0:nparms,accepted_tot- \
accepted_step:accepted_tot])
mycov_chain = np.cov(chain_burn[0:nparms,int(accepted_tot/4):accepted_tot])
thisscalefac = 1.0
#Compute scaling factors for step sizes based on acceptance ratio
if (acc_ratio <= 0.2):
thisscalefac = max(acc_ratio/0.3, 0.15)
elif (acc_ratio > 0.4):
thisscalefac = min(acc_ratio/0.3, 2.5)
scalefac = scalefac * thisscalefac
#Calculate covariance matrix of recent samples
for j in range(0,nparms):
for k in range(0,nparms):
if (acc_ratio > 0.05):
mycov[j,k] = mycov_chain[j,k] * scalefac
#if (j == k):
#mycov[j,k] =
#scalefac* max(mycov_chain[j,j] / \
# mycov_step[j,j], 1) * mycov_step[j,j]
else:
#if (j == k):
mycov[j,k] = thisscalefac * mycov[j,k]
if (j == k):
print(j, scalefac,mycov[j,j]/(parm_step[j]**2))
print('BURNSTEP', i/nburn, acc_ratio, thisscalefac, scalefac)
mycov_step = np.cov(chain_prop[0:nparms,accepted_tot- \
accepted_step:accepted_tot])
#print(np.corrcoef(chain[0:4,i-nburn:i]))
accepted_step = 0
if (i == burnsteps*nburn):
#Parameter chain plots
for p in range(0,nparms):
fig = plt.figure()
xchain = np.cumsum(np.ones(int(nburn*burnsteps)))
plt.plot(xchain, chain[p,0:int(nburn*burnsteps)])
plt.xlabel('Evaluations')
plt.ylabel(model.parm_names[p])
if not os.path.exists(UQ_output+'/MCMC_output/plots/chains'):
os.makedirs(UQ_output+'/MCMC_output/plots/chains')
plt.savefig(UQ_output+'/MCMC_output/plots/chains/burnin_chain_'+model.parm_names[p]+'.pdf')
plt.close(fig)
#get proposal step
parms = np.random.multivariate_normal(parm_last, mycov)
#------- run the model and calculate log likelihood -------------------
post = posterior(parms)
#determine whether proposal step is accepted
if ( (post - post_last < np.log(random.uniform(0,1)))):
#if not accepted, go back to previous step
for j in range(0,nparms):
parms[j] = parm_last[j]
else:
#proposal step is accepted
post_last = post
accepted_tot = accepted_tot+1
accepted_step = accepted_step+1
chain_prop[0:nparms,accepted_tot] = parms-parm_last
chain_burn[0:nparms,accepted_tot] = parms
parm_last = parms
#keep track of best solution so far
if (post > post_best):
post_best = post
parms_best = parms
print(post_best)
output_best = model.output.flatten()
#populate the chain matrix
for j in range(0,nparms):
chain[j][i] = parms[j]
chain[nparms][i] = post_last
for j in range(0,model.nobs):
output[j,i] = model.output[0,j]
if (i % 1000 == 0):
print(' -- '+str(i)+' --\n')
print("Computing statistics")
chain_afterburn = chain[0:nparms,int(nburn*burnsteps):]
chain_sorted = chain_afterburn
output_sorted = output[0:model.nobs,int(nburn*burnsteps):]
output_sorted.sort()
np.savetxt(UQ_output+'/MCMC_output/MCMC_chain.txt', np.transpose(chain_afterburn))
#Print out some statistics
parm_data=open(options.parm_list,'r')
parm_best=open(UQ_output+'/MCMC_output/parms_best.txt','w')
p=0
for s in parm_data:
row = s.split()
parm_best.write(row[0]+' '+row[1]+' '+str(parms_best[p])+'\n')
p=p+1
parm_data.close()
parm_best.close()
np.savetxt(UQ_output+'/MCMC_output/correlation_matrix.txt',np.corrcoef(chain_afterburn))
#parameter correlation plots (threshold correlations)
#corr_thresh = 0.8
#for p1 in range(0,nparms-1):
# for p2 in range(p1+1,nparms):
# if (abs(parmcorr[p1,p2]) > corr_thresh):
# fig = plt.figure()
# plt.hexbin(chain_afterburn[p1,:],chain_afterburn[p2,:])
# cbar = plt.colorbar()
# cbar.set_label('bin count')
# plt.xlabel(model.parm_names[p1])
# plt.ylabel(model.parm_names[p2])
#
# plt.suptitle('r = '+str(parmcorr[p1,p2]))
# if not os.path.exists(UQ_output+'/MCMC_output/plots/corr'):
# os.makedirs(UQ_output+'/MCMC_output/plots/corr')
# plt.savefig(UQ_output+'/MCMC_output/plots/corr/corr_'+model.parm_names[p1]+'_'+model.parm_names[p2]+'.pdf')
# plt.close(fig)
#Parameter chain plots
for p in range(0,nparms):
fig = plt.figure()
xchain = np.cumsum(np.ones(nevals-int(nburn*burnsteps)))
plt.plot(xchain, chain_afterburn[p,:])
plt.xlabel('Evaluations')
plt.ylabel(model.parm_names[p])
if not os.path.exists(UQ_output+'/MCMC_output/plots/chains'):
os.makedirs(UQ_output+'/MCMC_output/plots/chains')
plt.savefig(UQ_output+'/MCMC_output/plots/chains/chain_'+model.parm_names[p]+'.pdf')
plt.close(fig)
chain_sorted.sort()
parm95=open(UQ_output+'/MCMC_output/parms_95pctconf.txt','w')
for p in range(0,nparms):
parm95.write(str(model.parm_names[p])+' '+ \
str(chain_sorted[p,int(0.025*(nevals-nburn*burnsteps))])+' '+ \
str(chain_sorted[p,int(0.975*(nevals-nburn*burnsteps))])+'\n')
parm95.close()
print("Ratio of accepted steps to total steps:")
print(float(accepted_tot)/nevals)
out95=open(UQ_output+'/MCMC_output/outputs_95pctconf.txt','w')
for p in range(0,model.nobs):
out95.write(str(output_sorted[p,int(0.025*(nevals-nburn*burnsteps))])+' '+ \
str(output_sorted[p,int(0.975*(nevals-nburn*burnsteps))])+'\n')
out95.close()
#make parameter histogram plots
for p in range(0,nparms):
fig = plt.figure()
n, bins, patches = plt.hist(chain_afterburn[p,:],25)
plt.xlabel(model.parm_names[p])
plt.ylabel('Probability Density')
if not os.path.exists(UQ_output+'/MCMC_output/plots/pdfs'):
os.makedirs(UQ_output+'/MCMC_output/plots/pdfs')
plt.savefig(UQ_output+'/MCMC_output/plots/pdfs/'+model.parm_names[p]+'.pdf')
plt.close(fig)
#make prediction plots
obs_set = list(set(model.obs_name))
for s in range(0,len(obs_set)):
thisob = [ix for ix, value in enumerate(model.obs_name) if value == obs_set[s]]
fig = plt.figure()
ax=fig.add_subplot(111)
x = np.cumsum(np.ones([len(thisob)],float))
ax.errorbar(x,model.obs[thisob], yerr=model.obs_err[thisob], label='Observations')
ax.plot(x,output_best[thisob],'r', label = 'Model best')
ax.plot(x,output_sorted[thisob,int(0.025*(nevals-nburn*burnsteps))], \
'k--', label='Model 95% CI')
ax.plot(x,output_sorted[thisob,int(0.975*(nevals-nburn*burnsteps))],'k--')
if (options.parm_default != ''):
ax.plot(x,default_output[thisob], 'g', label='Default')
#plt.xlabel(model.xlabel)
#plt.ylabel(model.ylabel)
box = ax.get_position()
ax.set_position([box.x0,box.y0,box.width*0.8,box.height])
ax.legend(loc='center left', bbox_to_anchor=(1,0.5), fontsize='small')
if not os.path.exists(UQ_output+'/MCMC_output/plots/predictions'):
os.makedirs(UQ_output+'/MCMC_output/plots/predictions')
plt.savefig(UQ_output+'/MCMC_output/plots/predictions/Predictions_'+obs_set[s]+'.pdf')
plt.close(fig)
return parms_best
#Create the model object
model = models.MyModel(case=options.casename)
if (options.parm_default != ''):
parms_default = np.loadtxt(options.parm_default)
model.run(parms_default)
default_output = model.output.flatten()
parms = MCMC(model.pdef, int(options.nevals), burnsteps=int(options.burnsteps), \
nburn=int(options.nevals)/(2*int(options.burnsteps)), \
default_output=default_output)
else:
#run MCMC
parms = MCMC(model.pdef, int(options.nevals), burnsteps=int(options.burnsteps), \
nburn=int(options.nevals)/(2*int(options.burnsteps)))
plt.show()