Skip to content
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
4 changes: 2 additions & 2 deletions dowhy/causal_refuters/add_unobserved_common_cause.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _infer_default_kappa_t(
elif effect_on_t == "linear":
# Estimating the regression coefficient from standardized features to t
corrcoef_var_t = np.corrcoef(observed_common_causes, t, rowvar=False)[-1, :-1]
std_dev_t = np.std(t)[0]
std_dev_t = float(np.std(t))
max_coeff = max(corrcoef_var_t) * std_dev_t
min_coeff = min(corrcoef_var_t) * std_dev_t
else:
Expand Down Expand Up @@ -318,7 +318,7 @@ def _infer_default_kappa_y(
min_coeff, max_coeff = min(flips), max(flips)
elif effect_on_y == "linear":
corrcoef_var_y = np.corrcoef(observed_common_causes, y, rowvar=False)[-1, :-1]
std_dev_y = np.std(y)[0]
std_dev_y = float(np.std(y))
max_coeff = max(corrcoef_var_y) * std_dev_y
min_coeff = min(corrcoef_var_y) * std_dev_y
else:
Expand Down
30 changes: 30 additions & 0 deletions tests/causal_refuters/test_add_unobserved_common_cause.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,33 @@ def test_evalue_logistic_regression(self, mock_fig, estimator_method):
assert refute.stats["evalue_upper_ci"] is None
assert refute.stats["evalue_lower_ci"] < refute.stats["evalue_estimate"]
assert mock_fig.call_count > 0

def test_infer_default_kappa_linear_does_not_crash(self):
"""Regression test: _infer_default_kappa_t/y crashed with IndexError when effect strengths
were omitted and confounders_effect_on_treatment/outcome were 'linear' because np.std()
returns a scalar that cannot be indexed with [0]."""
data = dowhy.datasets.linear_dataset(
beta=10,
num_common_causes=2,
num_samples=500,
treatment_is_binary=False,
)
model = CausalModel(
data=data["df"],
treatment=data["treatment_name"],
outcome=data["outcome_name"],
graph=data["gml_graph"],
)
identified_estimand = model.identify_effect()
estimate = model.estimate_effect(identified_estimand, method_name="backdoor.linear_regression")
# No effect_strength_on_treatment / effect_strength_on_outcome supplied — forces
# _infer_default_kappa_t and _infer_default_kappa_y to run the "linear" branch.
refute = model.refute_estimate(
identified_estimand,
estimate,
method_name="add_unobserved_common_cause",
confounders_effect_on_treatment="linear",
confounders_effect_on_outcome="linear",
simulation_method="direct-simulation",
)
assert refute is not None
Loading