Skip to content

Commit bb5d660

Browse files
igerberclaude
andcommitted
Add replicate-weight min_n fallback regression test
Exercises the SRS fallback path under a JK1 replicate-weight design, verifying that fallback SEs are finite/positive and differ from the replicate-based SEs. Covers the min_n × replicate interaction. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 50ab3bd commit bb5d660

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

tests/test_prep.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,3 +2805,53 @@ def test_replicate_weight_aggregation(self):
28052805
# All cells should have finite, positive SEs
28062806
assert panel["outcome_se"].notna().all()
28072807
assert (panel["outcome_se"] > 0).all()
2808+
2809+
def test_replicate_weight_min_n_fallback(self):
2810+
"""SRS fallback works correctly under replicate-weight designs."""
2811+
from diff_diff.prep_dgp import generate_survey_did_data
2812+
2813+
micro = generate_survey_did_data(
2814+
n_units=200,
2815+
n_periods=4,
2816+
cohort_periods=[3],
2817+
n_strata=3,
2818+
psu_per_stratum=6,
2819+
include_replicate_weights=True,
2820+
panel=False,
2821+
seed=42,
2822+
)
2823+
rep_cols = [c for c in micro.columns if c.startswith("rep_")]
2824+
design = SurveyDesign(
2825+
weights="weight",
2826+
replicate_weights=rep_cols,
2827+
replicate_method="JK1",
2828+
)
2829+
2830+
# min_n high enough to force SRS fallback on all cells
2831+
with pytest.warns(UserWarning, match="SRS fallback"):
2832+
panel_srs, _ = aggregate_survey(
2833+
micro,
2834+
by=["stratum", "period"],
2835+
outcomes="outcome",
2836+
survey_design=design,
2837+
min_n=9999,
2838+
)
2839+
assert panel_srs["srs_fallback"].all()
2840+
assert panel_srs["outcome_se"].notna().all()
2841+
assert (panel_srs["outcome_se"] > 0).all()
2842+
2843+
# Default min_n → replicate-based variance (no fallback)
2844+
panel_rep, _ = aggregate_survey(
2845+
micro,
2846+
by=["stratum", "period"],
2847+
outcomes="outcome",
2848+
survey_design=design,
2849+
)
2850+
assert not panel_rep["srs_fallback"].any()
2851+
2852+
# SEs should differ between SRS fallback and replicate-based
2853+
assert not np.allclose(
2854+
panel_srs["outcome_se"].values,
2855+
panel_rep["outcome_se"].values,
2856+
rtol=1e-6,
2857+
)

0 commit comments

Comments
 (0)