comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [marginal, loglik] = marginal_nodes(engine, query)
2 % MARGINAL_NODES Compute the marginal on the specified query nodes (enumerative_inf)
3 % [marginal, loglik] = marginal_nodes(engine, query)
4
5
6 if isempty(query) & nargout < 2
7 marginal.T = 1;
8 marginal.domain = [];
9 return;
10 end
11
12 evidence = engine.evidence;
13 bnet = bnet_from_engine(engine);
14 assert(isempty(bnet.cnodes));
15 n = length(bnet.dag);
16 observed = ~isemptycell(evidence);
17 vals = cat(1,evidence{observed});
18 vals = vals(:)';
19 ns = bnet.node_sizes;
20
21 sz = ns(query);
22 T = 0*myones(sz);
23 p = 0;
24 for i=1:prod(ns)
25 inst = ind2subv(ns, i); % i'th instantiation
26 if isempty(vals) | inst(observed) == vals % agrees with evidence
27 prob = exp(log_lik_complete(bnet, num2cell(inst(:))));
28 p = p + prob;
29 v = inst(query);
30 j = subv2ind(sz, v);
31 T(j) = T(j) + prob;
32 end
33 end
34
35 [T, lik] = normalise(T);
36 lik = p;
37 loglik = log(lik);
38
39 Tsmall = shrink_obs_dims_in_table(T, query, evidence);
40 marginal.domain = query;
41 marginal.T = Tsmall;