comparison toolboxes/FullBNT-1.0.7/bnt/examples/limids/pigs1.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 % pigs model from Lauritzen and Nilsson, 2001
2
3 seed = 0;
4 rand('state', seed);
5 randn('state', seed);
6
7 % we number nodes down and to the right
8 h = [1 5 9 13];
9 t = [2 6 10];
10 d = [3 7 11];
11 u = [4 8 12 14];
12
13 N = 14;
14 dag = zeros(N);
15
16 % causal arcs
17 for i=1:3
18 dag(h(i), [t(i) h(i+1)]) = 1;
19 dag(d(i), [u(i) h(i+1)]) = 1;
20 end
21 dag(h(4), u(4)) = 1;
22
23 % information arcs
24 fig = 2;
25 switch fig
26 case 0,
27 % no info arcs
28 case 1,
29 % no-forgetting policy (figure 1)
30 for i=1:3
31 dag(t(i), d(i:3)) = 1;
32 end
33 case 2,
34 % reactive policy (figure 2)
35 for i=1:3
36 dag(t(i), d(i)) = 1;
37 end
38 case 7,
39 % omniscient policy (figure 7: di has access to hidden state h(i-1))
40 dag(t(1), d(1)) = 1;
41 for i=2:3
42 %dag([h(i-1) t(i-1) d(i-1)], d(i)) = 1;
43 dag([h(i-1) d(i-1)], d(i)) = 1; % t(i-1) is redundant given h(i-1)
44 end
45 end
46
47
48 ns = 2*ones(1,N);
49 ns(u) = 1;
50
51 % parameter tying
52 params = ones(1,N);
53 uparam = 1;
54 final_uparam = 2;
55 tparam = 3;
56 h1_param = 4;
57 hparam = 5;
58 dparams = 6:8;
59
60 params(u(1:3)) = uparam;
61 params(u(4)) = final_uparam;
62 params(t) = tparam;
63 params(h(1)) = h1_param;
64 params(h(2:end)) = hparam;
65 params(d) = dparams;
66
67 limid = mk_limid(dag, ns, 'chance', [h t], 'decision', d, 'utility', u, 'equiv_class', params);
68
69 % h = 1 means healthy, h = 2 means diseased
70 % d = 1 means don't treat, d = 2 means treat
71 % t = 1 means test shows healthy, t = 2 means test shows diseased
72
73 if 0
74 % use random params
75 limid.CPD{final_uparam} = tabular_utility_node(limid, u(4));
76 limid.CPD{uparam} = tabular_utility_node(limid, u(1));
77 limid.CPD{tparam} = tabular_CPD(limid, t(1));
78 limid.CPD{h1_param} = tabular_CPD(limid, h(1));
79 limid.CPD{hparam} = tabular_CPD(limid, h(2));
80 else
81 limid.CPD{final_uparam} = tabular_utility_node(limid, u(4), [1000 300]);
82 limid.CPD{uparam} = tabular_utility_node(limid, u(1), [0 -100]); % costs have negative utility!
83
84 % h P(t=1) P(t=2)
85 % 1 0.9 0.1
86 % 2 0.2 0.8
87 limid.CPD{tparam} = tabular_CPD(limid, t(1), [0.9 0.2 0.1 0.8]);
88
89 % P(h1)
90 limid.CPD{h1_param} = tabular_CPD(limid, h(1), [0.9 0.1]);
91
92 % hi di P(hj=1) P(hj=2), j = i+1, i=1:3
93 % 1 1 0.8 0.2
94 % 2 1 0.1 0.9
95 % 1 2 0.9 0.1
96 % 2 2 0.5 0.5
97 limid.CPD{hparam} = tabular_CPD(limid, h(2), [0.8 0.1 0.9 0.5 0.2 0.9 0.1 0.5]);
98 end
99
100 % Decision nodes get assigned uniform policies by default
101 for i=1:3
102 limid.CPD{dparams(i)} = tabular_decision_node(limid, d(i));
103 end
104
105
106 fname = '/home/cs/murphyk/matlab/Misc/loopybel.txt';
107
108 engines = {};
109 engines{end+1} = global_joint_inf_engine(limid);
110 engines{end+1} = jtree_limid_inf_engine(limid);
111 %engines{end+1} = belprop_inf_engine(limid, 'max_iter', 1*N, 'filename', fname, 'tol', 1e-3);
112
113 exact = [1 2];
114 %approx = 3;
115 approx = [];
116
117 max_iter = 1;
118 order = d(end:-1:1);
119 %order = d(1:end);
120
121 NE = length(engines);
122 MEU = zeros(1, NE);
123 niter = zeros(1, NE);
124 strategy = cell(1, NE);
125 for e=1:NE
126 [strategy{e}, MEU(e), niter(e)] = solve_limid(engines{e}, 'max_iter', max_iter, 'order', order);
127 end
128 MEU
129
130 % check results match those in the paper (p. 22)
131 direct_policy = eye(2); % treat iff test is positive
132 never_policy = [1 0; 1 0]; % never treat
133 tol = 1e-0; % results in paper are reported to 0dp
134 for e=exact(:)'
135 switch fig
136 case 2, % reactive policy
137 assert(approxeq(MEU(e), 727, tol));
138 assert(approxeq(strategy{e}{d(1)}(:), never_policy(:)))
139 assert(approxeq(strategy{e}{d(2)}(:), direct_policy(:)))
140 assert(approxeq(strategy{e}{d(3)}(:), direct_policy(:)))
141 case 1, assert(approxeq(MEU(e), 729, tol));
142 case 7, assert(approxeq(MEU(e), 732, tol));
143 end
144 end
145
146
147 for e=approx(:)'
148 for i=1:3
149 approxeq(strategy{exact(1)}{d(i)}, strategy{e}{d(i)})
150 dispcpt(strategy{e}{d(i)})
151 end
152 end
153