-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMB_by_MMB.py
232 lines (205 loc) · 9.76 KB
/
MMB_by_MMB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import os
import numpy as np
from Utils.CI_test import CI_Test
from Utils.zhang_orient import orient_rules
from Utils.MMB_TC_Z import MMB_TC
from Utils.subsets1 import subsets1
class MMB_by_MMB:
def __init__(self,Data, target, alpha, p, maxK, verbose = False):
self.data = Data
self.target = target
self.alpha = alpha
self.p = p
self.maxK = maxK
self.verbose = verbose
self.all_test = 0
self.adj_test = 0
self.mmb_test = 0
self.pag = np.zeros((p, p), dtype=int) # 1 '>'; -1 '-'; 2 'o'; 0 NULL
# i --> j pag[i, j] = 1, pag[j, i] = -1
# i o-> j pag[i, j] = 1, pag[j, i] = 2
# i o-o j pag[i, j] = 2, pag[j, i] = 2
# i <-> j pag[i, j] = 1, pag[j, i] = 1
self.is_mmb = np.zeros((p, p), dtype=int) # is: 1 ,not:-1, not know:0
self.all_adj = np.empty(p, dtype=object)
self.all_MMB = np.empty(p, dtype=object)
self.all_not_adj_in_MMB = np.empty(p, dtype=object)
self.all_sepset = np.empty((p, p), dtype=object)
for i in range(p):
self.all_adj[i] = []
self.all_MMB[i] = []
self.all_not_adj_in_MMB[i] = []
self.done_MMB = np.full(p, False, dtype=bool)
self.find_edge = np.full((p,p), False, dtype=bool)
self.waitlist = [target]
self.donelist = []
def find_mmb(self,A):
if self.done_MMB[A]:
raise ValueError('had found MMB of A')
self.all_MMB[A], ntest1, self.is_mmb = MMB_TC(self.data, A, self.alpha, self.is_mmb)
not_MMB_A = np.where(self.is_mmb[A, :] == -1)[0]
#------The nodes in T and the nodes not in T of MMB are mutually independent, given MMB of T.
for no_mmb in not_MMB_A:
if self.all_sepset[A, no_mmb] == None and self.all_sepset[no_mmb, A] == None:
self.all_sepset[A, no_mmb] = self.all_MMB[A]
self.all_sepset[no_mmb, A] = self.all_MMB[A]
self.find_edge[A, no_mmb] = True
self.find_edge[no_mmb, A] = True
self.all_test += ntest1
self.mmb_test += ntest1
self.done_MMB[A] = True
if self.verbose: print(f"MMB of {A} : {self.all_MMB[A]}, CI test num:{ntest1}")
def find_adj_from_MMB(self, A):
# Find adjacent nodes from MMB
self.all_adj[A] = self.all_MMB[A]
tmp_adj = self.all_adj[A]
for B in tmp_adj:
if self.find_edge[A, B]:
if self.pag[A, B] == 0 and self.all_sepset[A, B] != None:
self.all_adj[A] = np.setdiff1d(self.all_adj[A], B)
self.all_not_adj_in_MMB[A].append(B)
sepSetSize = 0
while (len(self.all_adj[A])-1) >= sepSetSize and sepSetSize <= self.maxK:
for B in self.all_MMB[A]:
if self.find_edge[A, B]:
continue
self.pag[A, B] = 2
self.pag[B, A] = 2
tmp_set = np.setdiff1d(self.all_adj[A], B)
tmp_sepsets = subsets1(tmp_set, sepSetSize)
for i in range(len(tmp_sepsets)):
sepset = tmp_sepsets[i]
CI, p_value = CI_Test(B, A, sepset, self.data, self.alpha)
self.all_test += 1
self.adj_test += 1
if CI: # CI=True means B and A are independent and not adjacent
self.all_sepset[A, B] = sepset
self.all_sepset[B, A] = sepset
self.pag[A, B] = 0
self.pag[B, A] = 0
self.find_edge[A, B] = True
self.find_edge[B, A] = True
self.all_adj[A] = np.setdiff1d(self.all_adj[A], B)
self.all_not_adj_in_MMB[A].append(B)
break
sepSetSize += 1
for B in self.all_MMB[A]:
if not self.find_edge[A, B]:
self.find_edge[A, B] = True
self.find_edge[B, A] = True
# find S1 V-structure: B1 -> A <- B2
if len(self.all_adj[A]) >= 2 :
for i in range(0, len(self.all_adj[A]) - 1):
B1 = self.all_adj[A][i]
for j in range(i + 1, len(self.all_adj[A])):
B2 = self.all_adj[A][j]
if self.pag[B1, A] == 1 and self.pag[B2, A] == 1:
continue
if self.find_edge[B1, B2] and self.find_edge[B2, B1]:
if (self.pag[B1, B2] == 0) and (self.pag[B2, B1] == 0):
if A not in self.all_sepset[B1, B2]:
self.pag[B1, A] = 1
self.pag[B2, A] = 1
if self.verbose: print(f"find V-:{B1}*->{A}<-*{B2}")
def stop_condition_three(self, T, maxdepth=5, done=None, depth=1):
if depth > maxdepth:
return False
if done is None and depth == 1:
done = list(np.where((self.pag[T, :] != 2) & (self.pag[:, T] != 2) & (self.pag[T, :] != 0) & (self.pag[:, T] != 0))[0])
if T not in done: done.append(T)
adj_T = np.where(self.pag[T, :] != 0)[0]
adj_T = np.setdiff1d(adj_T, done)
adj_T = list(adj_T)
if len(adj_T) == 0:
if T in self.donelist:
return True
else:
return False
all_break_T = np.full(len(adj_T), False, dtype=bool)
for i in range(len(adj_T)):
if self.pag[T, adj_T[i]] == 1:
all_break_T[i] = True
if np.all(all_break_T): # If the current adj node has been blocked, all paths of T passing through the adj will be blocked.
return True
else:
for i in range(len(adj_T)):
if not all_break_T[i]:
all_break_T[i] = self.stop_condition_three(int(adj_T[i]), done=done, depth=depth+1)
if np.all(all_break_T):
return True
else:
return False
def find_trueV_structure(self,A):
if not self.done_MMB[A]:
raise ValueError('not found MMB of A')
# find S2 V-structure:A -> B <- C
for C in self.all_not_adj_in_MMB[A]:
for B in self.all_adj[A]:
if self.pag[A, B] == 1 and self.pag[C, B] == 1:
continue
if not self.find_edge[B, C]:
if not self.done_MMB[B]:
self.find_mmb(B)
self.find_adj_from_MMB(B)
adj = (self.pag[B, C] != 0)
else:
if C not in self.all_MMB[B]:
adj = False
else:
adj = (self.pag[B, C] != 0)
else:
adj = (self.pag[B, C] != 0)
if self.pag[A, B] == 1 and self.pag[C, B] == 1:
continue
if (B not in self.all_sepset[A, C]) and (adj):
self.pag[A, B] = 1
self.pag[C, B] = 1
if self.pag[B, C] == 0:
self.pag[B, C] = 2
self.find_edge[B, C] = True
self.find_edge[C, B] = True
if self.verbose:print(f"find V- in target({A}) :{A}*->{B}<-*{C}")
def mmb_by_mmb(self,meek_verbose = False):
num_calculated = 0
while len(self.donelist) <= self.p and len(self.waitlist) > 0: # stop two
A = self.waitlist.pop(0)
if A in self.donelist:
continue
else:
self.donelist.append(A)
if not self.done_MMB[A]:
self.find_mmb(A)
self.find_adj_from_MMB(A)
for B in self.all_MMB[A]:
if B not in self.waitlist and B not in self.donelist:
self.waitlist.append(B)
# find true V-structures S2
self.find_trueV_structure(A)
self.pag = orient_rules(self.pag,self.all_sepset,self.find_edge,detail_out=meek_verbose)
num_calculated += 1
if num_calculated > len(self.all_adj[self.target]):
if 2 not in self.pag[self.target, :] and 2 not in self.pag[:, self.target]: # stop one
if self.verbose: print("stop one")
break
if self.stop_condition_three(self.target):
if self.verbose: print("stop three")
break
P = np.where(np.logical_and(self.pag[:, self.target] == 1, self.pag[self.target, :] == -1))[0]
C = np.where(np.logical_and(self.pag[self.target, :] == 1, self.pag[:, self.target] == -1))[0]
dis_depth1 = np.where(np.logical_and(self.pag[self.target, :] == 1, self.pag[:, self.target] == 1))[0]
un = np.where((self.pag[self.target, :] == 2) | (self.pag[:, self.target] == 2))[0]
ci_test = self.all_test
return P, C, dis_depth1, un, ci_test
if __name__ == '__main__':
data_name = 'mildew'
maxK = 10 # the maximal degree of any variable
alpha = 0.01
target = 26
num_samples = 5000
data_path = os.path.join(r'example_data', f'{data_name}_{num_samples}_1.csv')
if not os.path.exists(data_path):
print(f'\n{data_path} does not exist.\n\n')
data_matrix = np.loadtxt(data_path, delimiter=',')
learn_graph = MMB_by_MMB(data_matrix, target, alpha, data_matrix.shape[1], maxK)
P, C, dis_depth1, un, ci_test = learn_graph.mmb_by_mmb()
print(f"target {target}'s parent nodes: {P}, child nodes: {C}, ci_test: {ci_test}")