-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpc_gibbs_rmhmc_mh.m
190 lines (159 loc) · 6.39 KB
/
gpc_gibbs_rmhmc_mh.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
function [stats] = gpc_gibbs_rmhmc_mh(X, Y, opt)
% Subject and cross-validation parameters
[n,k] = size(Y);
n_gps = length(opt.X0_RMHMC)/n;
%n_gps = k-1;
opt = check_params(opt); % check all required parameters are specified
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MCMC Parameter Specification
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generic MCMC parameters
write_interval = round(opt.nGibbsIter/10);
a = opt.PriorParam(1);
b = opt.PriorParam(2);
% starting estimates
startidx= []; % we may not be using all the gps (e.g. k-1 latent functions
for c = 1:n_gps;
startidx = [startidx (1:n)+(c-1)*n];
end
f = opt.X0_RMHMC(startidx);
LogTheta = opt.X0_MH;
% initialize posteriors
f_all = zeros(size(f,1),opt.nGibbsIter); fidx = 1;
LogTheta_all = zeros(size(LogTheta,1),opt.nGibbsIter);
% Metropolis proposal distribution
MH_Proposal = eye(length(LogTheta));
L_Proposal = chol(MH_Proposal)';
% initialize stats
stats.iter = 1;
stats.opt = opt;
stats.prior_theta = [a, b];
stats.arate_mh = zeros(1,opt.nGibbsIter);
stats.arate_rmhmc = zeros(1,opt.nGibbsIter);
stats.failrate_rmhmc = zeros(1,opt.nGibbsIter);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Begin Gibbs Sampling Block
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute & invert K
K = feval(opt.CovFunc,X,LogTheta);
[InvK, LogDetK, L_Kt] = invert_K(K,Y);
acc_theta_all = 0; acc_f_all = 0; gidx = 1:50; fail_f_all = 0;
for g = 1:opt.nGibbsIter
if mod(g,50) == 0
arate_f = acc_f_all / 50;
arate_theta = acc_theta_all / 50;
fail_f = fail_f_all / 50;
disp(['Gibbs iter: ',num2str(g),' arate(f)=',num2str(arate_f,'%2.2f'),' arate(theta)=',num2str(arate_theta,'%2.2f'),' fail(f)=',num2str(fail_f,'%2.2f'), ]);
acc_theta_all = 0; acc_f_all = 0; fail_f_all = 0;
% update stats
stats.iter = g;
stats.arate_mh(gidx) = arate_theta;
stats.arate_rmhmc(gidx) = arate_f;
stats.failrate_rmhmc(gidx) = fail_f_all;
gidx = gidx + 50;
end
% save output
if mod(g,write_interval) == 0 && opt.WriteInterim && ...
isfield(opt,'OutputFilename') && ...
~isempty(opt.OutputFilename)
save([opt.OutputFilename,'stats'],'stats');
save([opt.OutputFilename,'f_all'],'f_all','-v7.3');
save([opt.OutputFilename,'LogTheta_all'],'LogTheta_all','-v7.3');
end
% sample f
%%%%%%%%%%
gxargs_f = {InvK, Y};
fxargs_f = {Y, InvK, LogDetK};
if opt.UseRMHMC
try
f_new = rmhmc(f, 'rmhmc_posterior_f', fxargs_f,...
'rmhmc_compute_G_f', gxargs_f, opt.rmhmc);
f_new = f_new'; % transpose (for consistency)
catch
fail_f_all = fail_f_all + 1;
f_new = f;
end
else
if opt.UseGMassForHMC
Gf = feval('hmc_compute_G_f_fixedW', f, gxargs_f{:});
else
Gf = eye(length(f));
end
L_Gf = chol(Gf)';
InvGf = inv(Gf);
[Ef, f_new] = hmc(f, 'hmc_posterior_f', opt.rmhmc, L_Gf, InvGf, fxargs_f{:});
f_new = f_new(:,end); % just take the last sample (for consistency)
end
% test acceptance
if norm(f) ~= norm(f_new), acc_f = 1; else acc_f = 0; end
% update f
f = f_new;
acc_f_all = acc_f_all + acc_f;
% sample theta
%%%%%%%%%%%%%%
if opt.OptimiseTheta
% whiten f
nu = (L_Kt\f);
% make a step in theta
LogTheta_new = LogTheta + opt.mh.StepSize*(L_Proposal*randn(length(LogTheta),1));
% compute new K and invert it
K_new = feval(opt.CovFunc, X, LogTheta_new);
[InvK_new, LogDetK_new, L_Kt_new] = invert_K(K_new,Y);
f_new = L_Kt_new*nu;
% compute p(y|f) and p(y|f')
[tmp, LogLik_f] = likelihood_multinomial(f,Y);
[tmp, LogLik_f_new] = likelihood_multinomial(f_new,Y);
% compute p(theta) and p(theta')
LogPrior_theta_all = zeros(size(LogTheta));
LogPrior_theta_new_all = zeros(size(LogTheta));
const = a*log(b) - gammaln(a);
for i = 1:length(LogPrior_theta_all)
LogPrior_theta_all(i) = const + (a - 1)*LogTheta(i) - b*exp(LogTheta(i));
LogPrior_theta_new_all(i) = const + (a - 1)*LogTheta_new(i) - b*exp(LogTheta_new(i));
end
LogPrior_theta = sum(LogPrior_theta_all);
LogPrior_theta_new = sum(LogPrior_theta_new_all);
Ratio = (LogLik_f_new + LogPrior_theta_new) - (LogLik_f + LogPrior_theta);
if Ratio > 0 || (Ratio > log(rand)) % accept
% update theta
LogTheta = LogTheta_new;
acc_theta = 1;
% update f
f = f_new;
InvK = InvK_new;
LogDetK = LogDetK_new;
L_Kt = L_Kt_new;
else % reject
acc_theta = 0;
end
LogTheta_all(:,g) = LogTheta;
%if norm(LogTheta) ~= norm(LogTheta_new), acc_theta = 1; else acc_theta = 0; end
acc_theta_all = acc_theta_all + acc_theta;
end
f_all(:,fidx) = f;
fidx = fidx + 1;
if g == opt.BurnIn
tic; % start timer
end
end
stats.time_taken = toc;
stats.arate_f_mean = mean(stats.arate_rmhmc);
stats.arate_theta_mean = mean(stats.arate_mh);
disp(['Mean acceptance rate (f): ',num2str(stats.arate_f_mean,'%2.2f')]);
disp(['Mean acceptance rate (theta): ',num2str(stats.arate_theta_mean,'%2.2f')]);
% % Exclude burn-in samples
% if isfield(opt,'BurnIn')
% srange = opt.BurnIn+1:length(f_all);
% else
% srange = 1:length(f_all);
% end
% f_post = f_all(:,srange); % ./ repmat( sum(f_all(:,MCMC_range),2), 1, length(MCMC_range));
% LogTheta_post = LogTheta_all(:,srange);
if isfield(opt,'OutputFilename') && ~isempty(opt.OutputFilename)
%save([opt.OutputFilename,'f_post'],'f_post','-v7.3');
%save([opt.OutputFilename,'LogTheta_post'],'LogTheta_post','-v7.3');
save([opt.OutputFilename,'LogTheta_all'],'LogTheta_all','-v7.3');
save([opt.OutputFilename,'f_all'],'f_all','-v7.3');
save([opt.OutputFilename,'stats'],'stats','-v7.3');
end
end