Skip to content
Open
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
7 changes: 3 additions & 4 deletions dowhy/causal_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,10 +744,9 @@ def estimate_effect(
)
# Check if estimator's target estimand is identified
elif identified_estimand.estimands[identifier_name] is None:
logger.error("No valid identified estimand available.")
return CausalEstimate(
None, None, None, None, None, None, control_value=control_value, treatment_value=treatment_value
)
error_msg = f"No valid identified estimand for '{identifier_name}'. Ensure that the identification step succeeded for this estimator method (e.g. the graph must contain valid instruments for 'iv.instrumental_variable')."
logger.error(error_msg)
raise ValueError(error_msg)

if fit_estimator:
estimator.fit(
Expand Down
30 changes: 30 additions & 0 deletions tests/test_causal_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,33 @@ def setUp(self):

if __name__ == "__main__":
unittest.main()


def test_estimate_effect_raises_valueerror_for_missing_estimand():
Comment thread
abhiprd2000 marked this conversation as resolved.
"""estimate_effect() must raise ValueError when no valid estimand is identified.

Previously, it silently returned None, giving users no indication of failure.
See issue #1551 https://github.com/py-why/dowhy/issues/1551
"""
import dowhy.datasets
from dowhy import CausalModel

data = dowhy.datasets.linear_dataset(
beta=10,
num_common_causes=3,
num_instruments=0,
num_samples=500,
treatment_is_binary=True,
)
model = CausalModel(
data=data["df"],
treatment=data["treatment_name"],
outcome=data["outcome_name"],
graph=data["dot_graph"],
)
estimand = model.identify_effect(proceed_when_unidentifiable=True)
with pytest.raises(ValueError, match=r"No valid identified estimand for 'iv'"):
model.estimate_effect(
identified_estimand=estimand,
method_name="iv.instrumental_variable",
)
Loading