wolffd@0
|
1 function [marginals, loglik] = enter_soft_evidence(engine, CPDpot, observed, pot_type, filter)
|
wolffd@0
|
2 % ENTER_SOFT_EVIDENCE Add the specified soft evidence to the network (ff)
|
wolffd@0
|
3 % [marginals, loglik] = enter_soft_evidence(engine, CPDpot, observed, pot_type, filter)
|
wolffd@0
|
4
|
wolffd@0
|
5 assert(pot_type == 'd');
|
wolffd@0
|
6 [ss T] = size(CPDpot);
|
wolffd@0
|
7 fwd = cell(ss,T);
|
wolffd@0
|
8 hnodes = engine.hnodes(:)';
|
wolffd@0
|
9 onodes = engine.onodes(:)';
|
wolffd@0
|
10 bnet = bnet_from_engine(engine);
|
wolffd@0
|
11 ns = bnet.node_sizes;
|
wolffd@0
|
12 onodes2 = [onodes onodes+ss];
|
wolffd@0
|
13 ns(onodes2) = 1;
|
wolffd@0
|
14
|
wolffd@0
|
15 logscale = zeros(1,T);
|
wolffd@0
|
16 H = length(hnodes);
|
wolffd@0
|
17 local_logscale = zeros(1,ss);
|
wolffd@0
|
18
|
wolffd@0
|
19 obschild = zeros(1,ss);
|
wolffd@0
|
20 for i=hnodes
|
wolffd@0
|
21 ocs = myintersect(children(bnet.dag, i), onodes);
|
wolffd@0
|
22 assert(length(ocs)==1);
|
wolffd@0
|
23 obschild(i) = ocs(1);
|
wolffd@0
|
24 end
|
wolffd@0
|
25
|
wolffd@0
|
26 t = 1;
|
wolffd@0
|
27 for i=hnodes
|
wolffd@0
|
28 fwd{i,t} = CPDpot{i,t};
|
wolffd@0
|
29 c = obschild(i);
|
wolffd@0
|
30 temp = pot_to_marginal(CPDpot{c,t});
|
wolffd@0
|
31 ev = dpot(i, ns(i), temp.T);
|
wolffd@0
|
32 fwd{i,t} = multiply_by_pot(fwd{i,t}, ev);
|
wolffd@0
|
33 [fwd{i,t}, local_logscale(i)] = normalize_pot(fwd{i,t});
|
wolffd@0
|
34 end
|
wolffd@0
|
35 logscale(t) = sum(local_logscale);
|
wolffd@0
|
36
|
wolffd@0
|
37 for t=2:T
|
wolffd@0
|
38 for i=hnodes
|
wolffd@0
|
39 ps = parents(bnet.dag, i+ss);
|
wolffd@0
|
40 assert(all(ps<=ss)); % in previous slice
|
wolffd@0
|
41 prior = CPDpot{i,t};
|
wolffd@0
|
42 for p=ps(:)'
|
wolffd@0
|
43 prior = multiply_by_pot(prior, fwd{p,t-1});
|
wolffd@0
|
44 end
|
wolffd@0
|
45 fwd{i,t} = marginalize_pot(prior, i+ss);
|
wolffd@0
|
46 fwd{i,t} = set_domain_pot(fwd{i,t}, i);
|
wolffd@0
|
47 c = obschild(i);
|
wolffd@0
|
48 temp = pot_to_marginal(CPDpot{c,t});
|
wolffd@0
|
49 ev = dpot(i, ns(i), temp.T);
|
wolffd@0
|
50 fwd{i,t} = multiply_by_pot(fwd{i,t}, ev);
|
wolffd@0
|
51 [fwd{i,t}, local_logscale(i)] = normalize_pot(fwd{i,t});
|
wolffd@0
|
52 end
|
wolffd@0
|
53 logscale(t) = sum(local_logscale);
|
wolffd@0
|
54 end
|
wolffd@0
|
55
|
wolffd@0
|
56 loglik = sum(logscale);
|
wolffd@0
|
57
|
wolffd@0
|
58
|
wolffd@0
|
59 if filter
|
wolffd@0
|
60 marginals = fwd;
|
wolffd@0
|
61 return;
|
wolffd@0
|
62 end
|
wolffd@0
|
63
|
wolffd@0
|
64 back = cell(ss,T);
|
wolffd@0
|
65 t = T;
|
wolffd@0
|
66 for i=hnodes
|
wolffd@0
|
67 back{i,t} = dpot(i, ns(i));
|
wolffd@0
|
68 back{i,t} = set_domain_pot(back{i,t}, i+ss);
|
wolffd@0
|
69 end
|
wolffd@0
|
70 for t=T-1:-1:1
|
wolffd@0
|
71 for i=hnodes
|
wolffd@0
|
72 pot = CPDpot{i,t+1};
|
wolffd@0
|
73 pot = multiply_by_pot(pot, back{i,t+1});
|
wolffd@0
|
74 c = obschild(i);
|
wolffd@0
|
75 temp = pot_to_marginal(CPDpot{c,t+1});
|
wolffd@0
|
76 ev = dpot(i, ns(i), temp.T);
|
wolffd@0
|
77 pot = multiply_by_pot(pot, ev);
|
wolffd@0
|
78 back{i,t} = marginalize_pot(pot, i);
|
wolffd@0
|
79 back{i,t} = normalize_pot(back{i,t});
|
wolffd@0
|
80 back{i,t} = set_domain_pot(back{i,t}, i+ss);
|
wolffd@0
|
81 end
|
wolffd@0
|
82 end
|
wolffd@0
|
83
|
wolffd@0
|
84
|
wolffd@0
|
85
|
wolffd@0
|
86 % COMBINE
|
wolffd@0
|
87 for t=1:T
|
wolffd@0
|
88 for i=hnodes
|
wolffd@0
|
89 back{i,t} = set_domain_pot(back{i,t}, i);
|
wolffd@0
|
90 fwd{i,t} = multiply_by_pot(fwd{i,t}, back{i,t});
|
wolffd@0
|
91 marginals{i,t} = normalize_pot(fwd{i,t});
|
wolffd@0
|
92 %fwdback{i,t} = normalize_pot(multiply_pots(fwd{i,t}, back{i,t}));
|
wolffd@0
|
93 end
|
wolffd@0
|
94 end
|