-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_himmelblau.m
273 lines (228 loc) · 8.62 KB
/
example_himmelblau.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
%% The TEMCMC sampler
%
% The TEMCMC sampler is based on the orginal TMCMC sampler proposed by
% Ching and Chen (2007). For the TEMCMC sampler, the MH sampler is replaced
% by the Affine-invariant Ensemble sampler in the resampling procedure
% given the latter's strength in sampling from highly anisotropic
% distributions, which is the case of the transitional distributions.
%
%% The Himmelblau's Function:
%
% In this example, we will evaluate the performance of the TEMCMC sampler
% against the TMCMC sampler in sampling from the Himmelblau's function, a
% 2D distribution function with 4 local minima.
%
% The Himmelblau's function is defined as:
%
% f(x,y) = (x^2 + y - 11)^2 + (x + y^2 - 7)^2;
%
% with the local minima located at: f(3.0, 2.0), f(-2.805118, 3.131312),
% f(-3.779310, -3.283186), and f(3.584428, -1848126). To convert these
% local minima into local maxima, into a region of high probability, the
% loglikelihood becomes:
%
% log_like(x,y) = -f(x, y);
%
clc; close all;
% The Negative-Log of the Himmelblau's Function:
logPfun = @(x) - ((x(:,1).^2 + x(:,2) - 11).^2 + (x(:,1) + x(:,2).^2 - 7).^2);
% Plotting the Himmelblau's Function:
[X1,X2] = meshgrid(-6:.01:6,-6:.01:6);
Z = logPfun([X1(:) X2(:)]);
Z = reshape(Z,size(X1));
figure;
hold on; box on; grid on;
contour(X1,X2,exp(Z))
colormap(parula)
title('Himmelblau Function')
xlim([-5 5]); ylim([-5 5]);
xlabel('$x_1$','Interpreter','latex'); ylabel('$x_2$','Interpreter','latex');
set(gca,'fontsize',20)
%% Define the Prior:
lowerBound = -5; upperBound = 5;
prior_pdf = @(x) unifpdf(x(:,1),lowerBound,upperBound).*unifpdf(x(:,2),lowerBound,upperBound);
prior_rnd = @(N) [unifrnd(lowerBound,upperBound,N,1),unifrnd(lowerBound,upperBound,N,1)];
%% Define the Likelihood function:
logl = @(x) - ((x(:,1).^2 + x(:,2) - 11).^2 + (x(:,1) + x(:,2).^2 - 7).^2);
%% Run TMCMC sampler:
Nsamples = 1000;
tic;
TMCMC = TMCMCsampler('nsamples',Nsamples,'loglikelihood',logl,...
'priorpdf',prior_pdf,'priorrnd',prior_rnd,'burnin',0);
timeTMCMC = toc;
fprintf('Time elapsed is for the TMCMC sampler: %f \n',timeTMCMC)
samples_tmcmc = TMCMC.samples;
%% Run the TEMCMC sampler:
Nsamples = 1000;
%parpool(2);
tic;
TEMCMC = TEMCMCsampler('nsamples',Nsamples,'loglikelihood',logl,...
'priorpdf',prior_pdf,'priorrnd',prior_rnd,'burnin',0);
timeTEMCMC = toc;
fprintf('Time elapsed is for the TEMCMC sampler: %f \n',timeTEMCMC)
samples_temcmc = TEMCMC.samples;
%% Plot the combined scatterplot matrix:
figure();
subplot(1,2,1)
[~,ax1] = plotmatrix(samples_tmcmc);
title('TMCMC Scatterplot Matrix', 'Fontsize', 20);
for i=1:2
ax1(i,1).FontSize = 16;
ax1(2,i).FontSize = 16;
ylabel(ax1(i,1),sprintf('x_{%d}', i));
xlabel(ax1(2,i),sprintf('x_{%d}', i));
end
set(gca,'FontSize',18)
subplot(1,2,2)
[~,ax2] = plotmatrix(samples_temcmc);
title('TEMCMC Scatterplot Matrix', 'Fontsize', 20);
for i=1:2
ax2(i,1).FontSize = 16;
ax2(2,i).FontSize = 16;
ylabel(ax2(i,1),sprintf('x_{%d}', i));
xlabel(ax2(2,i),sprintf('x_{%d}', i));
end
set(gca,'FontSize',18)
%% Plot the combined scatterplot:
figure;
subplot(1,2,1)
hold on; box on; grid on;
contour(X1,X2,exp(Z))
colormap(parula)
scatter(samples_tmcmc(:,1), samples_tmcmc(:,2),18,'r','filled');
legend('Himmelblau function', 'TMCMC samples', 'linewidth', 2)
xlim([-5 5]); ylim([-5 5]);
xlabel('$x_1$','Interpreter','latex'); ylabel('$x_2$','Interpreter','latex');
set(gca,'fontsize',20)
subplot(1,2,2)
hold on; box on; grid on;
contour(X1,X2,exp(Z))
colormap(parula)
scatter(samples_temcmc(:,1), samples_temcmc(:,2),18,'r','filled');
legend('Himmelblau function', 'TEMCMC samples', 'linewidth', 2)
xlim([-5 5]); ylim([-5 5]);
xlabel('$x_1$','Interpreter','latex'); ylabel('$x_2$','Interpreter','latex');
set(gca,'fontsize',20)
%% Plotting the Transitional distributions and the respective samples:
beta_tmcmc = TMCMC.beta;
beta_temcmc = TEMCMC.beta;
% Plotting the Transitional functions for TMCMC:
for k = 1:length(TMCMC.beta)
log_transition_func = @(x) log(prior_pdf(x)) + (beta_tmcmc(k)).*logl(x);
Z1 = zeros(length(X1(:)),length(beta_tmcmc));
Z1(:,k) = log_transition_func([X1(:) X2(:)]);
Z_tmcmc(:,:,k) = reshape(Z1(:,k), size(X1));
end
% Plotting the Transitional functions for TEMCMC:
for k = 1:length(TEMCMC.beta)
log_transition_func = @(x) log(prior_pdf(x)) + (beta_temcmc(k)).*logl(x);
Z2 = zeros(length(X1(:)),length(beta_temcmc));
Z2(:,k) = log_transition_func([X1(:) X2(:)]);
Z_temcmc(:,:,k) = reshape(Z2(:,k), size(X1));
end
% Plotting the transitional distributions and the samples for TMCMC:
allsamples_tmcmc = TMCMC.allsamples;
figure;
for k = 1:length(beta_tmcmc)
subplot(2,3,k)
hold on; box on; grid on
contour(X1,X2,exp(Z_tmcmc(:,:,k)))
colormap(parula)
scatter(allsamples_tmcmc(:,1,k), allsamples_tmcmc(:,2,k), 18, 'r', 'filled');
xlim([-5 5]); ylim([-5 5]);
xlabel('$x_1$','Interpreter','latex'); ylabel('$x_2$','Interpreter','latex');
title(sprintf('$j = %d$', (k-1)),'Interpreter','latex')
set(gca,'fontsize',20)
end
% Plotting the transitional distributions and the samples for TMCMC:
allsamples_temcmc = TEMCMC.allsamples;
figure;
for k = 1:length(beta_temcmc)
subplot(2,3,k)
hold on; box on; grid on
contour(X1,X2,exp(Z_temcmc(:,:,k)))
colormap(parula)
scatter(allsamples_temcmc(:,1,k), allsamples_temcmc(:,2,k), 18, 'r', 'filled');
xlim([-5 5]); ylim([-5 5]);
xlabel('$x_1$','Interpreter','latex'); ylabel('$x_2$','Interpreter','latex');
title(sprintf('$j = %d$', (k-1)),'Interpreter','latex')
set(gca,'fontsize',20)
end
%% Plotting the marginal ECDFs:
loglike = @(x,y) - ((x.^2 + y - 11).^2 + (x + y.^2 - 7).^2);
prior = @(x,y) unifpdf(x,lowerBound,upperBound).*unifpdf(y,lowerBound,upperBound);
fun = @(x,y) prior(x,y).*exp(loglike(x,y));
% Marginal distribution of x1:
fun_x1 = @(x) integral(@(y) fun(x,y),lowerBound,upperBound);
% Marginal distribution of x2:
fun_x2 = @(y) integral(@(x) fun(x,y),lowerBound,upperBound);
fun_x1_out = zeros(1000,1); fun_x2_out = zeros(1000,1);
x1 = linspace(lowerBound,upperBound,1000); x2 = linspace(lowerBound,upperBound,1000);
for i = 1:1000
fun_x1_out(i) = fun_x1(x1(i));
fun_x2_out(i) = fun_x2(x2(i));
end
fun_x1_out = normalize(cumsum(fun_x1_out),'range',[0,1]);
fun_x2_out = normalize(cumsum(fun_x2_out),'range',[0,1]);
%% Combined ECDF Plots:
figure();
subplot(1,2,1)
hold on; box on; grid on;
plot(x1',fun_x1_out,'k','LineWidth',1.5)
[f1a,x1a] = ecdf(samples_temcmc(:,1));
plot(x1a,f1a,'r','LineWidth',1.5)
[f1b,x1b] = ecdf(samples_tmcmc(:,1));
plot(x1b,f1b,'b','LineWidth',1.5)
xlabel('x_1')
ylabel('F(x_1)')
xlim([-5 5])
legend('Posterior Marginal CDF', 'ECDF TEMCMC samples', 'ECDF TMCMC samples', 'linewidth',2);
title('ECDF of samples for x_1')
set(gca, 'Fontsize', 15)
subplot(1,2,2)
hold on
box on
grid on
plot(x2',fun_x2_out,'k','LineWidth',1.5)
[f2a,x2a] = ecdf(samples_temcmc(:,2));
plot(x2a,f2a,'r','LineWidth',1.5)
[f2b,x2b] = ecdf(samples_tmcmc(:,2));
plot(x2b,f2b,'b','LineWidth',1.5)
xlabel('x_2')
ylabel('F(x_2)')
xlim([-5 5])
legend('Posterior Marginal CDF', 'ECDF TEMCMC samples', 'ECDF TMCMC samples', 'linewidth',2);
title('ECDF of samples for x_2')
set(gca, 'Fontsize', 15)
%% TMCMC vs TEMCMC Statistics:
dim = size(samples_temcmc, 2); % dimensionality of the problem
target_accept = 0.23 + (0.21./dim);
% Plot their beta values:
figure;
subplot(1,2,1)
xin = 0:length(TEMCMC.beta)-1;
yin = 0:length(TMCMC.beta)-1;
hold on; box on; grid on;
plot(xin, TEMCMC.beta, '--rs', 'MarkerFaceColor','r','linewidth', 1.5)
plot(yin, TMCMC.beta, '--bs', 'MarkerFaceColor','b','linewidth', 1.5)
legend('TEMCMC \beta_j values', 'TMCMC \beta_j values', 'linewidth', 2)
title('Plot of \beta_j values')
xlabel('$j$','Interpreter','latex'); ylabel('$\beta_j$','Interpreter','latex');
set(gca, 'fontsize', 20)
subplot(1,2,2)
ain = 1:length(TEMCMC.acceptance);
bin = 1:length(TMCMC.acceptance);
hold on; box on; grid on;
plot(ain, TEMCMC.acceptance, '--rs', 'MarkerFaceColor','r','linewidth', 1.5)
plot(bin, TMCMC.acceptance, '--bs', 'MarkerFaceColor','b','linewidth', 1.5)
plot([1 5],[target_accept target_accept] , 'c','linewidth', 1.5)
plot([1 5],[0.15 0.15] , 'k','linewidth', 1.5)
plot([1 5],[0.5 0.5] , 'k','linewidth', 1.5)
legend('TEMCMC acceptance rates', 'TMCMC acceptance rates',...
'Target acceptance rate', 'Optimum acceptance limits', 'linewidth', 2)
title('Plot of Acceptance rates')
xlabel('$j$','Interpreter','latex'); ylabel('Acceptance rate');
xlim([1 5]); ylim([0 0.8])
set(gca, 'fontsize', 20)
%% Save the data:
save('example_himmelblau_m');