diff options
Diffstat (limited to 'sourcecodes/bnt-master/BNT/examples/limids')
| -rw-r--r-- | sourcecodes/bnt-master/BNT/examples/limids/CVS/Entries | 6 | ||||
| -rw-r--r-- | sourcecodes/bnt-master/BNT/examples/limids/CVS/Repository | 1 | ||||
| -rw-r--r-- | sourcecodes/bnt-master/BNT/examples/limids/CVS/Root | 1 | ||||
| -rw-r--r-- | sourcecodes/bnt-master/BNT/examples/limids/amnio.m | 135 | ||||
| -rw-r--r-- | sourcecodes/bnt-master/BNT/examples/limids/asia_dt1.m | 100 | ||||
| -rw-r--r-- | sourcecodes/bnt-master/BNT/examples/limids/id1.m | 50 | ||||
| -rw-r--r-- | sourcecodes/bnt-master/BNT/examples/limids/oil1.m | 91 | ||||
| -rw-r--r-- | sourcecodes/bnt-master/BNT/examples/limids/pigs1.m | 153 |
8 files changed, 537 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/examples/limids/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Entries new file mode 100644 index 00000000..f7f12046 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Entries @@ -0,0 +1,6 @@ +/amnio.m/1.1.1.1/Mon Sep 13 03:21:04 2004// +/asia_dt1.m/1.1.1.1/Mon Jun 7 15:53:54 2004// +/id1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/oil1.m/1.1.1.1/Mon Sep 13 02:27:08 2004// +/pigs1.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/limids/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Repository new file mode 100644 index 00000000..bd5dd5af --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/limids diff --git a/sourcecodes/bnt-master/BNT/examples/limids/CVS/Root b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/limids/amnio.m b/sourcecodes/bnt-master/BNT/examples/limids/amnio.m new file mode 100644 index 00000000..fd621b6c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/amnio.m @@ -0,0 +1,135 @@ + +clear all +B0 = 1; Rtriple = 2; Damnio = 3; +B1 = 4; Ramnio = 5; Dabort = 6; +B2 = 7; U = 8; + +N = 8; +dag = zeros(N,N); +dag(B0, [Rtriple B1 Ramnio]) = 1; +dag(Rtriple, [Damnio Dabort]) = 1; +dag(Damnio, [B1 Ramnio]) = 1; +dag(B1, B2) = 1; +dag(Ramnio, [Dabort U]) = 1; +dag(Dabort, B2) = 1; +dag(B2, U) = 1; + + + +ns = zeros(1,N); +ns(B0) = 2; +ns(B1) = 3; +ns(B2) = 4; +ns(Rtriple) = 2; +ns(Ramnio) = 3; +ns(Damnio) = 2; +ns(Dabort) = 2; +ns(U) = 1; + +limid = mk_limid(dag, ns, 'chance', [B0 B1 B2], ... + 'decision', [Damnio Dabort], 'utility', [U]); + +% states of nature +healthy = 1; downs = 2; miscarry = 3; aborted = 4; +% test results +pos = 1; neg = 2; unk = 3; +% actions +yes = 1; no = 2; + +% Prior probability baby has downs syndrome +tbl = zeros(2,1); +p = 1/1000; % from www.downs-syndrome.org.uk figure +p = 24/10000; % www-personal.umich.edu/~bobwolfe/560/review/Downs.pdf (for women agen 35-40) +tbl(healthy) = 1-p; +tbl(downs) = p; +limid.CPD{B0} = tabular_CPD(limid, B0, tbl); + +% Reliability of triple screen test +% Unreliable sensor +% B0 -> Rtriple +tbl = zeros(2,2); % Rtriple = pos, neg +p = 0.5; % high false positive rate (guess) +tbl(healthy, :) = [p 1-p]; +p = 0.6; % low detection rate (march of dimes figure) +tbl(downs, :) = [p 1-p]; +limid.CPD{Rtriple} = tabular_CPD(limid, Rtriple, tbl); + +limid.CPD{Damnio} = tabular_decision_node(limid, Damnio); + +% Effect of amnio on baby B0,Damnio -> B1 + % 1/200 risk of miscarry +p = 1/200; % (march of dimes figure) +tbl = zeros(2, 2, 3); % B1 = healthy, downs, miscarry +tbl(healthy, no, :) = [1 0 0]; +tbl(downs, no, :) = [0 1 0]; +tbl(healthy, yes, :) = [1-p 0 p]; +tbl(downs, yes, :) = [0 1-p p]; +limid.CPD{B1} = tabular_CPD(limid, B1, tbl); + +% Reliability of amnio B0, Damnio -> Ramnio +% Perfect sensor +tbl = zeros(2,2,3); % Ramnio = pos, neg, unk +tbl(:, no, :) = repmat([0 0 1], 2 ,1); +tbl(healthy, yes, :) = [0 1 0]; +tbl(downs, yes, :) = [1 0 0]; +limid.CPD{Ramnio} = tabular_CPD(limid, Ramnio, tbl); + +limid.CPD{Dabort} = tabular_decision_node(limid, Dabort); + +% Effect of abortion on baby B1, Dabort -> B2 +tbl = zeros(3, 2, 4); % B2 = healthy, downs, miscarry, aborted +tbl(:, yes, :) = repmat([0 0 0 1], 3, 1); +tbl(healthy, no, :) = [1 0 0 0]; +tbl(downs, no, :) = [0 1 0 0]; +tbl(miscarry, no, :) = [0 0 1 0]; +limid.CPD{B2} = tabular_CPD(limid, B2, tbl); + +% Utility U(Ramnio, B2) +tbl = zeros(3, 4); +tbl(:, healthy) = 5000; +tbl(:, downs) = -50000; +tbl(:, miscarry) = -1000; +tbl(:, aborted) = -1000; + +if 0 +%tbl(unk, miscarry) = 0; % this case is impossible +tbl(pos, miscarry) = -1; +tbl(neg, miscarry) = -1000; +if 1 + tbl(unk, aborted) = -100; + tbl(pos, aborted) = -1; + tbl(neg, aborted) = -500; +else % pro-life utility fn + tbl(unk, aborted) = -500000; + tbl(pos, aborted) = -500000; + tbl(neg, aborted) = -500000; +end +end + +limid.CPD{U} = tabular_utility_node(limid, U, tbl); + + + +engine = jtree_limid_inf_engine(limid); +[strategy, MEU] = solve_limid(engine); + +% Rtriple U(Damnio=1=yes) U(Damnio=2=no) +% 1=pos 0 1 +% 2=neg 0 1 +dispcpt(strategy{Damnio}) +if isequal(strategy{Damnio}(1,:), strategy{Damnio}(2,:)) + % Rtriple result irrelevant + doAmnio = argmax(strategy{Damnio}(1,:)) +else + doAmnio = 1; +end + +% Rtriple Ramnio U(Dabort=yes=1) U(Dabort=no=2) +% 1=pos 1=pos 1 0 +% 2=neg 1=pos 1 0 +% 1=pos 2=neg 0 1 +% 2=neg 2=neg 0 1 +% 1=pos 3=unk 0 1 +% 2=neg 3=unk 0 1 +dispcpt(strategy{Dabort}) + diff --git a/sourcecodes/bnt-master/BNT/examples/limids/asia_dt1.m b/sourcecodes/bnt-master/BNT/examples/limids/asia_dt1.m new file mode 100644 index 00000000..de305818 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/asia_dt1.m @@ -0,0 +1,100 @@ +% decision theoretic version of asia network +% Cowell et al, p177 +% We explicitely add the no-forgetting arcs. + +Smoking = 1; +VisitToAsia = 2; +Bronchitis = 3; +LungCancer = 4; +TB = 5; +Do_Xray = 6; +TBorCancer = 7; +Util_Xray = 8; +Dys = 9; +posXray = 10; +Do_Hosp = 11; +Util_Hosp = 12; + +n = 12; +dag = zeros(n); +dag(Smoking, [Bronchitis LungCancer]) = 1; +dag(VisitToAsia, [TB Do_Xray Do_Hosp]) = 1; +dag(Bronchitis, Dys) = 1; +dag(LungCancer, [Util_Hosp TBorCancer]) = 1; +dag(TB, [Util_Hosp TBorCancer Util_Xray]) = 1; +dag(Do_Xray, [posXray Util_Xray Do_Hosp]) = 1; +dag(TBorCancer, [Dys posXray]) = 1; +dag(Dys, Do_Hosp) = 1; +dag(posXray, Do_Hosp) = 1; +dag(Do_Hosp, Util_Hosp) = 1; + +dnodes = [Do_Xray Do_Hosp]; +unodes = [Util_Xray Util_Hosp]; +cnodes = mysetdiff(1:n, [dnodes unodes]); % chance nodes +ns = 2*ones(1,n); +ns(unodes) = 1; +limid = mk_limid(dag, ns, 'chance', cnodes, 'decision', dnodes, 'utility', unodes); + +% 1 = yes, 2 = no +limid.CPD{VisitToAsia} = tabular_CPD(limid, VisitToAsia, [0.01 0.99]); +limid.CPD{Bronchitis} = tabular_CPD(limid, Bronchitis, [0.6 0.3 0.4 0.7]); +limid.CPD{Dys} = tabular_CPD(limid, Dys, [0.9 0.7 0.8 0.1 0.1 0.3 0.2 0.9]); +limid.CPD{TBorCancer} = tabular_CPD(limid, TBorCancer, [1 1 1 0 0 0 0 1]); + +limid.CPD{LungCancer} = tabular_CPD(limid, LungCancer, [0.1 0.01 0.9 0.99]); +limid.CPD{Smoking} = tabular_CPD(limid, Smoking, [0.5 0.5]); +limid.CPD{TB} = tabular_CPD(limid, TB, [0.05 0.01 0.95 0.99]); +limid.CPD{posXray} = tabular_CPD(limid, posXray, [0.98 0.5 0.05 0.5 0.02 0.5 0.95 0.5]); + +limid.CPD{Util_Hosp} = tabular_utility_node(limid, Util_Hosp, [180 120 160 15 2 4 0 40]); +limid.CPD{Util_Xray} = tabular_utility_node(limid, Util_Xray, [0 1 10 10]); + +for i=dnodes(:)' + limid.CPD{i} = tabular_decision_node(limid, i); +end + +engines = {}; +engines{end+1} = global_joint_inf_engine(limid); +engines{end+1} = jtree_limid_inf_engine(limid); +%engines{end+1} = belprop_inf_engine(limid); + +exact = [1 2]; +%approx = 3; +approx = []; + + +NE = length(engines); +MEU = zeros(1, NE); +niter = zeros(1, NE); +strategy = cell(1, NE); + +tol = 1e-2; +for e=1:length(engines) + [strategy{e}, MEU(e), niter(e)] = solve_limid(engines{e}); +end + +for e=exact(:)' + assert(approxeq(MEU(e), 47.49, tol)) + assert(isequal(strategy{e}{Do_Xray}(:)', [1 0 0 1])) + + % Check the hosptialize strategy is correct (p180) + % We assume the patient has not been to Asia and therefore did not have an Xray. + % In this case it is optimal not to hospitalize regardless of whether the patient has + % dyspnoea or not (and of course regardless of the value of pos_xray). + asia = 2; + do_xray = 2; + for dys = 1:2 + for pos_xray = 1:2 + assert(argmax(squeeze(strategy{e}{Do_Hosp}(asia, do_xray, dys, pos_xray, :))) == 2) + end + end +end + + +for e=approx(:)' + approxeq(strategy{exact(1)}{Do_Xray}, strategy{e}{Do_Xray}) + approxeq(strategy{exact(1)}{Do_Hosp}, strategy{e}{Do_Hosp}) +end + + + diff --git a/sourcecodes/bnt-master/BNT/examples/limids/id1.m b/sourcecodes/bnt-master/BNT/examples/limids/id1.m new file mode 100644 index 00000000..ddacd67a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/id1.m @@ -0,0 +1,50 @@ +% influence diagram with no loops +% +% rv dec +% \ / +% utility + +N = 3; +dag = zeros(N); +X = 1; D = 2; U = 3; +dag([X D], U)=1; + +ns = zeros(1,N); +ns(X) = 2; ns(D) = 2; ns(U) = 1; + +limid = mk_limid(dag, ns, 'chance', X, 'decision', D, 'utility', U); + +% use random params +limid.CPD{X} = tabular_CPD(limid, X); +limid.CPD{D} = tabular_decision_node(limid, D); +limid.CPD{U} = tabular_utility_node(limid, U); + +%fname = '/home/cs/murphyk/matlab/Misc/loopybel.txt'; +global BNT_HOME +fname = sprintf('%s/loopybel.txt', BNT_HOME); + +engines = {}; +engines{end+1} = global_joint_inf_engine(limid); +engines{end+1} = jtree_limid_inf_engine(limid); +%engines{end+1} = belprop_inf_engine(limid, 'max_iter', 2*N, 'filename', fname); +engines{end+1} = belprop_inf_engine(limid, 'max_iter', 2*N); + +exact = [1 2]; +approx = 3; + +E = length(engines); +strategy = cell(1, E); +MEU = zeros(1, E); +for e=1:E + [strategy{e}, MEU(e)] = solve_limid(engines{e}); + MEU +end +MEU + +for e=exact(:)' + assert(approxeq(strategy{exact(1)}{D}, strategy{e}{D})) +end + +for e=approx(:)' + approxeq(strategy{exact(1)}{D}, strategy{e}{D}) +end diff --git a/sourcecodes/bnt-master/BNT/examples/limids/oil1.m b/sourcecodes/bnt-master/BNT/examples/limids/oil1.m new file mode 100644 index 00000000..192acc9d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/oil1.m @@ -0,0 +1,91 @@ +% oil wildcatter influence diagram in Cowell et al p172 + +% T = test for oil? +% UT = utility (negative cost) of testing +% O = amount of oil = Dry, Wet or Soaking +% R = results of test = NoStrucure, OpenStructure, ClosedStructure or NoResult +% D = drill? +% UD = utility of drilling + +% Decision sequence = T R D O + +T = 1; UT = 2; O = 3; R = 4; D = 5; UD = 6; +N = 6; +dag = zeros(N); +dag(T, [UT R D]) = 1; +dag(O, [R UD]) = 1; +dag(R, D) = 1; +dag(D, UD) = 1; + +ns = zeros(1,N); +ns(O) = 3; ns(R) = 4; ns(T) = 2; ns(D) = 2; ns(UT) = 1; ns(UD) = 1; + +limid = mk_limid(dag, ns, 'chance', [O R], 'decision', [T D], 'utility', [UT UD]); + +limid.CPD{O} = tabular_CPD(limid, O, [0.5 0.3 0.2]); +tbl = [0.6 0 0.3 0 0.1 0 0.3 0 0.4 0 0.4 0 0.1 0 0.3 0 0.5 0 0 1 0 1 0 1]; +limid.CPD{R} = tabular_CPD(limid, R, tbl); + +limid.CPD{UT} = tabular_utility_node(limid, UT, [-10 0]); +limid.CPD{UD} = tabular_utility_node(limid, UD, [-70 50 200 0 0 0]); + +if 1 + % start with uniform policies + limid.CPD{T} = tabular_decision_node(limid, T); + limid.CPD{D} = tabular_decision_node(limid, D); +else + % hard code optimal policies + limid.CPD{T} = tabular_decision_node(limid, T, [1.0 0.0]); + a = 0.5; b = 1-a; % arbitrary value + tbl = myreshape([0 a 1 a 1 a a a 1 b 0 b 0 b b b], ns([T R D])); + limid.CPD{D} = tabular_decision_node(limid, D, tbl); +end + +%fname = '/home/cs/murphyk/matlab/Misc/loopybel.txt'; + +engines = {}; +engines{end+1} = global_joint_inf_engine(limid); +engines{end+1} = jtree_limid_inf_engine(limid); +%engines{end+1} = belprop_inf_engine(limid, 'max_iter', 3*N, 'filename', fname); + +exact = [1 2]; +%approx = 3; +approx = []; + +E = length(engines); +strategy = cell(1, E); +MEU = zeros(1, E); +for e=1:E + [strategy{e}, MEU(e)] = solve_limid(engines{e}); + MEU +end +MEU + +for e=exact(:)' + assert(approxeq(MEU(e), 22.5)) + % U(T=yes) U(T=no) + % 1 0 + assert(argmax(strategy{e}{T}) == 1); % test = yes + t = 1; % test = yes + % strategy{D} T R U(D=yes=1) U(D=no=2) + % 1=yes 1=noS 0 1 Don't drill + % 2=no 1=noS 1 0 + % 1=yes 2=opS 1 0 + % 2=no 2=opS 1 0 + % 1=yes 3=clS 1 0 + % 2=no 3=clS 1 0 + % 1=yes 4=unk 1 0 + % 2=no 4=unk 1 0 + + for r=[2 3] % OpS, ClS + assert(argmax(squeeze(strategy{e}{D}(t,r,:))) == 1); % drill = yes + end + r = 1; % noS + assert(argmax(squeeze(strategy{e}{D}(t,r,:))) == 2); % drill = no +end + + +for e=approx(:)' + approxeq(strategy{exact(1)}{T}, strategy{e}{T}) + approxeq(strategy{exact(1)}{D}, strategy{e}{D}) +end diff --git a/sourcecodes/bnt-master/BNT/examples/limids/pigs1.m b/sourcecodes/bnt-master/BNT/examples/limids/pigs1.m new file mode 100644 index 00000000..6a56ad04 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/pigs1.m @@ -0,0 +1,153 @@ +% pigs model from Lauritzen and Nilsson, 2001 + +seed = 0; +rand('state', seed); +randn('state', seed); + +% we number nodes down and to the right +h = [1 5 9 13]; +t = [2 6 10]; +d = [3 7 11]; +u = [4 8 12 14]; + +N = 14; +dag = zeros(N); + +% causal arcs +for i=1:3 + dag(h(i), [t(i) h(i+1)]) = 1; + dag(d(i), [u(i) h(i+1)]) = 1; +end +dag(h(4), u(4)) = 1; + +% information arcs +fig = 2; +switch fig + case 0, + % no info arcs + case 1, + % no-forgetting policy (figure 1) + for i=1:3 + dag(t(i), d(i:3)) = 1; + end + case 2, + % reactive policy (figure 2) + for i=1:3 + dag(t(i), d(i)) = 1; + end + case 7, + % omniscient policy (figure 7: di has access to hidden state h(i-1)) + dag(t(1), d(1)) = 1; + for i=2:3 + %dag([h(i-1) t(i-1) d(i-1)], d(i)) = 1; + dag([h(i-1) d(i-1)], d(i)) = 1; % t(i-1) is redundant given h(i-1) + end +end + + +ns = 2*ones(1,N); +ns(u) = 1; + +% parameter tying +params = ones(1,N); +uparam = 1; +final_uparam = 2; +tparam = 3; +h1_param = 4; +hparam = 5; +dparams = 6:8; + +params(u(1:3)) = uparam; +params(u(4)) = final_uparam; +params(t) = tparam; +params(h(1)) = h1_param; +params(h(2:end)) = hparam; +params(d) = dparams; + +limid = mk_limid(dag, ns, 'chance', [h t], 'decision', d, 'utility', u, 'equiv_class', params); + +% h = 1 means healthy, h = 2 means diseased +% d = 1 means don't treat, d = 2 means treat +% t = 1 means test shows healthy, t = 2 means test shows diseased + +if 0 + % use random params + limid.CPD{final_uparam} = tabular_utility_node(limid, u(4)); + limid.CPD{uparam} = tabular_utility_node(limid, u(1)); + limid.CPD{tparam} = tabular_CPD(limid, t(1)); + limid.CPD{h1_param} = tabular_CPD(limid, h(1)); + limid.CPD{hparam} = tabular_CPD(limid, h(2)); +else + limid.CPD{final_uparam} = tabular_utility_node(limid, u(4), [1000 300]); + limid.CPD{uparam} = tabular_utility_node(limid, u(1), [0 -100]); % costs have negative utility! + + % h P(t=1) P(t=2) + % 1 0.9 0.1 + % 2 0.2 0.8 + limid.CPD{tparam} = tabular_CPD(limid, t(1), [0.9 0.2 0.1 0.8]); + + % P(h1) + limid.CPD{h1_param} = tabular_CPD(limid, h(1), [0.9 0.1]); + + % hi di P(hj=1) P(hj=2), j = i+1, i=1:3 + % 1 1 0.8 0.2 + % 2 1 0.1 0.9 + % 1 2 0.9 0.1 + % 2 2 0.5 0.5 + limid.CPD{hparam} = tabular_CPD(limid, h(2), [0.8 0.1 0.9 0.5 0.2 0.9 0.1 0.5]); +end + +% Decision nodes get assigned uniform policies by default +for i=1:3 + limid.CPD{dparams(i)} = tabular_decision_node(limid, d(i)); +end + + +fname = '/home/cs/murphyk/matlab/Misc/loopybel.txt'; + +engines = {}; +engines{end+1} = global_joint_inf_engine(limid); +engines{end+1} = jtree_limid_inf_engine(limid); +%engines{end+1} = belprop_inf_engine(limid, 'max_iter', 1*N, 'filename', fname, 'tol', 1e-3); + +exact = [1 2]; +%approx = 3; +approx = []; + +max_iter = 1; +order = d(end:-1:1); +%order = d(1:end); + +NE = length(engines); +MEU = zeros(1, NE); +niter = zeros(1, NE); +strategy = cell(1, NE); +for e=1:NE + [strategy{e}, MEU(e), niter(e)] = solve_limid(engines{e}, 'max_iter', max_iter, 'order', order); +end +MEU + +% check results match those in the paper (p. 22) +direct_policy = eye(2); % treat iff test is positive +never_policy = [1 0; 1 0]; % never treat +tol = 1e-0; % results in paper are reported to 0dp +for e=exact(:)' + switch fig + case 2, % reactive policy + assert(approxeq(MEU(e), 727, tol)); + assert(approxeq(strategy{e}{d(1)}(:), never_policy(:))) + assert(approxeq(strategy{e}{d(2)}(:), direct_policy(:))) + assert(approxeq(strategy{e}{d(3)}(:), direct_policy(:))) + case 1, assert(approxeq(MEU(e), 729, tol)); + case 7, assert(approxeq(MEU(e), 732, tol)); + end +end + + +for e=approx(:)' + for i=1:3 + approxeq(strategy{exact(1)}{d(i)}, strategy{e}{d(i)}) + dispcpt(strategy{e}{d(i)}) + end +end + |
