wolffd@0
|
1 function [m, pot] = marginal_family(engine, query)
|
wolffd@0
|
2 % MARGINAL_NODES Compute the marginal on the family of the specified node (jtree_limid)
|
wolffd@0
|
3 % [m, pot] = marginal_family(engine, query)
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % query should be a single decision node
|
wolffd@0
|
6
|
wolffd@0
|
7 bnet = bnet_from_engine(engine);
|
wolffd@0
|
8 d = query;
|
wolffd@0
|
9 assert(myismember(d, bnet.decision_nodes));
|
wolffd@0
|
10 fam = family(bnet.dag, d);
|
wolffd@0
|
11
|
wolffd@0
|
12 clpot = init_clpot(bnet, engine.cliques, engine.clq_ass_to_node, engine.evidence, engine.exclude);
|
wolffd@0
|
13
|
wolffd@0
|
14 % collect to root (clique containing d)
|
wolffd@0
|
15 C = length(engine.cliques);
|
wolffd@0
|
16 seppot = cell(C, C); % separators are implicitely initialized to 1s
|
wolffd@0
|
17 for n=engine.postorder{d}(1:end-1)
|
wolffd@0
|
18 for p=parents(engine.rooted_jtree{d}, n)
|
wolffd@0
|
19 %clpot{p} = divide_by_pot(clpot{n}, seppot{p,n}); % dividing by 1 is redundant
|
wolffd@0
|
20 seppot{p,n} = marginalize_pot(clpot{n}, engine.separator{p,n});
|
wolffd@0
|
21 clpot{p} = multiply_by_pot(clpot{p}, seppot{p,n});
|
wolffd@0
|
22 end
|
wolffd@0
|
23 end
|
wolffd@0
|
24
|
wolffd@0
|
25 root = engine.clq_ass_to_node(d);
|
wolffd@0
|
26 assert(root == engine.postorder{d}(end));
|
wolffd@0
|
27 pot = marginalize_pot(clpot{root}, fam);
|
wolffd@0
|
28 m = pot_to_marginal(pot);
|
wolffd@0
|
29
|
wolffd@0
|
30 %%%%%%%%%%%
|
wolffd@0
|
31
|
wolffd@0
|
32
|
wolffd@0
|
33 function clpot = init_clpot(bnet, cliques, clq_ass_to_node, evidence, exclude)
|
wolffd@0
|
34
|
wolffd@0
|
35 % Set the clique potentials to all 1s
|
wolffd@0
|
36 C = length(cliques);
|
wolffd@0
|
37 clpot = cell(1, C);
|
wolffd@0
|
38 ns = bnet.node_sizes;
|
wolffd@0
|
39 for i=1:C
|
wolffd@0
|
40 clpot{i} = upot(cliques{i}, ns(cliques{i}));
|
wolffd@0
|
41 end
|
wolffd@0
|
42
|
wolffd@0
|
43 N = length(bnet.dag);
|
wolffd@0
|
44 nodes = mysetdiff(1:N, exclude);
|
wolffd@0
|
45
|
wolffd@0
|
46 for n=nodes(:)'
|
wolffd@0
|
47 fam = family(bnet.dag, n);
|
wolffd@0
|
48 e = bnet.equiv_class(n);
|
wolffd@0
|
49 c = clq_ass_to_node(n);
|
wolffd@0
|
50 pot = convert_to_pot(bnet.CPD{e}, 'u', fam(:), evidence);
|
wolffd@0
|
51 clpot{c} = multiply_by_pot(clpot{c}, pot);
|
wolffd@0
|
52 end
|