annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/mpe1.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 seed = 1;
wolffd@0 2 rand('state', seed);
wolffd@0 3 randn('state', seed);
wolffd@0 4
wolffd@0 5 N = 4;
wolffd@0 6 dag = zeros(N,N);
wolffd@0 7 C = 1; S = 2; R = 3; W = 4;
wolffd@0 8 dag(C,[R S]) = 1;
wolffd@0 9 dag(R,W) = 1;
wolffd@0 10 dag(S,W)=1;
wolffd@0 11
wolffd@0 12 false = 1; true = 2;
wolffd@0 13 ns = 2*ones(1,N); % binary nodes
wolffd@0 14
wolffd@0 15 bnet = mk_bnet(dag, ns);
wolffd@0 16 if 0
wolffd@0 17 bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]);
wolffd@0 18 bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]);
wolffd@0 19 bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]);
wolffd@0 20 bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]);
wolffd@0 21 else
wolffd@0 22 for i=1:N, bnet.CPD{i} = tabular_CPD(bnet, i); end
wolffd@0 23 end
wolffd@0 24
wolffd@0 25
wolffd@0 26
wolffd@0 27 evidence = cell(1,N);
wolffd@0 28 onodes = [1 3];
wolffd@0 29 data = sample_bnet(bnet);
wolffd@0 30 evidence(onodes) = data(onodes);
wolffd@0 31
wolffd@0 32 clear engine;
wolffd@0 33 engine{1} = belprop_inf_engine(bnet);
wolffd@0 34 engine{2} = jtree_inf_engine(bnet);
wolffd@0 35 engine{3} = global_joint_inf_engine(bnet);
wolffd@0 36 engine{4} = var_elim_inf_engine(bnet);
wolffd@0 37 E = length(engine);
wolffd@0 38
wolffd@0 39 clear mpe;
wolffd@0 40 for e=1:E
wolffd@0 41 mpe{e} = find_mpe(engine{e}, evidence);
wolffd@0 42 end
wolffd@0 43 for e=2:E
wolffd@0 44 assert(isequal(mpe{1}, mpe{e}))
wolffd@0 45 end