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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
function [marginal, msg, loglik] = smooth_evidence(engine, evidence)
% [marginal, msg, loglik] = smooth_evidence(engine, evidence) (pearl_dbn)
disp('warning: pearl_dbn smoothing is broken');
[ss T] = size(evidence);
bnet = bnet_from_engine(engine);
bnet2 = dbn_to_bnet(bnet, T);
ns = bnet2.node_sizes;
hnodes = mysetdiff(1:ss, engine.onodes);
hnodes = hnodes(:)';
onodes2 = unroll_set(engine.onodes(:), ss, T);
onodes2 = onodes2(:)';
hnodes2 = unroll_set(hnodes(:), ss, T);
hnodes2 = hnodes2(:)';
[engine.parent_index, engine.child_index] = mk_pearl_msg_indices(bnet2);
msg = init_msgs(bnet2.dag, ns, evidence, bnet2.equiv_class, bnet2.CPD);
verbose = 0;
pot_type = 'd';
niter = 1;
for iter=1:niter
% FORWARD
for t=1:T
if verbose, fprintf('t=%d\n', t); end
% each hidden node absorbs lambda from its observed child (if any)
for i=hnodes
c = engine.obschild(i);
if c > 0
if t==1
fam = family(bnet.dag, c);
e = bnet.equiv_class(c, 1);
CPDpot = CPD_to_pot(pot_type, bnet.CPD{e}, fam, bnet.node_sizes(:), bnet.cnodes(:), evidence(:,1));
else
fam = family(bnet.dag, 2); % within 2 slice network
e = bnet.equiv_class(c, 2);
CPDpot = CPD_to_pot(pot_type, bnet.CPD{e}, fam, bnet.node_sizes(:), bnet.cnodes(:), evidence(:,t-1:t));
end
temp = pot_to_marginal(CPDpot);
n = i + (t-1)*ss;
lam_msg = normalise(temp.T);
j = engine.child_index{n}(c+(t-1)*ss);
assert(j==1);
msg{n}.lambda_from_child{j} = lam_msg;
if verbose, fprintf('%d sends lambda to %d\n', c + (t-1)*ss, n); disp(lam_msg); end
end
end
% update pi
for i=hnodes
n = i + (t-1)*ss;
ps = parents(bnet2.dag, n);
if t==1
e = bnet.equiv_class(i,1);
else
e = bnet.equiv_class(i,2);
end
msg{n}.pi = compute_pi(bnet.CPD{e}, n, ps, msg);
if verbose, fprintf('%d computes pi\n', n); disp(msg{n}.pi); end
end
% send pi msg to children in next slice
for i=hnodes
n = i + (t-1)*ss;
%cs = myintersect(children(bnet2.dag, n), hnodes2);
cs = children(bnet2.dag, n);
for c=cs(:)'
j = engine.parent_index{c}(n); % n is c's j'th parent
pi_msg = normalise(compute_pi_msg(n, cs, msg, c, ns));
msg{c}.pi_from_parent{j} = pi_msg;
if verbose, fprintf('%d sends pi to %d\n', n, c); disp(pi_msg); end
end
end
end
% BACKWARD
for t=T:-1:1
if verbose, fprintf('t = %d\n', t); end
% update lambda
for i=hnodes
n = i + (t-1)*ss;
cs = children(bnet2.dag, n);
msg{n}.lambda = compute_lambda(n, cs, msg, ns);
if verbose, fprintf('%d computes lambda\n', n); disp(msg{n}.lambda); end
end
% send lambda msgs to hidden parents in prev slcie
for i=hnodes
n = i + (t-1)*ss;
%ps = myintersect(parents(bnet2.dag, n), hnodes2);
ps = parents(bnet2.dag, n);
for p=ps(:)'
j = engine.child_index{p}(n); % n is p's j'th child
if t > 1
e = bnet.equiv_class(i, 2);
else
e = bnet.equiv_class(i, 1);
end
lam_msg = normalise(compute_lambda_msg(bnet.CPD{e}, n, ps, msg, p));
msg{p}.lambda_from_child{j} = lam_msg;
if verbose, fprintf('%d sends lambda to %d\n', n, p); disp(lam_msg); end
end
end
% send pi msg to observed children
if 0
for i=hnodes
n = i + (t-1)*ss;
cs = myintersect(children(bnet2.dag, n), onodes2);
%cs = children(bnet2.dag, n);
for c=cs(:)'
j = engine.parent_index{c}(n); % n is c's j'th parent
pi_msg = normalise(compute_pi_msg(n, cs, msg, c, ns));
msg{c}.pi_from_parent{j} = pi_msg;
if verbose, fprintf('%d sends pi to %d\n', n, c); disp(pi_msg); end
end
end
end
end
end
marginal = cell(ss,T);
lik = zeros(1,ss*T);
for t=1:T
for i=hnodes
n = i + (t-1)*ss;
[bel, lik(n)] = normalise(msg{n}.pi .* msg{n}.lambda);
marginal{i,t} = bel;
end
end
loglik = 0;
%loglik = sum(log(lik));
%%%%%%%
function lambda = compute_lambda(n, cs, msg, ns)
% Pearl p183 eq 4.50
lambda = prod_lambda_msgs(n, cs, msg, ns);
%%%%%%%
function pi_msg = compute_pi_msg(n, cs, msg, c, ns)
% Pearl p183 eq 4.53 and 4.51
pi_msg = msg{n}.pi .* prod_lambda_msgs(n, cs, msg, ns, c);
%%%%%%%%%
function lam = prod_lambda_msgs(n, cs, msg, ns, except)
if nargin < 5, except = -1; end
%lam = msg{n}.lambda_from_self(:);
lam = ones(ns(n), 1);
for i=1:length(cs)
c = cs(i);
if c ~= except
lam = lam .* msg{n}.lambda_from_child{i};
end
end
%%%%%%%%%
function msg = init_msgs(dag, ns, evidence, eclass, CPD)
% INIT_MSGS Initialize the lambda/pi message and state vectors (pearl_dbn)
% msg = init_msgs(dag, ns, evidence)
N = length(dag);
msg = cell(1,N);
observed = ~isemptycell(evidence(:));
for n=1:N
ps = parents(dag, n);
msg{n}.pi_from_parent = cell(1, length(ps));
for i=1:length(ps)
p = ps(i);
msg{n}.pi_from_parent{i} = ones(ns(p), 1);
end
cs = children(dag, n);
msg{n}.lambda_from_child = cell(1, length(cs));
for i=1:length(cs)
c = cs(i);
msg{n}.lambda_from_child{i} = ones(ns(n), 1);
end
msg{n}.lambda = ones(ns(n), 1);
msg{n}.pi = ones(ns(n), 1);
% Initialize the lambdas with any evidence
if observed(n)
v = evidence{n};
msg{n}.lambda = zeros(ns(n), 1);
msg{n}.lambda(v) = 1; % delta function
msg{n}.lambda = [];
end
end
|