-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.py
521 lines (387 loc) · 9.82 KB
/
dao.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import numpy as np
from numpy.random import default_rng
from numpy.linalg import inv
from numpy.linalg import norm
from numpy.linalg import cholesky
from itertools import chain
# The DaO code in this file is a Python implementation of the DaO algorithm, taken from
# the following GitHub repository: https://github.com/bja43/DaO_simulation
# The original code was written by Bryan Andrews and is licensed under the MIT License.
#
# Andrews, B., & Kummerfeld, E. (2024). Better Simulations for Validating Causal Discovery with the
# DAG-Adaptation of the Onion Method. arXiv preprint arXiv:2405.13100.
def er_dag(p, d=0.5, ad=None, rng=default_rng()):
'''
Randomly generates an Erdos-Renyi (lower triangular) direct acyclic graph
given an ordering.
Parameters
----------
p = |variables|
d = |edges| / |possible edges| (ignored if ad is not None)
ad = average degree
rng = random number generator
Returns
-------
g = direct acyclic graph
'''
# npe = |possible edges|
npe = int(p * (p - 1) / 2)
# ne = |edges|
if ad is not None: d = ad / (p - 1)
ne = int(d * npe)
# generate edges
e = np.append(np.zeros(npe - ne, np.uint8), np.ones(ne, np.uint8))
rng.shuffle(e)
# generate graph
g = np.zeros([p, p], np.uint8)
g.T[np.triu_indices(p, 1)] = e
return g
def sf_out(g, rng=default_rng()):
'''
Rewires entries within rows-row sum (in-degree) doesnt change.
Parameters
----------
g = directed acyclic graph
rng = random number generator
Returns
-------
g = direct acyclic graph
'''
# p = |variables|
p = g.shape[0]
# reorder g if not lower triangular
nlt = any([g[i, j] for j in range(p) for i in range(j)])
if nlt:
ord = sofic_order(g)
g = g[ord][:, ord]
else:
g = g.copy()
for i in range(1, p):
J = [[j] for j in range(i)]
J += [[j] * int(np.sum(g[:i, j])) for j in range(i)]
J = list(chain.from_iterable(J))
rng.shuffle(J)
in_deg = np.sum(g[i])
g[i] = np.zeros(p)
for j in J:
if in_deg == 0: break
if g[i, j] == 0:
in_deg -= 1
g[i, j] = 1
# reorder g if not lower triangular
if nlt:
ord = invert_order(ord)
g = g[ord][:, ord]
return g
def sf_in(g, rng=default_rng()):
'''
Rewires entries within cols-col sum (out-degree) doesnt change.
Parameters
----------
g = directed acyclic graph
rng = random number generator
Returns
-------
g = direct acyclic graph
'''
# p = |variables|
p = g.shape[0]
# reorder g if not lower triangular
nlt = any([g[i, j] for j in range(p) for i in range(j)])
if nlt:
ord = sofic_order(g)
g = g[ord][:, ord]
else:
g = g.copy()
for i in range(1, p):
J = [[j] for j in range(i)]
J += [[j] * int(np.sum(g[p - j - 1, p - i:])) for j in range(i)]
J = list(chain.from_iterable(J))
rng.shuffle(J)
out_deg = np.sum(g[:, p - i - 1])
g[:, p - i - 1] = np.zeros(p)
for j in J:
if out_deg == 0: break
if g[p - j - 1, p - i - 1] == 0:
out_deg -= 1
g[p - j - 1, p - i - 1] = 1
# reorder g if not lower triangular
if nlt:
ord = invert_order(ord)
g = g[ord][:, ord]
return g
def num_source(g):
'''
Helper function: counts the number of source variables.
Parameters
-----------
g = directed acyclic graph
Returns:
--------
m = source count
'''
# p = |variables|
p = g.shape[0]
src = [i for i in range(p) if np.sum(g[i]) == 0]
return len(src)
def sofic_order(g):
'''
Helper function: returns a source first consistent order.
Parameters
-----------
g = directed acyclic graph
Returns:
--------
ord = order
'''
# p = |variables|
p = g.shape[0]
# convert g to booleans
g = g.astype(bool)
ord = [i for i in range(p) if np.sum(g[i]) == 0]
while len(ord) < p:
for i in range(p):
if i in ord: continue
if np.sum(g[i]) == np.sum(g[i][ord]):
ord.append(i)
break
if i == p - 1:
raise ValueError("cycle detected")
return ord
def invert_order(ord):
'''
Helper function: inverts the order.
Parameters
-----------
ord = order
Returns:
--------
inv_ord = inverse order
'''
# p = |variables|
p = len(ord)
inv_ord = [0 for i in range(p)]
for i in range(p): inv_ord[ord[i]] = i
return inv_ord
def mpii(g, i, rng=default_rng()):
'''
Helper function: samples a multivariate Pearson type II.
Parameters
-----------
g = directed acyclic graph
i = index
rng = random number generator
Returns:
--------
w = mpii sample
'''
# p = |variables|
p = g.shape[0]
# k = |parents|
k = np.sum(g[i])
# initialize w
w = np.zeros(i)
# update w
if k > 0:
q = rng.beta(k / 2, (p - i) / 2)
y = rng.standard_normal(k)
u = y / norm(y)
w[:k] = np.sqrt(q) * u
return w
def pmat(g, i):
'''
Helper function: returns a permutation matrix.
Parameters:
-----------
g = directed acyclic graph
i = index
Returns:
--------
P = permutation matrix
'''
P = np.zeros([i, i], dtype=np.uint8)
k = 0
for q in (1, 0):
for j in np.where(g[i, :i] == q)[0]:
P[j, k] = 1
k += 1
return P
def corr(g):
'''
Randomly generates a correlation matrix where f(R) ~ 1 given a direct
acyclic graph.
Parameters
----------
g = directed acyclic graph
Returns
-------
R = correlation matrix
B = beta matrix
O = error vector
'''
# reorder g
ord = sofic_order(g)
g = g[ord][:, ord]
# p = |variables|; m = |source variables|
p = g.shape[0]
m = num_source(g)
# initialize correlation / coefficient / error matrices
R = np.eye(p)
B = np.zeros([p, p])
O = np.ones(p)
for i in range(m, p):
P = pmat(g, i)
L = cholesky(P.T @ R[:i, :i] @ P)
w = mpii(g, i)
r = P @ L @ w
b = P @ inv(L).T @ w
o = 1 - np.sum(w * w)
R[:i, i] = r
R[i, :i] = r
B[i, :i] = b
O[i] = o
# reorder R, B, and O
ord = invert_order(ord)
R = R[ord][:, ord]
B = B[ord][:, ord]
O = O[ord]
return R, B, O
def cov(g, lb_b=0, ub_b=1, lb_o=1, ub_o=2, rng=default_rng()):
'''
Randomly generates a covariance matrix given a directed acyclic graph.
Parameters
----------
g = directed acyclic graph
lb_b = lower bound for beta
ub_b = upper bound for beta
lb_o = lower bound for omega
ub_o = upper bound for omega
rng = random number generator
Returns
-------
S = covariance matrix
B = beta matrix
O = error vector
'''
# p = |variables|
p = g.shape[0]
# reorder g
ord = sofic_order(g)
g = g[ord][:, ord]
# e = |edges|
e = np.sum(g)
# generate edge weights
B = np.zeros([p, p])
B[np.where(g)] = rng.choice([-1, 1], e) * rng.uniform(lb_b, ub_b, e)
# generate variance terms
O = rng.uniform(lb_o, ub_o, p)
# calculate covariance
IB = inv(np.eye(p) - B)
S = IB @ np.diag(O) @ IB.T
# reorder S, B, and O
ord = invert_order(ord)
S = S[ord][:, ord]
B = B[ord][:, ord]
O = O[ord]
return S, B, O
def simulate(B, O, n, err=None, rng=default_rng()):
'''
Randomly simulates data with the provided parameters.
Parameters
----------
B = beta matrix
O = error vector
n = sample size
err = error distribution
rng = random number generator
Returns
-------
X = data
'''
# p = |variables|
p = B.shape[0]
# reorder B and O
ord = sofic_order(B)
B = B[ord][:, ord]
O = O[ord]
# set default error as normal
if err is None: err = lambda *x: rng.normal(0, np.sqrt(x[0]), x[1])
# simulate data
X = np.zeros([n, p])
for i in range(p):
# parents
J = np.where(B[i])[0]
# linear effect
for j in J: X[:, i] += B[i, j] * X[:, j]
# add error
X[:, i] += err(O[i], n)
# reorder X
ord = invert_order(ord)
X = X[:, ord]
return X
def standardize(X):
'''
Standardizes the data.
Parameters
----------
X = data
Returns
-------
X = data
'''
return (X - X.mean(0)) / X.std(0)
def randomize_graph(g, rng=default_rng()):
'''
Randomly reorders the variables of the graph.
Parameters
----------
g = directed acyclic graph
rng = random number generator
Returns
-------
g = directed acyclic graph
'''
# p = |variables|
p = g.shape[0]
# random reorder g
pi = [i for i in range(p)]
rng.shuffle(pi)
return g[pi][:, pi]
def cov_to_corr(S):
'''
Rescales covariance to correlation.
Parameters
----------
S = covariance matrix
Returns
-------
R = correlation matrix
'''
D = np.diag(np.sqrt(np.diag(S)))
ID = inv(D)
return ID @ S @ ID
def cov_to_dag(g, S):
'''
Converts covariance to directed acyclic graph parameters.
Parameters
----------
g = directed acyclic graph
S = covariance matrix
Returns
-------
B = beta matrix
O = error vector
'''
# p = |variables|
p = S.shape[0]
B = np.zeros((p, p))
O = np.diag(S)
for i in range(p):
pa = np.where(g[i])[0]
if len(pa) > 0:
yX = S[i, pa]
XX = S[np.ix_(pa, pa)]
IXX = inv(XX)
B[i, pa] = yX @ IXX
O[i] -= yX @ IXX @ yX
return B, O