-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nsample.py
106 lines (79 loc) · 2.94 KB
/
test_nsample.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
from pytorch_lightning import seed_everything
seed_everything(42)
import numpy as np
import h5py
from causaldag import partial_correlation_suffstat, partial_correlation_test, MemoizedCI_Tester, gsp, pcalg
from pc import pc_order_dep
from ccpg import ccpg
from data import synthetic_instance
nnodes = 10
model = synthetic_instance(nnodes, 1.0, True)
print(model.DAG.arcs)
for r in range(5):
# save a bunch of samples for Rstudio
for nsample in np.arange(100, 5000, 100):
samples = model.sample(nsample)
with h5py.File(f'simulate-data/nsample/samples_{nsample}_{r}.h5', "w") as f:
f.create_dataset("samples", data=samples)
print(f"-------------Run {r}----------------")
# ccpg
nsample = 100
while True:
samples = model.sample(nsample)
nsample += 100
suffstat = partial_correlation_suffstat(samples.T)
ci_tester = MemoizedCI_Tester(partial_correlation_test, suffstat, alpha=1e-3)
c, e = ccpg(set(range(nnodes)), ci_tester, verbose=False)
if max([len(i) for i in c]) > 1:
continue
else:
edges = set()
for (i,j) in e:
edges.add((list(c[i])[0],list(c[j])[0]))
if edges == model.DAG.arcs:
break
print('ccpg', nsample)
# pc_order_dep
nsample = 100
while True:
samples = model.sample(nsample)
nsample += 100
suffstat = partial_correlation_suffstat(samples.T)
ci_tester = MemoizedCI_Tester(partial_correlation_test, suffstat, alpha=1e-3)
est_dag = pc_order_dep(set(range(nnodes)), ci_tester)
if est_dag.arcs == model.DAG.arcs and est_dag.edges == set():
break
print('pc_order_dep', nsample)
# pc
nsample = 100
while True:
samples = model.sample(nsample)
nsample += 100
suffstat = partial_correlation_suffstat(samples.T)
ci_tester = MemoizedCI_Tester(partial_correlation_test, suffstat, alpha=1e-3)
est_dag = pcalg(set(range(nnodes)), ci_tester)
if est_dag.arcs == model.DAG.arcs and est_dag.edges == set():
break
print('pc', nsample)
# gsp
nsample = 100
while True:
samples = model.sample(nsample)
nsample += 100
suffstat = partial_correlation_suffstat(samples.T)
ci_tester = MemoizedCI_Tester(partial_correlation_test, suffstat, alpha=1e-3)
est_dag = gsp(set(range(nnodes)), ci_tester)
if est_dag.arcs == model.DAG.arcs:
break
print('gsp', nsample)
# gsp+
nsample = 100
while True:
samples = model.sample(nsample)
nsample += 100
suffstat = partial_correlation_suffstat(samples.T)
ci_tester = MemoizedCI_Tester(partial_correlation_test, suffstat, alpha=1e-3)
est_dag = gsp(set(range(nnodes)), ci_tester, depth=None, nruns=10)
if est_dag.arcs == model.DAG.arcs:
break
print('gsp+', nsample)