import numpy as np
import matplotlib.pyplot as plt
from causallearn.search.ConstraintBased.PC import pc
def simulate_data(n_obs):
'''
Simulate data from the following graph
A B
\ /
v v
C
/ \
v v
D E
'''
A = np.random.normal(size = n_obs)
B = np.random.normal(size = n_obs)
C = A + B + np.random.normal(size = n_obs)*0.25
D = C + np.random.normal(size = n_obs)*0.25
E = C + np.random.normal(size = n_obs)*0.25
return np.stack((A,B,C,D,E), axis =1)
# generate data
n = 10000
data = simulate_data(n)
# test different permutations
permutations = [[0,1,2,3,4], [0,1,3,2,4]]
for permutation in permutations:
graph = pc(data[:,permutation], 0.05, 'fisherz', node_names = np.array(["A","B", "C", "D", "E"])[permutation], verbose = False)
graph.draw_pydot_graph(labels=np.array(["A","B", "C", "D", "E"])[permutation])
plt.show()
When the ordering of variables C and D is permuted, the PC algorithm returns the Graph with an undirected edge from C to D. However, when the ordering is unpermuted, the PC algorithm correctly directs the edge from C to D. This happens, even though the p-values in the CI tests are unchanged for the two permutations.
Is this expected behaviour or can you help me fix this?