forked from PRML/PRMLT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregressVb.m
72 lines (62 loc) · 1.4 KB
/
regressVb.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
function [model, energy] = regressVb(X, t, prior)
% Fit empirical Bayesian linear model with EM
% X: m x n data
% t: 1 x n response
if nargin < 3
a0 = 1e-4;
b0 = 1e-4;
c0 = 1e-4;
d0 = 1e-4;
else
a0 = prior.a;
b0 = prior.b;
c0 = prior.c;
d0 = prior.d;
end
[m,n] = size(X);
xbar = mean(X,2);
tbar = mean(t,2);
X = bsxfun(@minus,X,xbar);
t = bsxfun(@minus,t,tbar);
XX = X*X';
Xt = X*t';
maxiter = 100;
energy = -inf(1,maxiter+1);
dg = sub2ind([m,m],1:m,1:m);
I = eye(m);
tol = 1e-8;
a = a0+m/2;
c = c0+n/2;
Ealpha = 1e-4;
Ebeta = 1e-4;
for iter = 2:maxiter
invS = Ebeta*XX;
invS(dg) = invS(dg)+Ealpha;
U = chol(invS);
Ew = Ebeta*(U\(U'\Xt));
w2 = dot(Ew,Ew);
e2 = sum((t-Ew'*X).^2);
invU = U\I;
trS = dot(invU(:),invU(:));
invUX = U\X;
trXSX = dot(invUX(:),invUX(:));
b = b0+0.5*(w2+trS);
d = d0+0.5*(e2+trXSX);
Ealpha = a/b;
Ebeta = c/d;
logdetS = -2*sum(log(diag(U)));
energy(iter) = -a*log(b)-c*log(d)+0.5*logdetS;
if energy(iter)-energy(iter-1) < tol*abs(energy(iter-1)); break; end
end
const = gammaln(a)-gammaln(a0)+gammaln(c)-gammaln(c0)+a0*log(b0)+c0*log(d0)+0.5*(m-n*log(2*pi));
energy = energy(2:iter)+const;
w0 = tbar-dot(Ew,xbar);
model.w0 = w0;
model.w = Ew;
model.Ealpha = Ealpha;
model.Ebeta = Ebeta;
model.a = a;
model.b = b;
model.c = c;
model.d = d;
model.xbar = xbar;