wolffd@0
|
1 % Computing most probable explanation.
|
wolffd@0
|
2
|
wolffd@0
|
3 % If you don't break ties consistently, loopy can give wrong mpe
|
wolffd@0
|
4 % even though the graph has no cycles, and even though the max-marginals are the same.
|
wolffd@0
|
5 % This example was contributed by Wentau Yih <wtyih@yahoo.com> 29 Jan 02.
|
wolffd@0
|
6
|
wolffd@0
|
7 % define loop-free graph structure (all edges point down)
|
wolffd@0
|
8 %
|
wolffd@0
|
9 % Xe1 Xe2
|
wolffd@0
|
10 % | |
|
wolffd@0
|
11 % E1 E2
|
wolffd@0
|
12 % \ /
|
wolffd@0
|
13 % R1
|
wolffd@0
|
14 % |
|
wolffd@0
|
15 % Xr1
|
wolffd@0
|
16
|
wolffd@0
|
17 N = 6;
|
wolffd@0
|
18 dag = zeros(N,N);
|
wolffd@0
|
19 Xe1 = 1; Xe2 = 2; E1 = 3; E2 = 4; R1 = 5; Xr1 = 6;
|
wolffd@0
|
20 dag(Xe1, E1) = 1;
|
wolffd@0
|
21 dag(Xe2, E2) = 1;
|
wolffd@0
|
22 dag([E1 E2], R1) = 1;
|
wolffd@0
|
23 dag(R1, Xr1) = 1;
|
wolffd@0
|
24
|
wolffd@0
|
25 node_sizes = [ 1 1 2 2 2 1 ];
|
wolffd@0
|
26
|
wolffd@0
|
27 % create BN
|
wolffd@0
|
28
|
wolffd@0
|
29 bnet = mk_bnet(dag, node_sizes, 'observed', [Xe1 Xe2 Xr1]);
|
wolffd@0
|
30
|
wolffd@0
|
31 % fill in CPT
|
wolffd@0
|
32
|
wolffd@0
|
33 bnet.CPD{Xe1} = tabular_CPD(bnet, Xe1, [1]);
|
wolffd@0
|
34 bnet.CPD{Xe2} = tabular_CPD(bnet, Xe2, [1]);
|
wolffd@0
|
35 bnet.CPD{E1} = tabular_CPD(bnet, E1, [0.2 0.8]);
|
wolffd@0
|
36 bnet.CPD{E2} = tabular_CPD(bnet, E2, [0.3 0.7]);
|
wolffd@0
|
37 bnet.CPD{R1} = tabular_CPD(bnet, R1, [1 1 1 0.8 0 0 0 0.2]);
|
wolffd@0
|
38 bnet.CPD{Xr1} = tabular_CPD(bnet, Xr1, [0.15 0.85]);
|
wolffd@0
|
39
|
wolffd@0
|
40 clear engine;
|
wolffd@0
|
41 engine{1} = belprop_inf_engine(bnet);
|
wolffd@0
|
42 engine{2} = jtree_inf_engine(bnet);
|
wolffd@0
|
43 engine{3} = global_joint_inf_engine(bnet);
|
wolffd@0
|
44 engine{4} = var_elim_inf_engine(bnet);
|
wolffd@0
|
45
|
wolffd@0
|
46 evidence = cell(1,N);
|
wolffd@0
|
47 evidence{Xe1} = 1; evidence{Xe2} = 1; evidence{Xr1} = 1;
|
wolffd@0
|
48
|
wolffd@0
|
49 mpe = find_mpe(engine{1}, evidence, 'break_ties', 0) % gives wrong results
|
wolffd@0
|
50 mpe = find_mpe(engine{1}, evidence)
|
wolffd@0
|
51 for i=2:4
|
wolffd@0
|
52 mpe = find_mpe(engine{i}, evidence)
|
wolffd@0
|
53 end
|