使用ID算法识别效果#

这是一个关于在因果推理的因果识别步骤中使用ID算法的教程笔记本。

论文链接:https://ftp.cs.ucla.edu/pub/stat_ser/shpitser-thesis.pdf 伪代码已在第40页提供。

[1]:
from dowhy import CausalModel
import pandas as pd
import numpy as np
from IPython.display import Image, display

示例#

以下部分展示了ID算法在多个测试案例中的工作情况。在图中,T表示处理变量,Y表示结果变量,Xs是额外的变量。

案例 1#

此示例展示了算法在最简单图上的性能。

[2]:
# Random data
treatment = "T"
outcome = "Y"
causal_graph = "digraph{T->Y;}"
columns = list(treatment) + list(outcome)
df = pd.DataFrame(columns=columns)

# Causal Model Initialization
causal_model = CausalModel(df, treatment, outcome, graph=causal_graph)

# View graph
causal_model.view_model()
from IPython.display import Image, display
print("Graph:")
display(Image(filename="causal_model.png"))

# Causal Identification using the ID Algorithm
identified_estimand = causal_model.identify_effect(method_name="id-algorithm")
print("\nResult for identification using ID Algorithm:")
print(identified_estimand)

../_images/example_notebooks_identifying_effects_using_id_algorithm_4_0.png
Graph:
../_images/example_notebooks_identifying_effects_using_id_algorithm_4_2.png

Result for identification using ID Algorithm:
Predictor: P(Y|T)

案例 2#

此示例展示了算法在循环图上的性能。此示例表明ID算法需要的是有向无环图(DAG)。

[3]:
# Random data
treatment = "T"
outcome = "Y"
causal_graph = "digraph{T->Y; Y->T;}"
columns = list(treatment) + list(outcome)
df = pd.DataFrame(columns=columns)

# Causal Model Initialization
causal_model = CausalModel(df, treatment, outcome, graph=causal_graph)

# View graph
causal_model.view_model()
from IPython.display import Image, display
print("Graph:")
display(Image(filename="causal_model.png"))

try:
    # Causal Identification using the ID Algorithm
    identified_estimand = causal_model.identify_effect(method_name="id-algorithm")
    print("\nResult for identification using ID Algorithm:")
    print(identified_estimand)
except:
    print("Identification Failed: The graph must be a directed acyclic graph (DAG).")

../_images/example_notebooks_identifying_effects_using_id_algorithm_6_0.png
Graph:
../_images/example_notebooks_identifying_effects_using_id_algorithm_6_2.png
Identification Failed: The graph must be a directed acyclic graph (DAG).

案例 3#

这个例子展示了算法在存在中介变量(X1)时的表现。

[4]:
# Random data
treatment = "T"
outcome = "Y"
variables = ["X1"]
causal_graph = "digraph{T->X1;X1->Y;}"
columns = list(treatment) + list(outcome) + list(variables)
df = pd.DataFrame(columns=columns)

# Causal Model Initialization
causal_model = CausalModel(df, treatment, outcome, graph=causal_graph)

# View graph
causal_model.view_model()
from IPython.display import Image, display
print("Graph:")
display(Image(filename="causal_model.png"))

# Causal Identification using the ID Algorithm
identified_estimand = causal_model.identify_effect(method_name="id-algorithm")
print("\nResult for identification using ID Algorithm:")
print(identified_estimand)

../_images/example_notebooks_identifying_effects_using_id_algorithm_8_0.png
Graph:
../_images/example_notebooks_identifying_effects_using_id_algorithm_8_2.png

Result for identification using ID Algorithm:
Sum over {X1}:
        Predictor: P(X1|T)
        Predictor: P(Y|T,X1)

案例 4#

该示例展示了算法在存在从TY的直接和间接路径(通过X1)时的性能。

[5]:
# Random data
treatment = "T"
outcome = "Y"
variables = ["X1"]
causal_graph = "digraph{T->Y;T->X1;X1->Y;}"
columns = list(treatment) + list(outcome) + list(variables)
df = pd.DataFrame(columns=columns)

# Causal Model Initialization
causal_model = CausalModel(df, treatment, outcome, graph=causal_graph)

# View graph
causal_model.view_model()
from IPython.display import Image, display
print("Graph:")
display(Image(filename="causal_model.png"))

# Causal Identification using the ID Algorithm
identified_estimand = causal_model.identify_effect(method_name="id-algorithm")
print("\nResult for identification using ID Algorithm:")
print(identified_estimand)

../_images/example_notebooks_identifying_effects_using_id_algorithm_10_0.png
Graph:
../_images/example_notebooks_identifying_effects_using_id_algorithm_10_2.png

Result for identification using ID Algorithm:
Sum over {X1}:
        Predictor: P(Y|T,X1)
        Predictor: P(X1|T)

案例 5#

这个例子展示了算法在存在混淆变量(X1)和工具变量(X2)时的表现。

[6]:
# Random data
treatment = "T"
outcome = "Y"
variables = ["X1", "X2"]
causal_graph = "digraph{T->Y;X1->T;X1->Y;X2->T;}"
columns = list(treatment) + list(outcome) + list(variables)
df = pd.DataFrame(columns=columns)

# Causal Model Initialization
causal_model = CausalModel(df, treatment, outcome, graph=causal_graph)

# View graph
causal_model.view_model()
from IPython.display import Image, display
print("Graph:")
display(Image(filename="causal_model.png"))

# Causal Identification using the ID Algorithm
identified_estimand = causal_model.identify_effect(method_name="id-algorithm")
print("\nResult for identification using ID Algorithm:")
print(identified_estimand)

../_images/example_notebooks_identifying_effects_using_id_algorithm_12_0.png
Graph:
../_images/example_notebooks_identifying_effects_using_id_algorithm_12_2.png

Result for identification using ID Algorithm:
Sum over {X1}:
        Predictor: P(Y|X1,X2,T)
        Predictor: P(X1)

案例 6#

此示例展示了算法在不相交图情况下的性能。

[7]:
# Random data
treatment = "T"
outcome = "Y"
variables = ["X1"]
causal_graph = "digraph{T;X1->Y;}"
columns = list(treatment) + list(outcome) + list(variables)
df = pd.DataFrame(columns=columns)

# Causal Model Initialization
causal_model = CausalModel(df, treatment, outcome, graph=causal_graph)

# View graph
causal_model.view_model()
from IPython.display import Image, display
print("Graph:")
display(Image(filename="causal_model.png"))

# Causal Identification using the ID Algorithm
identified_estimand = causal_model.identify_effect(method_name="id-algorithm")
print("\nResult for identification using ID Algorithm:")
print(identified_estimand)

../_images/example_notebooks_identifying_effects_using_id_algorithm_14_0.png
Graph:
../_images/example_notebooks_identifying_effects_using_id_algorithm_14_2.png

Result for identification using ID Algorithm:
Sum over {X1}:
        Predictor: P(X1,Y)
[ ]: