-
Notifications
You must be signed in to change notification settings - Fork 8
/
do_simple_mcmc_lambda.m
executable file
·268 lines (227 loc) · 7.23 KB
/
do_simple_mcmc_lambda.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
function [qchain, aratio, oob] = do_simple_mcmc_lambda(param, post, Nchain)
% [chain, aratio] = do_simple_mcmc_lambda(param) - Use a Metropolis-Hastings algorithm to generate
% a 10,000 iterate chain drawn from the posterior of the problem defined by param.
% Acceptance ratio returned as aratio.
%
% [chain, aratio] = do_simple_mcmc_lambda(param, post) - Same as above but use the posterior
% estimates to initialize the proposal matrix.
%
% [chain, aratio] = do_simple_mcmc_lambda(param, post, Nchain) - Same as above, but generate Nchain
% samples.
%
% This code is similar to do_simple_mcmc, but samples lambda from a gamma
% distribution rather than including it as part of the Gaussian proposal
% matrix.
%
%
% NOTE: This does not use any of the unknown parameters in anyway.
if nargin < 3
Nchain = 1e4;
end
Nbeta = param.Nbeta;
y = param.y;
G = param.G;
N = param.N;
if strcmp(param.unknowns, 'beta')
calcase = 1;
Np = Nbeta;
ll = @(q) log_likelihood(param, q);
elseif strcmp(param.unknowns, 'beta_lambda')
calcase = 2;
Np = Nbeta + 1;
ll = @(q) log_likelihood(param, q(1:Nbeta), q(Nbeta+1));
elseif strcmp(param.unknowns, 'beta_lambda_phi')
calcase = 3;
Np = Nbeta + 2;
ll = @(q) log_likelihood(param, q(1:Nbeta), q(Nbeta+1), q(Nbeta+2));
end
% Set up parameter ranges
if calcase == 1
qrange = param.betarange;
elseif calcase == 2
qrange = [param.betarange; param.lambdarange];
elseif calcase == 3
qrange = [param.betarange; param.lambdarange; param.phirange];
end
qchain = zeros(Nchain, Np);
% For now, initialize to the true values. This isn't as unfair as it
% sounds since we're looking at distributions, not the single guess,
% and this wouldn't guarantee the distribution is correct.
qchain(1, 1:Nbeta) = param.beta;
if calcase > 1
qchain(1, Nbeta+1) = param.lambda;
end
if calcase > 3
qchain(1, Nbeta+2) = param.phi;
end
% NOTE: Using the actual value of beta for simplicity. A somewhat better
% exercise would be to use the MLE estimate. Since we are verifying the
% distribution, though, and not just the point estimate, this should still
% provide a reasonable test.
% Also worth noting - this is using the true value of phi to evaluate
% Ri. This is also additional accuracy, but again, shouldn't be too
% detrimental for this test.
res = y - G*param.beta;
Ri = eval_corrfuncinv(param);
param.Ri = Ri;
s2ols = res'*Ri*res / (param.N - Np);
% Lambda will be sampled from a gamma distribution rather than the
% Gaussian proposal.
if calcase == 1
V = zeros(Np);
else
V = zeros(Np-1);
end
if calcase > 1
V(1:param.Nbeta, 1:param.Nbeta) = inv(G'*Ri*G) * s2ols;
else
% Case 1 has lambda known, so use it here.
V(1:param.Nbeta, 1:param.Nbeta) = inv(G'*Ri*G) / param.lambda;
end
if calcase > 1
% For now, just put something reasonable for the range of the hyper
% parameters we're using in testing.
%V(param.Nbeta+1, param.Nbeta+1) = 10;
lrange = param.lambdarange(2) - param.lambdarange(1);
end
if calcase > 2
prange = param.phirange(2) - param.phirange(1);
V(end, end) = prange / 100;
end
L = chol(V)';
acceptnum = 1;
oob = 0;
% Parameters for sampling lambda from the gamma distribution
Ns = 0.01;
s2s = s2ols;
alpha = 0.5 * (Ns + N);
for k = 2:Nchain
accept = false;
if ~mod(k, fix(Nchain/100))
disp(sprintf('Percent complete: %d%%\tAcceptance Ratio: %d', ...
round(100*k/Nchain), acceptnum/(k-1)))
end
qp = qchain(k-1, :)';
if calcase == 1
qstar = qp + L*randn(length(L), 1);
elseif calcase == 2
qstar = qp(1:Nbeta) + L*randn(length(L), 1);
% Add in lambda, sampled from the gamma distribution
qstar = [qstar(1:Nbeta); qp(end)];
elseif calcase == 3
qstar = qp([1:Nbeta, Nbeta+2]) + L*randn(length(L), 1);
% Add in in lambda, sampled from gamma distribution
qstar = [qstar(1:Nbeta); qp(end-1); qstar(end)];
end
if calcase > 1
if calcase == 2
ss = sum_of_squares(param, qstar(1:end-1));
elseif calcase == 3
ss = sum_of_squares(param, qstar(1:end-2), qstar(end));
else
disp('ERROR: Invalid calibration cases (should never see this).')
end
beta = 0.5 * (Ns * s2s + ss);
% New lambda sample. Note this is put in the chain afterwards,
% regardless of rejection / acceptance of the proposed parameters.
lnew = gamrnd(alpha, 1 / beta);
end
if sum(qstar <= qrange(:, 1)) || sum(qstar >= qrange(:, 2))
% Set the probability to 0 if the sample is out-of-bounds
oob = oob + 1;
r = 0;
else
r = exp(ll(qstar) - ll(qp));
end
if r >= 1
qchain(k,:) = qstar';
accept = true;
else
flip = rand;
if flip < r
qchain(k,:) = qstar';
accept = true;
else
qchain(k,:) = qp';
end
end
if accept
acceptnum = acceptnum + 1;
end
if calcase == 2
qchain(k, end) = lnew;
elseif calcase == 3
qchain(k, end-1) = lnew;
end
end
aratio = acceptnum/Nchain;
end
function ss = sum_of_squares(param, beta, phi)
if nargin < 3
% Ri = eval_corrfuncinv(param);
% Abuse! We've added Ri in this case. When phi is known this
% saves from doing a Cholesky factorization each iteration
% of the chain.
Ri = param.Ri;
else
Ri = eval_corrfuncinv(param, phi);
end
if isrow(beta)
beta = beta';
end
G = param.G;
y = param.y;
res = y - G*beta;
ss = res' * Ri * res;
end
function ll = log_likelihood(param, beta, lambda, phi)
% Evaluate the log-likelihood, using the number of arguments passed to
% determine the correct form of the likelihood function to use.
G = param.G; y = param.y;
N = param.N; Nbeta = param.Nbeta;
prior = param.prior;
if nargin < 4
phi = param.phi;
end
if nargin < 3
lambda = param.lambda;
end
Ri = eval_corrfuncinv(param, phi);
res = y - G*beta;
if strcmp(prior.type, 'noninformative')
if nargin == 2
% Beta unknown
ll = - 0.5 * lambda * res'*Ri*res;
elseif nargin == 3
% Beta, lambda unknown
ll = (0.5*N-1) * log(lambda) ...
- 0.5 * lambda * res'*Ri*res;
elseif nargin == 4
% Beta, lambda, phi unknown
d = eval_det(param, phi);
ll = - 0.5*log(d) ...
+ (0.5*N-1) * log(lambda) ...
- 0.5 * lambda * res'*Ri*res;
end
elseif strcmp(prior.type, 'gaussian')
mu0 = prior.mu0; sigma0 = prior.sigma0;
resbeta = mu0 - beta;
if nargin == 2
% Beta unknown
ll = - 0.5 * lambda * res'*Ri*res
- 0.5 * lambda * resbeta'*inv(sigma0)*resbeta;
elseif nargin == 3
% Beta, lambda unknown
ll = (0.5*(N+Nbeta) - 1) * log(lambda) ...
- 0.5 * lambda * res'*Ri*res
- 0.5 * lambda * resbeta'*inv(sigma0)*resbeta;
elseif nargin == 4
% Beta, lambda, phi unknown
d = eval_det(param, phi);
ll = - 0.5*log(d) ...
+ (0.5*(N+Nbeta)-1) * log(lambda) ...
- 0.5 * res'*Ri*res / lambda;
- 0.5 * lambda * resbeta'*inv(sigma0)*resbeta;
end
end
end