about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/SLP/examples/test_sem1.m
diff options
context:
space:
mode:
authorziejd22018-03-14 23:23:33 -0500
committerGitHub2018-03-14 23:23:33 -0500
commit1ff6baa44e22b91eefb48aea6f3befa078c0489b (patch)
treee0fd79d2e32fd2aedda2eadaed0f19af3514c520 /sourcecodes/bnt-master/SLP/examples/test_sem1.m
parent6882395afdadf4e982b25b5215071a0932730950 (diff)
parentc80226899f5cdd9f11c163817d59445213f5bef0 (diff)
downloadBNW-1ff6baa44e22b91eefb48aea6f3befa078c0489b.tar.gz
Merge pull request #1 from ziejd2/octave_php_separate
Octave php separate
Diffstat (limited to 'sourcecodes/bnt-master/SLP/examples/test_sem1.m')
-rw-r--r--sourcecodes/bnt-master/SLP/examples/test_sem1.m56
1 files changed, 56 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/SLP/examples/test_sem1.m b/sourcecodes/bnt-master/SLP/examples/test_sem1.m
new file mode 100644
index 00000000..942ec7dd
--- /dev/null
+++ b/sourcecodes/bnt-master/SLP/examples/test_sem1.m
@@ -0,0 +1,56 @@
+% Lawn sprinker example from Russell and Norvig p454
+% See www.cs.berkeley.edu/~murphyk/Bayes/usage.html for details.
+
+rand('state', 0);
+randn('state', 0);
+
+N = 4;
+dag = zeros(N,N);
+C = 1; S = 2; R = 3; W = 4;
+dag(C,[R S]) = 1;
+dag(R,W) = 1;
+dag(S,W)=1;
+
+false = 1; true = 2;
+ns = 2*ones(1,N); % binary nodes
+
+bnet = mk_bnet(dag, ns);
+bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]);
+bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]);
+bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]);
+bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]);
+
+nsamples = 500;
+samplesM = cell(N, nsamples);
+for i=1:nsamples
+  samplesM(:,i) = sample_bnet(bnet);
+end
+
+hide = rand(N, nsamples) > 0.9;
+[I,J]=find(hide);
+for k=1:length(I)
+  samplesM{I(k), J(k)} = [];
+end
+
+% Make a initial chain like dag
+G0 = zeros(N,N);
+for i=1:N-1
+   G0(i, i+1) = 1;
+end
+
+figure;
+draw_graph(G0);
+
+B0 = mk_bnet(G0, ns);
+% use random params
+for i=1:N
+  B0.CPD{i} = tabular_CPD(B0, i, 'prior_type', 'dirichlet', 'dirichlet_weight', 0);
+end
+
+max_loop = 30;
+%profile on -detail mmex
+[B0, order, best_score] = learn_struct_EM(B0, samplesM, max_loop);
+%profile report
+
+dag1 = B0.dag;
+dag1 = dag1(order,order);