Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/qmr1.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 % Make a QMR-like network | |
2 % This is a bipartite graph, where the top layer contains hidden disease nodes, | |
3 % and the bottom later contains observed finding nodes. | |
4 % The diseases have Bernoulli CPDs, the findings noisy-or CPDs. | |
5 % See quickscore_inf_engine for references. | |
6 | |
7 pMax = 0.01; | |
8 Nfindings = 10; | |
9 Ndiseases = 5; | |
10 %Nfindings = 20; | |
11 %Ndiseases = 10; | |
12 | |
13 N=Nfindings+Ndiseases; | |
14 findings = Ndiseases+1:N; | |
15 diseases = 1:Ndiseases; | |
16 | |
17 G = zeros(Ndiseases, Nfindings); | |
18 for i=1:Nfindings | |
19 v= rand(1,Ndiseases); | |
20 rents = find(v<0.8); | |
21 if (length(rents)==0) | |
22 rents=ceil(rand(1)*Ndiseases); | |
23 end | |
24 G(rents,i)=1; | |
25 end | |
26 | |
27 prior = pMax*rand(1,Ndiseases); | |
28 leak = 0.5*rand(1,Nfindings); % in real QMR, leak approx exp(-0.02) = 0.98 | |
29 %leak = ones(1,Nfindings); % turns off leaks, which makes inference much harder | |
30 inhibit = rand(Ndiseases, Nfindings); | |
31 inhibit(not(G)) = 1; | |
32 | |
33 | |
34 % first half of findings are +ve, second half -ve | |
35 % The very first and last findings are hidden | |
36 pos = 2:floor(Nfindings/2); | |
37 neg = (pos(end)+1):(Nfindings-1); | |
38 | |
39 % Make the bnet in the straightforward way | |
40 tabular_leaves = 0; | |
41 obs_nodes = myunion(pos, neg) + Ndiseases; | |
42 big_bnet = mk_qmr_bnet(G, inhibit, leak, prior, tabular_leaves, obs_nodes); | |
43 big_evidence = cell(1, N); | |
44 big_evidence(findings(pos)) = num2cell(repmat(2, 1, length(pos))); | |
45 big_evidence(findings(neg)) = num2cell(repmat(1, 1, length(neg))); | |
46 | |
47 %clf;draw_layout(big_bnet.dag); | |
48 %filename = '../public_html/Bayes/Figures/qmr.rnd.jpg'; | |
49 %% 3x3 inches | |
50 %set(gcf,'units','inches'); | |
51 %set(gcf,'PaperPosition',[0 0 3 3]) | |
52 %print(gcf,'-djpeg','-r100',filename); | |
53 | |
54 | |
55 % Marginalize out hidden leaves apriori | |
56 positive_leaves_only = 1; | |
57 [bnet, vals] = mk_minimal_qmr_bnet(G, inhibit, leak, prior, pos, neg, positive_leaves_only); | |
58 obs_nodes = bnet.observed; | |
59 evidence = cell(1, Ndiseases + length(obs_nodes)); | |
60 evidence(obs_nodes) = num2cell(vals); | |
61 | |
62 | |
63 clear engine; | |
64 engine{1} = quickscore_inf_engine(inhibit, leak, prior); | |
65 engine{2} = jtree_inf_engine(big_bnet); | |
66 engine{3} = jtree_inf_engine(bnet); | |
67 | |
68 %fname = '/home/cs/murphyk/matlab/Misc/loopybel.txt'; | |
69 global BNT_HOME | |
70 fname = sprintf('%s/loopybel.txt', BNT_HOME); | |
71 | |
72 | |
73 max_iter = 6; | |
74 engine{4} = pearl_inf_engine(bnet, 'protocol', 'parallel', 'max_iter', max_iter); | |
75 %engine{5} = belprop_inf_engine(bnet, 'max_iter', max_iter, 'filename', fname); | |
76 engine{5} = belprop_inf_engine(bnet, 'max_iter', max_iter); | |
77 | |
78 E = length(engine); | |
79 exact = 1:3; | |
80 loopy = [4 5]; | |
81 | |
82 ll = zeros(1,E); | |
83 tic; engine{1} = enter_evidence(engine{1}, pos, neg); toc | |
84 tic; [engine{2}, ll(2)] = enter_evidence(engine{2}, big_evidence); toc | |
85 tic; [engine{3}, ll(3)] = enter_evidence(engine{3}, evidence); toc | |
86 tic; [engine{4}, ll(4), niter(4)] = enter_evidence(engine{4}, evidence); toc | |
87 tic; [engine{5}, niter(5)] = enter_evidence(engine{5}, evidence); toc | |
88 | |
89 ll | |
90 | |
91 post = zeros(E, Ndiseases); | |
92 for e=1:E | |
93 for i=diseases(:)' | |
94 m = marginal_nodes(engine{e}, i); | |
95 post(e, i) = m.T(2); | |
96 end | |
97 end | |
98 | |
99 for e=exact(:)' | |
100 for i=diseases(:)' | |
101 assert(approxeq(post(1, i), post(e, i))); | |
102 end | |
103 end | |
104 | |
105 a = zeros(Ndiseases, 2); | |
106 for ei=1:length(loopy) | |
107 for i=diseases(:)' | |
108 a(i,ei) = approxeq(post(1, i), post(loopy(ei), i)); | |
109 end | |
110 end | |
111 disp('is the loopy posterior correct?'); | |
112 disp(a) |