Skip to content

fix(survey): dropped replicates cap the replicate design df instead of replacing it (library-wide) - #897

Merged
igerber merged 1 commit into
mainfrom
replicate-df-min-cap
Nov 22, 2026
Merged

igerber merged 1 commit into
mainfrom
replicate-df-min-cap

Conversation

@igerber

@igerber igerber commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Summary

Follow-up to #890; closes the TODO.md row "Replicate effective-df min-cap, library-wide".

Under a replicate-weight design the inference df is the design df: the QR rank of the analysis-weight matrix minus 1 (None when the rank is <= 1). When a replicate was dropped (an all-zero replicate column, or a failed replicate refit / solve), 13 sites REPLACED that df with n_valid - 1 instead of capping it. Two wrong outcomes, both fixed here; SEs and point estimates do not change:

  • df raised above the design df on a rank-deficient replicate matrix. Anti-conservative p-values and CIs.
  • t-inference on an undefined design df (rank <= 1 plus a dropped column), where the correct result is NaN inference.

Measured on the new test fixtures (60-unit staggered panel, JK1):

Rank-deficient matrix - 15 usable columns of rank 5 plus one all-zero column, design df 4:

Estimator p-value before, t(14) p-value after, t(4) survey_metadata.df_survey
CallawaySantAnna 9.13e-09 2.74e-04 14 -> 4
EfficientDiD 6.23e-10 1.22e-04 14 -> 4
ContinuousDiD 1.03e-07 5.80e-04 14 -> 4
DifferenceInDifferences (plain) 7.41e-11 6.50e-05 4 -> 4
DifferenceInDifferences (absorb) 7.41e-11 6.50e-05 14 -> 4
TwoWayFixedEffects 7.41e-11 6.50e-05 14 -> 4
SunAbraham 4.25e-09 2.18e-04 14 -> 4
StackedDiD 2.65e-08 3.80e-04 14 -> 4
ImputationDiD 1.66e-09 1.64e-04 14 -> 4
TwoStageDiD 1.66e-09 1.64e-04 14 -> 4

Undefined design df - three proportional columns plus one all-zero column, rank 1: every estimator above reported a t(2) p-value (1.2e-04 to 1.3e-02) with df_survey = 2; all now report NaN t-stat / p-value / CI with the SE still reported and df_survey = None.

What changed:

  • New helper survey.reduced_replicate_df(base_df, n_valid) next to cap_replicate_df (shipped in fix(survey): per-cell ATT(g,t) SEs honor replicate and PSU designs (CallawaySantAnna, staggered TripleDifference) #890): returns the capped df only when it is a strict reduction from at least two valid replicates, else None ("keep the working df").
  • Influence-function sites (staggered_aggregation._se_from_psi, which feeds the CallawaySantAnna overall / event-study / group relays, the staggered TripleDifference overall and Eq. 4.14 relays and the DMLDiD aggregates; 2x2x2 TripleDifference; EfficientDiD; ContinuousDiD): the working df is overridden only on a strict reduction. It is never raised, an undefined df stays the NaN-inference sentinel with metadata left None, and a statistic with fewer than two valid replicates changes no other statistic's df.
  • Refit / OLS sites (DifferenceInDifferences absorb, TwoWayFixedEffects static and event-study mode, SunAbraham, StackedDiD, ImputationDiD, TwoStageDiD, LinearRegression): the shared df is capped at min(df, n_valid - 1). LinearRegression.survey_df_ stays None on an undefined design df, so get_inference() takes its existing "undefined survey d.f." warning + NaN branch whether or not a replicate dropped.
  • Plain (no-absorb) DifferenceInDifferences now writes the df its regression used to survey_metadata (when failed solves lowered the df, metadata and summary() kept showing the design df).
  • DMLDiD inline per-cell rule consolidated onto cap_replicate_df (no behavior change); ChaisemartinDHaultfoeuille docstrings corrected (logic untouched).
  • The shared aggregation path reads the design df from the fit-cached value rather than ResolvedSurveyDesign.df_survey, which runs a pivoted QR on every access.

Behavior changes beyond the df value (also in the changelog fragment): a statistic with fewer than two valid replicates used to alter the rest of the fit - CallawaySantAnna-family event-study / group rows all went NaN and the overall relay could write 0 into survey_metadata.df_survey; EfficientDiD switched every later statistic to a normal reference; ContinuousDiD turned every statistic NaN. Now only the affected statistic is NaN. This needs a non-finite influence function on one statistic, so it is not expected on well-formed fits.

Methodology references (required if estimator / math changes)

  • Method name(s): replicate-weight variance inference df (JK1 / JKn / BRR / Fay / SDR) for every estimator with replicate-weight support. No estimator equation, weighting or variance computation changes.
  • Paper / source link(s): design df = QR rank of the analysis-weight matrix minus 1, R survey::degf() for svyrep.design; documented in docs/methodology/REGISTRY.md, Survey Data Support, replicate-df Note (rewritten in this PR).
  • Any intentional deviations from the source (and why): the cap min(df_survey, n_valid - 1) for dropped replicates is a library convention (already used by ChaisemartinDHaultfoeuille and DMLDiD per-cell inference, now uniform) and is recorded as a REGISTRY Note. On influence-function paths the cap provably never binds for a finite influence function (QR rank <= number of nonzero replicate columns = n_valid), so the inference df there is the design df. ChaisemartinDHaultfoeuille keeps its one global minimum over all replicate-variance sites (fail-closed) and is named as the exception to per-statistic isolation.

Validation

  • Tests added/updated:
    • New tests/test_replicate_df_cap.py (166 tests, both backends): estimator matrix on four real replicate designs (healthy, dropped full-rank, dropped rank-deficient, undefined) across 14 estimator arms; genuine reductions below the design df on the refit, OLS and influence-function paths; LinearRegression direct; per-statistic isolation; no repeated design-rank computation in post-fit aggregation; the never-binds property; helper tables. 50 of these fail on the pre-change tree.
    • tests/test_survey_dml.py: test_es_df_pins_inherited_replace_convention flipped to the cap and renamed test_es_df_caps_never_replaces.
    • tests/test_aggregate_contract.py: test_replicate_undefined_df_degenerate now expects the 0 sentinel in the EfficientDiD kit df (was None).
  • Backtest / simulation / notebook evidence (if applicable): before/after table above. Existing suites resolved for the changed modules pass (60 test files, 5,002 tests; the suites touched by the last change, the fit-cached design df, were re-run afterwards).

Security / privacy

  • Confirm no secrets/PII in this PR: Yes

Changelog

  • changelog.d/ fragment added (or N/A - no user-visible change): Yes (changelog.d/20261122-replicate-df-min-cap.md, Fixed and Behavioral Changes)

…g it (library-wide)

Under a replicate-weight design the inference df is the QR rank of the
analysis-weight matrix minus 1. When a replicate dropped (an all-zero
column, or a failed refit / solve) 13 sites replaced that df with
n_valid - 1, which raised the df above the design df on a rank-deficient
replicate matrix (t(14) instead of t(4)) and produced t-inference on an
undefined design df (t(2) instead of NaN inference).

- survey.reduced_replicate_df: strict-reduction override helper next to
  cap_replicate_df.
- Influence-function sites (_se_from_psi and its CS / staggered-DDD /
  DMLDiD relays, 2x2x2 TripleDifference, EfficientDiD, ContinuousDiD)
  override the working df only on a strict reduction from >= 2 valid
  replicates; a statistic with < 2 valid replicates no longer changes
  another statistic's df.
- Refit / OLS sites (DifferenceInDifferences, TwoWayFixedEffects incl.
  event-study mode, SunAbraham, StackedDiD, ImputationDiD, TwoStageDiD,
  LinearRegression) cap the shared df; plain DiD now writes the capped
  df to survey_metadata.
- DMLDiD inline rule consolidated onto cap_replicate_df; dCDH docstrings
  corrected.
- REGISTRY replicate-df Note rewritten; TODO row removed.
@github-actions

Copy link
Copy Markdown

✅ Looks good

No unmitigated P0/P1 findings identified in the supplied diff.

Executive summary

  • Dropped replicates cap the design df; undefined design df retains NaN inference.
  • The changed inference paths continue using safe_inference, preserving consistent t-statistic, p-value, and CI handling.
  • Registry notes and the changelog document the changed behavior.
  • In-memory syntax checks passed for all 20 changed Python files; extracted production helpers passed 41,615 input combinations. The pytest suites could not run because dependencies are unavailable.

Methodology

P3 — Informational: documented replicate-df convention.
Affected methods: replicate-weight inference across the changed DiD estimators and LinearRegression.

The implementation follows the rank-based design-df convention attributed to R’s survey::degf() and the explicitly documented cap and isolation rules in docs/methodology/REGISTRY.md:L8017-L8047. The new helper:L2552-L2575 preserves undefined df and prevents upward overrides.

Impact: Corrects overly permissive inference without changing point estimates or variance calculations.
Concrete fix: None required.

Cross-checking used repository references and docstrings; independent retrieval of cited external sources failed because network name resolution was unavailable.

Code Quality

No actionable findings. No new inline inference calculations or incomplete public-parameter propagation were identified.

Performance

No actionable findings. Shared aggregation uses the cached df, avoiding repeated QR rank computations.

Maintainability

No actionable findings. Centralized cap helpers clarify the distinction between a local df and an optional shared-df override.

Tech Debt

No actionable findings. Removing the completed replicate-df backlog row is consistent with the changes reviewed.

Security

No findings in the changed code.

Documentation/Tests

No actionable findings. The changelog fragment covers both inference corrections and statistic-isolation changes. Added tests cover rank deficiency, undefined df, genuine reductions, metadata, and degenerate-statistic isolation.

Validation limitation: Estimator-level tests remain unverified here: pytest, NumPy, pandas, and SciPy are unavailable. The standalone helper and syntax checks do not replace those tests.

@igerber igerber added the ready-for-ci Triggers CI test workflows label Nov 22, 2026
@igerber
igerber merged commit 8bd28da into main Nov 22, 2026
43 of 44 checks passed
@igerber
igerber deleted the replicate-df-min-cap branch November 22, 2026 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ci Triggers CI test workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant