forked from MBB-team/VBA-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBA_checkGN.m
34 lines (33 loc) · 940 Bytes
/
VBA_checkGN.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
function [dx,flag] = VBA_checkGN(S,dx0)
% Levenberg-Marquardt regularization on Gauss-Newton update step
% [dx] = VBA_checkGN(S,dx0)
% This function checks the Gauss-Newton update required for the
% optimization of the variational energies of parameters. This
% regularization should not happen in normal cases (nonlinear Gaussian
% hierarchical models).
% IN:
% - S: the nxn covariance matrix at the current mode
% - dx0: the Gauss-Newton update step for the mode
% OUT:
% - dx: the (Levenberg-Marquardt) regularized update step
% - flag=1 if regularization, flag=0 if not.
n = numel(S);
% Compute smallest eigenvalue
if n == 1
sev = S;
else
% warning('off','all')
sev = eigs(S,1,'sr',struct('disp',0));
% warning('on','all')
end
if sev <= 0
[V,D] = eig(S);
D = -diag(D).^-1;
tau = 1./max(abs(D));
P = V*diag(exp(tau.*D))*V';
dx = dx0 - P*dx0;
flag = 1;
else
dx = dx0;
flag = 0;
end