annotate toolboxes/FullBNT-1.0.7/bnt/examples/limids/pigs1.m @ 0:cc4b1211e677 tip

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