-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCMC_discrepancy_true_fn.m
381 lines (340 loc) · 14.6 KB
/
MCMC_discrepancy_true_fn.m
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
function results = MCMC_discrepancy_true_fn(settings)
% M is the total number of draws (inluding burn-in)
% burn_in is the length of the burn-in; may be set either as a count or as
% a proportion of M
% sim_xt is a matrix the first columns of which are the control settings
% for the simulation observations, and the other columns of which are the
% calibration settings for those observations.
% eta is a column vector of the output of the simulation
% obs_x is the control settings for the field observations
% y is a column vector of the field observations
% Sigma_y is the observation variance
% out_of_range is a function which takes as input a proposed theta_s
% vector, and returns a logical vector describing which elements of theta_s
% are within the support of the prior on theta_s. Note that it is
% implicitly assumed here that the prior on theta_s is uniform;
% out_of_range thus defines the range of the uniform prior on theta.
% init_theta is an initial value for theta in the MCMC routine.
% proposal is a function which takes as input theta and returns a proposal
% for a new draw theta_s.
% nugsize is a function which takes as input a square matrix A and returns
% a value n to use as a nugget to stabilize A computationally.
% Define logit function for later use
logit = @(x) log(x./(1-x));
%% Unpack struct
M = settings.M;
burn_in = settings.burn_in;
sim_xt = settings.sim_xt;
eta = settings.eta;
obs_x = settings.obs_x;
y = settings.y;
sigma2 = settings.sigma2;
log_sigma2_prior = settings.log_sigma2_prior;
log_omega_delta_prior = settings.log_omega_delta_prior;
log_lambda_delta_prior = settings.log_lambda_delta_prior;
out_of_range = settings.out_of_range;
init_theta = settings.init_theta;
omega = settings.omega;
rho = settings.rho;
lambda = settings.lambda;
omega_delta = settings.omega_delta_init;
lambda_delta = settings.lambda_delta_init;
sigma2_prop_density = settings.proposal.sigma2_prop_density ;
Sigma_sig = settings.proposal.Sigma_sig ; % proposal variance
% for sigma2
omega_prop_density = settings.proposal.omega_prop_density;
lambda_prop_density = settings.proposal.lambda_prop_density;
prop_density = settings.proposal.density; % proposal for theta
Sigma = settings.proposal.Sigma; % proposal variance
% for theta
Sigma_od = settings.proposal.Sigma_od; % proposal variance
% for omega_delta
Sigma_ld = settings.proposal.Sigma_ld; % proposal variance
% for lambda_delta
log_mh_correction_od = settings.proposal.log_mh_correction_od;
log_mh_correction_ld = settings.proposal.log_mh_correction_ld;
nugsize = settings.nugsize;
num_out = settings.num_out;
log_sig_mh_correction = settings.log_sig_mh_correction;
log_mh_correction = settings.log_mh_correction;
doplot = settings.doplot;
log_theta_prior = settings.log_theta_prior;
Cost_lambda = settings.Cost_lambda;
which_outputs = settings.which_outputs;
y_means = settings.output_means';
y_sds = settings.output_sds';
theta_ranges = settings.input_calib_ranges;
theta_mins = settings.input_calib_mins;
%% Set plot label values
labs = [ '\omega_1' ; '\omega_2' ; '\omega_3' ; '\lambda ' ] ;
for ii = 1:length(which_outputs)
if which_outputs(ii) == 0
labs(ii,:)=[];
end
end
%% If burn_in is a proportion rather than a count, then convert to count
if 0<burn_in && burn_in < 1
burn_in = ceil(burn_in * M) ;
end
%% Set cutoff value for adaptive variance
% More proposals out of support than this value (%) will cause adaptive
% variance reduction.
cutoff = 40;
%% Get some values and transformations to use later
num_cntrl = size(obs_x,2) ;
num_calib = length(init_theta) ;
n = size(obs_x,1);
num_obs = n/num_out; % Gets number of multivariate observations.
m = size(eta,1);
z = [y] ;
% Now make sigma2 into a covariance matrix:
sigma2_long = repelem(sigma2,num_obs);
Sigma_y = diag(sigma2_long);
%% Initialize some variables for later use
out_of_range_rec = zeros(size(init_theta)) ;
reject_rec = 0 ;
startplot = 10 ;
accepted = 0 ;
accepted_sig = 0 ; % # sigma2 accepted
accepted_od = 0 ; % # omega_delta accepted
accepted_ld = 0 ; % # lambda_delta accepted
theta = init_theta ;
msg = 0 ;
samples = init_theta ;
delta_rec = [omega_delta lambda_delta] ;
out_of_range_sig = zeros(1,num_out) ;
% The following multipliers are used for the adaptive covariances of the
% proposal density functions.
mult = 5 ; % multiplier for prop dens
mult_od = 5 ; % mult for pd of om_del
mult_ld = 5 ; % mult for pd of lam_dl
%% Get initial log likelihood
% Set new observation input matrix:
obs_theta = repmat(theta,n,1) ;
% Get discrepancy covariance:
Sigma_delta = gp_cov(omega_delta,obs_x,obs_x,lambda_delta,false);
% Combine these to get Sigma_z
Sigma_z = Sigma_y + Sigma_delta ;
% Add a nugget to Sigma_z for computational stability
Sigma_z = Sigma_z + eye(size(Sigma_z)) * nugsize(Sigma_z);
obs_x_os = obs_x(1:size(obs_x,1)/3,end) ...
* settings.input_cntrl_ranges + settings.input_cntrl_mins;
theta_os = theta .* theta_ranges + theta_mins ;
obs_x_theta = [obs_x_os repmat(theta_os,size(obs_x_os,1),1)];
true_y = (Ex_sim(obs_x_theta) - y_means)./y_sds ;
true_y = true_y(:) ;
L_D = logmvnpdf(y',true_y',Sigma_z) ;
loglik_theta = L_D + log_theta_prior(theta,Cost_lambda) ;
% Get log likelihood of sigma2
loglik_sigma2 = L_D + log_sigma2_prior(sigma2) ;
% Get log likelihood of omega_delta
loglik_od = L_D + log_omega_delta_prior(omega_delta) ;
% Get log likelihood of lambda_delta
loglik_ld = L_D + log_lambda_delta_prior(lambda_delta) ;
if doplot figure(); end % For observing MCMC
%% Begin MCMC routine
for ii = 1:M
%% Draw theta
theta_s = prop_density(theta,Sigma) ; % Get new proposal draw
theta_s_os = theta_s .* theta_ranges + theta_mins ;
if any(out_of_range(theta_s))
loglik_theta_s = -Inf ; % Reject this proposed draw.
out_of_range_rec = out_of_range_rec + out_of_range(theta_s) ;
% Keep track of how many go out of range, for adaptive variance
reject_rec = reject_rec + 1; % Keep track of rejections
else
obs_x_theta_s = [obs_x_os repmat(theta_s_os,size(obs_x_os,1),1)] ;
true_y_s = (Ex_sim(obs_x_theta_s) - y_means)./y_sds ;
true_y_s = true_y_s(:);
L_D_s = logmvnpdf(y',true_y_s',Sigma_z);
loglik_theta_s = L_D_s + log_theta_prior(theta_s,Cost_lambda);
L_D = logmvnpdf(y',true_y',Sigma_z) ;
loglik_theta = L_D + log_theta_prior(theta,Cost_lambda);
end
%% Get acceptance ratio statistic
log_alpha = loglik_theta_s - loglik_theta + ...
log_mh_correction(theta_s,theta);
%% Randomly accept or reject with prob. alpha; update accordingly
if log(rand) < log_alpha
accept = 1;
accepted = accepted + 1;
else
accept = 0;
end
if accept % Set up for next time
loglik_theta = loglik_theta_s ;
theta = theta_s ;
L_D = L_D_s;
true_y = true_y_s;
end
%% Draw omega_delta
omega_delta_s = omega_prop_density(omega_delta,Sigma_od) ;
%% Get new Sigma_z = Sigma_eta + [Sigma_y_Sigma_delta 0 ; 0 0]
% Set new discrep covariance matrix:
Sigma_delta_s=...
gp_cov(omega_delta_s,obs_x,obs_x,lambda_delta,false);
% Combine these to get new Sigma_z
Sigma_z_s = Sigma_y + Sigma_delta_s ;
% Add a nugget to Sigma_z for computational stability
Sigma_z_s = Sigma_z_s + eye(size(Sigma_z_s)) * nugsize(Sigma_z_s);
% Get log likelihood of omega_delta_s
L_D_s = logmvnpdf(y',true_y',Sigma_z_s) ;
loglik_od_s = L_D_s + log_omega_delta_prior(omega_delta_s);
%X%L_D = logmvnpdf(z',0,Sigma_z) ;
loglik_od = L_D + log_omega_delta_prior(omega_delta);
%% Get acceptance ratio statistic
log_alpha = loglik_od_s - loglik_od + ...
log_mh_correction(omega_delta_s,omega_delta);
%% Randomly accept or reject with prob. alpha; update accordingly
if log(rand) < log_alpha
accept = 1;
accepted_od = accepted_od + 1;
else
accept = 0;
end
if accept % Set up for next time
loglik_od = loglik_od_s ;
omega_delta = omega_delta_s ;
Sigma_delta = Sigma_delta_s ;
Sigma_z = Sigma_z_s;
L_D = L_D_s;
end
%% Draw lambda_delta
lambda_delta_s =lambda_prop_density(lambda_delta,Sigma_ld) ;
%% Get new Sigma_z = Sigma_eta + [Sigma_y_Sigma_delta 0 ; 0 0]
% Set new discrep covariance matrix:
Sigma_delta_s=...
gp_cov(omega_delta,obs_x,obs_x,lambda_delta_s,false);
% Combine these to get new Sigma_z
Sigma_z_s = Sigma_y + Sigma_delta_s ;
% Add a nugget to Sigma_z for computational stability
Sigma_z_s = Sigma_z_s + eye(size(Sigma_z_s)) * nugsize(Sigma_z_s);
% Get log likelihood of omega_delta_s
L_D_s = logmvnpdf(y',true_y',Sigma_z_s) ;
loglik_ld_s = L_D_s + log_lambda_delta_prior(lambda_delta_s);
%X%L_D = logmvnpdf(z',0,Sigma_z) ;
loglik_ld = L_D + log_lambda_delta_prior(lambda_delta);
%% Get acceptance ratio statistic
log_alpha = loglik_ld_s - loglik_ld + ...
log_mh_correction_ld(lambda_delta_s,lambda_delta);
%% Randomly accept or reject with prob. alpha; update accordingly
if log(rand) < log_alpha
accept = 1;
accepted_ld = accepted_ld + 1;
else
accept = 0;
end
if accept % Set up for next time
loglik_ld = loglik_ld_s ;
lambda_delta = lambda_delta_s ;
Sigma_delta = Sigma_delta_s ;
Sigma_z = Sigma_z_s;
L_D = L_D_s;
end
%% Recordkeeping
samples(ii+1,:) = theta;
% omega_delta_rec(ii+1,:) = omega_delta;
% lambda_delta_rec(ii+1,:) = lambda_delta;
delta_rec(ii+1,:) = [omega_delta lambda_delta];
%% Tune adaptive proposal variance
if mod(ii,100) == 0 && ii <= burn_in
%% Tune theta proposal variance
if accepted < 24 mult = max(mult*.5,mult*accepted/24)
end
if accepted > 24
%Sigma = Sigma * mult;%1.25;
% mult = 1.25 * mult
fprintf(repmat('\b',1,msg));
fprintf('Proposal variances increased\n');
mult = min(mult*2,mult*accepted/24)
msg = fprintf('Completed: %g/%g\n',ii,M);
end
Sigma = cov(logit(samples)) * mult
msg = fprintf('Completed: %g/%g\n',ii,M);
%% Tune discrepancy proposal variance
% First omega_delta
if accepted_od < 24 mult_od=max(mult_od*.5,mult_od*accepted_od/24)
end
if accepted_od > 24
fprintf(repmat('\b',1,msg));
fprintf('Omega delta proposal variances increased\n');
mult_od = min(mult_od*2,mult_od*accepted_od/24)
msg = fprintf('Completed: %g/%g\n',ii,M);
end
Sigma_od = cov(logit(delta_rec(:,1:(end-1)))) * mult_od
msg = fprintf('Completed: %g/%g\n',ii,M);
% Now lambda_delta
if accepted_ld < 24 mult_ld =max(mult_ld*.5,mult_ld*accepted_ld/24)
end
if accepted_ld > 24
fprintf(repmat('\b',1,msg));
fprintf('lambda delta proposal variance increased\n');
mult_ld = min(mult_ld*2,mult_ld*accepted_ld/24)
msg = fprintf('Completed: %g/%g\n',ii,M);
end
Sigma_ld = var(log(delta_rec(:,end))) * mult_ld;
msg = fprintf('Completed: %g/%g\n',ii,M);
end
if mod(ii,100) == 0
% Print info and newline
lag = min(50,size(samples,1)-startplot);
vf_acf = acf(samples(startplot:ii+1,1),lag);
vf_acf = vf_acf(lag);
thk_acf = acf(samples(startplot:ii+1,2),lag);
thk_acf = thk_acf(lag);
fprintf(repmat('\b',1,msg));
fprintf('accepted = %g\n',accepted)
fprintf('accepted_od = %g\n',accepted_od)
fprintf('accepted_ld = %g\n',accepted_ld)
fprintf('VF acf 50 = %g\n',vf_acf)
fprintf('Thk acf 50 = %g\n',thk_acf)
fprintf('\n')
msg = fprintf('Completed: %g/%g\n',ii,M);
%% Reset counters
accepted = 0;
accepted_od = 0;
accepted_ld = 0;
%X% out_of_range_rec = 0 * out_of_range_rec;
%X% out_of_range_sig = 0 * out_of_range_sig;
end
%% Output to console and plot to let us know progress
if mod(ii,100) == 0 && doplot == true
fprintf(repmat('\b',1,msg));
msg = fprintf('Completed: %g/%g\n',ii,M);
subplot(2,4,1);
plot(samples(startplot:end,1),'ko');
title('Volume fraction');
subplot(2,4,2);
plot(samples(startplot:end,2),'ko');
title('Thickness');
for jj = 1:sum(size(delta_rec,2))
subplot(2,4,jj+2);
plot(delta_rec(startplot:end,jj),'ko');
title(['\delta: ' labs(jj,:)]);
end
subplot(2,4,(sum(size(delta_rec,2))+3):8);
plot(logit(samples(startplot:end,1)),...
logit(samples(startplot:end,2)),'ko');
hold on
rr = mvnrnd(mean(logit(samples(startplot:end,:))),Sigma,250);
plot(rr(:,1),rr(:,2),'r.');
hold off
drawnow
end
%% Stop plotting burn_in
if ii > burn_in
startplot=burn_in;
end
end
%% Pack up and leave
samples_os = samples .* settings.input_calib_ranges + ...
settings.input_calib_mins;
results = struct('samples',samples,...
'samples_os',samples_os,...
'delta_samps',delta_rec,...
'Sigma',Sigma,...
'desired_obs',settings.desired_obs,...
'post_mean_theta',mean(samples(settings.burn_in:end,:)),...
'post_mean_delta_params',mean(delta_rec(settings.burn_in:end,:)),...
'settings',settings);
end