annotate toolboxes/FullBNT-1.0.7/graph/Old/dag_to_jtree.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [jtree, root, cliques, B, w, elim_order, moral_edges, fill_in_edges, strong] = ...
Daniel@0 2 dag_to_jtree(dag, node_sizes, partial_order, stages, clusters)
Daniel@0 3 % DAG_TO_JTREE Moralize and triangulate a DAG, and make a junction tree from its cliques.
Daniel@0 4 % [jtree, root, cliques, B, w, elim_order, moral_edges, fill_in_edges, strong] = ...
Daniel@0 5 % dag_to_jtree(dag, node_sizes, partial_order, stages, clusters)
Daniel@0 6 %
Daniel@0 7 % Input:
Daniel@0 8 % dag(i,j)
Daniel@0 9 % jtree(i,j) = 1 iff there is an arc between clique i and clique j
Daniel@0 10 % root = the root clique
Daniel@0 11 % cliques{i} = the nodes in clique i
Daniel@0 12 % B(i,j) = 1 iff node j occurs in clique i
Daniel@0 13 % w(i) = weight of clique i
Daniel@0 14
Daniel@0 15 N = length(bnet.dag);
Daniel@0 16 if nargin < 2, obs_nodes = []; end
Daniel@0 17 if nargin < 3, stages = { 1:N }; end
Daniel@0 18 if nargin < 4, clusters = {}; end
Daniel@0 19
Daniel@0 20 [MG, moral_edges] = moralize(bnet.dag);
Daniel@0 21
Daniel@0 22 % Add extra arcs between nodes in each cluster to ensure they occur in the same clique
Daniel@0 23 for i=1:length(clusters)
Daniel@0 24 c = clusters{i};
Daniel@0 25 MG(c,c) = 1;
Daniel@0 26 end
Daniel@0 27 MG = setdiag(MG, 0);
Daniel@0 28
Daniel@0 29 % Find an optimal elimination ordering (NP-hard problem!)
Daniel@0 30 ns = bnet.node_sizes(:);
Daniel@0 31 ns(obs_nodes) = 1; % observed nodes have only 1 possible value
Daniel@0 32 partial_order = determine_elim_constraints(bnet, obs_nodes);
Daniel@0 33
Daniel@0 34 if isempty(partial_order)
Daniel@0 35 strong = 0;
Daniel@0 36 elim_order = best_first_elim_order(MG, ns, stages);
Daniel@0 37 else
Daniel@0 38 strong = 1;
Daniel@0 39 elim_order = strong_elim_order(MG, ns, partial_order);
Daniel@0 40 end
Daniel@0 41
Daniel@0 42 [MTG, cliques, fill_in_edges] = triangulate(MG, elim_order);
Daniel@0 43
Daniel@0 44 % Connect the cliques up into a jtree,
Daniel@0 45 [jtree, root, B, w] = cliques_to_jtree(cliques, ns);
Daniel@0 46
Daniel@0 47 if 0
Daniel@0 48 disp('testing dag to jtree');
Daniel@0 49 % Find the cliques containing each node, and check they form a connected subtree
Daniel@0 50 clqs_con_node = cell(1,N);
Daniel@0 51 for i=1:N
Daniel@0 52 clqs_con_node{i} = find(B(:,i))';
Daniel@0 53 end
Daniel@0 54 check_jtree_property(clqs_con_node, jtree);
Daniel@0 55 end