forked from terejanu/AdaptiveGaussianSumFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
executable file
·422 lines (359 loc) · 15.2 KB
/
main.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
%--------------------------------------------------------------------------
% Simulations for the Adaptive Weights Gaussian Sum Filter paper
%
% Gabriel Terejanu ([email protected])
%--------------------------------------------------------------------------
close all;
clear all;
clc;
%% ------------------------------------------------------------------------
% init
%--------------------------------------------------------------------------
runLocal = true;
mcRuns = 1;
% if running in parallel make sure this are set right
cwd = '/home/csgrad/terejanu/gabi/gs-da/simulations_continuous';
inc = '/home/csgrad/terejanu/matlab_util';
%% ------------------------------------------------------------------------
% model
%--------------------------------------------------------------------------
% ---- LORENZ EXAMPLE -----
model.fn = 3; % state space dimensionality
model.fx = 'fx_lorenz';
model.hn = 1; % measurement dimensionality
model.hx = 'hx_lorenz';
% model.Q = diag([0 0 1]); %model.Q(3,3) = 2;
model.Q = diag([1 1 1]); model.Q(3,3) = 2;
model.sQ = sqrt(model.Q);
model.R = 1;
% ---- 2D EXAMPLE -----
% model.fn = 2; % state space dimensionality
% model.fx = 'fx_2dex';
% model.hn = 1; % measurement dimensionality
% model.hx = 'hx_2dex';
% model.Q = 0.2^2*ones(2);
% model.sQ = sqrt(model.Q);
% model.R = 1;
% ---- SIN(X) EXAMPLE -----
% model.fn = 1; % state space dimensionality
% model.fx = 'fx_sinx';
% model.hn = 1; % measurement dimensionality
% model.hx = 'hx_squared';
% model.Q = 1;
% model.sQ = sqrt(model.Q);
% model.R = 1;
%% ------------------------------------------------------------------------
% prior Gaussian Sum !!!!!! make them cells (mu & sig) !!!!!!!!
%--------------------------------------------------------------------------
% ---- LORENZ EXAMPLE -----
% prior.n = 2;
% [prior.weig(1), prior.mu{1}, prior.sig{1}, prior.weig(2), prior.mu{2}, prior.sig{2}] = ...
% split(1, [.5;-1.5;25], eye(3), 1, 0.5);
% prior.weig(1) = 0.5;
% prior.weig(1) = 0.5;
prior.mu{1} = [-.2 -.2 8]';
prior.mu{2} = [.2 .2 8]';
% prior.mu{3} = [0 0 8]';
prior.sig{1} = sqrt(0.35)*eye(3,3);
prior.sig{2} = sqrt(0.35)*eye(3,3);
% prior.sig{3} = sqrt(0.35)*eye(3,3);
prior.n = 2;
prior.weig = [0.9 0.1];
% prior.weig = [0.2 0.2 0.6];
% [X,Y] = meshgrid(1:0.2:2,-3:0.2:-2);
% prior.n = prod(size(X));
% ix = 0;
% for i = 1 : size(X,1)
% for j = 1 : size(X,2)
% ix = ix + 1;
% prior.mu{ix} = [X(i,j) Y(i,j) 6]';
% prior.sig{ix} = sqrt(0.35)*eye(3,3);
% end;
% end;
% prior.weig = ones(1,prior.n)/prior.n;
% ---- 2D EXAMPLE -----
% prior.mu{1} = [0.1 0]';
% prior.mu{2} = [-0.1 0]';
% prior.sig{1} = 0.1^2*eye(2);
% prior.sig{2} = 0.1^2*eye(2);
% prior.n = 2;
% prior.weig = [0.9 0.1];
% ---- SIN(X) EXAMPLE -----
% prior.n = 2;
% prior.weig = [0.1 0.9];
% prior.mu{1} = -0.2;
% prior.mu{2} = 0.2;
% prior.sig{1} = 1;
% prior.sig{2} = 1;
%% ------------------------------------------------------------------------
% time
%--------------------------------------------------------------------------
% ---- LORENZ EXAMPLE -----
time.t0 = 0;
time.dt = 0.01;
time.tf = 0.51;
time.tspan = time.t0 : time.dt : time.tf;
time.nSteps = length(time.tspan);
time.dt_N = 1000;
time.dt_sde = time.dt / time.dt_N;
% ---- 2D EXAMPLE -----
% time.t0 = 0;
% time.dt = 0.25;
% time.tf = 10;
% time.tspan = time.t0 : time.dt : time.tf;
% time.nSteps = length(time.tspan);
% ---- SIN(X) EXAMPLE -----
% time.t0 = 0;
% time.dt = 0.1;
% time.tf = 8;
% time.tspan = time.t0 : time.dt : time.tf;
% time.nSteps = length(time.tspan);
%% ------------------------------------------------------------------------
% measurements available every n time steps
%--------------------------------------------------------------------------
measTs = 1;
measAvail = zeros(1,time.nSteps);
measAvail(1:measTs:time.nSteps) = 1;
%% ------------------------------------------------------------------------
% particle filter settings
%--------------------------------------------------------------------------
pf.no_particles = 10000;
pf.no_bins = 100; % when computing the histogram
pf.resample = true; % resampling after measurement update
pf.neff = 0.8; % treshold for the number of effective samples
pf.plot_pdf = true; % if mcRuns = 1 makes sense to plot the pdf in time
noPdfs = 10; % number pdf slices to be plotted
pf.plot_pdf_available = round(linspace(1,time.nSteps,noPdfs));
pf.bounds = 0.05; % enlarge the bounds when plotting the pdf (bounds are given by particle filter)
%% ------------------------------------------------------------------------
% filter used for propagation of the 2 momemnts
%--------------------------------------------------------------------------
%----------------------------------------
% avail: EKF - Extended Kalman Filter
myfilter.name = 'EKF';
%----------------------------------------
% avail: IUT - Unscented Transformation 6n+1 sigma points
myfilter.integration_method = 'IUT';
% GQ_perComp - Gaussian Quadrature - quadrature points obtained
% for each gaussian component
% myfilter.integration_method = 'GQ_perComp';
% myfilter.integration_GQ_bound = 3; % max bound given the standard deviation of a gaussian component
% myfilter.integration_GQ_no_points = 100; % number of points per dimension
% GQ_all - Gaussian Quadrature - quadrature points obtained
% for the entire domain
% myfilter.integration_method = 'GQ_all';
% myfilter.integration_GQ_bound = 3; % max bound given the standard deviation of a gaussian component
% myfilter.integration_GQ_no_points = 100; % number of points per dimension
%% ------------------------------------------------------------------------
% init Job Distribution
%--------------------------------------------------------------------------
if (~runLocal)
jm = findResource('jobmanager','name','underground');
job = createJob(jm);
set(job,'FileDependencies',{cwd})
for iRun = 1 : mcRuns
obj(iRun) = createTask(job, @runFilters, 5, {iRun,model,prior,time,measAvail,pf,inc,myfilter});
end;
submit(job);
waitForState(job,'finished');
result = getAllOutputArguments(job);
end;
%% ------------------------------------------------------------------------
% init for MC results
%--------------------------------------------------------------------------
% error wrt the truth
abserrMC_GS1 = zeros(model.fn,length(time.tspan));
abserrMC_GS2 = zeros(model.fn,length(time.tspan));
abserrMC_GS4 = zeros(model.fn,length(time.tspan));
abserrMC_PF = zeros(model.fn,length(time.tspan));
% error wrt the truth
errMC_GS1 = zeros(size(time.tspan));
errMC_GS2 = zeros(size(time.tspan));
errMC_GS4 = zeros(size(time.tspan));
errMC_PF = zeros(size(time.tspan));
% error wrt the particle filter
errPF_GS1 = zeros(size(time.tspan));
errPF_GS2 = zeros(size(time.tspan));
errPF_GS4 = zeros(size(time.tspan));
% average loglikelihood
ll_GS1 = zeros(size(time.tspan));
ll_GS2 = zeros(size(time.tspan));
ll_GS4 = zeros(size(time.tspan));
% average estimator variance / dimension
s_GS1 = zeros(size(time.tspan));
s_GS2 = zeros(size(time.tspan));
s_GS4 = zeros(size(time.tspan));
for iRun = 1 : mcRuns
%--------------------------------------------------------------------------
% distributed collection
%--------------------------------------------------------------------------
if (~runLocal)
taskoutput = get(obj(iRun), 'OutputArguments');
xt = taskoutput{1}; % truth
F1 = taskoutput{2}; % no update
F2 = taskoutput{3}; % classic Bayes update
F4 = taskoutput{4}; % continuous update - FPKE
PF = taskoutput{5}; % particle filter
end;
%--------------------------------------------------------------------------
% local run
%--------------------------------------------------------------------------
if (runLocal)
[xt,F1,F2,F4,PF] = runFilters(iRun,model,prior,time,measAvail,pf,inc,myfilter);
end;
saveALL.xt{iRun} = xt;
saveALL.F1{iRun} = F1;
saveALL.F2{iRun} = F2;
saveALL.F4{iRun} = F4;
saveALL.PF{iRun} = PF;
%--------------------------------------------------------------------------
% get the estimates (1st & 2nd moment)
%--------------------------------------------------------------------------
xGS1 = F1.x; xGS2 = F2.x; xGS4 = F4.x; xPF = PF.x;
sGS1 = F1.P; sGS2 = F2.P;sGS4 = F4.P; sPF = PF.P;
lGS1 = F1.ll; lGS2 = F2.ll; lGS4 = F4.ll;
%--------------------------------------------------------------------------
% get the estimates (1st & 2nd moment, loglikelihood)
%--------------------------------------------------------------------------
% for i = 1 : model.fn
% for j = 1 : time.nSteps
% abserrMC_GS1(i,j) = abserrMC_GS1(i,j) + sqrt(mean((xGS1{j}(i) - xt{j}(i)).^2)) / mcRuns;
% abserrMC_GS2(i,j) = abserrMC_GS2(i,j) + sqrt(mean((xGS2{j}(i) - xt{j}(i)).^2)) / mcRuns;
% abserrMC_GS4(i,j) = abserrMC_GS4(i,j) + sqrt(mean((xGS4{j}(i) - xt{j}(i)).^2)) / mcRuns;
% abserrMC_PF(i,j) = abserrMC_PF(i,j) + sqrt(mean((xPF{j}(i) - xt{j}(i)).^2)) / mcRuns;
% end;
% end;
for i = 1 : time.nSteps
errMC_GS1(i) = errMC_GS1(i) + norm(xGS1{i} - xt{i})^2 / mcRuns;
errMC_GS2(i) = errMC_GS2(i) + norm(xGS2{i} - xt{i})^2 / mcRuns;
errMC_GS4(i) = errMC_GS4(i) + norm(xGS4{i} - xt{i})^2 / mcRuns;
errMC_PF(i) = errMC_PF(i) + norm(xPF{i} - xt{i})^2 / mcRuns;
errPF_GS1(i) = errPF_GS1(i) + norm(xGS1{i} - xPF{i})^2 / mcRuns;
errPF_GS2(i) = errPF_GS2(i) + norm(xGS2{i} - xPF{i})^2 / mcRuns;
errPF_GS4(i) = errPF_GS4(i) + norm(xGS4{i} - xPF{i})^2 / mcRuns;
ll_GS1(i) = ll_GS1(i) + lGS1{i} / mcRuns;
ll_GS2(i) = ll_GS2(i) + lGS2{i} / mcRuns;
ll_GS4(i) = ll_GS4(i) + lGS4{i} / mcRuns;
s_GS1(i) = s_GS1(i) + trace(sGS1{i}) / model.fn / mcRuns;
s_GS2(i) = s_GS2(i) + trace(sGS2{i}) / model.fn / mcRuns;
s_GS4(i) = s_GS4(i) + trace(sGS4{i}) / model.fn / mcRuns;
end;
end
%% ------------------------------------------------------------------------
% destroy job
%--------------------------------------------------------------------------
if (~runLocal)
destroy(job);
end;
%% ------------------------------------------------------------------------
% PLOT RESULTS
%--------------------------------------------------------------------------
%% - ABS ERR (wrt truth) -----------------------------------------------------
% for i = 1 : model.fn
% figure; hold on;
% plot(time.tspan,abserrMC_GS1(i,:),'g');
% plot(time.tspan,abserrMC_GS2(i,:),'b');
% plot(time.tspan,abserrMC_GS4(i,:),'r');
% plot(time.tspan,abserrMC_PF(i,:),'k');
% legend('GS1 - no update','GS2 - classic','GS4 - w.upd.FPKE','PF - partile filter');
% title(['avg. ABS ERR comparison (wrt truth) - state ' num2str(i)]);
% hold off;
% end;
%% - RMSE (wrt truth) -----------------------------------------------------
figure; hold on;
plot(time.tspan,sqrt(errMC_GS1),'g');
plot(time.tspan,sqrt(errMC_GS2),'b');
plot(time.tspan,sqrt(errMC_GS4),'r');
plot(time.tspan,sqrt(errMC_PF),'k');
legend('GS1 - no update','GS2 - classic','GS4 - w.upd.FPKE','PF - partile filter');
title(['avg. RMSE comparison (wrt truth) - ' num2str(mcRuns)]);
hold off;
%% - RMSE (wrt particle filter) -------------------------------------------
figure; hold on;
plot(time.tspan,sqrt(errPF_GS1),'g');
plot(time.tspan,sqrt(errPF_GS2),'b');
plot(time.tspan,sqrt(errPF_GS4),'r');
legend('GS1 - no update','GS2 - classic','GS4 - w.upd.FPKE');
title(['avg. RMSE comparison (wrt particle filter) - ' num2str(mcRuns)]);
hold off;
%% - LOG-LIKELIHOOD -------------------------------------------------------
figure; hold on;
plot(time.tspan,ll_GS1,'g');
plot(time.tspan,ll_GS2,'b');
plot(time.tspan,ll_GS4,'r');
legend('GS1 - no update','GS2 - classic','GS4 - w.upd.FPKE');
title(['avg. Loglikelihood of the particles - ' num2str(mcRuns)]);
hold off;
%% - LOG-LIKELIHOOD NORMALIZED --------------------------------------------
% m_div = sqrt(ll_GS2.^2 + ll_GS4.^2);
%
% figure; hold on;
% plot(time.tspan,ll_GS2./m_div,'b');
% plot(time.tspan,ll_GS4./m_div,'r');
% legend('Classic GSF','Adaptive GSF');
% title(['avg. Loglikelihood of the particles - ' num2str(mcRuns)]);
% hold off;
%% - AVERAGE NORMALIZED CROSS-ENTROPY -------------------------------------
nH2 = zeros(size(ll_GS2));
nH4 = zeros(size(ll_GS4));
for i = 1 : size(ll_GS2,1)
tmp2 = -ll_GS2(i,:)./pf.no_particles;
tmp4 = -ll_GS4(i,:)./pf.no_particles;
tmp_div = sqrt(tmp2.^2+tmp4.^2);
if tmp_div > 0,
nH2(i,:) = tmp2./tmp_div;
nH4(i,:) = tmp4./tmp_div;
end;
end;
figure; hold on;
plot(time.tspan,mean(nH2,1),'b');
plot(time.tspan,mean(nH4,1),'r');
legend('GS2 - classic','GS4 - w.upd.CKE');
title(['Average Normalized Cross-Entropy - ' num2str(mcRuns)]);
hold off;
%% - VARIANCE / DIMENSION -------------------------------------------------
% figure; hold on;
% plot(time.tspan,s_GS1,'g');
% plot(time.tspan,s_GS2,'m');
% plot(time.tspan,s_GS4,'b');
% legend('GS1 - no update','GS2 - classic','GS3 - w.upd.CKE','GS4 - w.upd.FPKE');
% title(['avg. Variance per dimension - ' num2str(mcRuns)]);
% hold off;
%% - PLOT PDF SNAPSHOTS ---------------------------------------------------
% if (pf.plot_pdf)
%
% % loop over state dimensions
% for i = 1 : model.fn
% figure;
% for k = 1 : length(pf.plot_pdf_available)
% idx = pf.plot_pdf_available(k);
%
% subplot(221); hold on; % - Particle filter
% patch(PF.pdf{idx}.dim(i).mmX,PF.pdf{idx}.dim(i).mmY,PF.pdf{idx}.dim(i).mmZ,'b');
% legend('PF');
%
% subplot(222); hold on; % - GS2 - classic update
% patch(F2.pdf{idx}.dim(i).mmX,F2.pdf{idx}.dim(i).mmY,F2.pdf{idx}.dim(i).mmZ,'b');
% legend('GS2 - classic');
%
% subplot(224); hold on; % - GS4 - continuous update - FPKE
% patch(F4.pdf{idx}.dim(i).mmX,F4.pdf{idx}.dim(i).mmY,F4.pdf{idx}.dim(i).mmZ,'b');
% legend('GS4 - FPKE');
%
% end
% subplot(221); view(-30,37); grid on; hold off;
% subplot(222); view(-30,37); grid on; hold off;
% subplot(224); view(-30,37); grid on; hold off;
% title('Our comparisson');
%
% % figure; hold on; % - GS1 - no update
% % for k = 1 : length(pf.plot_pdf_available)
% % idx = pf.plot_pdf_available(k);
% % patch(F1.pdf{idx}.dim(i).mmX,F1.pdf{idx}.dim(i).mmY,F1.pdf{idx}.dim(i).mmZ,'b');
% % end
% % view(-30,37); grid on; hold off;
% % title('Pdf if no update whatsoever');
% end;
%
% end;
save final_results;