annotate toolboxes/FullBNT-1.0.7/bnt/inference/static/@enumerative_inf_engine/marginal_nodes.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [marginal, loglik] = marginal_nodes(engine, query)
wolffd@0 2 % MARGINAL_NODES Compute the marginal on the specified query nodes (enumerative_inf)
wolffd@0 3 % [marginal, loglik] = marginal_nodes(engine, query)
wolffd@0 4
wolffd@0 5
wolffd@0 6 if isempty(query) & nargout < 2
wolffd@0 7 marginal.T = 1;
wolffd@0 8 marginal.domain = [];
wolffd@0 9 return;
wolffd@0 10 end
wolffd@0 11
wolffd@0 12 evidence = engine.evidence;
wolffd@0 13 bnet = bnet_from_engine(engine);
wolffd@0 14 assert(isempty(bnet.cnodes));
wolffd@0 15 n = length(bnet.dag);
wolffd@0 16 observed = ~isemptycell(evidence);
wolffd@0 17 vals = cat(1,evidence{observed});
wolffd@0 18 vals = vals(:)';
wolffd@0 19 ns = bnet.node_sizes;
wolffd@0 20
wolffd@0 21 sz = ns(query);
wolffd@0 22 T = 0*myones(sz);
wolffd@0 23 p = 0;
wolffd@0 24 for i=1:prod(ns)
wolffd@0 25 inst = ind2subv(ns, i); % i'th instantiation
wolffd@0 26 if isempty(vals) | inst(observed) == vals % agrees with evidence
wolffd@0 27 prob = exp(log_lik_complete(bnet, num2cell(inst(:))));
wolffd@0 28 p = p + prob;
wolffd@0 29 v = inst(query);
wolffd@0 30 j = subv2ind(sz, v);
wolffd@0 31 T(j) = T(j) + prob;
wolffd@0 32 end
wolffd@0 33 end
wolffd@0 34
wolffd@0 35 [T, lik] = normalise(T);
wolffd@0 36 lik = p;
wolffd@0 37 loglik = log(lik);
wolffd@0 38
wolffd@0 39 Tsmall = shrink_obs_dims_in_table(T, query, evidence);
wolffd@0 40 marginal.domain = query;
wolffd@0 41 marginal.T = Tsmall;