wolffd@0: function [jtree, root, cliques, B, w, elim_order, moral_edges, fill_in_edges, strong] = ... wolffd@0: dag_to_jtree(dag, node_sizes, partial_order, stages, clusters) wolffd@0: % DAG_TO_JTREE Moralize and triangulate a DAG, and make a junction tree from its cliques. wolffd@0: % [jtree, root, cliques, B, w, elim_order, moral_edges, fill_in_edges, strong] = ... wolffd@0: % dag_to_jtree(dag, node_sizes, partial_order, stages, clusters) wolffd@0: % wolffd@0: % Input: wolffd@0: % dag(i,j) wolffd@0: % jtree(i,j) = 1 iff there is an arc between clique i and clique j wolffd@0: % root = the root clique wolffd@0: % cliques{i} = the nodes in clique i wolffd@0: % B(i,j) = 1 iff node j occurs in clique i wolffd@0: % w(i) = weight of clique i wolffd@0: wolffd@0: N = length(bnet.dag); wolffd@0: if nargin < 2, obs_nodes = []; end wolffd@0: if nargin < 3, stages = { 1:N }; end wolffd@0: if nargin < 4, clusters = {}; end wolffd@0: wolffd@0: [MG, moral_edges] = moralize(bnet.dag); wolffd@0: wolffd@0: % Add extra arcs between nodes in each cluster to ensure they occur in the same clique wolffd@0: for i=1:length(clusters) wolffd@0: c = clusters{i}; wolffd@0: MG(c,c) = 1; wolffd@0: end wolffd@0: MG = setdiag(MG, 0); wolffd@0: wolffd@0: % Find an optimal elimination ordering (NP-hard problem!) wolffd@0: ns = bnet.node_sizes(:); wolffd@0: ns(obs_nodes) = 1; % observed nodes have only 1 possible value wolffd@0: partial_order = determine_elim_constraints(bnet, obs_nodes); wolffd@0: wolffd@0: if isempty(partial_order) wolffd@0: strong = 0; wolffd@0: elim_order = best_first_elim_order(MG, ns, stages); wolffd@0: else wolffd@0: strong = 1; wolffd@0: elim_order = strong_elim_order(MG, ns, partial_order); wolffd@0: end wolffd@0: wolffd@0: [MTG, cliques, fill_in_edges] = triangulate(MG, elim_order); wolffd@0: wolffd@0: % Connect the cliques up into a jtree, wolffd@0: [jtree, root, B, w] = cliques_to_jtree(cliques, ns); wolffd@0: wolffd@0: if 0 wolffd@0: disp('testing dag to jtree'); wolffd@0: % Find the cliques containing each node, and check they form a connected subtree wolffd@0: clqs_con_node = cell(1,N); wolffd@0: for i=1:N wolffd@0: clqs_con_node{i} = find(B(:,i))'; wolffd@0: end wolffd@0: check_jtree_property(clqs_con_node, jtree); wolffd@0: end