about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/examples/static/softev1.m
diff options
context:
space:
mode:
authorziejd22017-09-28 15:04:40 -0500
committerziejd22017-09-28 15:04:40 -0500
commit8070dc963753142bb86c4ed698d91fd623ed28e7 (patch)
treed0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/BNT/examples/static/softev1.m
parent7cc31810d53176e805532b2789955f4eedbce6bb (diff)
downloadBNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning.

I am calling this BNW_1.02. It can be accessed at:
compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/BNT/examples/static/softev1.m')
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/softev1.m60
1 files changed, 60 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/examples/static/softev1.m b/sourcecodes/bnt-master/BNT/examples/static/softev1.m
new file mode 100644
index 00000000..5af7ba22
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/softev1.m
@@ -0,0 +1,60 @@
+% Check that adding soft evidence to a hidden node is equivalent to evaluating its leaf CPD.
+
+% Make an HMM
+T = 3; Q = 2; O = 2; cts_obs = 0; param_tying = 0;
+bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying);
+N = 2*T;
+onodes = bnet.observed;
+hnodes = mysetdiff(1:N, onodes);
+for i=1:N
+  bnet.CPD{i} = tabular_CPD(bnet, i);
+end
+
+ev = sample_bnet(bnet);
+evidence = cell(1,N);
+evidence(onodes) = ev(onodes);
+
+engine = jtree_inf_engine(bnet);
+
+[engine, ll] = enter_evidence(engine, evidence);
+query = 1;
+m = marginal_nodes(engine, query);
+
+
+% Make a Markov chain with the same backbone
+bnet2 = mk_markov_chain_bnet(T, Q);
+for i=1:T
+  S = struct(bnet.CPD{hnodes(i)}); % violate object privacy
+  bnet2.CPD{i} = tabular_CPD(bnet2, i, S.CPT);
+end
+
+% Evaluate the observed leaves of the HMM
+soft_ev = cell(1,T);
+for i=1:T
+  S = struct(bnet.CPD{onodes(i)}); % violate object privacy
+  dist = S.CPT(:, evidence{onodes(i)});
+  soft_ev{i} = dist;
+end
+
+% Use the leaf potentials as soft evidence
+engine2 = jtree_inf_engine(bnet2);
+[engine2, ll2] = enter_evidence(engine2, cell(1,T), 'soft', soft_ev);
+m2 = marginal_nodes(engine2, query);
+
+assert(approxeq(m2.T, m.T))
+assert(approxeq(ll2, ll))
+
+
+
+% marginal on node 1 without evidence
+[engine2, ll2] = enter_evidence(engine2, cell(1,T));
+m2 = marginal_nodes(engine2, 1);
+
+% add soft evidence
+soft_ev=cell(1,T);
+soft_ev{1}=[0.7 0.3]; 
+[engine2, ll2] = enter_evidence(engine2, cell(1,T), 'soft', soft_ev);
+m3 = marginal_nodes(engine2, 1);
+
+assert(approxeq(normalise(m2.T .* [0.7 0.3]'), m3.T))
+