annotate toolboxes/FullBNT-1.0.7/bnt/learning/learn_struct_pdag_ic_star.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 [pdag, G] = learn_struct_pdag_ic_star(cond_indep, n, k, varargin)
Daniel@0 2 % LEARN_STRUCT_PDAG_IC_STAR Learn a partially oriented DAG (pattern) with latent
Daniel@0 3 % variables using the IC* algorithm
Daniel@0 4 % P = learn_struct_pdag_ic_star(cond_indep, n, k, ...)
Daniel@0 5 %
Daniel@0 6 % n is the number of nodes.
Daniel@0 7 % k is an optional upper bound on the fan-in (default: n)
Daniel@0 8 % cond_indep is a boolean function that will be called as follows:
Daniel@0 9 % feval(cond_indep, x, y, S, ...)
Daniel@0 10 % where x and y are nodes, and S is a set of nodes (positive integers),
Daniel@0 11 % and ... are any optional parameters passed to this function.
Daniel@0 12 %
Daniel@0 13 % The output P is an adjacency matrix, in which
Daniel@0 14 % P(i,j) = -1 if there is either a latent variable L such that i <-L-> j
Daniel@0 15 % OR there is a directed edge from i->j.
Daniel@0 16 % P(i,j) = -2 if there is a marked directed i-*>j edge.
Daniel@0 17 % P(i,j) = P(j,i) = 1 if there is and undirected edge i--j
Daniel@0 18 % P(i,j) = P(j,i) = 2 if there is a latent variable L such that i<-L->j.
Daniel@0 19 %
Daniel@0 20 % The IC* algorithm learns a latent structure associated with a set of observed
Daniel@0 21 % variables.
Daniel@0 22 % The latent structure revealed is the projection in which every latent variable is
Daniel@0 23 % 1) a root node
Daniel@0 24 % 2) linked to exactly two observed variables.
Daniel@0 25 % Latent variables in the projection are represented using a bidirectional graph,
Daniel@0 26 % and thus remain implicit.
Daniel@0 27 %
Daniel@0 28 % See Pearl, "Causality: Models, Reasoning, and Inference", 2000, p52 for more details.
Daniel@0 29 % Written by Tamar Kushnir, 2000
Daniel@0 30
Daniel@0 31 sep = cell(n,n);
Daniel@0 32 ord = 0;
Daniel@0 33 done = 0;
Daniel@0 34 G = ones(n,n);
Daniel@0 35 G = setdiag(G,0);
Daniel@0 36 while ~done
Daniel@0 37 done = 1;
Daniel@0 38 [X,Y] = find(G);
Daniel@0 39 for i=1:length(X)
Daniel@0 40 x = X(i); y = Y(i);
Daniel@0 41 nbrs = mysetdiff(myunion(neighbors(G, x), neighbors(G,y)), [x y]);
Daniel@0 42 if length(nbrs) >= ord & G(x,y) ~= 0
Daniel@0 43 done = 0;
Daniel@0 44 SS = subsets(nbrs, ord, ord); % all subsets of size ord
Daniel@0 45 for si=1:length(SS)
Daniel@0 46 S = SS{si};
Daniel@0 47 if feval(cond_indep, x, y, S, varargin{:})
Daniel@0 48 G(x,y) = 0;
Daniel@0 49 G(y,x) = 0;
Daniel@0 50 sep{x,y} = myunion(sep{x,y}, S);
Daniel@0 51 sep{y,x} = myunion(sep{y,x}, S);
Daniel@0 52 break; % no need to check any more subsets
Daniel@0 53 end
Daniel@0 54 end
Daniel@0 55 end
Daniel@0 56 end
Daniel@0 57 ord = ord + 1;
Daniel@0 58 end
Daniel@0 59
Daniel@0 60 % Create the minimal pattern,
Daniel@0 61 % i.e., the only directed edges are V structures.
Daniel@0 62 pdag = G;
Daniel@0 63 [X, Y] = find(G);
Daniel@0 64 % We want to generate all unique triples x,y,z
Daniel@0 65 % where y is a common neighbor to x and z
Daniel@0 66 for i=1:length(X)
Daniel@0 67 x = X(i);
Daniel@0 68 y = Y(i);
Daniel@0 69 Z = find(G(y,:));
Daniel@0 70 Z = mysetdiff(Z, x);
Daniel@0 71 for z=Z(:)'
Daniel@0 72 if G(x,z)==0 & ~ismember(y, sep{x,z}) & ~ismember(y, sep{z,x})
Daniel@0 73 pdag(x,y) = -1; pdag(y,x) = 0;
Daniel@0 74 pdag(z,y) = -1; pdag(y,z) = 0;
Daniel@0 75 end
Daniel@0 76 end
Daniel@0 77 end
Daniel@0 78
Daniel@0 79 % Convert the minimal pattern to a complete one using the following rules:
Daniel@0 80 % Rule 1:
Daniel@0 81 % if a and b are non-adjacent nodes with a common neighbor c,
Daniel@0 82 % if a->c and not b->c then c-*>b (marked arrow).
Daniel@0 83 % Rule 2:
Daniel@0 84 % if a and b are adjacent and there is a directed path (marked links) from a to b
Daniel@0 85 % then a->b (add arrowhead).
Daniel@0 86 %Pearl (2000)
Daniel@0 87
Daniel@0 88 arrowin = [-1 -2 2];
Daniel@0 89 old_pdag = zeros(n);
Daniel@0 90 iter = 0;
Daniel@0 91 while ~isequal(pdag, old_pdag)
Daniel@0 92 iter = iter + 1;
Daniel@0 93 old_pdag = pdag;
Daniel@0 94 % rule 1
Daniel@0 95 [X, Y] = find(pdag);
Daniel@0 96 for i=1:length(X)
Daniel@0 97 x = X(i);
Daniel@0 98 y = Y(i);
Daniel@0 99 Z = find(pdag(y,:));
Daniel@0 100 Z = mysetdiff(Z, x);
Daniel@0 101 for z=Z(:)'
Daniel@0 102 if G(x,z)==0 & ismember(pdag(x,y),arrowin) & ~ismember(pdag(z,y),arrowin)
Daniel@0 103 pdag(y,z) = -2; pdag(z,y) = 0;
Daniel@0 104 end
Daniel@0 105 end
Daniel@0 106 end
Daniel@0 107 % rule 2
Daniel@0 108 [X, Y] = find(G);
Daniel@0 109 %check all adjacent nodes because if pdag(x,y) = -1
Daniel@0 110 %and pdag(y,x) = 0 there could still be an bidirected edge between x & y.
Daniel@0 111 for i=1:length(X)
Daniel@0 112 x = X(i);
Daniel@0 113 y = Y(i);
Daniel@0 114 if ~ismember(pdag(x,y), arrowin) %x->y doesn't exist yet
Daniel@0 115 %find marked path from x to y
Daniel@0 116 add_arrow = marked_path(x,y,pdag);
Daniel@0 117 if add_arrow
Daniel@0 118 if pdag(y,x)==-1 %bidirected edge
Daniel@0 119 pdag(x,y) = 2; pdag(y,x) = 2;
Daniel@0 120 else
Daniel@0 121 pdag(x,y) = -1;pdag(y,x) = 0;
Daniel@0 122 end
Daniel@0 123 end
Daniel@0 124 end
Daniel@0 125 end
Daniel@0 126 end
Daniel@0 127
Daniel@0 128
Daniel@0 129 %%%%%%%%%%%%%
Daniel@0 130
Daniel@0 131 function t = marked_path(x,y,L)
Daniel@0 132 % MARKED_PATH is a boolean function which returns 1 if a marked path
Daniel@0 133 % between nodes x and y exists in the partially directed latent structure L.
Daniel@0 134 %
Daniel@0 135 % t = marked_path(x,y,L)
Daniel@0 136 %
Daniel@0 137 % x and y are the starting and ending nodes in the path, respectively.
Daniel@0 138 % L is a latent structure (partially directed graph with possible latent variables).
Daniel@0 139 %
Daniel@0 140 % Rule 2 of IC* algorithm (see Pearl, 2000)
Daniel@0 141
Daniel@0 142 t=0;
Daniel@0 143
Daniel@0 144 %find set of marked links from x
Daniel@0 145 marked = find(L(x,:)==-2);
Daniel@0 146 if ismember(y,marked)
Daniel@0 147 t=1; %marked path found
Daniel@0 148 else
Daniel@0 149 for m=marked(:)'
Daniel@0 150 t = marked_path(m,y,L);
Daniel@0 151 if t==1
Daniel@0 152 break; %stop when marked path found
Daniel@0 153 end
Daniel@0 154 end
Daniel@0 155 end