wolffd@0: function [pdag, G] = learn_struct_pdag_pc(cond_indep, n, k, varargin) wolffd@0: % LEARN_STRUCT_PDAG_PC Learn a partially oriented DAG (pattern) using the PC algorithm wolffd@0: % P = learn_struct_pdag_pc(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 an i->j edge. wolffd@0: % P(i,j) = P(j,i) = 1 if there is an undirected edge i <-> j wolffd@0: % wolffd@0: % The PC algorithm does structure learning assuming all variables are observed. wolffd@0: % See Spirtes, Glymour and Scheines, "Causation, Prediction and Search", 1993, p117. wolffd@0: % This algorithm may take O(n^k) time if there are n variables and k is the max fan-in, wolffd@0: % but this is quicker than the Verma-Pearl IC algorithm, which is always O(n^n). wolffd@0: 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: nbrs = mysetdiff(neighbors(G, y), x); % bug fix by Raanan Yehezkel 6/27/04 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: SS = subsets1(nbrs, 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: %if isempty(S) wolffd@0: % fprintf('%d indep of %d ', x, y); wolffd@0: %else wolffd@0: % fprintf('%d indep of %d given ', x, y); fprintf('%d ', S); wolffd@0: %end wolffd@0: %fprintf('\n'); wolffd@0: wolffd@0: % diagnostic wolffd@0: %[CI, r] = cond_indep_fisher_z(x, y, S, varargin{:}); wolffd@0: %fprintf(': r = %6.4f\n', r); wolffd@0: 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: 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: % This code generates x,y,z and z,y,x. 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: %fprintf('%d -> %d <- %d\n', x, y, z); 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, wolffd@0: % i.e., every directed edge in P is compelled wolffd@0: % (must be directed in all Markov equivalent models), wolffd@0: % and every undirected edge in P is reversible. wolffd@0: % We use the rules of Pearl (2000) p51 (derived in Meek (1995)) wolffd@0: 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: [A,B] = find(pdag==-1); % a -> b wolffd@0: for i=1:length(A) wolffd@0: a = A(i); b = B(i); wolffd@0: C = find(pdag(b,:)==1 & G(a,:)==0); % all nodes adj to b but not a wolffd@0: if ~isempty(C) wolffd@0: pdag(b,C) = -1; pdag(C,b) = 0; wolffd@0: %fprintf('rule 1: a=%d->b=%d and b=%d-c=%d implies %d->%d\n', a, b, b, C, b, C); wolffd@0: end wolffd@0: end wolffd@0: % rule 2 wolffd@0: [A,B] = find(pdag==1); % unoriented a-b edge wolffd@0: for i=1:length(A) wolffd@0: a = A(i); b = B(i); wolffd@0: if any( (pdag(a,:)==-1) & (pdag(:,b)==-1)' ); wolffd@0: pdag(a,b) = -1; pdag(b,a) = 0; wolffd@0: %fprintf('rule 2: %d -> %d\n', a, b); wolffd@0: end wolffd@0: end wolffd@0: % rule 3 wolffd@0: [A,B] = find(pdag==1); % a-b wolffd@0: for i=1:length(A) wolffd@0: a = A(i); b = B(i); wolffd@0: C = find( (pdag(a,:)==1) & (pdag(:,b)==-1)' ); wolffd@0: % C contains nodes c s.t. a-c->ba wolffd@0: G2 = setdiag(G(C, C), 1); wolffd@0: if any(G2(:)==0) % there are 2 different non adjacent elements of C wolffd@0: pdag(a,b) = -1; pdag(b,a) = 0; wolffd@0: %fprintf('rule 3: %d -> %d\n', a, b); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: