wolffd@0: function [m, pot] = marginal_family(engine, query) wolffd@0: % MARGINAL_NODES Compute the marginal on the family of the specified node (jtree_limid) wolffd@0: % [m, pot] = marginal_family(engine, query) wolffd@0: % wolffd@0: % query should be a single decision node wolffd@0: wolffd@0: bnet = bnet_from_engine(engine); wolffd@0: d = query; wolffd@0: assert(myismember(d, bnet.decision_nodes)); wolffd@0: fam = family(bnet.dag, d); wolffd@0: wolffd@0: clpot = init_clpot(bnet, engine.cliques, engine.clq_ass_to_node, engine.evidence, engine.exclude); wolffd@0: wolffd@0: % collect to root (clique containing d) wolffd@0: C = length(engine.cliques); wolffd@0: seppot = cell(C, C); % separators are implicitely initialized to 1s wolffd@0: for n=engine.postorder{d}(1:end-1) wolffd@0: for p=parents(engine.rooted_jtree{d}, n) wolffd@0: %clpot{p} = divide_by_pot(clpot{n}, seppot{p,n}); % dividing by 1 is redundant wolffd@0: seppot{p,n} = marginalize_pot(clpot{n}, engine.separator{p,n}); wolffd@0: clpot{p} = multiply_by_pot(clpot{p}, seppot{p,n}); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: root = engine.clq_ass_to_node(d); wolffd@0: assert(root == engine.postorder{d}(end)); wolffd@0: pot = marginalize_pot(clpot{root}, fam); wolffd@0: m = pot_to_marginal(pot); wolffd@0: wolffd@0: %%%%%%%%%%% wolffd@0: wolffd@0: wolffd@0: function clpot = init_clpot(bnet, cliques, clq_ass_to_node, evidence, exclude) wolffd@0: wolffd@0: % Set the clique potentials to all 1s wolffd@0: C = length(cliques); wolffd@0: clpot = cell(1, C); wolffd@0: ns = bnet.node_sizes; wolffd@0: for i=1:C wolffd@0: clpot{i} = upot(cliques{i}, ns(cliques{i})); wolffd@0: end wolffd@0: wolffd@0: N = length(bnet.dag); wolffd@0: nodes = mysetdiff(1:N, exclude); wolffd@0: wolffd@0: for n=nodes(:)' wolffd@0: fam = family(bnet.dag, n); wolffd@0: e = bnet.equiv_class(n); wolffd@0: c = clq_ass_to_node(n); wolffd@0: pot = convert_to_pot(bnet.CPD{e}, 'u', fam(:), evidence); wolffd@0: clpot{c} = multiply_by_pot(clpot{c}, pot); wolffd@0: end