diff_diff.DMLDiD#
- class diff_diff.DMLDiD[source]#
Bases:
CallawaySantAnnaBootstrapMixin,CallawaySantAnnaAggregationMixin,BaseEstimatorChang (2020) DML DiD: staggered ATT(g,t) with cross-fitted ML nuisances.
panel=True(default) estimates Case 1 on panel data;panel=Falseestimates Case 2 on declared repeated cross sections (level outcomes, λ-corrected variance).Per (g, t) cell: K-fold cross-fitting of the propensity (
propensity_learner, out-of-foldpredict_proba) and the control-only outcome regression (outcome_learner, trained on the cell’s controls, Chang’sI_kz^c), then the pooled orthogonal score mean and the augmented-score plug-in SE. On panel fits the folds are D-stratified and the outcome nuisance is the outcome-change regressionE[dY | X, D=0]; on RCS fits the folds are D x T stratified and the nuisance is the level regressionE[(T - lam)Y | X, D=0]with the lambda-corrected variance. (Under a survey/cluster design whose PSU is strictly coarser than the sampling unit, folds are PSU-COHESIVE instead of stratified, and the per-cell SE is design-based rather than the plug-insqrt(mean(psi_bar**2)/n); declared designs also weight the moments — REGISTRY DMLDiD survey Notes.) Aggregation (event study, group, simple; plus total on panel non-survey fits — RCS and declared-survey fits failtotalclosed) is POST-FIT viaresults.aggregate().- Parameters:
propensity_learner (str or object, default "logit") –
"logit", or ANY object withfit/predict_proba(the classifier protocol) — e.g. a user-constructed or sklearn estimator. String names select library defaults only.outcome_learner (str or object, default "linear") –
"linear","ridge","sieve", or ANY object withfit/predict(the regressor protocol).n_folds (int, default 5) – Cross-fitting folds K (DML2; per-cell assignment).
control_group (str, default "never_treated") –
"never_treated"or"not_yet_treated"(CS semantics).anticipation (int, default 0) – Anticipation periods (shifts the base period and the not-yet-treated threshold; CS semantics).
alpha (float, default 0.05) – Significance level.
n_bootstrap (int, default 0) – Multiplier-bootstrap iterations (0 = analytical inference).
bootstrap_weights (str, optional) –
"rademacher"(default),"mammen", or"webb".seed (int, optional) – Root seed for the per-cell fold draws and the bootstrap. With
seed=None, POINT ESTIMATES vary across fits — cross-fitting draws random folds; setseedfor reproducible results with the library’s deterministic built-in learners. The same seed yields DIFFERENT folds with vs without a coarser-than-unit PSU design (PSU-cohesive folds consume the RNG differently than stratified folds — a config change, not a reproducibility break). A user-supplied STOCHASTIC learner object must additionally be seeded by the user (e.g. sklearnrandom_state) —cross_fit_predictdeep-copies the learner template where copyable but never seeds its internal RNG.base_period (str, default "varying") –
"varying"or"universal"(CS semantics; universal materializes per-cohort zero reference cells).cband (bool, default True) – Uniform (sup-t) bands on the post-fit event-study aggregation’s bootstrap replay.
pscore_trim (float, default 0.01) – Propensity clip bound: fitted propensities are clipped to
[pscore_trim, 1 - pscore_trim]after the extremeness warning (clip, never drop; Chang’s paper gives no trimming rule). Must be in(0, 0.5)and large enough that1 - pscore_trim < 1in float64 (a sub-ulp trim would disable the upper clip).panel (bool, default True) –
Trueestimates Chang’s Case 1 (repeated outcomes) on panel data (one row per unit-period, cell score on outcome CHANGES).Falseestimates Case 2 (repeated cross sections) on DECLARED cross-sectional data — one observation per row with a row-uniqueunitID, cell score on outcome LEVELS with the post-period sampling share λ̂ and the λ-corrected Theorem 2 variance. Case 2 additionally assumes stationary cross-sectional sampling (Chang Assumption 2.3: each wave samples the SAME target population — the composition of(D, X)is stable across waves, while outcomes are the period-specific potential outcomes, so trends and treatment effects are expected, not violations), which is not data-checkable —fit()warns.aggregate('total')is unavailable on RCS fits (fails closed, the library-wide RC convention).cluster (str, optional) – Column name for COARSER-than-unit clustering. The column is synthesized into a
SurveyDesign(psu=cluster)(or injected as the PSU of a declared PSU-less design): it drives PSU-cohesive cross-fitting folds (when strictly coarser than the sampling unit), the per-cell cluster-robust SE, the bootstrap draw structure, anddf_inference— while the moment kernels stay UNWEIGHTED and nosample_weightreaches the learners. Whensurvey_design=also supplies a PSU, the design’s PSU wins (warning on differing partitions).
Methods
__init__([propensity_learner, ...])fit(data, outcome, unit, time, first_treat)Estimate staggered ATT(g,t) via per-cell cross-fitted Chang scores.
get_params([deep])Get estimator parameters (sklearn-compatible).
set_params(**params)Set estimator parameters (sklearn-compatible, transactional).
Attributes
n_bootstrapbootstrap_weightsalphaseedanticipationbase_period- __init__(propensity_learner='logit', outcome_learner='linear', n_folds=5, control_group='never_treated', anticipation=0, alpha=0.05, n_bootstrap=0, bootstrap_weights=None, seed=None, base_period='varying', cband=True, pscore_trim=0.01, panel=True, cluster=None)[source]#
- Parameters:
- Return type:
None
- classmethod __new__(*args, **kwargs)#