-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtwo_stage_bootstrap.py
More file actions
520 lines (460 loc) · 20 KB
/
two_stage_bootstrap.py
File metadata and controls
520 lines (460 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
"""
Bootstrap inference methods for the Two-Stage DiD estimator.
This module contains TwoStageDiDBootstrapMixin, which provides multiplier
bootstrap inference on the GMM influence function. Extracted from two_stage.py
for module size management.
"""
import warnings
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple
import numpy as np
import pandas as pd
from scipy.sparse.linalg import factorized as sparse_factorized
from diff_diff.bootstrap_utils import (
compute_effect_bootstrap_stats as _compute_effect_bootstrap_stats,
)
from diff_diff.bootstrap_utils import (
generate_bootstrap_weights_batch as _generate_bootstrap_weights_batch,
)
from diff_diff.bootstrap_utils import (
generate_survey_multiplier_weights_batch as _generate_survey_multiplier_weights_batch,
)
from diff_diff.linalg import solve_ols
from diff_diff.two_stage_results import TwoStageBootstrapResults
# Maximum number of elements before falling back to per-column sparse aggregation.
# Keep in sync with two_stage.py.
_SPARSE_DENSE_THRESHOLD = 10_000_000
__all__ = [
"TwoStageDiDBootstrapMixin",
]
class TwoStageDiDBootstrapMixin:
"""Mixin providing bootstrap inference methods for TwoStageDiD."""
# Type hints for attributes accessed from the main class
n_bootstrap: int
bootstrap_weights: str
alpha: float
seed: Optional[int]
horizon_max: Optional[int]
if TYPE_CHECKING:
from scipy import sparse
def _build_fe_design(
self,
df: pd.DataFrame,
unit: str,
time: str,
covariates: Optional[List[str]],
omega_0_mask: pd.Series,
) -> Tuple["sparse.csr_matrix", "sparse.csr_matrix", Dict[Any, int], Dict[Any, int]]: ...
@staticmethod
def _compute_gmm_scores(
c_by_cluster: np.ndarray,
gamma_hat: np.ndarray,
s2_by_cluster: np.ndarray,
) -> np.ndarray: ...
def _compute_cluster_S_scores(
self,
df: pd.DataFrame,
unit: str,
time: str,
covariates: Optional[List[str]],
omega_0_mask: pd.Series,
unit_fe: Dict[Any, float],
time_fe: Dict[Any, float],
delta_hat: Optional[np.ndarray],
kept_cov_mask: Optional[np.ndarray],
X_2: np.ndarray,
eps_2: np.ndarray,
cluster_ids: np.ndarray,
survey_weights: Optional[np.ndarray] = None,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""
Compute per-cluster S_g scores for bootstrap.
Returns
-------
S : np.ndarray, shape (G, k)
Per-cluster influence scores.
bread : np.ndarray, shape (k, k)
(X'_2 X_2)^{-1}.
unique_clusters : np.ndarray
Unique cluster identifiers.
"""
n = len(df)
k = X_2.shape[1]
cov_list = covariates
if covariates and kept_cov_mask is not None and not np.all(kept_cov_mask):
cov_list = [c for c, k_ in zip(covariates, kept_cov_mask) if k_]
X_1_sparse, X_10_sparse, _, _ = self._build_fe_design(
df, unit, time, cov_list, omega_0_mask
)
p = X_1_sparse.shape[1]
# Reconstruct Y and compute eps_10
alpha_i = df[unit].map(unit_fe).values
beta_t = df[time].map(time_fe).values
alpha_i = np.where(pd.isna(alpha_i), 0.0, alpha_i).astype(float)
beta_t = np.where(pd.isna(beta_t), 0.0, beta_t).astype(float)
fitted_1 = alpha_i + beta_t
if delta_hat is not None and cov_list:
if kept_cov_mask is not None and not np.all(kept_cov_mask):
fitted_1 = fitted_1 + np.dot(df[cov_list].values, delta_hat[kept_cov_mask])
else:
fitted_1 = fitted_1 + np.dot(df[cov_list].values, delta_hat)
y_tilde = df["_y_tilde"].values
y_vals = y_tilde + fitted_1
eps_10 = np.empty(n)
omega_0 = omega_0_mask.values
eps_10[omega_0] = y_vals[omega_0] - fitted_1[omega_0]
eps_10[~omega_0] = y_vals[~omega_0]
# gamma_hat — with survey weights, both cross-products need W
if survey_weights is not None:
XtX_10 = X_10_sparse.T @ X_10_sparse.multiply(survey_weights[:, None])
Xt1_X2 = X_1_sparse.T @ (X_2 * survey_weights[:, None])
else:
XtX_10 = X_10_sparse.T @ X_10_sparse
Xt1_X2 = X_1_sparse.T @ X_2
try:
solve_XtX = sparse_factorized(XtX_10.tocsc())
if Xt1_X2.ndim == 1:
gamma_hat = solve_XtX(Xt1_X2).reshape(-1, 1)
else:
gamma_hat = np.column_stack(
[solve_XtX(Xt1_X2[:, j]) for j in range(Xt1_X2.shape[1])]
)
except RuntimeError:
gamma_hat = np.linalg.lstsq(XtX_10.toarray(), Xt1_X2, rcond=None)[0]
if gamma_hat.ndim == 1:
gamma_hat = gamma_hat.reshape(-1, 1)
# Per-cluster aggregation — survey weights multiply eps_10 before sparse multiply
if survey_weights is not None:
weighted_eps_10 = survey_weights * eps_10
else:
weighted_eps_10 = eps_10
weighted_X10 = X_10_sparse.multiply(weighted_eps_10[:, None])
unique_clusters, cluster_indices = np.unique(cluster_ids, return_inverse=True)
G = len(unique_clusters)
n_elements = weighted_X10.shape[0] * weighted_X10.shape[1]
c_by_cluster = np.zeros((G, p))
if n_elements > _SPARSE_DENSE_THRESHOLD:
# Per-column path: limits peak memory for large FE matrices
weighted_X10_csc = weighted_X10.tocsc()
for j_col in range(p):
col_data = weighted_X10_csc.getcol(j_col).toarray().ravel()
np.add.at(c_by_cluster[:, j_col], cluster_indices, col_data)
else:
# Dense path: faster for moderate-size matrices
weighted_X10_dense = weighted_X10.toarray()
for j_col in range(p):
np.add.at(c_by_cluster[:, j_col], cluster_indices, weighted_X10_dense[:, j_col])
if survey_weights is not None:
weighted_eps_2 = survey_weights * eps_2
else:
weighted_eps_2 = eps_2
weighted_X2 = X_2 * weighted_eps_2[:, None]
s2_by_cluster = np.zeros((G, k))
for j_col in range(k):
np.add.at(s2_by_cluster[:, j_col], cluster_indices, weighted_X2[:, j_col])
S = self._compute_gmm_scores(c_by_cluster, gamma_hat, s2_by_cluster)
# Bread — (X'_2 W X_2)^{-1} with survey weights
with np.errstate(invalid="ignore", over="ignore", divide="ignore"):
if survey_weights is not None:
XtX_2 = X_2.T @ (X_2 * survey_weights[:, None])
else:
XtX_2 = np.dot(X_2.T, X_2)
try:
bread = np.linalg.solve(XtX_2, np.eye(k))
except np.linalg.LinAlgError:
bread = np.linalg.lstsq(XtX_2, np.eye(k), rcond=None)[0]
return S, bread, unique_clusters
def _run_bootstrap(
self,
df: pd.DataFrame,
unit: str,
time: str,
first_treat: str,
covariates: Optional[List[str]],
omega_0_mask: pd.Series,
omega_1_mask: pd.Series,
unit_fe: Dict[Any, float],
time_fe: Dict[Any, float],
grand_mean: float,
delta_hat: Optional[np.ndarray],
cluster_var: str,
kept_cov_mask: Optional[np.ndarray],
treatment_groups: List[Any],
ref_period: int,
balance_e: Optional[int],
original_att: float,
original_event_study: Optional[Dict[int, Dict[str, Any]]],
original_group: Optional[Dict[Any, Dict[str, Any]]],
aggregate: Optional[str],
resolved_survey: Optional[Any] = None,
) -> Optional[TwoStageBootstrapResults]:
"""Run multiplier bootstrap on GMM influence function."""
if self.n_bootstrap < 50:
warnings.warn(
f"n_bootstrap={self.n_bootstrap} is low. Consider n_bootstrap >= 199 "
"for reliable inference.",
UserWarning,
stacklevel=3,
)
rng = np.random.default_rng(self.seed)
y_tilde = df["_y_tilde"].values.copy() # .copy() to avoid mutating df column
n = len(df)
cluster_ids = df[cluster_var].values
# Extract survey weights for S-score computation and Stage-2 WLS
survey_weights: Optional[np.ndarray] = None
survey_weight_type: str = "pweight"
if resolved_survey is not None:
survey_weights = resolved_survey.weights
survey_weight_type = resolved_survey.weight_type
# Handle NaN y_tilde (from unidentified FEs) — matches _stage2_static logic
nan_mask = ~np.isfinite(y_tilde)
if nan_mask.any():
y_tilde[nan_mask] = 0.0
# --- Static specification bootstrap ---
D = omega_1_mask.values.astype(float) # .astype() already creates a copy
D[nan_mask] = 0.0 # Exclude NaN y_tilde obs from bootstrap estimation
# Degenerate case: all treated obs have NaN y_tilde
if D.sum() == 0:
return None
X_2_static = D.reshape(-1, 1)
coef_static = solve_ols(
X_2_static, y_tilde, return_vcov=False,
weights=survey_weights, weight_type=survey_weight_type,
)[0]
eps_2_static = y_tilde - np.dot(X_2_static, coef_static)
S_static, bread_static, unique_clusters = self._compute_cluster_S_scores(
df=df,
unit=unit,
time=time,
covariates=covariates,
omega_0_mask=omega_0_mask,
unit_fe=unit_fe,
time_fe=time_fe,
delta_hat=delta_hat,
kept_cov_mask=kept_cov_mask,
X_2=X_2_static,
eps_2=eps_2_static,
cluster_ids=cluster_ids,
survey_weights=survey_weights,
)
n_clusters = len(unique_clusters)
# Generate bootstrap weights — PSU-level when survey design is present
_use_survey_bootstrap = resolved_survey is not None and (
resolved_survey.strata is not None
or resolved_survey.psu is not None
or resolved_survey.fpc is not None
)
if _use_survey_bootstrap:
psu_weights, psu_ids = _generate_survey_multiplier_weights_batch(
self.n_bootstrap, resolved_survey, self.bootstrap_weights, rng
)
# Map unique_clusters (PSU values) to PSU weight columns.
# When survey+PSU is active, cluster_var == "_survey_cluster" so
# unique_clusters are the PSU ids used in S-score aggregation.
psu_id_to_col = {int(p): c for c, p in enumerate(psu_ids)}
cluster_to_psu_col = np.array([psu_id_to_col[int(cl)] for cl in unique_clusters])
all_weights = psu_weights[:, cluster_to_psu_col]
else:
all_weights = _generate_bootstrap_weights_batch(
self.n_bootstrap, n_clusters, self.bootstrap_weights, rng
)
# T_b = bread @ (sum_g w_bg * S_g) = bread @ (W @ S)' per boot
# IF_b = bread @ S_g for each cluster, then perturb
# boot_coef = all_weights @ S_static @ bread_static.T -> (B, k)
# For static (k=1): boot_att = all_weights @ S_static @ bread_static.T
boot_att_vec = np.dot(all_weights, S_static) # (B, 1)
boot_att_vec = np.dot(boot_att_vec, bread_static.T) # (B, 1)
boot_overall = boot_att_vec[:, 0]
boot_overall_shifted = boot_overall + original_att
overall_se, overall_ci, overall_p = _compute_effect_bootstrap_stats(
original_att,
boot_overall_shifted,
alpha=self.alpha,
context="TwoStageDiD overall ATT",
)
# --- Event study bootstrap ---
event_study_ses = None
event_study_cis = None
event_study_p_values = None
if original_event_study and aggregate in ("event_study", "all"):
# Recompute S scores for event study specification
rel_times = df["_rel_time"].values
if self.pretrends:
evt_rel = rel_times[~df["_never_treated"].values]
else:
evt_rel = rel_times[omega_1_mask.values]
all_horizons = sorted(set(int(h) for h in evt_rel if np.isfinite(h)))
if self.horizon_max is not None:
all_horizons = [h for h in all_horizons if abs(h) <= self.horizon_max]
if balance_e is not None:
cohort_rel_times = self._build_cohort_rel_times(df, first_treat)
balanced_cohorts = set()
if all_horizons:
max_h = max(all_horizons)
required_range = set(range(-balance_e, max_h + 1))
for g, horizons in cohort_rel_times.items():
if required_range.issubset(horizons):
balanced_cohorts.add(g)
if not balanced_cohorts:
all_horizons = [] # No qualifying cohorts -> skip event study bootstrap
else:
balance_mask = df[first_treat].isin(balanced_cohorts).values
else:
balance_mask = np.ones(n, dtype=bool)
est_horizons = [h for h in all_horizons if h != ref_period]
# Filter out Prop 5 horizons (same logic as _stage2_event_study)
has_never_treated = df["_never_treated"].any()
h_bar_boot = np.inf
if not has_never_treated and len(treatment_groups) > 1:
h_bar_boot = max(treatment_groups) - min(treatment_groups)
if h_bar_boot < np.inf:
est_horizons = [h for h in est_horizons if h < h_bar_boot]
if est_horizons:
horizon_to_col = {h: j for j, h in enumerate(est_horizons)}
k_es = len(est_horizons)
X_2_es = np.zeros((n, k_es))
for i in range(n):
if not balance_mask[i]:
continue
if nan_mask[i]:
continue # NaN y_tilde -> exclude from bootstrap event study
h = rel_times[i]
if np.isfinite(h):
h_int = int(h)
if h_int in horizon_to_col:
X_2_es[i, horizon_to_col[h_int]] = 1.0
coef_es = solve_ols(
X_2_es, y_tilde, return_vcov=False,
weights=survey_weights, weight_type=survey_weight_type,
)[0]
eps_2_es = y_tilde - np.dot(X_2_es, coef_es)
S_es, bread_es, _ = self._compute_cluster_S_scores(
df=df,
unit=unit,
time=time,
covariates=covariates,
omega_0_mask=omega_0_mask,
unit_fe=unit_fe,
time_fe=time_fe,
delta_hat=delta_hat,
kept_cov_mask=kept_cov_mask,
X_2=X_2_es,
eps_2=eps_2_es,
cluster_ids=cluster_ids,
survey_weights=survey_weights,
)
# boot_coef_es: (B, k_es)
boot_coef_es = np.dot(np.dot(all_weights, S_es), bread_es.T)
event_study_ses = {}
event_study_cis = {}
event_study_p_values = {}
for h in original_event_study:
if original_event_study[h].get("n_obs", 0) == 0:
continue
if np.isnan(original_event_study[h]["effect"]):
continue # Skip Prop 5 and other NaN-effect horizons
if h not in horizon_to_col:
continue
j = horizon_to_col[h]
orig_eff = original_event_study[h]["effect"]
boot_h = boot_coef_es[:, j]
shifted_h = boot_h + orig_eff
se_h, ci_h, p_h = _compute_effect_bootstrap_stats(
orig_eff,
shifted_h,
alpha=self.alpha,
context=f"TwoStageDiD event study (h={h})",
)
event_study_ses[h] = se_h
event_study_cis[h] = ci_h
event_study_p_values[h] = p_h
# --- Group bootstrap ---
group_ses = None
group_cis = None
group_p_values = None
if original_group and aggregate in ("group", "all"):
group_to_col = {g: j for j, g in enumerate(treatment_groups)}
k_grp = len(treatment_groups)
X_2_grp = np.zeros((n, k_grp))
ft_vals = df[first_treat].values
treated_mask = omega_1_mask.values
for i in range(n):
if treated_mask[i]:
if nan_mask[i]:
continue # NaN y_tilde -> exclude from group bootstrap
g = ft_vals[i]
if g in group_to_col:
X_2_grp[i, group_to_col[g]] = 1.0
coef_grp = solve_ols(
X_2_grp, y_tilde, return_vcov=False,
weights=survey_weights, weight_type=survey_weight_type,
)[0]
eps_2_grp = y_tilde - np.dot(X_2_grp, coef_grp)
S_grp, bread_grp, _ = self._compute_cluster_S_scores(
df=df,
unit=unit,
time=time,
covariates=covariates,
omega_0_mask=omega_0_mask,
unit_fe=unit_fe,
time_fe=time_fe,
delta_hat=delta_hat,
kept_cov_mask=kept_cov_mask,
X_2=X_2_grp,
eps_2=eps_2_grp,
cluster_ids=cluster_ids,
survey_weights=survey_weights,
)
boot_coef_grp = np.dot(np.dot(all_weights, S_grp), bread_grp.T)
group_ses = {}
group_cis = {}
group_p_values = {}
for g in original_group:
if g not in group_to_col:
continue
j = group_to_col[g]
orig_eff = original_group[g]["effect"]
boot_g = boot_coef_grp[:, j]
shifted_g = boot_g + orig_eff
se_g, ci_g, p_g = _compute_effect_bootstrap_stats(
orig_eff,
shifted_g,
alpha=self.alpha,
context=f"TwoStageDiD group effect (g={g})",
)
group_ses[g] = se_g
group_cis[g] = ci_g
group_p_values[g] = p_g
return TwoStageBootstrapResults(
n_bootstrap=self.n_bootstrap,
weight_type=self.bootstrap_weights,
alpha=self.alpha,
overall_att_se=overall_se,
overall_att_ci=overall_ci,
overall_att_p_value=overall_p,
event_study_ses=event_study_ses,
event_study_cis=event_study_cis,
event_study_p_values=event_study_p_values,
group_ses=group_ses,
group_cis=group_cis,
group_p_values=group_p_values,
bootstrap_distribution=boot_overall_shifted,
)
# =========================================================================
# Utility
# =========================================================================
@staticmethod
def _build_cohort_rel_times(
df: pd.DataFrame,
first_treat: str,
) -> Dict[Any, Set[int]]:
"""Build mapping of cohort -> set of observed relative times."""
treated_mask = ~df["_never_treated"]
treated_df = df.loc[treated_mask]
result: Dict[Any, Set[int]] = {}
ft_vals = treated_df[first_treat].values
rt_vals = treated_df["_rel_time"].values
for i in range(len(treated_df)):
h = rt_vals[i]
if np.isfinite(h):
result.setdefault(ft_vals[i], set()).add(int(h))
return result