annotate toolboxes/FullBNT-1.0.7/graph/dag_to_essential_graph.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
Daniel@0 2 function [eg] = dag_to_essential_graph(dag)
Daniel@0 3 cpdag = dag_to_cpdag(dag);
Daniel@0 4 eg = dag + dag .* (cpdag + cpdag');
Daniel@0 5
Daniel@0 6 return;
Daniel@0 7
Daniel@0 8
Daniel@0 9
Daniel@0 10
Daniel@0 11 % Coverts a DAG into Essential Graph where edges are coded by 2 and 3, 2 is
Daniel@0 12 % directed edge and 3 is bidirected edge and is at one (the same as the original DAG) of the two
Daniel@0 13 % symetrical places.
Daniel@0 14
Daniel@0 15 % Is implemented by the algorithm of Max Chickering in D.M.Chickering (1995).
Daniel@0 16 % A transformational characterization of equivalent Bayesian network structures.
Daniel@0 17 % In Proceedings of Eleventh Conference on Uncertainty in Artificial Intelligence, Montreal, QU,
Daniel@0 18 % pages 87-98. Morgan Kaufmann
Daniel@0 19 % http://research.microsoft.com/~dmax/publications/uai95.pdf
Daniel@0 20
Daniel@0 21 % Implemented by Tomas Kocka, AAU.
Daniel@0 22
Daniel@0 23 function [eg] = dag_to_essential_graph(dagx)
Daniel@0 24
Daniel@0 25 %print_dag(dagx); % Just checking input
Daniel@0 26
Daniel@0 27 order = topological_sort(dagx); % get the topological order of nodes and their number
Daniel@0 28
Daniel@0 29 % fprintf('the topological order is: %d',order);
Daniel@0 30 % fprintf('\n');
Daniel@0 31
Daniel@0 32 [nx,ny] = size(dagx); % gets the number of nodes, note that nx == ny
Daniel@0 33 [I,J] = find(dagx); % finds all nonzero elements in the adjacency matrix, i.e. arcs in the DAG - however we will overwrite it in a special order
Daniel@0 34 % we will sort the arcs from lowest possible y and highest possible x, arcs are x->y
Daniel@0 35 e = 1;
Daniel@0 36 for y = 1:ny
Daniel@0 37 for x = nx:-1:1
Daniel@0 38 %fprintf('x %d ',order(x)); fprintf('y %d ',order(y));
Daniel@0 39 if dagx(order(x),order(y)) == 1
Daniel@0 40 I(e) = order(x);
Daniel@0 41 J(e) = order(y);
Daniel@0 42 e = e + 1;
Daniel@0 43 %fprintf('x order %d',x);
Daniel@0 44 %fprintf('y order %d',y);
Daniel@0 45 %fprintf('\n');
Daniel@0 46 end
Daniel@0 47 end
Daniel@0 48 end
Daniel@0 49
Daniel@0 50
Daniel@0 51 % fprintf('the arcs are: %d',I);
Daniel@0 52 % fprintf('\n');
Daniel@0 53 % fprintf('the arcs are: %d',J);
Daniel@0 54 % fprintf('\n');
Daniel@0 55
Daniel@0 56
Daniel@0 57 % Now we have to decide which arcs are part of the essential graph and
Daniel@0 58 % which are undirected edges in the essential graph.
Daniel@0 59 % Undecided arc in the DAG are 1, directed in EG are 2 and undirected in EG
Daniel@0 60 % are 3.
Daniel@0 61
Daniel@0 62
Daniel@0 63 for e = 1:length(I)
Daniel@0 64 if dagx(I(e),J(e)) == 1
Daniel@0 65 cont = true;
Daniel@0 66 for w = 1:nx
Daniel@0 67 if dagx(w,I(e)) == 2
Daniel@0 68 if dagx(w,J(e)) ~= 0
Daniel@0 69 dagx(w,J(e)) = 2;
Daniel@0 70 else
Daniel@0 71 for ww = 1:nx
Daniel@0 72 if dagx(ww,J(e)) ~= 0
Daniel@0 73 dagx(ww,J(e)) = 2;
Daniel@0 74 end
Daniel@0 75 end % and now skip the rest and start with another arc from the list
Daniel@0 76 w = nx;
Daniel@0 77 cont = false;
Daniel@0 78 end
Daniel@0 79 end
Daniel@0 80 end
Daniel@0 81 if cont
Daniel@0 82 exists = false;
Daniel@0 83 for z = 1:nx
Daniel@0 84 %fprintf('test %d',dagx(z,J(e)));
Daniel@0 85 if dagx(z,J(e)) ~= 0 & z ~= I(e) & dagx(z,I(e)) == 0
Daniel@0 86 exists = true;
Daniel@0 87 for ww = 1:nx
Daniel@0 88 if dagx(ww,J(e)) == 1
Daniel@0 89 dagx(ww,J(e)) = 2;
Daniel@0 90 end
Daniel@0 91 end
Daniel@0 92 end
Daniel@0 93 end
Daniel@0 94 if ~ exists
Daniel@0 95 for ww = 1:nx
Daniel@0 96 if dagx(ww,J(e)) == 1
Daniel@0 97 dagx(ww,J(e)) = 3;
Daniel@0 98 end
Daniel@0 99 end
Daniel@0 100 end
Daniel@0 101 end
Daniel@0 102 end
Daniel@0 103 end
Daniel@0 104
Daniel@0 105 %print_dag(dagx); % Just checking output
Daniel@0 106
Daniel@0 107
Daniel@0 108
Daniel@0 109
Daniel@0 110
Daniel@0 111
Daniel@0 112