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