forked from MBB-team/VBA-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBA_UNLtemp.m
84 lines (61 loc) · 1.85 KB
/
VBA_UNLtemp.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
function [posterior,suffStat] = VBA_UNLtemp(y,posterior,suffStat,dim,u,options)
% VB update of the inverse tempterature of un-normalized likelihoods
if options.DisplayWin % Display progress
try
STR = 'VB update of the temperature hyperparameter... ';
set(options.display.hm(1),'string',STR);
set(options.display.hm(2),'string','0%');
drawnow
end
end
% Preallocate intermediate variables
a0 = posterior.a_sigma;
b0 = posterior.b_sigma;
beta0 = a0./b0;
c = 0;
d = 0;
s1 = 0;
s2 = 0;
div = 0;
Vy = zeros(options.dim.p,options.dim.n_t);
%--- Loop over time series ---%
for t=1:dim.n_t
if ~options.isYout
% evaluate re-normalized likelihood (as well as gradients and Hessians)
[EUi,Ui,Zi,d2Uidx2,d2UidP2,Vy(:,t)] = VBA_evalAL2([],posterior.muPhi,beta0,u(:,t),y(:,t),options);
c = c - beta0*EUi;
d = d - Ui - 0.5*trace(posterior.SigmaPhi*d2UidP2);
s1 = s1 + Ui;
s2 = s2 + Zi;
% Accelerate divergent update
if isweird({EUi,Ui,Zi,d2Uidx2,d2UidP2})
div = 1;
break
end
end
% Display progress
if mod(t,dim.n_t./10) < 1
if options.DisplayWin
try
set(options.display.hm(2),'string',[num2str(floor(100*t/dim.n_t)),'%']);
drawnow
end
end
end
end
% Display progress
if options.DisplayWin
try
set(options.display.hm(2),'string','OK');
drawnow
end
end
% figure('name',['VB update of beta: beta0=',num2str(beta0)]),plot(Vy)
% posterior covariance matrix
posterior.a_sigma = options.priors.a_sigma + c;
posterior.b_sigma = options.priors.b_sigma + d;
% update sufficient statistics
b = posterior.a_sigma/posterior.b_sigma;
suffStat.logL = b*s1 - s2;
suffStat.vy = Vy;
suffStat.div = div;