Daniel@0: function [pdag, G] = learn_struct_pdag_ic_star(cond_indep, n, k, varargin) Daniel@0: % LEARN_STRUCT_PDAG_IC_STAR Learn a partially oriented DAG (pattern) with latent Daniel@0: % variables using the IC* algorithm Daniel@0: % P = learn_struct_pdag_ic_star(cond_indep, n, k, ...) Daniel@0: % Daniel@0: % n is the number of nodes. Daniel@0: % k is an optional upper bound on the fan-in (default: n) Daniel@0: % cond_indep is a boolean function that will be called as follows: Daniel@0: % feval(cond_indep, x, y, S, ...) Daniel@0: % where x and y are nodes, and S is a set of nodes (positive integers), Daniel@0: % and ... are any optional parameters passed to this function. Daniel@0: % Daniel@0: % The output P is an adjacency matrix, in which Daniel@0: % P(i,j) = -1 if there is either a latent variable L such that i <-L-> j Daniel@0: % OR there is a directed edge from i->j. Daniel@0: % P(i,j) = -2 if there is a marked directed i-*>j edge. Daniel@0: % P(i,j) = P(j,i) = 1 if there is and undirected edge i--j Daniel@0: % P(i,j) = P(j,i) = 2 if there is a latent variable L such that i<-L->j. Daniel@0: % Daniel@0: % The IC* algorithm learns a latent structure associated with a set of observed Daniel@0: % variables. Daniel@0: % The latent structure revealed is the projection in which every latent variable is Daniel@0: % 1) a root node Daniel@0: % 2) linked to exactly two observed variables. Daniel@0: % Latent variables in the projection are represented using a bidirectional graph, Daniel@0: % and thus remain implicit. Daniel@0: % Daniel@0: % See Pearl, "Causality: Models, Reasoning, and Inference", 2000, p52 for more details. Daniel@0: % Written by Tamar Kushnir, 2000 Daniel@0: Daniel@0: sep = cell(n,n); Daniel@0: ord = 0; Daniel@0: done = 0; Daniel@0: G = ones(n,n); Daniel@0: G = setdiag(G,0); Daniel@0: while ~done Daniel@0: done = 1; Daniel@0: [X,Y] = find(G); Daniel@0: for i=1:length(X) Daniel@0: x = X(i); y = Y(i); Daniel@0: nbrs = mysetdiff(myunion(neighbors(G, x), neighbors(G,y)), [x y]); Daniel@0: if length(nbrs) >= ord & G(x,y) ~= 0 Daniel@0: done = 0; Daniel@0: SS = subsets(nbrs, ord, ord); % all subsets of size ord Daniel@0: for si=1:length(SS) Daniel@0: S = SS{si}; Daniel@0: if feval(cond_indep, x, y, S, varargin{:}) Daniel@0: G(x,y) = 0; Daniel@0: G(y,x) = 0; Daniel@0: sep{x,y} = myunion(sep{x,y}, S); Daniel@0: sep{y,x} = myunion(sep{y,x}, S); Daniel@0: break; % no need to check any more subsets Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: ord = ord + 1; Daniel@0: end Daniel@0: Daniel@0: % Create the minimal pattern, Daniel@0: % i.e., the only directed edges are V structures. Daniel@0: pdag = G; Daniel@0: [X, Y] = find(G); Daniel@0: % We want to generate all unique triples x,y,z Daniel@0: % where y is a common neighbor to x and z Daniel@0: for i=1:length(X) Daniel@0: x = X(i); Daniel@0: y = Y(i); Daniel@0: Z = find(G(y,:)); Daniel@0: Z = mysetdiff(Z, x); Daniel@0: for z=Z(:)' Daniel@0: if G(x,z)==0 & ~ismember(y, sep{x,z}) & ~ismember(y, sep{z,x}) Daniel@0: pdag(x,y) = -1; pdag(y,x) = 0; Daniel@0: pdag(z,y) = -1; pdag(y,z) = 0; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % Convert the minimal pattern to a complete one using the following rules: Daniel@0: % Rule 1: Daniel@0: % if a and b are non-adjacent nodes with a common neighbor c, Daniel@0: % if a->c and not b->c then c-*>b (marked arrow). Daniel@0: % Rule 2: Daniel@0: % if a and b are adjacent and there is a directed path (marked links) from a to b Daniel@0: % then a->b (add arrowhead). Daniel@0: %Pearl (2000) Daniel@0: Daniel@0: arrowin = [-1 -2 2]; Daniel@0: old_pdag = zeros(n); Daniel@0: iter = 0; Daniel@0: while ~isequal(pdag, old_pdag) Daniel@0: iter = iter + 1; Daniel@0: old_pdag = pdag; Daniel@0: % rule 1 Daniel@0: [X, Y] = find(pdag); Daniel@0: for i=1:length(X) Daniel@0: x = X(i); Daniel@0: y = Y(i); Daniel@0: Z = find(pdag(y,:)); Daniel@0: Z = mysetdiff(Z, x); Daniel@0: for z=Z(:)' Daniel@0: if G(x,z)==0 & ismember(pdag(x,y),arrowin) & ~ismember(pdag(z,y),arrowin) Daniel@0: pdag(y,z) = -2; pdag(z,y) = 0; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: % rule 2 Daniel@0: [X, Y] = find(G); Daniel@0: %check all adjacent nodes because if pdag(x,y) = -1 Daniel@0: %and pdag(y,x) = 0 there could still be an bidirected edge between x & y. Daniel@0: for i=1:length(X) Daniel@0: x = X(i); Daniel@0: y = Y(i); Daniel@0: if ~ismember(pdag(x,y), arrowin) %x->y doesn't exist yet Daniel@0: %find marked path from x to y Daniel@0: add_arrow = marked_path(x,y,pdag); Daniel@0: if add_arrow Daniel@0: if pdag(y,x)==-1 %bidirected edge Daniel@0: pdag(x,y) = 2; pdag(y,x) = 2; Daniel@0: else Daniel@0: pdag(x,y) = -1;pdag(y,x) = 0; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: %%%%%%%%%%%%% Daniel@0: Daniel@0: function t = marked_path(x,y,L) Daniel@0: % MARKED_PATH is a boolean function which returns 1 if a marked path Daniel@0: % between nodes x and y exists in the partially directed latent structure L. Daniel@0: % Daniel@0: % t = marked_path(x,y,L) Daniel@0: % Daniel@0: % x and y are the starting and ending nodes in the path, respectively. Daniel@0: % L is a latent structure (partially directed graph with possible latent variables). Daniel@0: % Daniel@0: % Rule 2 of IC* algorithm (see Pearl, 2000) Daniel@0: Daniel@0: t=0; Daniel@0: Daniel@0: %find set of marked links from x Daniel@0: marked = find(L(x,:)==-2); Daniel@0: if ismember(y,marked) Daniel@0: t=1; %marked path found Daniel@0: else Daniel@0: for m=marked(:)' Daniel@0: t = marked_path(m,y,L); Daniel@0: if t==1 Daniel@0: break; %stop when marked path found Daniel@0: end Daniel@0: end Daniel@0: end