Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/learning/learn_struct_pdag_pc.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function [pdag, G] = learn_struct_pdag_pc(cond_indep, n, k, varargin) | |
2 % LEARN_STRUCT_PDAG_PC Learn a partially oriented DAG (pattern) using the PC algorithm | |
3 % P = learn_struct_pdag_pc(cond_indep, n, k, ...) | |
4 % | |
5 % n is the number of nodes. | |
6 % k is an optional upper bound on the fan-in (default: n) | |
7 % cond_indep is a boolean function that will be called as follows: | |
8 % feval(cond_indep, x, y, S, ...) | |
9 % where x and y are nodes, and S is a set of nodes (positive integers), | |
10 % and ... are any optional parameters passed to this function. | |
11 % | |
12 % The output P is an adjacency matrix, in which | |
13 % P(i,j) = -1 if there is an i->j edge. | |
14 % P(i,j) = P(j,i) = 1 if there is an undirected edge i <-> j | |
15 % | |
16 % The PC algorithm does structure learning assuming all variables are observed. | |
17 % See Spirtes, Glymour and Scheines, "Causation, Prediction and Search", 1993, p117. | |
18 % This algorithm may take O(n^k) time if there are n variables and k is the max fan-in, | |
19 % but this is quicker than the Verma-Pearl IC algorithm, which is always O(n^n). | |
20 | |
21 | |
22 sep = cell(n,n); | |
23 ord = 0; | |
24 done = 0; | |
25 G = ones(n,n); | |
26 G=setdiag(G,0); | |
27 while ~done | |
28 done = 1; | |
29 [X,Y] = find(G); | |
30 for i=1:length(X) | |
31 x = X(i); y = Y(i); | |
32 %nbrs = mysetdiff(myunion(neighbors(G, x), neighbors(G,y)), [x y]); | |
33 nbrs = mysetdiff(neighbors(G, y), x); % bug fix by Raanan Yehezkel <raanany@ee.bgu.ac.il> 6/27/04 | |
34 if length(nbrs) >= ord & G(x,y) ~= 0 | |
35 done = 0; | |
36 %SS = subsets(nbrs, ord, ord); % all subsets of size ord | |
37 SS = subsets1(nbrs, ord); | |
38 for si=1:length(SS) | |
39 S = SS{si}; | |
40 if feval(cond_indep, x, y, S, varargin{:}) | |
41 %if isempty(S) | |
42 % fprintf('%d indep of %d ', x, y); | |
43 %else | |
44 % fprintf('%d indep of %d given ', x, y); fprintf('%d ', S); | |
45 %end | |
46 %fprintf('\n'); | |
47 | |
48 % diagnostic | |
49 %[CI, r] = cond_indep_fisher_z(x, y, S, varargin{:}); | |
50 %fprintf(': r = %6.4f\n', r); | |
51 | |
52 G(x,y) = 0; | |
53 G(y,x) = 0; | |
54 sep{x,y} = myunion(sep{x,y}, S); | |
55 sep{y,x} = myunion(sep{y,x}, S); | |
56 break; % no need to check any more subsets | |
57 end | |
58 end | |
59 end | |
60 end | |
61 ord = ord + 1; | |
62 end | |
63 | |
64 | |
65 % Create the minimal pattern, | |
66 % i.e., the only directed edges are V structures. | |
67 pdag = G; | |
68 [X, Y] = find(G); | |
69 % We want to generate all unique triples x,y,z | |
70 % This code generates x,y,z and z,y,x. | |
71 for i=1:length(X) | |
72 x = X(i); | |
73 y = Y(i); | |
74 Z = find(G(y,:)); | |
75 Z = mysetdiff(Z, x); | |
76 for z=Z(:)' | |
77 if G(x,z)==0 & ~ismember(y, sep{x,z}) & ~ismember(y, sep{z,x}) | |
78 %fprintf('%d -> %d <- %d\n', x, y, z); | |
79 pdag(x,y) = -1; pdag(y,x) = 0; | |
80 pdag(z,y) = -1; pdag(y,z) = 0; | |
81 end | |
82 end | |
83 end | |
84 | |
85 % Convert the minimal pattern to a complete one, | |
86 % i.e., every directed edge in P is compelled | |
87 % (must be directed in all Markov equivalent models), | |
88 % and every undirected edge in P is reversible. | |
89 % We use the rules of Pearl (2000) p51 (derived in Meek (1995)) | |
90 | |
91 old_pdag = zeros(n); | |
92 iter = 0; | |
93 while ~isequal(pdag, old_pdag) | |
94 iter = iter + 1; | |
95 old_pdag = pdag; | |
96 % rule 1 | |
97 [A,B] = find(pdag==-1); % a -> b | |
98 for i=1:length(A) | |
99 a = A(i); b = B(i); | |
100 C = find(pdag(b,:)==1 & G(a,:)==0); % all nodes adj to b but not a | |
101 if ~isempty(C) | |
102 pdag(b,C) = -1; pdag(C,b) = 0; | |
103 %fprintf('rule 1: a=%d->b=%d and b=%d-c=%d implies %d->%d\n', a, b, b, C, b, C); | |
104 end | |
105 end | |
106 % rule 2 | |
107 [A,B] = find(pdag==1); % unoriented a-b edge | |
108 for i=1:length(A) | |
109 a = A(i); b = B(i); | |
110 if any( (pdag(a,:)==-1) & (pdag(:,b)==-1)' ); | |
111 pdag(a,b) = -1; pdag(b,a) = 0; | |
112 %fprintf('rule 2: %d -> %d\n', a, b); | |
113 end | |
114 end | |
115 % rule 3 | |
116 [A,B] = find(pdag==1); % a-b | |
117 for i=1:length(A) | |
118 a = A(i); b = B(i); | |
119 C = find( (pdag(a,:)==1) & (pdag(:,b)==-1)' ); | |
120 % C contains nodes c s.t. a-c->ba | |
121 G2 = setdiag(G(C, C), 1); | |
122 if any(G2(:)==0) % there are 2 different non adjacent elements of C | |
123 pdag(a,b) = -1; pdag(b,a) = 0; | |
124 %fprintf('rule 3: %d -> %d\n', a, b); | |
125 end | |
126 end | |
127 end | |
128 | |
129 |