Skip to content

Commit 24b6b2a

Browse files
igerberclaude
andcommitted
Add warning on ratio sieve all-K fallback
Symmetric with the inverse propensity fallback: emit UserWarning when no sieve candidate succeeds and ratio falls back to constant 1. Updated test to assert the warning fires. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f94f499 commit 24b6b2a

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

diff_diff/efficient_did_covariates.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ def estimate_propensity_ratio_sieve(
235235
best_ic = ic_val
236236
best_ratio = r_hat.copy()
237237

238+
# Warn if no sieve fit succeeded (falling back to constant ratio 1)
239+
if best_ic == np.inf:
240+
warnings.warn(
241+
"Propensity ratio sieve estimation failed for all K values. "
242+
"Falling back to constant ratio of 1 (no ratio adjustment). "
243+
"The DR estimator relies on outcome regression only.",
244+
UserWarning,
245+
stacklevel=2,
246+
)
247+
238248
# Overlap diagnostics: warn if ratios require significant clipping
239249
n_extreme = int(np.sum((best_ratio < 1.0 / ratio_clip) | (best_ratio > ratio_clip)))
240250
if n_extreme > 0:

tests/test_efficient_did.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,8 +1448,8 @@ def test_covariates_pt_all_bootstrap(self):
14481448
class TestSieveFallbacks:
14491449
"""Tier 2: sieve estimation failure fallbacks."""
14501450

1451-
def test_ratio_sieve_fallback_tiny_group(self):
1452-
"""When comparison group is too small for any basis, fall back to constant ratio."""
1451+
def test_ratio_sieve_fallback_tiny_group_warns(self):
1452+
"""When comparison group is too small for any basis, fall back with warning."""
14531453
from diff_diff.efficient_did_covariates import estimate_propensity_ratio_sieve
14541454

14551455
rng = np.random.default_rng(42)
@@ -1460,9 +1460,11 @@ def test_ratio_sieve_fallback_tiny_group(self):
14601460
# Tiny comparison group: only 2 units (fewer than any basis dimension)
14611461
mask_gp = np.zeros(n, dtype=bool)
14621462
mask_gp[50:52] = True
1463-
ratio = estimate_propensity_ratio_sieve(X, mask_g, mask_gp, k_max=3)
1464-
# Should produce valid ratios (fallback to constant 1)
1463+
with pytest.warns(UserWarning, match="Propensity ratio sieve estimation failed"):
1464+
ratio = estimate_propensity_ratio_sieve(X, mask_g, mask_gp, k_max=3)
14651465
assert np.all(np.isfinite(ratio))
1466+
# Fallback: constant ratio of 1 (clipped to [1/ratio_clip, ratio_clip])
1467+
assert np.allclose(ratio, 1.0)
14661468

14671469
def test_inverse_propensity_sieve_fallback_warns(self):
14681470
"""When group is too small for sieve, fall back with warning."""

0 commit comments

Comments
 (0)