forked from MBB-team/VBA-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBA_ReDisplay.m
652 lines (575 loc) · 25.4 KB
/
VBA_ReDisplay.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
function [hfp,out] = VBA_ReDisplay(posterior,out,newFig,fromPause)
% re-creates the graphical output of the VBA inversion + diagnostics
% function [hfp] = VBA_ReDisplay(posterior,out,newFig)
% VBA_ReDisplay first looks for a figure with a tag 'VBNLSS', i.e. a figure
% that was already opened to review a model inversion, and clears it if it
% finds it (except if newFig=1).
% IN:
% - posterior/out: standard output of VBA_NLStateSpaceModel.m
% - newFig: a flag for creating a new VBA display figure.
% OUT:
% - hfp: handle of the display figure
% - out: standard output of VBA_NLStateSpaceModel.m, augmented with
% diagnostics, if those were not included in the out structure before the
% call to VBA_ReDisplay.m.
% set default
if ~exist('newFig','var')
newFig=0;
end
if ~exist('fromPause','var')
fromPause=0;
end
% create shortcuts
options = out.options;
% check if a figure already exist or create a new one if needed or requested
hfp = findobj('tag','VBNLSS');
if isempty(hfp) || newFig
pos0 = get(0,'screenSize');
pos = [0.51*pos0(3),0.05*pos0(4),0.45*pos0(3),0.9*pos0(4)];
hfp = figure( ...
'position' , pos , ...
'color' , [1 1 1] , ...
'name' , options.figName , ...
'menubar' , 'none' , ...
'tag' , 'VBNLSS' , ...
'Renderer' , 'OpenGL' );
else
hfp = hfp(1);
clf(hfp)
set(hfp,'name',options.figName);
end
% ensure display in switched on
options.DisplayWin = 1;
% compute missing statistics if needed
if ~isfield(out,'diagnostics')
out.diagnostics = VBA_getDiagnostics(posterior,out);
end
% store data in figure for later
ud.posterior = posterior;
ud.out = out;
set(hfp,'userdata',ud);
% setup tabs
if ~isempty(out.diagnostics.kernels)
labels = {'summary','VB inversion','diagnostics','kernels','conv','priors'};
callbacks = {@mySummary,@myVB,@myDiagnostics,@myKernels,@myConv,@myPriors};
else
labels = {'summary','VB inversion','diagnostics','conv','priors'};
callbacks = {@mySummary,@myVB,@myDiagnostics,@myConv,@myPriors};
end
if out.dim.n > 0 && ~isinf(out.options.priors.a_alpha) && ~isequal(out.options.priors.b_alpha,0) && ~out.options.OnLine
labels{end+1} = 'deterministic';
callbacks{end+1} = @myDeterministic;
end
if fromPause
active = 2;
else
active = 1;
end
[handles] = spm_uitab(hfp,labels,callbacks,'diagnostics_tabs',active);
set(handles.htab , 'backgroundcolor' , [1 1 1] );
set(handles.hh , 'backgroundcolor' , [1 1 1] );
set(handles.hp , 'backgroundcolor' , [1 1 1] );
set(handles.hp , 'HighlightColor' , 0.8*[1 1 1] );
set(handles.htab(1), 'tooltipstring' , 'summary description of the VB inversion' );
set(handles.htab(2), 'tooltipstring' , 'results of the VB inversion (posterior pdfs)');
set(handles.htab(3), 'tooltipstring' , 'VB inversion diagnostics (residuals and parameters covariance matrices)');
if ~isempty(out.diagnostics.kernels)
ind = 4;
set(handles.htab(4),'tooltipstring','system''s 1st-order Volterra kernels')
else
ind = 3;
end
set(handles.htab(ind+1),'tooltipstring','history of free energy values along VB optimization')
set(handles.htab(ind+2),'tooltipstring','priors and associated predictive densities (under the Laplace assumption)')
if out.dim.n > 0 && ~isinf(out.options.priors.a_alpha) && ~isequal(out.options.priors.b_alpha,0) && ~out.options.OnLine
set(handles.htab(ind+3),'tooltipstring','results of the VB inversion of the deterministic system')
end
if fromPause
feval(@myVB,hfp)
else
feval(@mySummary,hfp)
end
% =========================================================================
% tabs callbacks
% =========================================================================
function mySummary(hfig)
try hfig; catch, hfig = get(gco,'parent'); end
cleanPanel(hfig);
ud = get(hfig,'userdata');
out = ud.out;
str = VBA_summary(out,1);
for i=1:length(str)
str{i} = sprintf(str{i});
end
str{7} = sprintf(['Estimation efficiency (minus posterior entropies):','\n ']);
if ~isnan(out.diagnostics.efficiency.X)
str{7} = sprintf([str{7},' - hidden states: ',num2str(out.diagnostics.efficiency.X,'%4.3e'),'\n ']);
end
if ~isnan(out.diagnostics.efficiency.X0)
str{7} = sprintf([str{7},' - initial conditions: ',num2str(out.diagnostics.efficiency.X0,'%4.3e'),'\n ']);
end
if ~isnan(out.diagnostics.efficiency.Theta)
str{7} = sprintf([str{7},' - evolution parameters: ',num2str(out.diagnostics.efficiency.Theta,'%4.3e'),'\n ']);
end
if ~isnan(out.diagnostics.efficiency.Phi)
str{7} = sprintf([str{7},' - observation parameters: ',num2str(out.diagnostics.efficiency.Phi,'%4.3e'),'\n ']);
end
if ~isnan(out.diagnostics.efficiency.alpha)
str{7} = sprintf([str{7},' - state noise precision hyperparameter: ',num2str(out.diagnostics.efficiency.alpha,'%4.3e'),'\n ']);
end
if ~any(isnan(out.diagnostics.efficiency.sigma))
gsi = find([out.options.sources.type]==0);
sig_str = catnum2str(out.diagnostics.efficiency.sigma,gsi,length(gsi)>1);
str{7} = sprintf([str{7},' - data noise precision hyperparameter: ',sig_str,'\n ']);
end
uicontrol( ...
'parent' , hfig , ...
'style' , 'text' , ...
'tag' , 'VBLaplace' , ...
'units' , 'normalized' , ...
'position' , [0.1,0.05,0.8,0.85] , ...
'backgroundcolor' , [1,1,1] , ...
'HorizontalAlignment' , 'left' , ...
'fontsize' , 11 , ...
'string' , str );
function myDeterministic(hfig)
try hfig; catch, hfig = get(gco,'parent'); end
cleanPanel(hfig);
ud = get(hfig,'userdata');
% Second: re-display VB-Laplace inversion output
y = ud.out.y;
posterior = ud.out.options.init.posterior;
out = ud.out.options.init.out;
options = out.options;
options.noPause = 1;
options.DisplayWin = 1;
dim = out.dim;
suffStat = out.suffStat;
posterior.a_alpha = Inf;
posterior.b_alpha = 0;
% Initialize display figure
options.display.hfp = hfig;
options.figName = get(hfig,'name');
[options] = VBA_initDisplay(options);
delete(options.display.htt)
delete(options.display.hpause)
delete(options.display.hm)
delete(options.display.ho)
if options.dim.n == 0 || isinf(posterior.a_alpha(end))
try delete(options.display.ha(8)); end
end
hfig = options.display.hfp;
drawnow
% Display data and hidden states (if any)
if options.dim.n > 0
VBA_updateDisplay(posterior,suffStat,options,y,0,'X')
end
% Display precision hyperparameters
VBA_updateDisplay(posterior,suffStat,options,y,0,'precisions')
if ~options.OnLine && ~options.binomial
xlabel(options.display.ha(6),' ')
if numel(options.display.ha)>7 && ishghandle(options.display.ha(8))
xlabel(options.display.ha(8),' ')
end
end
% Display model evidence
VBA_updateDisplay(posterior,suffStat,options,y,0,'F')
% Display parameters
if dim.n_theta >= 1
VBA_updateDisplay(posterior,suffStat,options,y,0,'theta')
end
if dim.n_phi >= 1
VBA_updateDisplay(posterior,suffStat,options,y,0,'phi')
end
try getSubplots ; end
uicontrol( ...
'parent' , hfig , ...
'style' , 'pushbutton' , ...
'tag' , 'VBLaplace' , ...
'units' , 'normalized' , ...
'position' , [0.40 0.93 0.20 0.02] , ...
'backgroundcolor' , .8*[1,1,1] , ...
'string' , 'diagnose deterministic?' , ...
'callback' , @diagnoseDeterministic );
function diagnoseDeterministic(ho,e)
ud = get(get(ho,'parent'),'userdata');
posterior = ud.out.options.init.posterior;
out = ud.out.options.init.out;
VBA_ReDisplay(posterior,out,1);
function myPriors(hfig)
try hfig; catch, hfig = get(gco,'parent'); end
cleanPanel(hfig);
ud = get(hfig,'userdata');
% Second: re-display VB-Laplace inversion output
out = ud.out ;
y = out.y ;
posterior = out.options.priors ;
options = out.options ;
options.noPause = 1 ;
options.DisplayWin = 1 ;
dim = out.dim ;
suffStat = out.suffStat ;
suffStat.gx = out.diagnostics.pgx ;
% set dx = -prior.muX (for display purposes)
suffStat.dx0 = -posterior.muX0 ;
suffStat.dtheta = -posterior.muTheta ;
suffStat.dphi = -posterior.muPhi ;
suffStat.vy = out.diagnostics.pvy ;
% Initialize display figure
options.display.hfp = hfig;
options.figName = get(hfig,'name');
[options] = VBA_initDisplay(options,1);
delete(options.display.htt)
delete(options.display.hpause)
delete(options.display.hm)
delete(options.display.ho)
if options.dim.n == 0 || isinf(posterior.a_alpha(end))
try delete(options.display.ha(8)); end
end
% Display data and hidden states (if any)
if options.dim.n > 0
options.OnLine = 0;
VBA_updateDisplay(posterior,suffStat,options,y,0,'X')
end
% Display precision hyperparameters
VBA_updateDisplay(posterior,suffStat,options,y,0,'precisions')
if ~options.OnLine && ~options.binomial
xlabel(options.display.ha(6),' ') ;
if numel(options.display.ha)>7 && ishghandle(options.display.ha(8))
xlabel(options.display.ha(8),' ') ;
end
end
% Display model evidence
VBA_updateDisplay(posterior,suffStat,options,y,0,'F')
% Display parameters
if dim.n_theta >= 1
VBA_updateDisplay(posterior,suffStat,options,y,0,'theta')
end
if dim.n_phi >= 1
VBA_updateDisplay(posterior,suffStat,options,y,0,'phi')
end
try getSubplots ; end
function myConv(hfig)
try hfig; catch, hfig = get(gco,'parent'); end
cleanPanel(hfig);
ud = get(hfig,'userdata');
hPanel = getPanel(hfig);
out = ud.out;
diagnostics = out.diagnostics;
if length(out.suffStat.F)>2
nit = length(out.suffStat.F)-1;
ha = axes('parent',hPanel,'units','normalized','tag','VBLaplace','position',[0.15,0.6,0.5,0.3],'nextplot','add','xlim',[0,nit],'xtick',[0,nit],'xticklabel',{'prior (0)',['posterior (',num2str(nit),')']},'xgrid','off','ygrid','on');
plot(ha,[0:nit],out.suffStat.F)
plot(ha,[0:nit],out.suffStat.F,'.') ;
[haf,hp1,hp2] = plotUncertainTimeSeries(diagnostics.LLH0*ones(1,2),3^2*ones(1,2),[0,nit],ha);
set(hp1,'facecolor',[1 0 0])
set(hp2,'color',[1 0 0])
text(nit/2,diagnostics.LLH0-3/2,'log p(y|H0)','color',[1 0 0],'parent',ha);
if ~out.options.OnLine
title(ha,'VB optimization: F values','fontsize',11)
xlabel(ha,'inner (Gauss-Newton) iterations')
else
title(ha,'online VB: F values','fontsize',11)
xlabel(ha,'time samples')
end
ylabel(ha,'Free energy')
box(ha,'off')
if ~out.options.OnLine
xtl = {'first iteration','last iteration'};
else
xtl = {'first time point','last time point'};
end
ha = axes('parent',hPanel,'units','normalized','tag','VBLaplace','position',[0.15,0.15,0.5,0.3],'nextplot','add','xlim',[0,nit-1],'xtick',[0,nit-1],'xticklabel',xtl,'xgrid','off','ygrid','on');
plot(ha,[0:nit-1],diff(out.suffStat.F)) ;
plot(ha,[0:nit-1],diff(out.suffStat.F),'.') ;
if ~out.options.OnLine
title(ha,'VB optimization: F increments','fontsize',11)
xlabel(ha,'inner (Gauss-Newton) iterations')
else
title(ha,'online VB: F increments','fontsize',11)
xlabel(ha,'time samples')
end
ylabel(ha,'Free energy differences')
box(ha,'off')
try
getSubplots ;
end
end
options = orderfields(out.options);
options = rmfield(options,'tStart');
options = rmfield(options,'checkGrads');
options = rmfield(options,'verbose');
options = rmfield(options,'delays');
options = rmfield(options,'isYout');
options = rmfield(options,'skipf');
finames = fieldnames(options);
nopt = length(finames);
str = {'Optional fields:';' '};
for i=1:nopt
tmp = getfield(options,finames{i});
if ~isempty(tmp) && isnumeric(tmp)
str{end+1} = [finames{i},' = ',num2str(max(tmp))];
end
end
uicontrol('parent',hfig,'style','text','tag','VBLaplace','units','normalized','position',[0.75,0.1,0.2,0.8],'backgroundcolor',[1,1,1],'HorizontalAlignment','left','fontsize',11,'string',str);
function myKernels(hfig)
try hfig; catch, hfig = get(gco,'parent'); end
cleanPanel(hfig);
ud = get(hfig,'userdata');
du = size(ud.out.diagnostics.kernels.y.m,3);
unames = cell(du,1);
for i=1:du %dim u
unames{i} = ['#',num2str(i)];
end
handles(1) = uicontrol('style','popupmenu','parent',hfig,'tag','VBLaplace','units','normalized','position',[0.85 0.9 0.10 0.02],'fontsize',12,'string',unames,'callback',@myKerneli);
handles(2) = uicontrol('style','text','parent',hfig,'tag','VBLaplace','BackgroundColor',get(hfig,'color'),'units','normalized','position',[0.82 0.93 0.16 0.02],'fontsize',12,'string','display input...');
feval(@myKerneli,handles(1),[])
function myKerneli(hObject,evt)
hfig = get(hObject,'parent');
ind = get(hObject,'Value');
ud = get(hfig,'userdata');
try
if isequal(get(ud.handles.hkernels(2),'parent'),hfig)
delete(ud.handles.hkernels) ;
end
end
out = ud.out;
kernels = out.diagnostics.kernels;
hPanel = getPanel(hfig);
if ~isempty(kernels.x)
% input effects - hidden states
handles.hkernels(1) = subplot(2,1,1,'parent',hPanel,'nextplot','add','ygrid','on','tag','VBLaplace');
pos = get(handles.hkernels(1),'position');
set(handles.hkernels(1),'position',[0.2 pos(2) 0.6 pos(4)]);
[t1,t2,hp] = plotUncertainTimeSeries(kernels.x.m(:,:,ind),kernels.x.v(:,:,ind),[],handles.hkernels(1));
set(hp,'marker','.')
set(handles.hkernels(1),'XLim',[0.5 size(kernels.x.m,2)+0.5],'xtick',[1:size(kernels.x.m,2)],'xticklabel',[0:size(kernels.x.m,2)-1])
title(handles.hkernels(1),['states'' Volterra kernels: input #',num2str(ind),' (R2=',num2str(mean(kernels.x.R2),'%4.2f'),')'],'fontsize',12)
ylabel(handles.hkernels(1),'(lagged) input weight')
xlabel(handles.hkernels(1),'time lag')
end
% input effects - observables
handles.hkernels(2) = subplot(2,1,2,'parent',hPanel,'nextplot','add','ygrid','on','tag','VBLaplace');
pos = get(handles.hkernels(2),'position');
set(handles.hkernels(2),'position',[0.2 pos(2) 0.6 pos(4)])
hold(handles.hkernels(2),'on')
plot(handles.hkernels(2),kernels.g.m(1,:,ind)','marker','.','color',[0 0 0])
plot(handles.hkernels(2),kernels.y.m(1,:,ind)','marker','.','linestyle',':','color',[0 0 0])
legend(handles.hkernels(2),{['simulated observables',' (R2=',num2str(mean(kernels.g.R2),'%4.2f'),')'],['observed samples',' (R2=',num2str(mean(kernels.y.R2),'%4.2f'),')']})
plot(handles.hkernels(2),kernels.y.m(:,:,ind)','marker','.','linestyle',':')
[t1,t2,hp] = plotUncertainTimeSeries(kernels.g.m(:,:,ind),kernels.g.v(:,:,ind),[],handles.hkernels(2));
set(hp,'marker','.') ;
set(handles.hkernels(2),'XLim',[0.5 size(kernels.g.m,2)+0.5],'xtick',[1:size(kernels.g.m,2)],'xticklabel',[0:size(kernels.g.m,2)-1]) ;
title(handles.hkernels(2),['observables'' Volterra kernels: input #',num2str(ind)],'fontsize',12)
ylabel(handles.hkernels(2),'(lagged) input weight')
xlabel(handles.hkernels(2),'time lag')
ud.handles = handles;
set(hfig,'userdata',ud);
try, getSubplots; end
function myDiagnostics(hfig)
try hfig; catch, hfig = get(gco,'parent'); end
cleanPanel(hfig);
ud = get(hfig,'userdata');
out = ud.out;
y = out.y;
diagnostics = out.diagnostics;
hPanel = getPanel(hfig);
% display micro-time hidden-states
if ~isempty(diagnostics.MT_x)
display.ha(1) = subplot(4,2,1,'parent',hPanel,'nextplot','add','tag','VBLaplace','ygrid','on','box','off');
title(display.ha(1),'micro-time resolution predicted data','fontsize',11)
xlabel(display.ha(1),'time','fontsize',8)
ylabel(display.ha(1),'g(x) & y','fontsize',8)
plot(display.ha(1),diagnostics.microTime,diagnostics.MT_gx')
plot(display.ha(1),diagnostics.microTime(diagnostics.sampleInd),diagnostics.MT_gx(:,diagnostics.sampleInd)','.')
plot(display.ha(1),diagnostics.microTime(diagnostics.sampleInd),y,':')
axis(display.ha(1),'tight')
display.ha(2) = subplot(4,2,2,'parent',hPanel,'nextplot','add','tag','VBLaplace','ygrid','on','box','off');
title(display.ha(2),'micro-time resolution hidden states','fontsize',11)
xlabel(display.ha(2),'time','fontsize',8)
ylabel(display.ha(2),'x','fontsize',8)
plot(display.ha(2),diagnostics.microTime,diagnostics.MT_x')
plot(display.ha(2),diagnostics.microTime(diagnostics.sampleInd),diagnostics.MT_x(:,diagnostics.sampleInd)','.')
axis(display.ha(2),'tight')
end
if numel(out.options.sources)>1
vis = 'on';
else
vis = 'off';
end
ds = numel(ud.out.diagnostics.dy);
snames = cell(ds,1);
for i=1:ds %dim s
snames{i} = ['#',num2str(i)];
end
handles(1) = uicontrol('style','popupmenu','parent',hfig,'tag','VBLaplace','units','normalized','position',[0.55 0.5 0.10 0.02],'fontsize',12,'string',snames,'callback',@myDiagnosticsi,'visible',vis);
handles(2) = uicontrol('style','text','parent',hfig,'tag','VBLaplace','BackgroundColor',get(hfig,'color'),'units','normalized','position',[0.52 0.53 0.16 0.02],'fontsize',12,'string','source:','visible',vis);
feval(@myDiagnosticsi,handles(1),[])
% display state noise
if ~isempty(diagnostics.dx.dx)
xlim = [diagnostics.dx.nx(1)-diagnostics.dx.d,diagnostics.dx.nx(end)+diagnostics.dx.d];
display.ha(4) = subplot(4,2,6,'parent',hPanel,'nextplot','add','xlim',xlim,'ygrid','on','tag','VBLaplace','box','off');
title(display.ha(4),'state noise empirical distribution','fontsize',11)
xlabel(display.ha(4),'eta(t) = x(t+1)-f(x(t))','fontsize',8)
ylabel(display.ha(4),'p(eta|y)','fontsize',8)
bar(diagnostics.dx.nx,diagnostics.dx.ny,'facecolor',[.8 .8 .8],'parent',display.ha(4))
plot(display.ha(4),diagnostics.dx.grid,diagnostics.dx.pg,'r')
plot(display.ha(4),diagnostics.dx.grid,diagnostics.dx.pg2,'g')
legend(display.ha(4),{'empirical histogram','Gaussian approx','posterior approx'})
display.ha(8) = subplot(4,2,4,'parent',hPanel,'nextplot','add','tag','VBLaplace','ygrid','on','box','off');
try
plotUncertainTimeSeries(out.suffStat.dx,out.suffStat.vdx,diagnostics.microTime(diagnostics.sampleInd),display.ha(8));
catch
plot(display.ha(8),diagnostics.microTime(diagnostics.sampleInd),out.suffStat.dx','marker','.')
end
axis(display.ha(8),'tight')
title(display.ha(8),'state noise time series','fontsize',11)
xlabel(display.ha(8),'time','fontsize',8)
ylabel(display.ha(8),'eta(t) = x(t+1)-f(x(t))','fontsize',8)
end
% display parameters posterior correlation matrix
display.ha(6) = subplot(4,2,8,'parent',hPanel);
imagesc(diagnostics.C,'parent',display.ha(6))
title(display.ha(6),'parameters posterior correlation matrix','fontsize',11)
set(display.ha(6),'tag','VBLaplace','xtick',diagnostics.ltick,'ytick',diagnostics.ltick,'xticklabel',diagnostics.ticklabel,'yticklabel',diagnostics.ticklabel,'box','off','nextplot','add');
for i=1:length(diagnostics.tick)
plot(display.ha(6),[0.5 size(diagnostics.C,1)+0.5],[diagnostics.tick(i) diagnostics.tick(i)],'color',[1 1 1])
plot(display.ha(6),[diagnostics.tick(i) diagnostics.tick(i)],[0.5 size(diagnostics.C,1)+0.5],'color',[1 1 1])
end
grid(display.ha(6),'off')
axis(display.ha(6),'square')
set(display.ha(6),'clim',[-34/32 1]);
col = colormap('jet');
col(1,:) = 0.5*ones(1,3);
colormap(display.ha(6),col);
try display.hc(2) = colorbar('peer',display.ha(6)); end
try getSubplots; end
function myDiagnosticsi(hObject,evt,si)
hfig = get(hObject,'parent');
ind = get(hObject,'Value');
ud = get(hfig,'userdata');
hPanel = getPanel(hfig);
try
if isequal(get(ud.handles.hdiagnostics(1),'parent'),hPanel)
delete(ud.handles.hdiagnostics)
end
end
out = ud.out;
dy = out.diagnostics.dy(ind);
sourceType = out.options.sources(ind).type;
sourceYidx = out.options.sources(ind).out;
% display data noise
xlim = [dy.nx(1)-dy.d,dy.nx(end)+dy.d];
handles.hdiagnostics(1) = subplot(4,2,5,'parent',hPanel,'nextplot','add','xlim',xlim,'ygrid','on','tag','VBLaplace','box','off');
title(handles.hdiagnostics(1),'residuals empirical distribution','fontsize',11)
xlabel(handles.hdiagnostics(1),'e(t) = y(t)-g(x(t))','fontsize',8)
ylabel(handles.hdiagnostics(1),'p(e|y)','fontsize',8)
bar(dy.nx,dy.ny,'facecolor',[.8 .8 .8],'parent',handles.hdiagnostics(1))
plot(handles.hdiagnostics(1),dy.grid,dy.pg,'r')
if sourceType==0
plot(handles.hdiagnostics(1),dy.grid,dy.pg2,'g')
end
if sourceType==0
legend(handles.hdiagnostics(1),{'empirical histogram','Gaussian approx','posterior approx'})
else
legend(handles.hdiagnostics(1),{'empirical histogram','Gaussian approx'})
end
if out.options.dim.n > 0
gri = out.diagnostics.microTime(out.diagnostics.sampleInd);
ti = 'time';
else
if out.options.dim.n_t>1
gri = 1:out.options.dim.n_t;
ti = 'time';
else
gri = 1:out.options.dim.p;
ti = 'data dimensions';
end
end
handles.hdiagnostics(2) = subplot(4,2,3,'parent',hPanel,'nextplot','add','tag','VBLaplace','ygrid','on','box','off');
plot(handles.hdiagnostics(2),gri,out.suffStat.dy(sourceYidx,:)','marker','.')
axis(handles.hdiagnostics(2),'tight')
title(handles.hdiagnostics(2),'residuals time series','fontsize',11)
xlabel(handles.hdiagnostics(2),ti,'fontsize',8)
ylabel(handles.hdiagnostics(2),'e(t) = y(t)-g(x(t))','fontsize',8)
% display autocorrelation of residuals
if ~isweird(dy.R) && out.dim.n_t > 1
handles.hdiagnostics(3) = subplot(4,2,7,'parent',hPanel);
plot(handles.hdiagnostics(3),[-out.options.dim.n_t:out.options.dim.n_t-1],fftshift(dy.R)')
axis(handles.hdiagnostics(3),'tight')
title(handles.hdiagnostics(3),'residuals empirical autocorrelation','fontsize',11)
xlabel(handles.hdiagnostics(3),'lag tau','fontsize',8)
ylabel(handles.hdiagnostics(3),'Corr[e(t),e(t+tau)]','fontsize',8)
set(handles.hdiagnostics(3),'tag','VBLaplace','ygrid','on','box','off');
end
ud.handles = handles;
set(hfig,'userdata',ud);
try, getSubplots; end
function myVB(hfig)
try hfig; catch, hfig = get(gco,'parent'); end
cleanPanel(hfig);
ud = get(hfig,'userdata');
out = ud.out;
y = out.y;
posterior = ud.posterior;
options = out.options;
options.noPause = 1;
options.DisplayWin =1;
suffStat = out.suffStat;
dim = out.dim;
% Initialize display figure
options.display.hfp = hfig;
options.figName = get(hfig,'name');
[options] = VBA_initDisplay(options);
delete(options.display.htt)
delete(options.display.hpause)
delete(options.display.hm)
delete(options.display.ho)
if options.dim.n == 0 || isinf(posterior.a_alpha(end))
try delete(options.display.ha(8)); end
end
hfig = options.display.hfp;
drawnow
% Display data and hidden states (if any)
if options.dim.n > 0
VBA_updateDisplay(posterior,suffStat,options,y,0,'X')
end
% Display precision hyperparameters
VBA_updateDisplay(posterior,suffStat,options,y,0,'precisions')
if ~options.OnLine && sum([options.sources(:).type]==0) > 0
xlabel(options.display.ha(6),' ')
if numel(options.display.ha)>7 && ishghandle(options.display.ha(8))
xlabel(options.display.ha(8),' ')
end
end
% Display model evidence
VBA_updateDisplay(posterior,suffStat,options,y,0,'F')
% Display parameters
if dim.n_theta >= 1
VBA_updateDisplay(posterior,suffStat,options,y,0,'theta')
end
if dim.n_phi >= 1
VBA_updateDisplay(posterior,suffStat,options,y,0,'phi')
end
try getSubplots; end
%% helpers
function cleanPanel(hfig)
hc = intersect(findobj('tag','VBLaplace'),get(hfig,'children'));
if ~isempty(hc)
delete(hc)
end
hPanel = getPanel(hfig);
hc = intersect(findobj('tag','VBLaplace'),get(hPanel,'children'));
if ~isempty(hc)
delete(hc)
end
function str = catnum2str(x,ind,many)
str = [];
for i=1:length(ind)
si=ind(i);
str = [str,', ',num2str(x(si),'%4.3e')];
if many
str = [str,' (source #',num2str(si),')'];
end
end
str(1:2) = [];