comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/qmr2.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 % Test jtree_compiled on a toy QMR network.
2
3 rand('state', 0);
4 randn('state', 0);
5 pMax = 0.01;
6 Nfindings = 10;
7 Ndiseases = 5;
8
9 N=Nfindings+Ndiseases;
10 findings = Ndiseases+1:N;
11 diseases = 1:Ndiseases;
12
13 G = zeros(Ndiseases, Nfindings);
14 for i=1:Nfindings
15 v= rand(1,Ndiseases);
16 rents = find(v<0.8);
17 if (length(rents)==0)
18 rents=ceil(rand(1)*Ndiseases);
19 end
20 G(rents,i)=1;
21 end
22
23 prior = pMax*rand(1,Ndiseases);
24 leak = 0.5*rand(1,Nfindings); % in real QMR, leak approx exp(-0.02) = 0.98
25 %leak = ones(1,Nfindings); % turns off leaks, which makes inference much harder
26 inhibit = rand(Ndiseases, Nfindings);
27 inhibit(not(G)) = 1;
28
29 % first half of findings are +ve, second half -ve
30 % The very first and last findings are hidden
31 pos = 2:floor(Nfindings/2);
32 neg = (pos(end)+1):(Nfindings-1);
33
34 big = 1;
35
36 if big
37 % Make the bnet in the straightforward way
38 tabular_leaves = 1;
39 obs_nodes = myunion(pos, neg) + Ndiseases;
40 bnet = mk_qmr_bnet(G, inhibit, leak, prior, tabular_leaves, obs_nodes);
41 evidence = cell(1, N);
42 evidence(findings(pos)) = num2cell(repmat(2, 1, length(pos)));
43 evidence(findings(neg)) = num2cell(repmat(1, 1, length(neg)));
44 else
45 % Marginalize out hidden leaves apriori
46 positive_leaves_only = 1;
47 [bnet, vals] = mk_minimal_qmr_bnet(G, inhibit, leak, prior, pos, neg, positive_leaves_only);
48 obs_nodes = bnet.observed;
49 evidence = cell(1, Ndiseases + length(obs_nodes));
50 evidence(obs_nodes) = num2cell(vals);
51 end
52
53 engine = {};
54 engine{end+1} = jtree_inf_engine(bnet);
55
56 E = length(engine);
57 exact = 1:E;
58 ll = zeros(1,E);
59 for e=1:E
60 tic; [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence); toc
61 end
62
63 assert(all(approxeq(ll(exact), ll(exact(1)))))
64
65 post = zeros(E, Ndiseases);
66 for e=1:E
67 for i=diseases(:)'
68 m = marginal_nodes(engine{e}, i);
69 post(e, i) = m.T(2);
70 end
71 end
72 for e=exact(:)'
73 for i=diseases(:)'
74 assert(approxeq(post(1, i), post(e, i)));
75 end
76 end
77