annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/qmr2.m @ 0:cc4b1211e677 tip

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