-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlikelihood_gaussian.m
58 lines (50 loc) · 1.27 KB
/
likelihood_gaussian.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
function [LL, L] = likelihood_gaussian(y,X,theta,cov)
persistent last_hyp
persistent last_L
persistent last_LL
persistent last_y
N = size(y,1);
logell = log(theta(1));
logsf = log(sqrt(theta(2)));
sn2 = theta(3);
hyp = [logell;logsf;sn2];
if isempty(last_hyp); last_hyp = zeros(size(hyp)); end
if isempty(last_y); last_y = nan(N,1); end
% compute Cholesky if hyperparameters have changed
if nargin < 5 || any(last_hyp ~= hyp)
K = feval(cov{:}, [logell; logsf], X);
L = chol(K+sn2*eye(N))';
else
L = last_L;
end
if isempty(last_LL) || any(last_L(:) ~= L(:)) || any(y ~= last_y)
LL = -0.5*y'*solve_chol(L',y) -sum(log(diag(L))) - 0.5*N*log(2*pi);
else
LL = last_LL;
end
last_L = L;
last_LL = LL;
last_hyp = hyp;
last_y = y;
% if nargin < 5 || any(last_hyp ~= hyp)
% K = feval(cov{:}, [logell; logsf], X);
% L = chol(K+sn2*eye(N))';
% last_hyp = hyp;
% last_L = L;
% %if y ~= last_y
% LL = -0.5*y'*solve_chol(L',y) -sum(log(diag(L))) - 0.5*N*log(2*pi);
% last_LL = LL;
% last_y = y;
% %else
% % LL = last_LL;
% %end
% else
% L = last_L;
% LL = last_LL;
% end
% else
% L = last_L;
% LL = last_LL;
% end
%fprintf('hyp = %2.6f %2.6f %2.6f \n',hyp(1),hyp(2),hyp(3))
end