-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompute_likelihood_unnorm.m
86 lines (57 loc) · 2.69 KB
/
compute_likelihood_unnorm.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
function log_likelihood = compute_likelihood_unnorm(data_struct,theta,obsModelType,Kz_inds,Kz,Ks)
switch obsModelType
case 'Gaussian'
invSigma = theta.invSigma;
mu = theta.mu;
T = size(data_struct.obs,2);
dimu = size(data_struct.obs,1);
log_likelihood = -inf*ones(Kz,Ks,T);
for kz=Kz_inds
for ks=1:Ks
cholinvSigma = chol(invSigma(:,:,kz,ks));
dcholinvSigma = diag(cholinvSigma);
u = cholinvSigma*(data_struct.obs - mu(:,kz*ones(1,T),ks));
log_likelihood(kz,ks,:) = -0.5*sum(u.^2,1) + sum(log(dcholinvSigma));
end
end
% normalizer = max(max(log_likelihood,[],1),[],2);
% log_likelihood = log_likelihood - normalizer(ones(Kz,1),ones(Ks,1),:);
% likelihood = exp(log_likelihood);
%
% normalizer = normalizer - (dimu/2)*log(2*pi);
case {'AR','SLDS'}
invSigma = theta.invSigma;
A = theta.A;
X = data_struct.X;
T = size(data_struct.obs,2);
dimu = size(data_struct.obs,1);
log_likelihood = -inf*ones(Kz,Ks,T);
if isfield(theta,'mu')
mu = theta.mu;
for kz=Kz_inds
for ks=1:Ks
cholinvSigma = chol(invSigma(:,:,kz,ks));
dcholinvSigma = diag(cholinvSigma);
u = cholinvSigma*(data_struct.obs - A(:,:,kz,ks)*X-mu(:,kz*ones(1,T),ks));
log_likelihood(kz,ks,:) = -0.5*sum(u.^2,1) + sum(log(dcholinvSigma));
end
end
else
for kz=Kz_inds
for ks=1:Ks
cholinvSigma = chol(invSigma(:,:,kz,ks));
dcholinvSigma = diag(cholinvSigma);
u = cholinvSigma*(data_struct.obs - A(:,:,kz,ks)*X);
log_likelihood(kz,ks,:) = -0.5*sum(u.^2,1) + sum(log(dcholinvSigma));
end
end
end
% normalizer = max(max(log_likelihood,[],1),[],2);
% log_likelihood = log_likelihood - normalizer(ones(Kz,1),ones(Ks,1),:);
% likelihood = exp(log_likelihood);
%
% normalizer = normalizer - (dimu/2)*log(2*pi);
case 'Multinomial'
log_likelihood = log(theta.p(:,:,data_struct.obs));
%normalizer = zeros(1,size(data_struct.obs,2));
end