py2sdid 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- py2sdid-0.1.0/.github/workflows/docs.yml +47 -0
- py2sdid-0.1.0/.github/workflows/publish.yml +103 -0
- py2sdid-0.1.0/.github/workflows/test.yml +31 -0
- py2sdid-0.1.0/.gitignore +17 -0
- py2sdid-0.1.0/LICENSE +21 -0
- py2sdid-0.1.0/PKG-INFO +211 -0
- py2sdid-0.1.0/README.md +180 -0
- py2sdid-0.1.0/REFERENCE.md +174 -0
- py2sdid-0.1.0/benchmarks/full_analysis.py +187 -0
- py2sdid-0.1.0/benchmarks/validation.py +189 -0
- py2sdid-0.1.0/pyproject.toml +54 -0
- py2sdid-0.1.0/src/py2sdid/__init__.py +30 -0
- py2sdid-0.1.0/src/py2sdid/_types.py +73 -0
- py2sdid-0.1.0/src/py2sdid/core.py +315 -0
- py2sdid-0.1.0/src/py2sdid/diagnostics.py +254 -0
- py2sdid-0.1.0/src/py2sdid/effects.py +154 -0
- py2sdid-0.1.0/src/py2sdid/first_stage.py +99 -0
- py2sdid-0.1.0/src/py2sdid/inference.py +697 -0
- py2sdid-0.1.0/src/py2sdid/linalg.py +158 -0
- py2sdid-0.1.0/src/py2sdid/panel.py +211 -0
- py2sdid-0.1.0/src/py2sdid/plotting.py +285 -0
- py2sdid-0.1.0/src/py2sdid/results.py +395 -0
- py2sdid-0.1.0/tests/conftest.py +210 -0
- py2sdid-0.1.0/tests/data/generate_fixture.py +141 -0
- py2sdid-0.1.0/tests/data/staggered_panel.parquet +0 -0
- py2sdid-0.1.0/tests/data/staggered_panel_metadata.json +34 -0
- py2sdid-0.1.0/tests/test_diagnostics.py +46 -0
- py2sdid-0.1.0/tests/test_fixture.py +117 -0
- py2sdid-0.1.0/tests/test_plotting.py +44 -0
- py2sdid-0.1.0/tests/test_robust.py +201 -0
- py2sdid-0.1.0/tests/test_smoke.py +186 -0
- py2sdid-0.1.0/tests/test_stress.py +142 -0
- py2sdid-0.1.0/tests/test_vs_r.py +258 -0
- py2sdid-0.1.0/uv.lock +1598 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
name: Deploy Docs
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
pages: write
|
|
11
|
+
id-token: write
|
|
12
|
+
|
|
13
|
+
concurrency:
|
|
14
|
+
group: "pages"
|
|
15
|
+
cancel-in-progress: true
|
|
16
|
+
|
|
17
|
+
jobs:
|
|
18
|
+
build:
|
|
19
|
+
runs-on: ubuntu-latest
|
|
20
|
+
steps:
|
|
21
|
+
- uses: actions/checkout@v4
|
|
22
|
+
|
|
23
|
+
- uses: actions/setup-python@v5
|
|
24
|
+
with:
|
|
25
|
+
python-version: "3.12"
|
|
26
|
+
|
|
27
|
+
- name: Install dependencies
|
|
28
|
+
run: |
|
|
29
|
+
pip install -e ".[dev]"
|
|
30
|
+
pip install pdoc
|
|
31
|
+
|
|
32
|
+
- name: Build docs
|
|
33
|
+
run: pdoc py2sdid -o docs/
|
|
34
|
+
|
|
35
|
+
- uses: actions/upload-pages-artifact@v3
|
|
36
|
+
with:
|
|
37
|
+
path: docs/
|
|
38
|
+
|
|
39
|
+
deploy:
|
|
40
|
+
needs: build
|
|
41
|
+
runs-on: ubuntu-latest
|
|
42
|
+
environment:
|
|
43
|
+
name: github-pages
|
|
44
|
+
url: ${{ steps.deployment.outputs.page_url }}
|
|
45
|
+
steps:
|
|
46
|
+
- id: deployment
|
|
47
|
+
uses: actions/deploy-pages@v4
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
name: Build & Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
|
|
14
|
+
- name: Set up Python
|
|
15
|
+
uses: actions/setup-python@v5
|
|
16
|
+
with:
|
|
17
|
+
python-version: "3.12"
|
|
18
|
+
|
|
19
|
+
- name: Install build tools
|
|
20
|
+
run: pip install build twine
|
|
21
|
+
|
|
22
|
+
- name: Build wheel and sdist
|
|
23
|
+
run: python -m build
|
|
24
|
+
|
|
25
|
+
- name: Check distributions
|
|
26
|
+
run: twine check dist/*
|
|
27
|
+
|
|
28
|
+
- name: Upload artifacts
|
|
29
|
+
uses: actions/upload-artifact@v4
|
|
30
|
+
with:
|
|
31
|
+
name: dist
|
|
32
|
+
path: dist/
|
|
33
|
+
|
|
34
|
+
# py2sdid is pure Python — one wheel works on all platforms.
|
|
35
|
+
verify:
|
|
36
|
+
needs: build
|
|
37
|
+
strategy:
|
|
38
|
+
matrix:
|
|
39
|
+
os: [ubuntu-latest, macos-latest, windows-latest]
|
|
40
|
+
python-version: ["3.10", "3.12"]
|
|
41
|
+
runs-on: ${{ matrix.os }}
|
|
42
|
+
steps:
|
|
43
|
+
- uses: actions/setup-python@v5
|
|
44
|
+
with:
|
|
45
|
+
python-version: ${{ matrix.python-version }}
|
|
46
|
+
|
|
47
|
+
- name: Download artifacts
|
|
48
|
+
uses: actions/download-artifact@v4
|
|
49
|
+
with:
|
|
50
|
+
name: dist
|
|
51
|
+
path: dist/
|
|
52
|
+
|
|
53
|
+
- name: Install wheel
|
|
54
|
+
shell: bash
|
|
55
|
+
run: pip install dist/*.whl
|
|
56
|
+
|
|
57
|
+
- name: Smoke test
|
|
58
|
+
shell: bash
|
|
59
|
+
run: |
|
|
60
|
+
python -c "
|
|
61
|
+
import py2sdid
|
|
62
|
+
print(f'py2sdid {py2sdid.__version__} loaded successfully')
|
|
63
|
+
print(f'Entry points: ts_did, bjs_did')
|
|
64
|
+
"
|
|
65
|
+
|
|
66
|
+
upload-release:
|
|
67
|
+
needs: [build, verify]
|
|
68
|
+
runs-on: ubuntu-latest
|
|
69
|
+
if: github.event_name == 'release'
|
|
70
|
+
permissions:
|
|
71
|
+
contents: write
|
|
72
|
+
steps:
|
|
73
|
+
- name: Download artifacts
|
|
74
|
+
uses: actions/download-artifact@v4
|
|
75
|
+
with:
|
|
76
|
+
name: dist
|
|
77
|
+
path: dist/
|
|
78
|
+
|
|
79
|
+
- name: Upload to GitHub Release
|
|
80
|
+
env:
|
|
81
|
+
GH_TOKEN: ${{ github.token }}
|
|
82
|
+
run: gh release upload "${{ github.event.release.tag_name }}" dist/* --clobber --repo "${{ github.repository }}"
|
|
83
|
+
|
|
84
|
+
publish-pypi:
|
|
85
|
+
needs: [build, verify]
|
|
86
|
+
runs-on: ubuntu-latest
|
|
87
|
+
if: github.event_name == 'release'
|
|
88
|
+
permissions:
|
|
89
|
+
id-token: write
|
|
90
|
+
environment:
|
|
91
|
+
name: pypi
|
|
92
|
+
url: https://pypi.org/p/py2sdid
|
|
93
|
+
steps:
|
|
94
|
+
- name: Download artifacts
|
|
95
|
+
uses: actions/download-artifact@v4
|
|
96
|
+
with:
|
|
97
|
+
name: dist
|
|
98
|
+
path: dist/
|
|
99
|
+
|
|
100
|
+
- name: Publish to PyPI
|
|
101
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
102
|
+
with:
|
|
103
|
+
attestations: false
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
20
|
+
uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: ${{ matrix.python-version }}
|
|
23
|
+
|
|
24
|
+
- name: Install dependencies
|
|
25
|
+
run: |
|
|
26
|
+
python -m pip install --upgrade pip
|
|
27
|
+
pip install -e ".[dev]"
|
|
28
|
+
|
|
29
|
+
- name: Run tests
|
|
30
|
+
run: |
|
|
31
|
+
python -m pytest tests/test_smoke.py tests/test_diagnostics.py tests/test_plotting.py -v
|
py2sdid-0.1.0/.gitignore
ADDED
py2sdid-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alan Huang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
py2sdid-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py2sdid
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Two-stage DiD and BJS imputation estimator for staggered treatment designs
|
|
5
|
+
Author: Alan Huang
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: causal-inference,difference-in-differences,event-study,panel-data,staggered-adoption,treatment-effects
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Requires-Dist: joblib>=1.3
|
|
19
|
+
Requires-Dist: matplotlib>=3.7
|
|
20
|
+
Requires-Dist: numpy>=1.24
|
|
21
|
+
Requires-Dist: polars>=0.20
|
|
22
|
+
Requires-Dist: pyarrow>=12.0
|
|
23
|
+
Requires-Dist: scipy>=1.10
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest-xdist; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
27
|
+
Provides-Extra: r-comparison
|
|
28
|
+
Requires-Dist: pandas>=2.0; extra == 'r-comparison'
|
|
29
|
+
Requires-Dist: rpy2>=3.5; extra == 'r-comparison'
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# py2sdid
|
|
33
|
+
|
|
34
|
+
Two-stage difference-in-differences (Gardner 2021) and BJS imputation estimator (Borusyak, Jaravel, Spiess 2024) for staggered treatment designs in Python, built on polars, scipy (sparse), and numpy.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install py2sdid
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**From source:**
|
|
43
|
+
```bash
|
|
44
|
+
git clone https://github.com/AlanHuang99/py2sdid.git
|
|
45
|
+
cd py2sdid
|
|
46
|
+
pip install -e ".[dev]"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Quick Start
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
import polars as pl
|
|
53
|
+
import py2sdid
|
|
54
|
+
|
|
55
|
+
data = pl.read_parquet("panel_data.parquet")
|
|
56
|
+
|
|
57
|
+
result = py2sdid.ts_did(
|
|
58
|
+
data,
|
|
59
|
+
yname="dep_var",
|
|
60
|
+
idname="unit_id",
|
|
61
|
+
tname="year",
|
|
62
|
+
gname="cohort_year",
|
|
63
|
+
cluster_var="cluster_id",
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
result.summary()
|
|
67
|
+
result.plot(kind="event_study")
|
|
68
|
+
result.diagnose()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Estimators
|
|
72
|
+
|
|
73
|
+
### `ts_did` -- Two-Stage DiD (Gardner 2021)
|
|
74
|
+
|
|
75
|
+
Estimates unit and time fixed effects on untreated observations only, residualizes the outcome for the full sample, and regresses on treatment indicators. Standard errors use GMM influence functions that correct for the generated-regressor problem.
|
|
76
|
+
|
|
77
|
+
### `bjs_did` -- BJS Imputation (Borusyak, Jaravel, Spiess 2024)
|
|
78
|
+
|
|
79
|
+
Same first stage as `ts_did`. Directly averages residuals for treated observations rather than running a second-stage regression. Standard errors use the BJS imputation formula (Equations 6, 8, 10 of the paper).
|
|
80
|
+
|
|
81
|
+
Both produce identical point estimates. They differ in the analytic standard error formula. Both support cluster bootstrap as an alternative.
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## Parameters
|
|
86
|
+
|
|
87
|
+
### `ts_did()` / `bjs_did()`
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
ts_did(
|
|
91
|
+
data, yname, idname, tname, gname,
|
|
92
|
+
*, xformla=None, wname=None, cluster_var=None,
|
|
93
|
+
anticipation=0, se=True, bootstrap=False,
|
|
94
|
+
n_bootstraps=500, seed=None, n_jobs=None, verbose=True,
|
|
95
|
+
) -> DiDResult
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
| Parameter | Type | Default | Description |
|
|
99
|
+
|-----------|------|---------|-------------|
|
|
100
|
+
| `data` | `pl.DataFrame` | required | Panel data in long format (one row per unit per period) |
|
|
101
|
+
| `yname` | `str` | required | Outcome variable column |
|
|
102
|
+
| `idname` | `str` | required | Unit identifier column |
|
|
103
|
+
| `tname` | `str` | required | Time period column (integer-valued) |
|
|
104
|
+
| `gname` | `str` | required | Treatment cohort column (see below) |
|
|
105
|
+
| `xformla` | `list[str]` | `None` | Time-varying covariate columns for the first stage |
|
|
106
|
+
| `wname` | `str` | `None` | Observation weight column |
|
|
107
|
+
| `cluster_var` | `str` | `idname` | Column to cluster standard errors on |
|
|
108
|
+
| `se` | `bool` | `True` | Compute standard errors |
|
|
109
|
+
| `bootstrap` | `bool` | `False` | Use cluster bootstrap instead of analytic SEs |
|
|
110
|
+
| `n_bootstraps` | `int` | `500` | Number of bootstrap replications |
|
|
111
|
+
| `seed` | `int` | `None` | Random seed for bootstrap |
|
|
112
|
+
| `n_jobs` | `int` | CPU count | Parallel workers for bootstrap |
|
|
113
|
+
| `verbose` | `bool` | `True` | Print progress |
|
|
114
|
+
|
|
115
|
+
### The `gname` Column
|
|
116
|
+
|
|
117
|
+
The `gname` column encodes the treatment cohort for each unit — the time period when treatment begins.
|
|
118
|
+
|
|
119
|
+
- An integer value (e.g. `2000`) means the unit first receives treatment in that period.
|
|
120
|
+
- `0` or `null` means the unit is never treated.
|
|
121
|
+
- All observations for a given unit should have the same `gname` value.
|
|
122
|
+
|
|
123
|
+
The estimator uses this to determine:
|
|
124
|
+
- Which observations are untreated (used in the first stage): all observations where `tname < gname`, plus all observations from never-treated units.
|
|
125
|
+
- Which observations are treated (used for treatment effect estimation): observations where `tname >= gname`.
|
|
126
|
+
- The relative time (event time) for each observation: `tname - gname`.
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Output
|
|
131
|
+
|
|
132
|
+
Both estimators return a `DiDResult` with these fields:
|
|
133
|
+
|
|
134
|
+
| Field | Type | Description |
|
|
135
|
+
|-------|------|-------------|
|
|
136
|
+
| `att_avg` | `float` | Overall average treatment effect on treated |
|
|
137
|
+
| `att_avg_se` | `float` | Standard error of overall ATT |
|
|
138
|
+
| `att_avg_ci` | `tuple` | 95% confidence interval |
|
|
139
|
+
| `att_avg_pval` | `float` | p-value (H0: ATT = 0) |
|
|
140
|
+
| `event_study` | `pl.DataFrame` | Per-period estimates for all relative time periods |
|
|
141
|
+
| `unit_fe` | `np.ndarray` | Estimated unit fixed effects |
|
|
142
|
+
| `time_fe` | `np.ndarray` | Estimated time fixed effects |
|
|
143
|
+
| `vcov` | `np.ndarray` | Variance-covariance matrix |
|
|
144
|
+
|
|
145
|
+
The `event_study` DataFrame contains columns: `rel_time`, `estimate`, `se`, `ci_lower`, `ci_upper`, `pval`, `count`. It includes all relative time periods present in the data, both pre-treatment and post-treatment.
|
|
146
|
+
|
|
147
|
+
Convenience properties:
|
|
148
|
+
- `result.att_by_horizon` — post-treatment rows only (`rel_time >= 0`)
|
|
149
|
+
- `result.pretrend_tests` — pre-treatment rows only (`rel_time < 0`)
|
|
150
|
+
|
|
151
|
+
### Methods
|
|
152
|
+
|
|
153
|
+
| Method | Description |
|
|
154
|
+
|--------|-------------|
|
|
155
|
+
| `summary()` | Formatted text summary |
|
|
156
|
+
| `plot(kind)` | Matplotlib figure. Kinds: `event_study`, `pretrends`, `treatment_status`, `counterfactual`, `honestdid`, `calendar` |
|
|
157
|
+
| `diagnose()` | Pre-trend F-test, TOST equivalence, HonestDiD sensitivity |
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## Data Format
|
|
162
|
+
|
|
163
|
+
Input: `polars.DataFrame` in long panel format.
|
|
164
|
+
|
|
165
|
+
| Column | Type | Description |
|
|
166
|
+
|--------|------|-------------|
|
|
167
|
+
| outcome | float | Outcome variable |
|
|
168
|
+
| unit id | int/str | Unit identifier |
|
|
169
|
+
| time | int | Time period (e.g. year) |
|
|
170
|
+
| cohort | int | Treatment cohort (0 or null = never-treated) |
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
unit_id | year | cohort_year | dep_var
|
|
174
|
+
--------|------|-------------|--------
|
|
175
|
+
1 | 1990 | 2000 | 4.23
|
|
176
|
+
1 | 1991 | 2000 | 4.51
|
|
177
|
+
...
|
|
178
|
+
501 | 1990 | 0 | 3.82
|
|
179
|
+
501 | 1991 | 0 | 3.90
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## Testing
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
uv run pytest tests/ # all tests
|
|
188
|
+
uv run pytest tests/test_vs_r.py -v -s # R validation (requires R + did2s + didimputation)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Test data fixture in `tests/data/` is generated deterministically via `tests/data/generate_fixture.py`.
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## API Reference
|
|
196
|
+
|
|
197
|
+
- **[REFERENCE.md](REFERENCE.md)** -- parameter tables, return types
|
|
198
|
+
- **[API docs](https://alanhuang99.github.io/py2sdid/)** -- searchable HTML reference (pdoc)
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## References
|
|
203
|
+
|
|
204
|
+
- Gardner, J. (2021). "Two-Stage Differences in Differences." Working paper.
|
|
205
|
+
- Borusyak, K., Jaravel, X., & Spiess, J. (2024). "Revisiting Event-Study Designs: Robust and Efficient Estimation." *Review of Economic Studies*, 91(6), 3253-3285.
|
|
206
|
+
- Rambachan, A. & Roth, J. (2023). "A More Credible Approach to Parallel Trends." *Review of Economic Studies*, 90(5), 2555-2591.
|
|
207
|
+
- Butts, K. & Gardner, J. (2022). "did2s: Two-Stage Difference-in-Differences." *The R Journal*, 14(1).
|
|
208
|
+
|
|
209
|
+
## License
|
|
210
|
+
|
|
211
|
+
MIT
|
py2sdid-0.1.0/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# py2sdid
|
|
2
|
+
|
|
3
|
+
Two-stage difference-in-differences (Gardner 2021) and BJS imputation estimator (Borusyak, Jaravel, Spiess 2024) for staggered treatment designs in Python, built on polars, scipy (sparse), and numpy.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install py2sdid
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**From source:**
|
|
12
|
+
```bash
|
|
13
|
+
git clone https://github.com/AlanHuang99/py2sdid.git
|
|
14
|
+
cd py2sdid
|
|
15
|
+
pip install -e ".[dev]"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
import polars as pl
|
|
22
|
+
import py2sdid
|
|
23
|
+
|
|
24
|
+
data = pl.read_parquet("panel_data.parquet")
|
|
25
|
+
|
|
26
|
+
result = py2sdid.ts_did(
|
|
27
|
+
data,
|
|
28
|
+
yname="dep_var",
|
|
29
|
+
idname="unit_id",
|
|
30
|
+
tname="year",
|
|
31
|
+
gname="cohort_year",
|
|
32
|
+
cluster_var="cluster_id",
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
result.summary()
|
|
36
|
+
result.plot(kind="event_study")
|
|
37
|
+
result.diagnose()
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Estimators
|
|
41
|
+
|
|
42
|
+
### `ts_did` -- Two-Stage DiD (Gardner 2021)
|
|
43
|
+
|
|
44
|
+
Estimates unit and time fixed effects on untreated observations only, residualizes the outcome for the full sample, and regresses on treatment indicators. Standard errors use GMM influence functions that correct for the generated-regressor problem.
|
|
45
|
+
|
|
46
|
+
### `bjs_did` -- BJS Imputation (Borusyak, Jaravel, Spiess 2024)
|
|
47
|
+
|
|
48
|
+
Same first stage as `ts_did`. Directly averages residuals for treated observations rather than running a second-stage regression. Standard errors use the BJS imputation formula (Equations 6, 8, 10 of the paper).
|
|
49
|
+
|
|
50
|
+
Both produce identical point estimates. They differ in the analytic standard error formula. Both support cluster bootstrap as an alternative.
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Parameters
|
|
55
|
+
|
|
56
|
+
### `ts_did()` / `bjs_did()`
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
ts_did(
|
|
60
|
+
data, yname, idname, tname, gname,
|
|
61
|
+
*, xformla=None, wname=None, cluster_var=None,
|
|
62
|
+
anticipation=0, se=True, bootstrap=False,
|
|
63
|
+
n_bootstraps=500, seed=None, n_jobs=None, verbose=True,
|
|
64
|
+
) -> DiDResult
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
| Parameter | Type | Default | Description |
|
|
68
|
+
|-----------|------|---------|-------------|
|
|
69
|
+
| `data` | `pl.DataFrame` | required | Panel data in long format (one row per unit per period) |
|
|
70
|
+
| `yname` | `str` | required | Outcome variable column |
|
|
71
|
+
| `idname` | `str` | required | Unit identifier column |
|
|
72
|
+
| `tname` | `str` | required | Time period column (integer-valued) |
|
|
73
|
+
| `gname` | `str` | required | Treatment cohort column (see below) |
|
|
74
|
+
| `xformla` | `list[str]` | `None` | Time-varying covariate columns for the first stage |
|
|
75
|
+
| `wname` | `str` | `None` | Observation weight column |
|
|
76
|
+
| `cluster_var` | `str` | `idname` | Column to cluster standard errors on |
|
|
77
|
+
| `se` | `bool` | `True` | Compute standard errors |
|
|
78
|
+
| `bootstrap` | `bool` | `False` | Use cluster bootstrap instead of analytic SEs |
|
|
79
|
+
| `n_bootstraps` | `int` | `500` | Number of bootstrap replications |
|
|
80
|
+
| `seed` | `int` | `None` | Random seed for bootstrap |
|
|
81
|
+
| `n_jobs` | `int` | CPU count | Parallel workers for bootstrap |
|
|
82
|
+
| `verbose` | `bool` | `True` | Print progress |
|
|
83
|
+
|
|
84
|
+
### The `gname` Column
|
|
85
|
+
|
|
86
|
+
The `gname` column encodes the treatment cohort for each unit — the time period when treatment begins.
|
|
87
|
+
|
|
88
|
+
- An integer value (e.g. `2000`) means the unit first receives treatment in that period.
|
|
89
|
+
- `0` or `null` means the unit is never treated.
|
|
90
|
+
- All observations for a given unit should have the same `gname` value.
|
|
91
|
+
|
|
92
|
+
The estimator uses this to determine:
|
|
93
|
+
- Which observations are untreated (used in the first stage): all observations where `tname < gname`, plus all observations from never-treated units.
|
|
94
|
+
- Which observations are treated (used for treatment effect estimation): observations where `tname >= gname`.
|
|
95
|
+
- The relative time (event time) for each observation: `tname - gname`.
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Output
|
|
100
|
+
|
|
101
|
+
Both estimators return a `DiDResult` with these fields:
|
|
102
|
+
|
|
103
|
+
| Field | Type | Description |
|
|
104
|
+
|-------|------|-------------|
|
|
105
|
+
| `att_avg` | `float` | Overall average treatment effect on treated |
|
|
106
|
+
| `att_avg_se` | `float` | Standard error of overall ATT |
|
|
107
|
+
| `att_avg_ci` | `tuple` | 95% confidence interval |
|
|
108
|
+
| `att_avg_pval` | `float` | p-value (H0: ATT = 0) |
|
|
109
|
+
| `event_study` | `pl.DataFrame` | Per-period estimates for all relative time periods |
|
|
110
|
+
| `unit_fe` | `np.ndarray` | Estimated unit fixed effects |
|
|
111
|
+
| `time_fe` | `np.ndarray` | Estimated time fixed effects |
|
|
112
|
+
| `vcov` | `np.ndarray` | Variance-covariance matrix |
|
|
113
|
+
|
|
114
|
+
The `event_study` DataFrame contains columns: `rel_time`, `estimate`, `se`, `ci_lower`, `ci_upper`, `pval`, `count`. It includes all relative time periods present in the data, both pre-treatment and post-treatment.
|
|
115
|
+
|
|
116
|
+
Convenience properties:
|
|
117
|
+
- `result.att_by_horizon` — post-treatment rows only (`rel_time >= 0`)
|
|
118
|
+
- `result.pretrend_tests` — pre-treatment rows only (`rel_time < 0`)
|
|
119
|
+
|
|
120
|
+
### Methods
|
|
121
|
+
|
|
122
|
+
| Method | Description |
|
|
123
|
+
|--------|-------------|
|
|
124
|
+
| `summary()` | Formatted text summary |
|
|
125
|
+
| `plot(kind)` | Matplotlib figure. Kinds: `event_study`, `pretrends`, `treatment_status`, `counterfactual`, `honestdid`, `calendar` |
|
|
126
|
+
| `diagnose()` | Pre-trend F-test, TOST equivalence, HonestDiD sensitivity |
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Data Format
|
|
131
|
+
|
|
132
|
+
Input: `polars.DataFrame` in long panel format.
|
|
133
|
+
|
|
134
|
+
| Column | Type | Description |
|
|
135
|
+
|--------|------|-------------|
|
|
136
|
+
| outcome | float | Outcome variable |
|
|
137
|
+
| unit id | int/str | Unit identifier |
|
|
138
|
+
| time | int | Time period (e.g. year) |
|
|
139
|
+
| cohort | int | Treatment cohort (0 or null = never-treated) |
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
unit_id | year | cohort_year | dep_var
|
|
143
|
+
--------|------|-------------|--------
|
|
144
|
+
1 | 1990 | 2000 | 4.23
|
|
145
|
+
1 | 1991 | 2000 | 4.51
|
|
146
|
+
...
|
|
147
|
+
501 | 1990 | 0 | 3.82
|
|
148
|
+
501 | 1991 | 0 | 3.90
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Testing
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
uv run pytest tests/ # all tests
|
|
157
|
+
uv run pytest tests/test_vs_r.py -v -s # R validation (requires R + did2s + didimputation)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Test data fixture in `tests/data/` is generated deterministically via `tests/data/generate_fixture.py`.
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## API Reference
|
|
165
|
+
|
|
166
|
+
- **[REFERENCE.md](REFERENCE.md)** -- parameter tables, return types
|
|
167
|
+
- **[API docs](https://alanhuang99.github.io/py2sdid/)** -- searchable HTML reference (pdoc)
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## References
|
|
172
|
+
|
|
173
|
+
- Gardner, J. (2021). "Two-Stage Differences in Differences." Working paper.
|
|
174
|
+
- Borusyak, K., Jaravel, X., & Spiess, J. (2024). "Revisiting Event-Study Designs: Robust and Efficient Estimation." *Review of Economic Studies*, 91(6), 3253-3285.
|
|
175
|
+
- Rambachan, A. & Roth, J. (2023). "A More Credible Approach to Parallel Trends." *Review of Economic Studies*, 90(5), 2555-2591.
|
|
176
|
+
- Butts, K. & Gardner, J. (2022). "did2s: Two-Stage Difference-in-Differences." *The R Journal*, 14(1).
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
MIT
|