Commit 0501068
fix(twfeweights): address review - input validation, house helpers, parity gates
Addresses all 21 items in igerber's review of igerber#812. The port's R-parity was
accepted; almost everything here is about the paths that go BEYOND the R
reference (the ATT(g,t)-frame input, user weights, non-standard cohort
labels), where R never faced the input so the port had no validation.
Correctness (items 1-5), all reproduced by the reviewer:
- Cohort labels: never-treated is exactly 0 or +inf. NaN / -inf raise instead
of being silently absorbed into cohort 0 (a single NaN label moved
`estimate` by ~1.4% with no warning). Within-unit invariance now uses
nunique(dropna=False) at all three invariance sites; non-finite period
labels are rejected up front.
- Balance roll-up propagates NaN as R does: `_frac_treated_extreme` returns NA
for a covariate with <3 distinct values, and the summary no longer turns
that into 0.0 via pandas' NaN-skipping sum. Masks on the `post` column, not
on a zero roll-up weight (a zero-weight post cell still contributes).
- `aggregation="twfe"` now enforces R's third restriction (xformla == ~1):
fits record their covariate names on the aggregation kit, at both build
sites (staggered.py and dml_did.py), and a covariate-adjusted fit raises.
A kit predating the bookkeeping warns; a non-CS result is a TypeError.
- ATT(g,t)-frame input: duplicate cells, non-finite group/time labels and
non-finite effects are rejected, and an incomplete grid fails closed for all
three aggregations. Two structural gaps mirror R instead of raising: a
cohort with no estimable post cell is dropped (did's first-period drop), and
under control_group="not_yet_treated" the CS estimands average over each
cohort's available post periods (aggte). Both warn.
- Unit weights must be finite, non-negative, with positive total and treated
mass; never-treated mass is required only where the comparison group enters
the formula, so overall/simple still work without one.
Parity and house conventions (items 6, 7, 9):
- Parity now asserts pretrend_bias, post_only, effective_sample_size and cell
ess/remainder. At the documented 0/0 cells the expectation is rebuilt from
R's OWN cells with our limit substituted only where R's number is noise, so
the assertion stays anchored to R. The scalar-split relaxation is restricted
to fwl_gmin1, and fwl_gmin1 cells are asserted.
- The FWL linear algebra goes through the house helpers: within_transform for
the two-way demeaning and solve_ols for the weighted solve. The bespoke
pivoted QR is gone - it was the same norm-pivoted QR solve_ols uses, so its
"drops later columns first" docstring was inaccurate and nothing R-specific
was lost. A frozen-numbers pin captured on the pre-refactor code guards the
weighted branches, which no parity fixture covers.
- negative_weight_share counted pre cells, so it read ~0.5 in every staggered
design. n_negative_post / negative_post_weight_share report the actual
pathology; summary() leads with them.
Tests (items 8, 13, 18, 19): decomposition edge cases, both plot backends,
collinear covariates, and regression tests for items 1-5. plot_twfe_weights
gains the backend="plotly" split its file-mates have. The constructed fixtures
are derived from their cells instead of carrying stale literals, and the two
weak tests now assert hand-computed values and all five exported names.
Goldens (items 10-12): regeneration is numerically inert - every one of the
192 changed numbers is a group/time label, none elsewhere. Cells now carry
original period labels throughout (implicit_* run in positional time), and the
tests assert labels rather than array position. The duplicated mpdta panel CSV
is dropped in favour of the shared mpdta_stata_panel.csv plus a derived-column
expression, with the generator asserting the two agree. The AIPW blocks are
labelled reserved for the follow-up rather than left looking unused.
Docs (items 14-17, 20, 21): README back to the sibling shape, changelog
trimmed, REGISTRY rewritten (the new hard errors, both R-mirroring carve-outs,
the 0/0 magnitudes enumerated, the annihilation note recast as numerical
hygiene with its threshold limitation stated), llms-full contract prose and a
plot subsection, doc-deps banner, requirements.R (twfeweights is not on CRAN),
and twfe_weights.rst registered in the snippet harness after fixing its
first_treat column name and making each block self-contained.
The weighted ECDF is O(n log n) instead of quadratic, holding parity at 1e-9.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>1 parent 142a572 commit 0501068
22 files changed
Lines changed: 1929 additions & 2845 deletions
File tree
- benchmarks
- R
- data
- changelog.d
- diff_diff
- guides
- visualization
- docs
- api
- methodology
- tests
- helpers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
133 | | - | |
| 133 | + | |
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
| |||
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
26 | | - | |
27 | | - | |
28 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
29 | 38 | | |
30 | 39 | | |
31 | 40 | | |
| |||
101 | 110 | | |
102 | 111 | | |
103 | 112 | | |
104 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
105 | 124 | | |
106 | 125 | | |
107 | 126 | | |
| |||
110 | 129 | | |
111 | 130 | | |
112 | 131 | | |
| 132 | + | |
| 133 | + | |
113 | 134 | | |
114 | 135 | | |
115 | 136 | | |
| |||
126 | 147 | | |
127 | 148 | | |
128 | 149 | | |
129 | | - | |
| 150 | + | |
130 | 151 | | |
131 | 152 | | |
132 | 153 | | |
133 | 154 | | |
134 | 155 | | |
135 | 156 | | |
136 | 157 | | |
| 158 | + | |
| 159 | + | |
137 | 160 | | |
138 | 161 | | |
139 | 162 | | |
| |||
151 | 174 | | |
152 | 175 | | |
153 | 176 | | |
154 | | - | |
| 177 | + | |
155 | 178 | | |
156 | 179 | | |
157 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
158 | 184 | | |
159 | 185 | | |
160 | 186 | | |
161 | | - | |
| 187 | + | |
162 | 188 | | |
163 | 189 | | |
164 | 190 | | |
| |||
192 | 218 | | |
193 | 219 | | |
194 | 220 | | |
195 | | - | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
196 | 224 | | |
197 | 225 | | |
| 226 | + | |
198 | 227 | | |
199 | 228 | | |
200 | 229 | | |
| |||
249 | 278 | | |
250 | 279 | | |
251 | 280 | | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
257 | 290 | | |
258 | 291 | | |
259 | 292 | | |
260 | 293 | | |
261 | 294 | | |
262 | 295 | | |
263 | 296 | | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
268 | 301 | | |
269 | 302 | | |
270 | | - | |
| 303 | + | |
271 | 304 | | |
272 | | - | |
| 305 | + | |
273 | 306 | | |
274 | 307 | | |
275 | 308 | | |
276 | 309 | | |
277 | 310 | | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
278 | 315 | | |
279 | 316 | | |
280 | 317 | | |
| |||
298 | 335 | | |
299 | 336 | | |
300 | 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 | + | |
301 | 365 | | |
302 | 366 | | |
303 | 367 | | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
308 | 381 | | |
309 | 382 | | |
310 | 383 | | |
| |||
344 | 417 | | |
345 | 418 | | |
346 | 419 | | |
347 | | - | |
348 | | - | |
349 | 420 | | |
350 | 421 | | |
351 | 422 | | |
352 | 423 | | |
353 | 424 | | |
354 | 425 | | |
355 | | - | |
356 | | - | |
357 | | - | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
358 | 438 | | |
359 | 439 | | |
360 | 440 | | |
| |||
382 | 462 | | |
383 | 463 | | |
384 | 464 | | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
385 | 486 | | |
386 | 487 | | |
387 | 488 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
22 | 24 | | |
23 | 25 | | |
24 | 26 | | |
| |||
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
30 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
31 | 35 | | |
32 | 36 | | |
33 | 37 | | |
| |||
0 commit comments