-
Notifications
You must be signed in to change notification settings - Fork 5
/
wallClockSyntheticClassGPML.m
244 lines (193 loc) · 8.83 KB
/
wallClockSyntheticClassGPML.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Test sqrtBBQ M & L versions vs: SMC, annealed importance sampling &
% Bayesian Monte Carlo on a synthetic binary classification problem with
% GPML.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Setup variables
close all;
clear all;
randn('state',10);
rand('state',10);
addpath(genpath(pwd));
startup;
% Generate Random binary classification problem:
ntr = 500; % number of training and test points
xtr = 10*sort(rand(ntr,1)); % sample dataset
p = @(x) 1./(1+exp(-5*sin(x))); % "true" underlying probability
ytr = 2*(p(xtr)>rand(ntr,1))-1; % draw labels +1/-1
i = randperm(ntr); nout = 3; % add outliers
xx = xtr;
yy = ytr;
dim = size(xx,2)+2; % Dimensionality of integral over hyperparameters.
range = [-10*ones(1,dim);10*ones(1,dim)]; % Bounding box for integration.
priorMu = 0*ones(1,dim); % Gaussian prior in log-space.
priorVar = 2*eye(dim);
kernelVar = exp(0)*eye(dim); % Input length scales for GP likelihood model.
lambda = exp(0); % Ouput length scale for GP likelihood model.
alpha = 0.8; % Fractional offset, as in paper.
numSamples = 100; % Total number of sqrtBBQ samples.
printing = 1; % Output intermediate results to console.
recalcGndTruth = 0; % Recalculate ground truth, or pull old result.
if ~recalcGndTruth % NB!! old result assumes seed unchanged, and
% only 500 datapoints sampled above!
load ./Results/synthgndtruth.mat;
end
numgndtruth = 1000000; % If recalculating, number of SMC samples to draw.
currtimesmc = 0;
currtimebq = 0;
%%%%%%%%%%%%% GPML STUFF %%%%%%%%%%%%%%%%%%%
% 1) set GP parameters
cov = {@covSum,{@covSEard,@covNoise}}; hyp.cov = [zeros(dim-2,1); 0; -Inf];
lik = {'likErf'}; hyp.lik = []; % Likelihood
mn = {'meanZero'}; hyp.mean = [];
inf = 'infLaplace';
% 2) LogLikFunction
likFunc = @(x) -1*gp(rewrap(hyp,transp(x)), inf, mn, cov, lik, xx, yy);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Storage for results
smctic = zeros(1000,1);
smcval = zeros(1000,1);
sqrtbqtic = zeros(1000,1);
%------------------------------ sqrtBBQ ----------------------------------%
[muBBQ_M, varBBQ_M, timeBBQ_M, xxIter_M, hyps_M] = wsabiM(range, ...
priorMu, ...
priorVar, ...
kernelVar, ...
lambda, ...
alpha, ...
numSamples, ...
likFunc, ...
printing);
[muBBQ_L, varBBQ_L, timeBBQ_L, xxIter_L, hyps_L] = wsabiL(range, ...
priorMu, ...
priorVar, ...
kernelVar, ...
lambda, ...
alpha, ...
numSamples, ...
likFunc, ...
printing);
totime = cumsum(timeBBQ_L);
fprintf('\n wsabiM Integral: %g \n',muBBQ_M(end));
fprintf('\n wsabiL Integral: %g \n',muBBQ_L(end));
fprintf('Total wsabiL time: %g \n',totime(end));
logmuBBQ_M = muBBQ_M;
logmuBBQ_L = muBBQ_L;
%---------------------------- SMC Sampling -------------------------------%
smcinterclk = 0;
smcSamples = zeros(1,dim);
smcOutput = likFunc(smcSamples);
smcinter = exp(smcOutput);
logsmcinter = smcOutput;
prstr = [];
fprintf('\n SMC Sampling ... \n');
while sum(smcinterclk) < totime(end)
t = cputime;
smcSamples(end+1,:) = mvnrnd(priorMu,priorVar);
smcOutput(end+1) = likFunc(smcSamples(end,:));
maxtmp = max(smcOutput);
smcinter(end+1) = mean(exp(smcOutput-maxtmp));
logsmcinter(end+1) = log(smcinter(end)) + maxtmp;
smcinterclk(end+1) = cputime - t;
prstr = sprintf('%2i%%',ceil((sum(smcinterclk)/totime(end)) * 100 + 0.00001));
fprintf(repmat('\b',1,length(prstr)));
fprintf(strcat(prstr,'%%'));
end
fprintf('\n Done \n');
logsmcinter(end);
%--------------------------- BMC Sampling --------------------------------%
numbmc = ceil(length(timeBBQ_M(:,1))/1);
mubq = zeros(numbmc,1);
varbq = zeros(numbmc,1);
timebq = zeros(numbmc,1);
fprintf('\n BMC Sampling ... \n');
for i = 1:length(timebq)
tic;
[mubq(i), varbq(i), kernelVar, lambda] = bq(range, ...
priorMu, ...
priorVar, ...
kernelVar, ...
lambda, ...
alpha, ...
smcSamples(1:i,:), ...
likFunc);
timebq(i) = toc;
if sum(timebq(1:i)) > totime;
break
end
if i > 1
fprintf(repmat('\b',1,length(prstr)));
end
prstr = sprintf('Iter = %i\n',i);
fprintf(prstr);
end
%--------------------------- AIS Sampling --------------------------------%
prior.mean = priorMu;
prior.covariance = priorVar;
iters_ais = 10;
tot_num_samples = logspace(0,log10(length(smcinter)),iters_ais);
mean_log_evidence = zeros(1,iters_ais);
aistimes = zeros(1,iters_ais);
for i = 1:iters_ais
opt.num_samples = ceil(tot_num_samples(i));
[mean_log_evidence_tmp, var_log_evidence, sample_locs, logliks, aistimes_tmp, stats] = ais_mh(likFunc, prior, opt);
mean_log_evidence(i) = mean_log_evidence_tmp(end);
aistimes(i) = sum(aistimes_tmp);
end
%------------------------ GND Truth Sampling -----------------------------%
if recalcGndTruth
%Ground Truth
smcsample2 = zeros(numgndtruth,dim);
smcout2 = zeros(numgndtruth,1);
smcint = zeros(numgndtruth,1);
logsmcint = zeros(numgndtruth,1);
fprintf('GndTruth...\n');
for i = 1:numgndtruth
smcsample2(i,:) = mvnrnd(priorMu,priorVar);
smcout2(i) = likFunc(smcsample2(i,:));
maxtru = max(smcout2);
smcint(i) = mean(exp(smcout2(1:i) - maxtru));
logsmcint(i) = log(smcint(i)) + maxtru;
if ~mod(i,numgndtruth/20)
fprintf('.');
end
end
fprintf('\n Done \n');
end
%-------------------------------------------------------------------------%
% Save results to file.
times = clock;
dat = date;
savestr = strcat(sprintf('./Results/SyntheticClassRun_%g_%g',times(4),times(5)),dat,sprintf('.mat'));
save(savestr);
% Append actively sampled points to ground truth.
for i = 1:length(xxIter_M)
smcout2(end+1) = likFunc(xxIter_M(i,:));
end
maxtru = max(smcout2);
actTrueInt = log(mean(exp(smcout2 - maxtru))) + maxtru;
%----------------------------- Plotting ----------------------------------%
close all;
cc = colormap(cbrewer('qual','Set1',8));
figure;
semilogy(cumsum(timeBBQ_M(1:end)),ones(size(timeBBQ_M))*actTrueInt,'color',cc(1,:),'Linewidth',3); hold on;
semilogy(cumsum(timeBBQ_L(1:end)),logmuBBQ_L(1:end),'color',cc(2,:),'LineWidth',1.5);
semilogy(cumsum(timeBBQ_M(1:end)),logmuBBQ_M(1:end),'color',cc(3,:),'LineWidth',1.5);
semilogy(cumsum(smcinterclk),logsmcinter,'color',cc(4,:),'Linewidth',1.5);
semilogy(aistimes,mean_log_evidence,'color',cc(5,:),'Linewidth',1.5);
semilogy(cumsum(timebq),mubq,'color',cc(6,:),'Linewidth',1.5);
legend('Ground Truth','oldSqrtBBQ','sqrtBBQ','SMC','AIS','BMC');
xlabel('Time in seconds','FontSize', 16);
ylabel('logZ','FontSize', 14,'FontWeight','bold');
set(gca,'FontName','Helvetica', 'FontSize', 14);
figure;
plot(cumsum(timeBBQ_M(1:end)),ones(size(timeBBQ_M))*actTrueInt,'color',cc(1,:),'Linewidth',3); hold on;
plot(cumsum(timeBBQ_L(1:end)),logmuBBQ_L(1:end),'color',cc(2,:),'LineWidth',1.5);
plot(cumsum(timeBBQ_M(1:end)),logmuBBQ_M(1:end),'color',cc(3,:),'LineWidth',1.5);
plot(cumsum(smcinterclk),logsmcinter,'color',cc(4,:),'Linewidth',1.5);
plot(aistimes,mean_log_evidence,'color',cc(5,:),'Linewidth',1.5);
plot(cumsum(timebq),mubq,'color',cc(6,:),'Linewidth',1.5);
legend('Ground Truth','\linearsqrt','\momentsqrt','SMC','AIS','BMC');
xlabel('Time in seconds','FontName','Helvetica','FontSize', 14);
ylabel('logZ','FontSize', 14,'FontName','Helvetica');
set(gca,'FontName','Helvetica', 'FontSize', 14);