Skip to content

Commit

Permalink
Fix frontdoor estimation bug (#1060)
Browse files Browse the repository at this point in the history
* fixed frontdoor bug

Signed-off-by: Amit Sharma <amit_sharma@live.com>

* fixed formatting issues

Signed-off-by: Amit Sharma <amit_sharma@live.com>

---------

Signed-off-by: Amit Sharma <amit_sharma@live.com>
  • Loading branch information
amit-sharma authored Nov 10, 2023
1 parent d8e662b commit 395d1fa
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
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)

0 comments on commit 395d1fa

Please sign in to comment.