Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to incrementally change the causal graph and learn the causal function only on the nodes affected in GCM? #1290

Closed
Yangliu-SY opened this issue Dec 10, 2024 · 2 comments
Labels
question Further information is requested

Comments

@Yangliu-SY
Copy link
Contributor

Yangliu-SY commented Dec 10, 2024

I use the functionality of attributing anomalies of a node to its parents and ancestors in GCM for root cause analysis. Sometimes, I need to add a new node to the graph and some edges between the new node and existing nodes. I would then like to learn the causal mechanism for the nodes affected by the change.
I have tried the following. I have an exiting causal graph and the associated causal model that has been learned. I modified the causal graph by adding the new node and associated new edges. I created a new causal model from the modified causal graph. I would like to copy the learned causal mechanisms in the existing causal model to the new causal model. Afterwards, I would learn the causal mechanisms on the nodes affected by the change in the causal graph.
But when I tried to assign the learned causal mechanism in the existing causal model to the new causal model, I received an error that said I couldn't do this assignment.

for node in causal_graph_exist.nodes:
    causal_model_new.causal_mechanism(node) = causal_model_exist.causal_mechanism(node).copy()

Is it possible to incrementally change the causal graph and learn the causal function only on the nodes affected? I looked up the documentation of DoWhy but I didn't find a way to achieve this. Let me know if you need more information.

  • DoWhy version 0.11.1
@Yangliu-SY Yangliu-SY added the question Further information is requested label Dec 10, 2024
@bloebp
Copy link
Member

bloebp commented Dec 16, 2024

Hi,

yes that is possible, you can use the functions on single nodes directly. Here is an example:

import networkx as nx
import numpy as np
import pandas as pd

import dowhy.gcm as gcm

X = np.random.normal(loc=0, scale=1, size=1000)
Y = 2 * X + np.random.normal(loc=0, scale=1, size=1000)
Z = 3 * Y + np.random.normal(loc=0, scale=1, size=1000)
data = pd.DataFrame(data=dict(X=X, Y=Y, Z=Z))

causal_model = gcm.StructuralCausalModel(nx.DiGraph([('X', 'Y'), ('Y', 'Z')]))
gcm.auto.assign_causal_mechanisms(causal_model, data)
gcm.fit(causal_model, data)

# Generate data for a new node 'W'
W = 2 * Z + X + np.random.normal(loc=0, scale=0.1, size=1000)
data["W"] = W

# Add the new node to the existing graph
causal_model.graph.add_edges_from([("Z", "W"), ("X", "W")])

# Automatically assign a model to the new node only (or assign one manually)
gcm.auto.assign_causal_mechanism_node(causal_model, "W", data, gcm.auto.AssignmentQuality.GOOD)

# Alternatively, we can also call the assign_causal_mechanisms function, but need to make sure that override_models is false
# gcm.auto.assign_causal_mechanisms(causal_model, data, override_models=False)

# Fit only the new node
gcm.fitting_sampling.fit_causal_model_of_target(causal_model, "W", data)

print(gcm.draw_samples(causal_model, 5).head())

@Yangliu-SY
Copy link
Contributor Author

Thanks a lot. I appreciate it very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants