wolffd@0
|
1 function [fwd, loglik] = filter_evidence(engine, CPDpot, observed, pot_type)
|
wolffd@0
|
2 % [fwd, loglik] = filter_evidence(engine, CPDpot, observed, pot_type) (ff)
|
wolffd@0
|
3
|
wolffd@0
|
4 [ss T] = size(CPDpot);
|
wolffd@0
|
5 fwd = cell(ss,T);
|
wolffd@0
|
6 hnodes = engine.hnodes(:)';
|
wolffd@0
|
7 onodes = engine.onodes(:)';
|
wolffd@0
|
8 bnet = bnet_from_engine(engine);
|
wolffd@0
|
9 ns = bnet.node_sizes;
|
wolffd@0
|
10 onodes2 = [onodes onodes+ss];
|
wolffd@0
|
11 ns(onodes2) = 1;
|
wolffd@0
|
12
|
wolffd@0
|
13 logscale = zeros(1,T);
|
wolffd@0
|
14 H = length(hnodes);
|
wolffd@0
|
15 local_logscale = zeros(1,ss);
|
wolffd@0
|
16
|
wolffd@0
|
17 t = 1;
|
wolffd@0
|
18 for i=hnodes
|
wolffd@0
|
19 fwd{i,t} = CPDpot{i,t};
|
wolffd@0
|
20 c = engine.obschild(i);
|
wolffd@0
|
21 if c > 0
|
wolffd@0
|
22 fwd{i,t} = multiply_by_pot(fwd{i,t}, CPDpot{c, t});
|
wolffd@0
|
23 end
|
wolffd@0
|
24 [fwd{i,t}, local_logscale(i)] = normalize_pot(fwd{i,t});
|
wolffd@0
|
25 end
|
wolffd@0
|
26 logscale(t) = sum(local_logscale);
|
wolffd@0
|
27
|
wolffd@0
|
28 for t=2:T
|
wolffd@0
|
29 for i=hnodes
|
wolffd@0
|
30 ps = parents(bnet.dag, i+ss);
|
wolffd@0
|
31 assert(all(ps<=ss)); % in previous slice
|
wolffd@0
|
32 prior = CPDpot{i,t};
|
wolffd@0
|
33 for p=ps(:)'
|
wolffd@0
|
34 prior = multiply_by_pot(prior, fwd{p,t-1});
|
wolffd@0
|
35 end
|
wolffd@0
|
36 fwd{i,t} = marginalize_pot(prior, i+ss);
|
wolffd@0
|
37 fwd{i,t} = set_domain_pot(fwd{i,t}, i);
|
wolffd@0
|
38 c = engine.obschild(i);
|
wolffd@0
|
39 if c > 0
|
wolffd@0
|
40 fwd{i,t} = multiply_by_pot(fwd{i,t}, CPDpot{c,t});
|
wolffd@0
|
41 end
|
wolffd@0
|
42 [fwd{i,t}, local_logscale(i)] = normalize_pot(fwd{i,t});
|
wolffd@0
|
43 end
|
wolffd@0
|
44 logscale(t) = sum(local_logscale);
|
wolffd@0
|
45 end
|
wolffd@0
|
46
|
wolffd@0
|
47 loglik = sum(logscale);
|
wolffd@0
|
48
|