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