wolffd@0: % Test jtree_compiled on a toy QMR network. wolffd@0: wolffd@0: rand('state', 0); wolffd@0: randn('state', 0); wolffd@0: pMax = 0.01; wolffd@0: Nfindings = 10; wolffd@0: Ndiseases = 5; wolffd@0: wolffd@0: N=Nfindings+Ndiseases; wolffd@0: findings = Ndiseases+1:N; wolffd@0: diseases = 1:Ndiseases; wolffd@0: wolffd@0: G = zeros(Ndiseases, Nfindings); wolffd@0: for i=1:Nfindings wolffd@0: v= rand(1,Ndiseases); wolffd@0: rents = find(v<0.8); wolffd@0: if (length(rents)==0) wolffd@0: rents=ceil(rand(1)*Ndiseases); wolffd@0: end wolffd@0: G(rents,i)=1; wolffd@0: end wolffd@0: wolffd@0: prior = pMax*rand(1,Ndiseases); wolffd@0: leak = 0.5*rand(1,Nfindings); % in real QMR, leak approx exp(-0.02) = 0.98 wolffd@0: %leak = ones(1,Nfindings); % turns off leaks, which makes inference much harder wolffd@0: inhibit = rand(Ndiseases, Nfindings); wolffd@0: inhibit(not(G)) = 1; wolffd@0: wolffd@0: % first half of findings are +ve, second half -ve wolffd@0: % The very first and last findings are hidden wolffd@0: pos = 2:floor(Nfindings/2); wolffd@0: neg = (pos(end)+1):(Nfindings-1); wolffd@0: wolffd@0: big = 1; wolffd@0: wolffd@0: if big wolffd@0: % Make the bnet in the straightforward way wolffd@0: tabular_leaves = 1; wolffd@0: obs_nodes = myunion(pos, neg) + Ndiseases; wolffd@0: bnet = mk_qmr_bnet(G, inhibit, leak, prior, tabular_leaves, obs_nodes); wolffd@0: evidence = cell(1, N); wolffd@0: evidence(findings(pos)) = num2cell(repmat(2, 1, length(pos))); wolffd@0: evidence(findings(neg)) = num2cell(repmat(1, 1, length(neg))); wolffd@0: else wolffd@0: % Marginalize out hidden leaves apriori wolffd@0: positive_leaves_only = 1; wolffd@0: [bnet, vals] = mk_minimal_qmr_bnet(G, inhibit, leak, prior, pos, neg, positive_leaves_only); wolffd@0: obs_nodes = bnet.observed; wolffd@0: evidence = cell(1, Ndiseases + length(obs_nodes)); wolffd@0: evidence(obs_nodes) = num2cell(vals); wolffd@0: end wolffd@0: wolffd@0: engine = {}; wolffd@0: engine{end+1} = jtree_inf_engine(bnet); wolffd@0: wolffd@0: E = length(engine); wolffd@0: exact = 1:E; wolffd@0: ll = zeros(1,E); wolffd@0: for e=1:E wolffd@0: tic; [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence); toc wolffd@0: end wolffd@0: wolffd@0: assert(all(approxeq(ll(exact), ll(exact(1))))) wolffd@0: wolffd@0: post = zeros(E, Ndiseases); wolffd@0: for e=1:E wolffd@0: for i=diseases(:)' wolffd@0: m = marginal_nodes(engine{e}, i); wolffd@0: post(e, i) = m.T(2); wolffd@0: end wolffd@0: end wolffd@0: for e=exact(:)' wolffd@0: for i=diseases(:)' wolffd@0: assert(approxeq(post(1, i), post(e, i))); wolffd@0: end wolffd@0: end wolffd@0: