forked from MBB-team/VBA-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVBA_updateDisplay.m
348 lines (294 loc) · 11 KB
/
VBA_updateDisplay.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
function VBA_updateDisplay(posterior,suffStat,options,y,it,flag)
% updates display of sufficient statistics
% function VBA_updateDisplay(F,posterior,suffStat,options,y,it,display,flag)
% This function deals with the screen display of iterative sufficient
% statistics updates of the VBA inversion algorithm
if options.extended
VBA_updateDisplay_extended(posterior,suffStat,options,y,it,flag);
%fprintf('Display extended\n');
return
end
F = real(suffStat.F);
if ~options.DisplayWin
return
end
display = options.display;
% check whether 'pause' button is toggled on
VBA_pause(options)
% First check whether this is standard DCM or ODE limit
if isequal(options.g_fname,@VBA_odeLim)
% Rebuild posterior from dummy 'ODE' posterior
options0 = options;
[posterior,options,dim,suffStat] = VBA_odeLim2NLSS(posterior,options,options.dim,suffStat,[],0);
options.display = options0.display;
% Then call VBA_updateDisplay again
if ~isempty(it)
VBA_updateDisplay(posterior,suffStat,options,y,it,'precisions')
end
if dim.n_phi > 0
VBA_updateDisplay(posterior,suffStat,options,y,it,'phi')
end
if dim.n > 0
VBA_updateDisplay(posterior,suffStat,options,y,it,'X')
end
if dim.n_theta > 0
VBA_updateDisplay(posterior,suffStat,options,y,it,'theta')
end
return
end
% Get sufficient statistics to be displayed
dTime = [1:size(y,2)];
try
gx = suffStat.gx(:,dTime);
vy = suffStat.vy(:,dTime);
catch
gx = [];
vy = [];
end
indEnd = length(dTime);
if ~options.binomial
if options.OnLine
sigmaHat = posterior.a_sigma(dTime)./posterior.b_sigma(dTime);
var_sigma = sigmaHat./posterior.b_sigma(dTime);
else
sigmaHat = posterior.a_sigma./posterior.b_sigma;
var_sigma = sigmaHat./posterior.b_sigma;
end
else
try
[stackyin,stdyin,gridgin] = VBA_Bin2Cont(gx(~options.isYout),y(~options.isYout));
[stackyout,stdyout,gridgout] = VBA_Bin2Cont(gx(~~options.isYout),y(~~options.isYout));
end
end
if options.dim.n > 0
mux = posterior.muX(:,dTime);
try
vx = VBA_getVar(posterior.SigmaX.current,indEnd);
catch
vx = zeros(size(mux));
end
if ~any(isinf(posterior.a_alpha))
if options.OnLine
alphaHat = posterior.a_alpha(dTime)./posterior.b_alpha(dTime);
var_alpha = alphaHat./posterior.b_alpha(dTime);
else
alphaHat = posterior.a_alpha./posterior.b_alpha;
var_alpha = alphaHat./posterior.b_alpha;
end
else
alphaHat = Inf;
var_alpha = 0;
end
vx0 = VBA_getVar(posterior.SigmaX0);
if options.updateX0
dx0 = suffStat.dx0;
else
dx0 = posterior.muX0;
end
end
if options.dim.n_theta > 0
if options.OnLine
dtheta = suffStat.dtheta(:,dTime);
else
dtheta = suffStat.dtheta;
end
vtheta = VBA_getVar(posterior.SigmaTheta,indEnd);
end
if options.dim.n_phi > 0
if options.OnLine
dphi = suffStat.dphi(:,dTime);
else
dphi = suffStat.dphi;
end
vphi = VBA_getVar(posterior.SigmaPhi,indEnd);
end
% check time dimension
if isequal(dTime,1) && size(y,1) > 1
gx = gx';
vy = vy';
y = y';
if options.dim.n > 0
mux = mux';
vx = vx';
end
dTime = [1:size(y,2)];
end
switch flag % What piece of the model to display?
case 'X' % Hidden-states related quantities
% update top-left subplot: predictive density
cla(display.ha(1))
if ~isempty(gx) && ~isempty(vy)
plot(display.ha(1),dTime,y','LineStyle',':','marker','.')
plotUncertainTimeSeries(gx,vy,dTime,display.ha(1));
set(display.ha(1),'ygrid','on','xgrid','off')
axis(display.ha(1),'tight')
end
% update top-right subplot: predicted VS observed data
cla(display.ha(2))
if ~isempty(gx) && ~isempty(vy)
if ~options.binomial
miy = min([gx(:);y(:)]);
may = max([gx(:);y(:)]);
plot(display.ha(2),[miy,may],[miy,may],'r')
gxout = gx(~~options.isYout);
yout = y(~~options.isYout);
gxin = gx(~options.isYout);
yin = y(~options.isYout);
plot(display.ha(2),gxout(:),yout(:),'r.')
plot(display.ha(2),gxin(:),yin(:),'k.')
if ~isempty(yout)
legend(display.ha(2),{'','excluded','fitted'})
end
else
plot(display.ha(2),[0,1],[0,1],'r')
gridp = 0:1e-2:1;
plot(display.ha(2),gridp,gridp+sqrt(gridp.*(1-gridp)),'r--')
plot(display.ha(2),gridp,gridp-sqrt(gridp.*(1-gridp)),'r--')
errorbar(gridgout,stackyout,stdyout,'r.','parent',display.ha(2))
errorbar(gridgin,stackyin,stdyin,'k.','parent',display.ha(2))
if ~isempty(gridgout)
legend(display.ha(2),{'','','','excluded','fitted'})
end
end
grid(display.ha(2),'on')
axis(display.ha(2),'tight')
end
% get display indices if delay embedding
if sum(options.delays) > 0
ind = 1:options.inF.dim.n;
else
ind = 1:size(mux,1);
end
% update middle-left subplot: hidden states
try
cla(display.ha(3))
plotUncertainTimeSeries(mux,vx,dTime,display.ha(3),ind);
catch
cla(display.ha(3))
plotUncertainTimeSeries(mux,vx,[],display.ha(3),ind);
end
set(display.ha(3),'ygrid','on','xgrid','off')
axis(display.ha(3),'tight')
% update middle-right subplot: initial conditions
if options.updateX0
cla(display.ha(4))
plotUncertainTimeSeries(-dx0,vx0,1,display.ha(4));
set(display.ha(4),'ygrid','on','xgrid','off')
elseif isequal(it,0)
plotUncertainTimeSeries(dx0,vx0,1,display.ha(4));
set(display.ha(4),'ygrid','on','xgrid','off')
end
displayDF(F,display)
case 'phi' % Observation parameters
% update top-left subplot: predictive density
cla(display.ha(1))
plot(display.ha(1),dTime,y',':')
plot(display.ha(1),dTime,y','.')
if ~isempty(gx) && ~isempty(vy)
plotUncertainTimeSeries(gx,vy,dTime,display.ha(1));
end
set(display.ha(1),'ygrid','on','xgrid','off')
axis(display.ha(1),'tight')
% update top-right subplot: predicted VS observed data
cla(display.ha(2))
if ~isempty(gx) && ~isempty(vy)
if ~options.binomial
miy = min([gx(:);y(:)]);
may = max([gx(:);y(:)]);
plot(display.ha(2),[miy,may],[miy,may],'r')
gxout = gx(~~options.isYout);
yout = y(~~options.isYout);
gxin = gx(~options.isYout);
yin = y(~options.isYout);
plot(display.ha(2),gxout(:),yout(:),'r.')
plot(display.ha(2),gxin(:),yin(:),'k.')
if ~isempty(yout)
legend(display.ha(2),{'','excluded','fitted'})
end
else
plot(display.ha(2),[0,1],[0,1],'r')
gridp = 0:1e-2:1;
plot(display.ha(2),gridp,gridp+sqrt(gridp.*(1-gridp)),'r--')
plot(display.ha(2),gridp,gridp-sqrt(gridp.*(1-gridp)),'r--')
errorbar(gridgout,stackyout,stdyout,'r.','parent',display.ha(2))
errorbar(gridgin,stackyin,stdyin,'k.','parent',display.ha(2))
if ~isempty(gridgout)
legend(display.ha(2),{'','','','excluded','fitted'})
end
end
grid(display.ha(2),'on')
axis(display.ha(2),'tight')
end
% update bottom-left subplot: observation parameters
if size(dphi,2) == 1 % for on-line wrapper
dTime = 1;
end
cla(display.ha(5))
plotUncertainTimeSeries(-dphi,vphi,dTime,display.ha(5));
set(display.ha(5),'ygrid','on','xgrid','off')
displayDF(F,display)
case 'theta' % Evolution parameters
% update bottom-right subplot: observation parameters
if size(dtheta,2) == 1 % for on-line wrapper
dTime = 1;
end
cla(display.ha(7))
plotUncertainTimeSeries(-dtheta,vtheta,dTime,display.ha(7));
set(display.ha(7),'ygrid','on','xgrid','off')
displayDF(F,display)
case 'precisions' % Precision hyperparameters
% update top-left subplot: predictive density
cla(display.ha(1))
plot(display.ha(1),dTime,y',':')
plot(display.ha(1),dTime,y','.')
if ~isempty(gx) && ~isempty(vy)
plotUncertainTimeSeries(gx,vy,dTime,display.ha(1));
end
if options.updateHP || (isequal(it,0) && ~options.binomial)
% update middle-left subplot: measurement noise
if ~options.binomial
if size(sigmaHat,2) > 1 % for on-line wrapper
cla(display.ha(6))
else
dTime = it+1;
set(display.ha(6),'xlim',[.2,it+1.8],'xtick',[])
end
logCI = log(sigmaHat+sqrt(var_sigma)) - log(sigmaHat);
plotUncertainTimeSeries(log(sigmaHat),logCI.^2,dTime,display.ha(6));
set(display.ha(6),'ygrid','on','xgrid','off')
end
% update middle-right subplot: state noise
if options.dim.n > 0 && ~any(isinf(alphaHat))
if size(alphaHat,2) > 1 % for on-line wrapper
cla(display.ha(8))
else
dTime = it+1;
set(display.ha(8),'xlim',[.2,it+1.8],'xtick',[])
end
logCI = log(alphaHat+sqrt(var_alpha)) - log(alphaHat);
plotUncertainTimeSeries(log(alphaHat),logCI.^2,dTime,display.ha(8));
set(display.ha(8),'ygrid','on','xgrid','off')
end
displayDF(F,display)
end
case 'F' % Free energy
% Output in main matlab window
dF = diff(F);
if it > 0 && options.verbose
fprintf(['VB iteration #',num2str(it),' F=','%e',' ... dF=','%4.3e'],F(end),dF(end))
fprintf('\n')
end
end
%drawnow
%--- subfunction ---%
function [] = displayDF(F,display)
if ~display.OnLine
try
dF = diff(F);
set(display.ho,'string',['Model evidence: log p(y|m) >= ',num2str(F(end),'%1.3e'),' , dF= ',num2str(dF(end),'%4.3e')])
catch
try
set(display.ho,'string',['Model evidence: log p(y|m) >= ',num2str(F(end),'%4.3e')])
end
end
end