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

Fix frontdoor estimation bug #1060

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dowhy/causal_estimators/two_stage_regression_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ def __init__(
)
)
else:
modified_target_estimand = copy.deepcopy(self._target_estimand)
self._second_stage_model = self.__class__.DEFAULT_SECOND_STAGE_MODEL(
modified_target_estimand,
test_significance=self._significance_test,
Expand Down
72 changes: 72 additions & 0 deletions tests/causal_estimators/test_two_stage_regression_estimator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import numpy as np
import pandas as pd
import pytest
from pytest import mark

from dowhy import CausalModel
from dowhy.causal_estimators.two_stage_regression_estimator import TwoStageRegressionEstimator

from .base import TestEstimator
Expand Down Expand Up @@ -71,3 +74,72 @@ def test_average_treatment_effect(
],
method_params={"num_simulations": 10, "num_null_simulations": 10},
)

def test_frontdoor_estimator(self):
"""
Test for frontdoor estimation, from @AlxndrMlk
See issue #616 https://github.com/py-why/dowhy/issues/616
"""

# Create the graph describing the causal structure
graph = """
graph [
directed 1

node [
id "X"
label "X"
]
node [
id "Z"
label "Z"
]
node [
id "Y"
label "Y"
]
node [
id "U"
label "U"
]

edge [
source "X"
target "Z"
]

edge [
source "Z"
target "Y"
]

edge [
source "U"
target "Y"
]

edge [
source "U"
target "X"
]
]
""".replace(
"\n", ""
)

N_SAMPLES = 10000
# Generate the data
U = np.random.randn(N_SAMPLES)
X = np.random.randn(N_SAMPLES) + 0.3 * U
Z = 0.7 * X + 0.3 * np.random.randn(N_SAMPLES)
Y = 0.65 * Z + 0.2 * U

# Data to df
df = pd.DataFrame(np.vstack([X, Z, Y]).T, columns=["X", "Z", "Y"])

# Create a model
model = CausalModel(data=df, treatment="X", outcome="Y", graph=graph)
estimand = model.identify_effect(proceed_when_unidentifiable=True)
# Estimate the effect with front-door
estimate = model.estimate_effect(identified_estimand=estimand, method_name="frontdoor.two_stage_regression")
assert estimate.value == pytest.approx(0.45, 0.025)