about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg1.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/fgraph/fg1.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/fgraph/fg1.m')
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/fgraph/fg1.m98
1 files changed, 98 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg1.m b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg1.m
new file mode 100644
index 00000000..0b8adc47
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg1.m
@@ -0,0 +1,98 @@
+% make an unrolled HMM, convert to factor graph, and check that 
+% loopy propagation on the fgraph gives the exact answers.
+
+seed = 1;
+rand('state', seed);
+randn('state', seed);
+
+T = 3;
+Q = 3;
+O = 3;
+cts_obs = 0;
+param_tying = 1;
+bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying);
+
+data = sample_bnet(bnet);
+
+fgraph = bnet_to_fgraph(bnet);
+big_bnet = fgraph_to_bnet(fgraph);
+% converting factor graph back does not recover the structure of the original bnet
+
+max_iter = 2*T;
+
+engine = {};
+engine{1} = jtree_inf_engine(bnet);
+engine{2} = belprop_inf_engine(bnet, 'max_iter', max_iter);
+engine{3} = belprop_fg_inf_engine(fgraph, 'max_iter', max_iter);
+engine{4} = jtree_inf_engine(big_bnet);
+nengines = length(engine);
+
+big_engine = 4;
+fgraph_engine = 3;
+
+
+N = 2*T;
+evidence = cell(1,N);
+onodes = bnet.observed;
+evidence(onodes) = data(onodes);
+hnodes = mysetdiff(1:N, onodes);
+
+bigN = length(big_bnet.dag);
+big_evidence = cell(1, bigN);
+big_evidence(onodes) = data(onodes);
+big_evidence(N+1:end) = {1}; % factors are observed to be 1
+
+ll = zeros(1, nengines);
+for i=1:nengines
+  if i==big_engine
+    tic; [engine{i}, ll(i)] = enter_evidence(engine{i}, big_evidence); toc
+  else
+    tic; [engine{i}, ll(i)] = enter_evidence(engine{i}, evidence); toc
+  end
+end
+
+% compare all engines to engine{1}
+
+% the log likelihood values may be bogus...
+for i=2:nengines
+  %assert(approxeq(ll(1), ll(i)));
+end
+
+
+marg = zeros(T, nengines, Q); % marg(t,e,:)
+for t=1:T
+  for e=1:nengines
+    m = marginal_nodes(engine{e}, t);
+    marg(t,e,:) = m.T;
+  end
+end
+marg
+
+
+m = cell(nengines, T);
+for i=1:T
+  for e=1:nengines
+    m{e,i} = marginal_nodes(engine{e}, hnodes(i));
+  end
+  for e=2:nengines
+    assert(approxeq(m{e,i}.T, m{1,i}.T));
+  end
+end
+
+mpe = {};
+ll = zeros(1, nengines);
+for e=1:nengines
+  if e==big_engine
+    mpe{e} = find_mpe(engine{e}, big_evidence);
+    mpe{e} = mpe{e}(1:N); % chop off dummy nodes
+  else
+    mpe{e} = find_mpe(engine{e}, evidence);
+  end
+end
+
+% fgraph can't compute loglikelihood for software reasons
+% jtree on the big_bnet gives the wrong ll
+for e=2:nengines
+  %assert(approxeq(ll(1), ll(e)));
+  assert(approxeq(cell2num(mpe{1}), cell2num(mpe{e})))
+end