annotate toolboxes/FullBNT-1.0.7/bnt/inference/static/@jtree_limid_inf_engine/Old/marginal_family.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 [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, or [] (to compute global max expected utility)
wolffd@0 6
wolffd@0 7 bnet = bnet_from_engine(engine);
wolffd@0 8 if isempty(query)
wolffd@0 9 compute_meu = 1;
wolffd@0 10 d = bnet.decision_nodes(1); % pick an arbitrary root to collect to
wolffd@0 11 fam = []; % marginalize root pot down to a point
wolffd@0 12 else
wolffd@0 13 compute_meu = 0;
wolffd@0 14 d = query;
wolffd@0 15 assert(myismember(d, bnet.decision_nodes));
wolffd@0 16 fam = family(bnet.dag, d);
wolffd@0 17 end
wolffd@0 18
wolffd@0 19 clpot = init_clpot(bnet, engine.cliques, engine.clq_ass_to_node, engine.evidence, engine.exclude);
wolffd@0 20
wolffd@0 21 % collect to root (clique containing d)
wolffd@0 22 C = length(engine.cliques);
wolffd@0 23 seppot = cell(C, C); % separators are implicitely initialized to 1s
wolffd@0 24 for n=engine.postorder{d}(1:end-1)
wolffd@0 25 for p=parents(engine.rooted_jtree{d}, n)
wolffd@0 26 %clpot{p} = divide_by_pot(clpot{n}, seppot{p,n}); % dividing by 1 is redundant
wolffd@0 27 seppot{p,n} = marginalize_pot(clpot{n}, engine.separator{p,n});
wolffd@0 28 clpot{p} = multiply_by_pot(clpot{p}, seppot{p,n});
wolffd@0 29 end
wolffd@0 30 end
wolffd@0 31
wolffd@0 32 root = engine.clq_ass_to_node(d);
wolffd@0 33 assert(root == engine.postorder{d}(end));
wolffd@0 34 pot = marginalize_pot(clpot{root}, fam);
wolffd@0 35 m = pot_to_marginal(pot);
wolffd@0 36
wolffd@0 37 %%%%%%%%%%%
wolffd@0 38
wolffd@0 39
wolffd@0 40 function clpot = init_clpot(bnet, cliques, clq_ass_to_node, evidence, exclude)
wolffd@0 41
wolffd@0 42 % Set the clique potentials to all 1s
wolffd@0 43 C = length(cliques);
wolffd@0 44 clpot = cell(1, C);
wolffd@0 45 ns = bnet.node_sizes;
wolffd@0 46 for i=1:C
wolffd@0 47 clpot{i} = upot(cliques{i}, ns(cliques{i}));
wolffd@0 48 end
wolffd@0 49
wolffd@0 50 N = length(bnet.dag);
wolffd@0 51 nodes = mysetdiff(1:N, exclude);
wolffd@0 52
wolffd@0 53 for n=nodes(:)'
wolffd@0 54 fam = family(bnet.dag, n);
wolffd@0 55 e = bnet.equiv_class(n);
wolffd@0 56 c = clq_ass_to_node(n);
wolffd@0 57 pot = convert_to_pot(bnet.CPD{e}, 'u', ns, fam, evidence);
wolffd@0 58 clpot{c} = multiply_by_pot(clpot{c}, pot);
wolffd@0 59 end