comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/Belprop/belprop_polytree_gauss.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 % Do the example from Satnam Alag's PhD thesis, UCB ME dept 1996 p46
2
3 % Make the following polytree, where all arcs point down
4
5 % 1 2
6 % \ /
7 % 3
8 % / \
9 % 4 5
10
11 N = 5;
12 dag = zeros(N,N);
13 dag(1,3) = 1;
14 dag(2,3) = 1;
15 dag(3, [4 5]) = 1;
16
17 ns = [2 1 2 1 2];
18
19 bnet = mk_bnet(dag, ns, 'discrete', []);
20
21 bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', [1 0]', 'cov', [4 1; 1 4]);
22 bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', 1, 'cov', 1);
23 B1 = [1 2; 1 0]; B2 = [2 1]';
24 bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', [0 0]', 'cov', [2 1; 1 1], ...
25 'weights', [B1 B2]);
26 H1 = [1 1];
27 bnet.CPD{4} = gaussian_CPD(bnet, 4, 'mean', 0, 'cov', 1, 'weights', H1);
28 H2 = [1 0; 1 1];
29 bnet.CPD{5} = gaussian_CPD(bnet, 5, 'mean', [0 0]', 'cov', eye(2), 'weights', H2);
30
31 engine = {};
32 engine{end+1} = jtree_inf_engine(bnet);
33 engine{end+1} = pearl_inf_engine(bnet, 'protocol', 'tree');
34 engine{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel');
35 E = length(engine);
36
37 if 1
38 % no evidence
39 evidence = cell(1,N);
40 ll = zeros(1,E);
41 for e=1:E
42 [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence);
43 add_ev = 1;
44 m = marginal_nodes(engine{e}, 3, add_ev);
45 assert(approxeq(m.mu, [3 2]'))
46 assert(approxeq(m.Sigma, [30 9; 9 6]))
47
48 m = marginal_nodes(engine{e}, 4, add_ev);
49 assert(approxeq(m.mu, 5))
50 assert(approxeq(m.Sigma, 55))
51
52 m = marginal_nodes(engine{e}, 5, add_ev);
53 assert(approxeq(m.mu, [3 5]'))
54 assert(approxeq(m.Sigma, [31 39; 39 55]))
55 end
56 end
57
58 if 1
59 % evidence on leaf 5
60 evidence = cell(1,N);
61 evidence{5} = [5 5]';
62 for e=1:E
63 [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence);
64 add_ev = 1;
65 m = marginal_nodes(engine{e}, 3, add_ev);
66 assert(approxeq(m.mu, [4.4022 1.0217]'))
67 assert(approxeq(m.Sigma, [0.7011 -0.4891; -0.4891 1.1087]))
68
69 m = marginal_nodes(engine{e}, 4, add_ev);
70 assert(approxeq(m.mu, 5.4239))
71 assert(approxeq(m.Sigma, 1.8315))
72
73 m = marginal_nodes(engine{e}, 1, add_ev);
74 assert(approxeq(m.mu, [0.3478 1.1413]'))
75 assert(approxeq(m.Sigma, [1.8261 -0.1957; -0.1957 1.0924]))
76
77 m = marginal_nodes(engine{e}, 2, add_ev);
78 assert(approxeq(m.mu, 0.9239))
79 assert(approxeq(m.Sigma, 0.8315))
80
81 m = marginal_nodes(engine{e}, 5, add_ev);
82 assert(approxeq(m.mu, evidence{5}))
83 assert(approxeq(m.Sigma, zeros(2)))
84 end
85 end
86
87 if 1
88 % evidence on leaf 4 (non-info-state version is uninvertible)
89 evidence = cell(1,N);
90 evidence{4} = 10;
91 for e=1:E
92 [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence);
93 add_ev = 1;
94 m = marginal_nodes(engine{e}, 3, add_ev);
95 assert(approxeq(m.mu, [6.5455 3.3636]'))
96 assert(approxeq(m.Sigma, [2.3455 -1.6364; -1.6364 1.9091]))
97
98 m = marginal_nodes(engine{e}, 5, add_ev);
99 assert(approxeq(m.mu, [6.5455 9.9091]'))
100 assert(approxeq(m.Sigma, [3.3455 0.7091; 0.7091 1.9818]))
101
102 m = marginal_nodes(engine{e}, 1, add_ev);
103 assert(approxeq(m.mu, [1.9091 0.9091]'))
104 assert(approxeq(m.Sigma, [2.1818 -0.8182; -0.8182 2.1818]))
105
106 m = marginal_nodes(engine{e}, 2, add_ev);
107 assert(approxeq(m.mu, 1.2727))
108 assert(approxeq(m.Sigma, 0.8364))
109 end
110 end
111
112
113 if 1
114 % evidence on leaves 4,5 and root 2
115 evidence = cell(1,N);
116 evidence{2} = 0;
117 evidence{4} = 10;
118 evidence{5} = [5 5]';
119 for e=1:E
120 [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence);
121 add_ev = 1;
122 m = marginal_nodes(engine{e}, 3, add_ev);
123 assert(approxeq(m.mu, [4.9964 2.4444]'));
124 assert(approxeq(m.Sigma, [0.6738 -0.5556; -0.5556 0.8889]));
125
126 m = marginal_nodes(engine{e}, 1, add_ev);
127 assert(approxeq(m.mu, [2.2043 1.2151]'));
128 assert(approxeq(m.Sigma, [1.2903 -0.4839; -0.4839 0.8065]));
129 end
130 end
131
132 if 1
133 [time, engine] = cmp_inference_static(bnet, engine, 'maximize', 0, 'check_ll', 0, ...
134 'singletons_only', 0, 'observed', [1 3 5]);
135 end