|
19 | 19 | { |
20 | 20 | "cell_type": "code", |
21 | 21 | "execution_count": null, |
22 | | - "metadata": { |
23 | | - "execution": { |
24 | | - "iopub.execute_input": "2026-01-18T18:10:35.801900Z", |
25 | | - "iopub.status.busy": "2026-01-18T18:10:35.801757Z", |
26 | | - "iopub.status.idle": "2026-01-18T18:10:36.233206Z", |
27 | | - "shell.execute_reply": "2026-01-18T18:10:36.232925Z" |
28 | | - } |
29 | | - }, |
| 22 | + "metadata": {}, |
30 | 23 | "outputs": [], |
31 | 24 | "source": [ |
32 | 25 | "import numpy as np\n", |
|
47 | 40 | { |
48 | 41 | "cell_type": "code", |
49 | 42 | "execution_count": null, |
50 | | - "metadata": { |
51 | | - "execution": { |
52 | | - "iopub.execute_input": "2026-01-18T18:10:36.234607Z", |
53 | | - "iopub.status.busy": "2026-01-18T18:10:36.234517Z", |
54 | | - "iopub.status.idle": "2026-01-18T18:10:36.239917Z", |
55 | | - "shell.execute_reply": "2026-01-18T18:10:36.239725Z" |
56 | | - } |
57 | | - }, |
| 43 | + "metadata": {}, |
58 | 44 | "outputs": [], |
59 | 45 | "source": [ |
60 | 46 | "# Generate synthetic DiD data with known ATT of 5.0\n", |
|
75 | 61 | { |
76 | 62 | "cell_type": "code", |
77 | 63 | "execution_count": null, |
78 | | - "metadata": { |
79 | | - "execution": { |
80 | | - "iopub.execute_input": "2026-01-18T18:10:36.251493Z", |
81 | | - "iopub.status.busy": "2026-01-18T18:10:36.251420Z", |
82 | | - "iopub.status.idle": "2026-01-18T18:10:36.254312Z", |
83 | | - "shell.execute_reply": "2026-01-18T18:10:36.254088Z" |
84 | | - } |
85 | | - }, |
| 64 | + "metadata": {}, |
86 | 65 | "outputs": [], |
87 | 66 | "source": [ |
88 | 67 | "# Examine the data structure\n", |
|
102 | 81 | { |
103 | 82 | "cell_type": "code", |
104 | 83 | "execution_count": null, |
105 | | - "metadata": { |
106 | | - "execution": { |
107 | | - "iopub.execute_input": "2026-01-18T18:10:36.255333Z", |
108 | | - "iopub.status.busy": "2026-01-18T18:10:36.255268Z", |
109 | | - "iopub.status.idle": "2026-01-18T18:10:36.257620Z", |
110 | | - "shell.execute_reply": "2026-01-18T18:10:36.257437Z" |
111 | | - } |
112 | | - }, |
| 84 | + "metadata": {}, |
113 | 85 | "outputs": [], |
114 | 86 | "source": [ |
115 | 87 | "# Create the estimator\n", |
|
144 | 116 | { |
145 | 117 | "cell_type": "code", |
146 | 118 | "execution_count": null, |
147 | | - "metadata": { |
148 | | - "execution": { |
149 | | - "iopub.execute_input": "2026-01-18T18:10:36.258588Z", |
150 | | - "iopub.status.busy": "2026-01-18T18:10:36.258510Z", |
151 | | - "iopub.status.idle": "2026-01-18T18:10:36.260137Z", |
152 | | - "shell.execute_reply": "2026-01-18T18:10:36.259955Z" |
153 | | - } |
154 | | - }, |
| 119 | + "metadata": {}, |
155 | 120 | "outputs": [], |
156 | 121 | "source": [ |
157 | 122 | "# Access individual components\n", |
|
176 | 141 | { |
177 | 142 | "cell_type": "code", |
178 | 143 | "execution_count": null, |
179 | | - "metadata": { |
180 | | - "execution": { |
181 | | - "iopub.execute_input": "2026-01-18T18:10:36.261002Z", |
182 | | - "iopub.status.busy": "2026-01-18T18:10:36.260952Z", |
183 | | - "iopub.status.idle": "2026-01-18T18:10:36.262794Z", |
184 | | - "shell.execute_reply": "2026-01-18T18:10:36.262624Z" |
185 | | - } |
186 | | - }, |
| 144 | + "metadata": {}, |
187 | 145 | "outputs": [], |
188 | 146 | "source": [ |
189 | 147 | "# Using formula interface (R-style)\n", |
|
199 | 157 | { |
200 | 158 | "cell_type": "code", |
201 | 159 | "execution_count": null, |
202 | | - "metadata": { |
203 | | - "execution": { |
204 | | - "iopub.execute_input": "2026-01-18T18:10:36.263682Z", |
205 | | - "iopub.status.busy": "2026-01-18T18:10:36.263631Z", |
206 | | - "iopub.status.idle": "2026-01-18T18:10:36.265008Z", |
207 | | - "shell.execute_reply": "2026-01-18T18:10:36.264810Z" |
208 | | - } |
209 | | - }, |
| 160 | + "metadata": {}, |
210 | 161 | "outputs": [], |
211 | 162 | "source": [ |
212 | 163 | "# Verify both methods give the same result\n", |
|
227 | 178 | { |
228 | 179 | "cell_type": "code", |
229 | 180 | "execution_count": null, |
230 | | - "metadata": { |
231 | | - "execution": { |
232 | | - "iopub.execute_input": "2026-01-18T18:10:36.265866Z", |
233 | | - "iopub.status.busy": "2026-01-18T18:10:36.265814Z", |
234 | | - "iopub.status.idle": "2026-01-18T18:10:36.268138Z", |
235 | | - "shell.execute_reply": "2026-01-18T18:10:36.267940Z" |
236 | | - } |
237 | | - }, |
| 181 | + "metadata": {}, |
238 | 182 | "outputs": [], |
239 | 183 | "source": [ |
240 | 184 | "# Add some covariates to our data\n", |
|
258 | 202 | { |
259 | 203 | "cell_type": "code", |
260 | 204 | "execution_count": null, |
261 | | - "metadata": { |
262 | | - "execution": { |
263 | | - "iopub.execute_input": "2026-01-18T18:10:36.269011Z", |
264 | | - "iopub.status.busy": "2026-01-18T18:10:36.268965Z", |
265 | | - "iopub.status.idle": "2026-01-18T18:10:36.270317Z", |
266 | | - "shell.execute_reply": "2026-01-18T18:10:36.270123Z" |
267 | | - } |
268 | | - }, |
| 205 | + "metadata": {}, |
269 | 206 | "outputs": [], |
270 | 207 | "source": [ |
271 | 208 | "# All coefficient estimates are available\n", |
|
289 | 226 | { |
290 | 227 | "cell_type": "code", |
291 | 228 | "execution_count": null, |
292 | | - "metadata": { |
293 | | - "execution": { |
294 | | - "iopub.execute_input": "2026-01-18T18:10:36.271191Z", |
295 | | - "iopub.status.busy": "2026-01-18T18:10:36.271130Z", |
296 | | - "iopub.status.idle": "2026-01-18T18:10:36.274752Z", |
297 | | - "shell.execute_reply": "2026-01-18T18:10:36.274501Z" |
298 | | - } |
299 | | - }, |
| 229 | + "metadata": {}, |
300 | 230 | "outputs": [], |
301 | 231 | "source": [ |
302 | 232 | "# Generate data with more structure\n", |
|
334 | 264 | { |
335 | 265 | "cell_type": "code", |
336 | 266 | "execution_count": null, |
337 | | - "metadata": { |
338 | | - "execution": { |
339 | | - "iopub.execute_input": "2026-01-18T18:10:36.275657Z", |
340 | | - "iopub.status.busy": "2026-01-18T18:10:36.275605Z", |
341 | | - "iopub.status.idle": "2026-01-18T18:10:36.277635Z", |
342 | | - "shell.execute_reply": "2026-01-18T18:10:36.277443Z" |
343 | | - } |
344 | | - }, |
| 267 | + "metadata": {}, |
345 | 268 | "outputs": [], |
346 | 269 | "source": [ |
347 | 270 | "# Using fixed effects with dummy variables\n", |
|
360 | 283 | { |
361 | 284 | "cell_type": "code", |
362 | 285 | "execution_count": null, |
363 | | - "metadata": { |
364 | | - "execution": { |
365 | | - "iopub.execute_input": "2026-01-18T18:10:36.278482Z", |
366 | | - "iopub.status.busy": "2026-01-18T18:10:36.278432Z", |
367 | | - "iopub.status.idle": "2026-01-18T18:10:36.280535Z", |
368 | | - "shell.execute_reply": "2026-01-18T18:10:36.280352Z" |
369 | | - } |
370 | | - }, |
| 286 | + "metadata": {}, |
371 | 287 | "outputs": [], |
372 | 288 | "source": [ |
373 | 289 | "# Using absorbed fixed effects (within-transformation)\n", |
|
396 | 312 | { |
397 | 313 | "cell_type": "code", |
398 | 314 | "execution_count": null, |
399 | | - "metadata": { |
400 | | - "execution": { |
401 | | - "iopub.execute_input": "2026-01-18T18:10:36.281443Z", |
402 | | - "iopub.status.busy": "2026-01-18T18:10:36.281372Z", |
403 | | - "iopub.status.idle": "2026-01-18T18:10:36.284078Z", |
404 | | - "shell.execute_reply": "2026-01-18T18:10:36.283923Z" |
405 | | - } |
406 | | - }, |
| 315 | + "metadata": {}, |
407 | 316 | "outputs": [], |
408 | 317 | "source": [ |
409 | 318 | "# Two-Way Fixed Effects estimator\n", |
|
433 | 342 | { |
434 | 343 | "cell_type": "code", |
435 | 344 | "execution_count": null, |
436 | | - "metadata": { |
437 | | - "execution": { |
438 | | - "iopub.execute_input": "2026-01-18T18:10:36.285055Z", |
439 | | - "iopub.status.busy": "2026-01-18T18:10:36.284989Z", |
440 | | - "iopub.status.idle": "2026-01-18T18:10:36.287340Z", |
441 | | - "shell.execute_reply": "2026-01-18T18:10:36.287159Z" |
442 | | - } |
443 | | - }, |
| 345 | + "metadata": {}, |
444 | 346 | "outputs": [], |
445 | 347 | "source": [ |
446 | 348 | "# Create clustered data\n", |
|
478 | 380 | { |
479 | 381 | "cell_type": "code", |
480 | 382 | "execution_count": null, |
481 | | - "metadata": { |
482 | | - "execution": { |
483 | | - "iopub.execute_input": "2026-01-18T18:10:36.288239Z", |
484 | | - "iopub.status.busy": "2026-01-18T18:10:36.288176Z", |
485 | | - "iopub.status.idle": "2026-01-18T18:10:36.290659Z", |
486 | | - "shell.execute_reply": "2026-01-18T18:10:36.290472Z" |
487 | | - } |
488 | | - }, |
| 383 | + "metadata": {}, |
489 | 384 | "outputs": [], |
490 | 385 | "source": [ |
491 | 386 | "# Compare standard errors: robust vs cluster-robust\n", |
|
524 | 419 | { |
525 | 420 | "cell_type": "code", |
526 | 421 | "execution_count": null, |
527 | | - "metadata": { |
528 | | - "execution": { |
529 | | - "iopub.execute_input": "2026-01-18T18:10:36.291566Z", |
530 | | - "iopub.status.busy": "2026-01-18T18:10:36.291505Z", |
531 | | - "iopub.status.idle": "2026-01-18T18:10:36.346217Z", |
532 | | - "shell.execute_reply": "2026-01-18T18:10:36.346015Z" |
533 | | - } |
534 | | - }, |
| 422 | + "metadata": {}, |
535 | 423 | "outputs": [], |
536 | 424 | "source": [ |
537 | 425 | "# Wild cluster bootstrap inference\n", |
|
556 | 444 | { |
557 | 445 | "cell_type": "code", |
558 | 446 | "execution_count": null, |
559 | | - "metadata": { |
560 | | - "execution": { |
561 | | - "iopub.execute_input": "2026-01-18T18:10:36.347172Z", |
562 | | - "iopub.status.busy": "2026-01-18T18:10:36.347118Z", |
563 | | - "iopub.status.idle": "2026-01-18T18:10:36.348824Z", |
564 | | - "shell.execute_reply": "2026-01-18T18:10:36.348617Z" |
565 | | - } |
566 | | - }, |
| 447 | + "metadata": {}, |
567 | 448 | "outputs": [], |
568 | 449 | "source": [ |
569 | 450 | "# Compare inference methods\n", |
|
586 | 467 | { |
587 | 468 | "cell_type": "code", |
588 | 469 | "execution_count": null, |
589 | | - "metadata": { |
590 | | - "execution": { |
591 | | - "iopub.execute_input": "2026-01-18T18:10:36.349842Z", |
592 | | - "iopub.status.busy": "2026-01-18T18:10:36.349787Z", |
593 | | - "iopub.status.idle": "2026-01-18T18:10:36.351291Z", |
594 | | - "shell.execute_reply": "2026-01-18T18:10:36.351112Z" |
595 | | - } |
596 | | - }, |
| 470 | + "metadata": {}, |
597 | 471 | "outputs": [], |
598 | 472 | "source": [ |
599 | 473 | "# Export to dictionary\n", |
|
609 | 483 | { |
610 | 484 | "cell_type": "code", |
611 | 485 | "execution_count": null, |
612 | | - "metadata": { |
613 | | - "execution": { |
614 | | - "iopub.execute_input": "2026-01-18T18:10:36.352211Z", |
615 | | - "iopub.status.busy": "2026-01-18T18:10:36.352147Z", |
616 | | - "iopub.status.idle": "2026-01-18T18:10:36.355144Z", |
617 | | - "shell.execute_reply": "2026-01-18T18:10:36.354965Z" |
618 | | - } |
619 | | - }, |
| 486 | + "metadata": {}, |
620 | 487 | "outputs": [], |
621 | 488 | "source": [ |
622 | 489 | "# Export to DataFrame (useful for combining multiple estimates)\n", |
|
648 | 515 | } |
649 | 516 | ], |
650 | 517 | "metadata": { |
651 | | - "kernelspec": { |
652 | | - "display_name": "Python 3", |
653 | | - "language": "python", |
654 | | - "name": "python3" |
655 | | - }, |
656 | 518 | "language_info": { |
657 | | - "codemirror_mode": { |
658 | | - "name": "ipython", |
659 | | - "version": 3 |
660 | | - }, |
661 | | - "file_extension": ".py", |
662 | | - "mimetype": "text/x-python", |
663 | | - "name": "python", |
664 | | - "nbconvert_exporter": "python", |
665 | | - "pygments_lexer": "ipython3", |
666 | | - "version": "3.12.4" |
| 519 | + "name": "python" |
667 | 520 | } |
668 | 521 | }, |
669 | 522 | "nbformat": 4, |
|
0 commit comments