Repository navigation
fix(survey): dropped replicates cap the replicate design df instead of replacing it (library-wide) - #897
Conversation
…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.
✅ Looks goodNo unmitigated P0/P1 findings identified in the supplied diff. Executive summary
MethodologyP3 — Informational: documented replicate-df convention. The implementation follows the rank-based design-df convention attributed to R’s Impact: Corrects overly permissive inference without changing point estimates or variance calculations. Cross-checking used repository references and docstrings; independent retrieval of cited external sources failed because network name resolution was unavailable. Code QualityNo actionable findings. No new inline inference calculations or incomplete public-parameter propagation were identified. PerformanceNo actionable findings. Shared aggregation uses the cached df, avoiding repeated QR rank computations. MaintainabilityNo actionable findings. Centralized cap helpers clarify the distinction between a local df and an optional shared-df override. Tech DebtNo actionable findings. Removing the completed replicate-df backlog row is consistent with the changes reviewed. SecurityNo findings in the changed code. Documentation/TestsNo 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: |
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 (
Nonewhen 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 withn_valid - 1instead of capping it. Two wrong outcomes, both fixed here; SEs and point estimates do not change: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:
survey_metadata.df_surveyUndefined 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 anddf_survey = None.What changed:
survey.reduced_replicate_df(base_df, n_valid)next tocap_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, elseNone("keep the working df").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; 2x2x2TripleDifference;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 leftNone, and a statistic with fewer than two valid replicates changes no other statistic's df.DifferenceInDifferencesabsorb,TwoWayFixedEffectsstatic and event-study mode,SunAbraham,StackedDiD,ImputationDiD,TwoStageDiD,LinearRegression): the shared df is capped atmin(df, n_valid - 1).LinearRegression.survey_df_staysNoneon an undefined design df, soget_inference()takes its existing "undefined survey d.f." warning + NaN branch whether or not a replicate dropped.DifferenceInDifferencesnow writes the df its regression used tosurvey_metadata(when failed solves lowered the df, metadata andsummary()kept showing the design df).DMLDiDinline per-cell rule consolidated ontocap_replicate_df(no behavior change);ChaisemartinDHaultfoeuilledocstrings corrected (logic untouched).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
0intosurvey_metadata.df_survey;EfficientDiDswitched every later statistic to a normal reference;ContinuousDiDturned 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)
survey::degf()forsvyrep.design; documented indocs/methodology/REGISTRY.md, Survey Data Support, replicate-df Note (rewritten in this PR).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/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;LinearRegressiondirect; 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_conventionflipped to the cap and renamedtest_es_df_caps_never_replaces.tests/test_aggregate_contract.py:test_replicate_undefined_df_degeneratenow expects the0sentinel in the EfficientDiD kit df (wasNone).Security / privacy
Changelog
changelog.d/20261122-replicate-df-min-cap.md,FixedandBehavioral Changes)