py-why / causal-learn

Causal Discovery in Python. It also includes (conditional) independence tests and score functions.
https://causal-learn.readthedocs.io/en/latest/
MIT License
1.04k stars 174 forks source link

How to efficiently assign forbidden edges by nodes? #142

Open kenneth-lee-ch opened 8 months ago

kenneth-lee-ch commented 8 months ago

I currently use the following code to add forbidden edges, but it's taking so long when the adjacency matrix (in the example below named partial_order) is large for adding background knowledge, is there any way to make this more efficient in the causal-learn package?

import numpy as np
nodenames = final_df.columns
bk= BackgroundKnowledge()
position_matrix = np.argwhere(partial_order == 1)
for coordinate in position_matrix:
   x, y = coordinate 
   node1 = nodenames[x]
   node2 = nodenames[y]
   bk.add_forbidden_by_node(GraphNode(node2), GraphNode(node1))
kunwuz commented 8 months ago

Hi, this improved version of BackGroundKnowledge might be helpful, which is proposed and implemented by @verae98. Also a related issue: #90

Not sure if the solution is 100% free of issues, but that could be great to try. Let me know if you have any better ideas, or would like to contribute perhaps by incorporating that solution :)