Skip to content

feat(design): design lock, held-out periods and reconcile for diagnose_design - #899

Open
igerber wants to merge 2 commits into
mainfrom
feat/design-lock
Open

igerber wants to merge 2 commits into
mainfrom
feat/design-lock

Conversation

@igerber

@igerber igerber commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Summary

Second part of design diagnosis (follows #898, no release in between). diagnose_design compares candidate estimators on an untreated panel; this PR carries the choice into the analysis.

  • Held-out periods. With more than one estimator, diagnose_design now leaves the last periods of the panel out of the comparison (new n_holdout=; default half of the periods at or after the latest adoption period, rounded down; an error naming n_holdout=0 if that is zero). The held-out rows are removed by period value before any outcome is validated, fingerprinted or fitted.
  • DesignDiagnosisResults.lock(label, data=) records one estimator as the pre-specified choice. When periods were held out it reruns that estimator alone with them included, at the declared adoption dates, with a seed derived from the recorded one, and reports that run. It refuses a panel whose periods or fingerprint differ from the diagnosed one, a configuration changed after the diagnosis, and a configuration that cannot be rebuilt from JSON.
  • DesignLock (frozen, plain JSON data): chosen estimator and configuration, declared design, panel fingerprint and period values, the comparison rows of every estimator compared, the chosen estimator's reported rows, size check and MDE, library and schema version (DESIGN_LOCK_SCHEMA_VERSION = "1.0"), and a sha256 of its canonical JSON. to_json() / from_json() / to_dict() / from_dict(), to_estimator(), to_dataframe(), summary().
  • DesignLock.reconcile(estimator, outcome=, unit=, time=, ...) returns a DesignReconciliationResults: one check per item and the label pre-specified or deviates. Always: estimator class, every constructor parameter, fit() keywords, column names. With results=: alpha. With data=: fingerprint of the diagnosed periods, unit count, panel balance, treated count, cohort count and per-cohort post-window length.
  • Fingerprint encoding. data_sha256 now encodes numbers by value (1 and 1.0 are the same cell), so the digest of a set of rows does not change when pandas stores an integer column as float because another row is missing. diagnose_design is unreleased, so no stored fingerprint is affected.

Not in this PR (tracked in TODO.md): wiring the label into DiagnosticReport / BusinessReport, and the "Study Design" tutorial. Both ship before the release that carries diagnose_design.

Methodology references (required if estimator / math changes)

  • Method name(s): DesignDiagnosis (docs/methodology/REGISTRY.md ## DesignDiagnosis). No estimator formula, weighting or variance computation changes.
  • Paper / source link(s): Blair, Cooper, Coppock & Humphreys (2019), "Declaring and Diagnosing Research Designs", APSR 113(3), p.852 - reconciling the planned and the realized design and reporting which it is. https://doi.org/10.1017/S0003055419000194
  • Any intentional deviations from the source (and why): everything beyond the reconciliation idea is a library choice with no paper source, documented as - **Note:** entries in the registry: held-out periods and their default count; the lock run using the full panel at the declared dates (it overlaps the comparison data, and the comparison runs on a shorter post window); the derived lock seed; the binary label with no grading, covering only the listed checks; reconcile as a declaration check (fitted results do not record their configuration, so a parameter changed with set_params() after fitting cannot be detected); realized timing reconciled through cohort count, treated count, post-window lengths and panel balance, not adoption dates; the lock hash as accident detection, not a signature; refusal of configurations that are not JSON data or not finite. How much selection inflation the held-out rerun removes is not validated numerically, and the registry says so.

Validation

  • Tests added/updated: tests/test_design.py (new TestHoldout, TestLock, TestLockSerialization, TestToEstimator, TestReconcile; 174 tests in the file), tests/test_methodology_design.py (new TestHeldOutRun; 17 tests), tests/test_naming_guard.py, tests/test_diagnostic_marker.py, tests/helpers/results_foundation.py.
  • Backtest / simulation / notebook evidence (if applicable): the 191 design tests pass on the pure-Python and Rust backends; the lock run is pinned equal to a plain run of the chosen estimator on the full panel at the derived seed; on a simulated AR(1) panel (50 units x 21 periods, rho = 0.8) the lock run of unit-clustered TWFE has a no-effect rejection rate of 5.0% (comparison: 6.3%). Sphinx -W build passes.

Security / privacy

  • Confirm no secrets/PII in this PR: Yes

Changelog

  • changelog.d/ fragment added (or N/A - no user-visible change): Yes

…e_design

DesignDiagnosisResults.lock records the pre-specified estimator as a JSON-serializable DesignLock; DesignLock.reconcile labels the analysis that was run pre-specified or deviates. With several estimators, diagnose_design holds the last periods out of the comparison (n_holdout) and lock() reruns only the chosen estimator with them. The data fingerprint now encodes numbers by value.
@github-actions

Copy link
Copy Markdown

Overall assessment

⚠️ Needs changes — one unmitigated P1 finding.

Executive summary

  • The lock’s mutation guard can miss an estimator-class change and associate existing diagnosis statistics with a different estimator.
  • No estimator formulas, weighting, or variance calculations changed.
  • Held-out reruns and reconciliation limitations are documented registry choices and are informational.
  • A changelog fragment exists; tutorial and report integration are tracked.
  • Syntax checks passed. Full tests could not run because NumPy, pandas, SciPy, and pytest are unavailable.

Methodology

P3 — Documented library choices; no action required.

Cross-checked DesignDiagnosis against docs/methodology/REGISTRY.md and the repository’s Blair et al. source audit, particularly p.852 on reconciling planned and realized analyses.

Impact: The overlapping full-panel rerun, shorter comparison window, derived seed, binary label, and declaration-only checks are explicitly documented choices, not methodology defects.

Concrete fix: None required.

Code Quality

P1 — Include estimator class in the diagnosis snapshot.
Location: diff_diff/design.py:L1696-L1702, diff_diff/design.py:L1823-L1849.

The mutation guard compares only get_params(). TwoWayFixedEffects inherits the DifferenceInDifferences constructor, so their default parameter dictionaries match. After diagnosing DifferenceInDifferences with n_holdout=0, this substitution passes the guard:

diagnosis.estimators["did"] = TwoWayFixedEffects()
lock = diagnosis.lock("did")

The lock records estimator_name="TwoWayFixedEffects" while retaining the original DiD comparison statistics. A source-only reproduction confirmed this path.

Impact: to_estimator() builds a different estimator from the one that produced the lock’s reported size, power, coverage, and MDE, without warning.

Concrete fix: Snapshot each candidate’s class alongside its parameters at diagnosis time and reject either changing before constructing the lock.

Performance

No material performance defect identified in the changed paths.

Maintainability

No additional findings.

Tech Debt

P3 — Tutorial and report integration are properly tracked.
Location: TODO.md:L90-L91.

Impact: The workflow currently exposes reconciliation through its own results object; integration into reports remains pending.

Concrete fix: None required in this PR; retain the tracked release commitments.

Security

No security finding identified. Estimator reconstruction uses an explicit supported-class allowlist; the hash’s accident-detection purpose is documented.

Documentation/Tests

The required fragment exists at changelog.d/20261123-design-lock.md.

Changed Python files passed syntax parsing. Full behavioral and numerical tests remain unverified locally because their dependencies are unavailable.

Path to Approval

  1. Record and verify the diagnosed estimator class as well as its parameters.
  2. Add regression tests replacing a diagnosed DifferenceInDifferences with default TwoWayFixedEffects: verify rejection with both n_holdout=0 and held-out periods, before any rerun occurs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant