Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/inference/static/@quickscore_inf_engine/private/quickscore.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 function prob = quickscore(fpos, fneg, inhibit, prior, leak) | |
2 % QUICKSCORE Heckerman's algorithm for BN2O networks. | |
3 % prob = quickscore(fpos, fneg, inhibit, prior, leak) | |
4 % | |
5 % Consider a BN2O (Binary Node 2-layer Noisy-or) network such as QMR with | |
6 % dieases on the top and findings on the bottom. (We assume all findings are observed, | |
7 % since hidden leaves can be marginalized away.) | |
8 % This algorithm takes O(2^|fpos|) time to compute the marginal on all the diseases. | |
9 % | |
10 % Inputs: | |
11 % fpos = the positive findings (a vector of numbers in {1, ..., Nfindings}) | |
12 % fneg = the negative findings (a vector of numbers in {1, ..., Nfindings}) | |
13 % inhibit(i,j) = inhibition prob. for finding i, disease j, or 1.0 if j is not a parent. | |
14 % prior(j) = prior prob. disease j is ON. We assume prior(off) = 1-prior(on). | |
15 % leak(i) = inhibition prob. for the leak node for finding i | |
16 % | |
17 % Output: | |
18 % prob(d) = Pr(disease d = on | ev) | |
19 % | |
20 % For details, see | |
21 % - Heckerman, "A tractable inference algorithm for diagnosing multiple diseases", UAI89. | |
22 % - Rish and Dechter, "On the impact of causal independence", UCI tech report, 1998. | |
23 % | |
24 % Note that this algorithm is numerically unstable, since it adds a large number of positive and | |
25 % negative terms and hopes that some of them exactly cancel. | |
26 % | |
27 % For matlab experts, use 'mex' to compile C_quickscore, which has identical behavior to this function. | |
28 | |
29 [nfindings ndiseases] = size(inhibit); | |
30 | |
31 % make the first disease be always on, for the leak term | |
32 Pon = [1 prior(:)']; | |
33 Poff = 1-Pon; | |
34 Uon = [leak(:) inhibit]; % U(f,d) = Pr(f=0|d=1) | |
35 Uoff = [leak(:) ones(nfindings, ndiseases)]; % Uoff(f,d) = Pr(f=0|d=0) | |
36 ndiseases = ndiseases + 1; | |
37 | |
38 npos = length(fpos); | |
39 post = zeros(ndiseases, 2); | |
40 % post(d,1) = alpha Pr(d=off), post(d,2) = alpha Pr(d=m) | |
41 | |
42 FP = length(fpos); | |
43 %allbits = logical(dec2bitv(0:(2^FP - 1), FP)); | |
44 allbits = logical(ind2subv(2*ones(1,FP), 1:(2^FP))-1); | |
45 | |
46 for si=1:2^FP | |
47 bits = allbits(si,:); | |
48 fprime = fpos(bits); | |
49 fmask = zeros(1, nfindings); | |
50 fmask(fneg)=1; | |
51 fmask(fprime)=1; | |
52 fmask = logical(fmask); | |
53 p = 1; | |
54 pterm = zeros(1, ndiseases); | |
55 ptermOff = zeros(1, ndiseases); | |
56 ptermOn = zeros(1, ndiseases); | |
57 for d=1:ndiseases | |
58 ptermOff(d) = prod(Uoff(fmask,d)); | |
59 ptermOn(d) = prod(Uon(fmask,d)); | |
60 pterm(d) = Poff(d)*ptermOff(d) + Pon(d)*ptermOn(d); | |
61 end | |
62 p = prod(pterm); | |
63 sign = (-1)^(length(fprime)); | |
64 for d=1:ndiseases | |
65 myp = p / pterm(d); | |
66 post(d,1) = post(d,1) + sign*(myp * ptermOff(d)); | |
67 post(d,2) = post(d,2) + sign*(myp * ptermOn(d)); | |
68 end | |
69 end | |
70 | |
71 post(:,1) = post(:,1) .* Poff(:); | |
72 post(:,2) = post(:,2) .* Pon(:); | |
73 post = mk_stochastic(post); | |
74 prob = post(2:end,2)'; % skip the leak term | |
75 | |
76 |