py-why / dowhy

DoWhy is a Python library for causal inference that supports explicit modeling and testing of causal assumptions. DoWhy is based on a unified language for causal inference, combining causal graphical models and potential outcomes frameworks.
https://www.pywhy.org/dowhy
MIT License
7k stars 924 forks source link

graph should be directed acyclic #1223

Closed asha24choudhary closed 1 week ago

asha24choudhary commented 2 months ago

Hi. I have a dataset and I do not have ground truth of the data. What I'm doing is causal discovery using FCI from causal learn library for instance and then I get an adjacency matrix like ([[ 0, 0, 0, 2, 0], [ 0, 0, 2, 0, 2], [ 0, 2, 0, 2, -1], [ 1, 0, 1, 0, -1], [ 0, 1, 1, 1, 0]]). then i create a networkx graph save the graph into .gml format (test.gml) and when i do

Create the dowhy Causal Model

        model = CausalModel(
            data=df,
            treatment=treatment,
            outcome=outcome,
            graph='test.gml'
        )

Identifying the causal effect

        identified_estimand = model.identify_effect()
        print(identified_estimand)

i get NetworkXError: graph should be directed acyclic. what should I do to get rid of this error.

I removed the graph as well and identified the effect, but can I rely on this effect or could you please tell me how shall i proceed. i want to determine the effect of the treatment over the outcome, can i use the graph from causal discovery algorithm or what shall i do?

Thank you in advance :)

github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 14 days with no activity.

amit-sharma commented 1 month ago

FCI outputs a Partial ancestral graph (PAG) whereas DoWhy expects a standard directed acyclic graph (DAG). You will need to convert your PAG to a simple graph with directed edges. Can you share a visualization of your graph?

github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 14 days with no activity.

asha24choudhary commented 1 month ago

Sure, this is the causal graph that I get.

image

This is how I generated the data

np.random.seed(11)

alpha = 0.5 beta = -5 delta = 0.4 gamma = -0.7

Z = np.random.normal(loc=1, size=500) T = alpha Z + np.random.normal(loc=2, size=500) M = beta T + np.random.normal(size=500) Y = delta Z + gamma M + np.random.normal(size=500) C = T + Y + np.random.normal(size=500)

df = pd.DataFrame(np.stack((Z, T, M, Y, C), axis=1), columns=['Z', 'T', 'M', 'Y', 'C'])

github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open for 14 days with no activity.

github-actions[bot] commented 1 week ago

This issue was closed because it has been inactive for 7 days since being marked as stale.