annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/Models/mk_minimal_qmr_bnet.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [bnet, vals] = mk_minimal_qmr_bnet(G, inhibit, leak, prior, pos, neg, pos_only)
wolffd@0 2 % MK_MINIMAL_QMR_BNET Make a QMR model which only contains the observed findings
wolffd@0 3 % [bnet, vals] = mk_minimal_qmr_bnet(G, inhibit, prior, leak, pos, neg)
wolffd@0 4 %
wolffd@0 5 % Input:
wolffd@0 6 % G(i,j) = 1 iff there is an arc from disease i to finding j
wolffd@0 7 % inhibit(i,j) = inhibition probability on i->j arc
wolffd@0 8 % leak(j) = inhibition prob. on leak->j arc
wolffd@0 9 % prior(i) = prob. disease i is on
wolffd@0 10 % pos = list of leaves that have positive observations
wolffd@0 11 % neg = list of leaves that have negative observations
wolffd@0 12 % pos_only = 1 means only include positively observed leaves in the model - the negative
wolffd@0 13 % ones are absorbed into the prior terms
wolffd@0 14 %
wolffd@0 15 % Output:
wolffd@0 16 % bnet
wolffd@0 17 % vals is their value
wolffd@0 18
wolffd@0 19 if pos_only
wolffd@0 20 obs = pos;
wolffd@0 21 else
wolffd@0 22 obs = myunion(pos, neg);
wolffd@0 23 end
wolffd@0 24 Nfindings = length(obs);
wolffd@0 25 [Ndiseases maxNfindings] = size(inhibit);
wolffd@0 26 N = Ndiseases + Nfindings;
wolffd@0 27 finding_node = Ndiseases+1:N;
wolffd@0 28
wolffd@0 29 % j = finding_node(i) means the i'th finding node is the j'th node in the bnet
wolffd@0 30 % k = obs(i) means the i'th observed (positive) finding is the k'th finding overall
wolffd@0 31 % If all findings are observed, and posonly = 0, we have i = obs(i) for all i.
wolffd@0 32
wolffd@0 33 %dag = sparse(N, N);
wolffd@0 34 dag = zeros(N, N);
wolffd@0 35 dag(1:Ndiseases, Ndiseases+1:N) = G(:,obs);
wolffd@0 36
wolffd@0 37 ns = 2*ones(1,N);
wolffd@0 38 bnet = mk_bnet(dag, ns, 'observed', finding_node);
wolffd@0 39
wolffd@0 40 CPT = cell(1, Ndiseases);
wolffd@0 41 for d=1:Ndiseases
wolffd@0 42 CPT{d} = [1-prior(d) prior(d)];
wolffd@0 43 end
wolffd@0 44
wolffd@0 45 if pos_only
wolffd@0 46 % Fold in the negative evidence into the prior
wolffd@0 47 for i=1:length(neg)
wolffd@0 48 n = neg(i);
wolffd@0 49 ps = parents(G,n);
wolffd@0 50 for pi=1:length(ps)
wolffd@0 51 p = ps(pi);
wolffd@0 52 q = inhibit(p,n);
wolffd@0 53 CPT{p} = CPT{p} .* [1 q];
wolffd@0 54 end
wolffd@0 55 % Arbitrarily attach the leak term to the first parent
wolffd@0 56 p = ps(1);
wolffd@0 57 q = leak(n);
wolffd@0 58 CPT{p} = CPT{p} .* [q q];
wolffd@0 59 end
wolffd@0 60 end
wolffd@0 61
wolffd@0 62 for d=1:Ndiseases
wolffd@0 63 bnet.CPD{d} = tabular_CPD(bnet, d, CPT{d}');
wolffd@0 64 end
wolffd@0 65
wolffd@0 66 for i=1:Nfindings
wolffd@0 67 fnode = finding_node(i);
wolffd@0 68 fid = obs(i);
wolffd@0 69 ps = parents(G, fid);
wolffd@0 70 bnet.CPD{fnode} = noisyor_CPD(bnet, fnode, leak(fid), inhibit(ps, fid));
wolffd@0 71 end
wolffd@0 72
wolffd@0 73 obs_nodes = finding_node;
wolffd@0 74 vals = sparse(1, maxNfindings);
wolffd@0 75 vals(pos) = 2;
wolffd@0 76 vals(neg) = 1;
wolffd@0 77 vals = full(vals(obs));
wolffd@0 78
wolffd@0 79
wolffd@0 80
wolffd@0 81
wolffd@0 82