comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/softev1.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 % Check that adding soft evidence to a hidden node is equivalent to evaluating its leaf CPD.
2
3 % Make an HMM
4 T = 3; Q = 2; O = 2; cts_obs = 0; param_tying = 0;
5 bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying);
6 N = 2*T;
7 onodes = bnet.observed;
8 hnodes = mysetdiff(1:N, onodes);
9 for i=1:N
10 bnet.CPD{i} = tabular_CPD(bnet, i);
11 end
12
13 ev = sample_bnet(bnet);
14 evidence = cell(1,N);
15 evidence(onodes) = ev(onodes);
16
17 engine = jtree_inf_engine(bnet);
18
19 [engine, ll] = enter_evidence(engine, evidence);
20 query = 1;
21 m = marginal_nodes(engine, query);
22
23
24 % Make a Markov chain with the same backbone
25 bnet2 = mk_markov_chain_bnet(T, Q);
26 for i=1:T
27 S = struct(bnet.CPD{hnodes(i)}); % violate object privacy
28 bnet2.CPD{i} = tabular_CPD(bnet2, i, S.CPT);
29 end
30
31 % Evaluate the observed leaves of the HMM
32 soft_ev = cell(1,T);
33 for i=1:T
34 S = struct(bnet.CPD{onodes(i)}); % violate object privacy
35 dist = S.CPT(:, evidence{onodes(i)});
36 soft_ev{i} = dist;
37 end
38
39 % Use the leaf potentials as soft evidence
40 engine2 = jtree_inf_engine(bnet2);
41 [engine2, ll2] = enter_evidence(engine2, cell(1,T), 'soft', soft_ev);
42 m2 = marginal_nodes(engine2, query);
43
44 assert(approxeq(m2.T, m.T))
45 assert(approxeq(ll2, ll))
46
47
48
49 % marginal on node 1 without evidence
50 [engine2, ll2] = enter_evidence(engine2, cell(1,T));
51 m2 = marginal_nodes(engine2, 1);
52
53 % add soft evidence
54 soft_ev=cell(1,T);
55 soft_ev{1}=[0.7 0.3];
56 [engine2, ll2] = enter_evidence(engine2, cell(1,T), 'soft', soft_ev);
57 m3 = marginal_nodes(engine2, 1);
58
59 assert(approxeq(normalise(m2.T .* [0.7 0.3]'), m3.T))
60