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