rd-stream 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.
- rd_stream-0.1.0/CHANGELOG.md +44 -0
- rd_stream-0.1.0/CITATION.cff +22 -0
- rd_stream-0.1.0/LICENSE.md +21 -0
- rd_stream-0.1.0/MANIFEST.in +2 -0
- rd_stream-0.1.0/PKG-INFO +165 -0
- rd_stream-0.1.0/README.md +116 -0
- rd_stream-0.1.0/pyproject.toml +68 -0
- rd_stream-0.1.0/rd_stream/__init__.py +25 -0
- rd_stream-0.1.0/rd_stream/_cluster.py +333 -0
- rd_stream-0.1.0/rd_stream/_engine.py +755 -0
- rd_stream-0.1.0/rd_stream/_kernels.py +494 -0
- rd_stream-0.1.0/rd_stream/_memory.py +230 -0
- rd_stream-0.1.0/rd_stream/_moments.py +371 -0
- rd_stream-0.1.0/rd_stream/_plot_slabs.py +190 -0
- rd_stream-0.1.0/rd_stream/_slabs.py +581 -0
- rd_stream-0.1.0/rd_stream/_window.py +441 -0
- rd_stream-0.1.0/rd_stream/api.py +1175 -0
- rd_stream-0.1.0/rd_stream/placebo.py +229 -0
- rd_stream-0.1.0/rd_stream/plot.py +867 -0
- rd_stream-0.1.0/rd_stream/py.typed +0 -0
- rd_stream-0.1.0/rd_stream/results.py +284 -0
- rd_stream-0.1.0/rd_stream.egg-info/PKG-INFO +165 -0
- rd_stream-0.1.0/rd_stream.egg-info/SOURCES.txt +32 -0
- rd_stream-0.1.0/rd_stream.egg-info/dependency_links.txt +1 -0
- rd_stream-0.1.0/rd_stream.egg-info/requires.txt +26 -0
- rd_stream-0.1.0/rd_stream.egg-info/top_level.txt +1 -0
- rd_stream-0.1.0/setup.cfg +4 -0
- rd_stream-0.1.0/tests/test_api.py +177 -0
- rd_stream-0.1.0/tests/test_grid.py +49 -0
- rd_stream-0.1.0/tests/test_memory.py +86 -0
- rd_stream-0.1.0/tests/test_plot.py +165 -0
- rd_stream-0.1.0/tests/test_references.py +129 -0
- rd_stream-0.1.0/tests/test_variants.py +169 -0
- rd_stream-0.1.0/tests/test_window.py +249 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-11-24)
|
|
4
|
+
|
|
5
|
+
- `rd_estimate`: sharp and fuzzy regression discontinuity, regression kinks and covariate
|
|
6
|
+
adjustment, with rdrobust's estimates, bandwidth selectors and variances (nearest-neighbor,
|
|
7
|
+
HC0 to HC3, and CRV1, CRV2 and CRV3 clusters as `vcov={"CRV1": col}`) and sampling weights. Several outcomes, or a
|
|
8
|
+
list of bandwidths, from one sort.
|
|
9
|
+
- A list of bandwidths is fitted from block power sums of each side, so each further
|
|
10
|
+
bandwidth costs well under a millisecond, whatever the number of observations in its
|
|
11
|
+
window.
|
|
12
|
+
- `rd_bandwidth`: rdbwselect's bandwidths, one selector or all ten.
|
|
13
|
+
- `donut=` on `rd_estimate`, `rd_bandwidth` and `rd_plot`: donut holes, one radius, a
|
|
14
|
+
left/right pair, or a list of them for `rd_estimate` (one fit each, from one read);
|
|
15
|
+
`donut_closed=` says whether the observations exactly at the radius are dropped too.
|
|
16
|
+
- `rd_placebo`: estimates at placebo cutoffs, each on the observations on its side of
|
|
17
|
+
the true cutoff, as a table and a figure.
|
|
18
|
+
- `etable(fits, rows=...)` and `RDResult.with_rows(...)`: which of the three rows to report,
|
|
19
|
+
and a fourth, the conventional estimate with robust inference.
|
|
20
|
+
- `rd_plot`: rdplot's binned means (the eight bin selection methods, or a given number of
|
|
21
|
+
bins) and global polynomial fits, drawn with `.plot()`; several outcomes from one read,
|
|
22
|
+
drawn as a grid of panels; and `memory="window"`, which reads the rows a slab at a time
|
|
23
|
+
from disk.
|
|
24
|
+
- `memory="auto" | "rows" | "by_value" | "window"`, `max_memory_gb=` and
|
|
25
|
+
`memory_reserve=`. With `"by_value"`, which "auto" takes when the running variable has
|
|
26
|
+
at most half as many distinct values as rows (and the fit has no clusters or weights),
|
|
27
|
+
the data are grouped by its value as they are read, and the fit runs on one row per
|
|
28
|
+
value with the same results: several times faster than holding the rows, with memory
|
|
29
|
+
that grows with the distinct values rather than the rows.
|
|
30
|
+
- `memory="window"`, for fits whose rows do not fit and whose running variable is
|
|
31
|
+
continuous, or that have clusters or weights: the full sample's statistics come from
|
|
32
|
+
streaming passes, including exact quartiles and exact distinct counts
|
|
33
|
+
(`hdfe_stream.core.distinct`, or counted from the slabs when they do not fit in one
|
|
34
|
+
pass). Only the rows near the cutoff are held. When a step's
|
|
35
|
+
window does not fit, the rows go to disk (`workdir=`) in slabs by distance from the
|
|
36
|
+
cutoff, read a slab at a time. "auto" takes it when neither other path fits. The
|
|
37
|
+
results are the same as with `"rows"` (all the R reference cases agree within 1e-8 through
|
|
38
|
+
it).
|
|
39
|
+
- `profile=True`: the rows, the distinct values of the running variable, the memory path
|
|
40
|
+
and its memory and disk, without fitting.
|
|
41
|
+
- Documentation (docs/, Sphinx) and examples on the U.S. Senate elections data and on
|
|
42
|
+
ten million simulated rows (examples/).
|
|
43
|
+
- Agreement with R's rdrobust 4.1.1 within 1e-8 on 104 option combinations of rdrobust and
|
|
44
|
+
rdbwselect, and 27 of rdplot (tests/).
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If you use this software in your research, please cite it as below."
|
|
3
|
+
type: software
|
|
4
|
+
title: "rd-stream"
|
|
5
|
+
abstract: "Regression discontinuity with robust bias-corrected inference on large data: R rdrobust 4.1.1's estimates, bandwidth selectors, variances and RD plots, from one sort and compiled, threaded passes, with donut holes, placebo cutoffs, several outcomes and bandwidths per call, and paths for data that do not fit in memory."
|
|
6
|
+
authors:
|
|
7
|
+
- family-names: Tucker
|
|
8
|
+
given-names: Lee
|
|
9
|
+
email: lee.c.tucker@census.gov
|
|
10
|
+
version: 0.1.0
|
|
11
|
+
date-released: 2026-11-24
|
|
12
|
+
license: MIT
|
|
13
|
+
repository-code: "https://github.com/leetucker/rd-stream"
|
|
14
|
+
url: "https://leetucker.github.io/rd-stream/"
|
|
15
|
+
keywords:
|
|
16
|
+
- econometrics
|
|
17
|
+
- regression discontinuity
|
|
18
|
+
- rdrobust
|
|
19
|
+
- local polynomial
|
|
20
|
+
- bandwidth selection
|
|
21
|
+
- robust bias correction
|
|
22
|
+
- out-of-core
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lee Tucker
|
|
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.
|
rd_stream-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rd-stream
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Regression discontinuity with robust bias-corrected inference on large data: rdrobust's estimates from one sort and compiled, threaded passes
|
|
5
|
+
Author: Lee Tucker
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/leetucker/rd-stream
|
|
8
|
+
Project-URL: Source, https://github.com/leetucker/rd-stream
|
|
9
|
+
Project-URL: Issues, https://github.com/leetucker/rd-stream/issues
|
|
10
|
+
Project-URL: Documentation, https://leetucker.github.io/rd-stream/
|
|
11
|
+
Keywords: econometrics,regression discontinuity,rdrobust,local polynomial,bandwidth selection,robust bias correction,out-of-core
|
|
12
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE.md
|
|
26
|
+
Requires-Dist: hdfe-stream>=0.13.0
|
|
27
|
+
Requires-Dist: stream-limits>=0.3.0
|
|
28
|
+
Requires-Dist: polars!=1.41.*,>=1.37
|
|
29
|
+
Requires-Dist: numpy>=2.0
|
|
30
|
+
Requires-Dist: scipy>=1.13
|
|
31
|
+
Requires-Dist: numba>=0.60
|
|
32
|
+
Requires-Dist: pyarrow>=16.0
|
|
33
|
+
Provides-Extra: benchmark
|
|
34
|
+
Requires-Dist: matplotlib>=3.7; extra == "benchmark"
|
|
35
|
+
Requires-Dist: psutil>=5.9; extra == "benchmark"
|
|
36
|
+
Requires-Dist: rdrobust>=2.1.1; extra == "benchmark"
|
|
37
|
+
Requires-Dist: diff-diff>=4.0; extra == "benchmark"
|
|
38
|
+
Provides-Extra: docs
|
|
39
|
+
Requires-Dist: sphinx>=8.0; extra == "docs"
|
|
40
|
+
Requires-Dist: myst-parser>=4.0; extra == "docs"
|
|
41
|
+
Requires-Dist: furo>=2024.8.6; extra == "docs"
|
|
42
|
+
Requires-Dist: sphinx-copybutton>=0.5; extra == "docs"
|
|
43
|
+
Requires-Dist: sphinx-design>=0.6; extra == "docs"
|
|
44
|
+
Provides-Extra: plot
|
|
45
|
+
Requires-Dist: matplotlib>=3.7; extra == "plot"
|
|
46
|
+
Provides-Extra: test
|
|
47
|
+
Requires-Dist: pytest>=7.0; extra == "test"
|
|
48
|
+
Dynamic: license-file
|
|
49
|
+
|
|
50
|
+
# rd-stream
|
|
51
|
+
|
|
52
|
+
Regression discontinuity estimates with robust bias-corrected inference, on
|
|
53
|
+
data with millions to hundreds of millions of rows. It computes what R's
|
|
54
|
+
rdrobust package computes (local-polynomial estimates, the data-driven
|
|
55
|
+
bandwidth selectors, the conventional and robust standard errors, and RD
|
|
56
|
+
plots), and agrees with it to 1e-8 or better.
|
|
57
|
+
|
|
58
|
+
It reads Parquet or a Polars frame and sorts the running variable once. Each
|
|
59
|
+
bandwidth step and the final fit are then compiled, threaded passes over the
|
|
60
|
+
observations within that step's bandwidth. When the running variable takes
|
|
61
|
+
few distinct values (ages, test scores, dates), the fit runs on one row per
|
|
62
|
+
value, which is exact.
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from rd_stream import rd_estimate, rd_placebo, rd_plot
|
|
66
|
+
|
|
67
|
+
fit = rd_estimate("vote ~ margin", "senate.parquet")
|
|
68
|
+
fit.summary()
|
|
69
|
+
|
|
70
|
+
# several outcomes from one sort; a fuzzy design; covariates with clusters
|
|
71
|
+
rd_estimate("y1 + y2 + y3 ~ score", "data/*.parquet", cutoff=60)
|
|
72
|
+
rd_estimate("earnings ~ age", data, cutoff=65, treatment="enrolled")
|
|
73
|
+
rd_estimate("y ~ x", data, covariates=["z1", "z2"], vcov={"CRV2": "school"})
|
|
74
|
+
|
|
75
|
+
# a bandwidth grid, from one sort
|
|
76
|
+
rd_estimate("y ~ x", data, bandwidth=[0.1, 0.15, 0.2, 0.25]).estimates()
|
|
77
|
+
|
|
78
|
+
# robustness: donut holes, and placebo cutoffs on each side of the true one
|
|
79
|
+
rd_estimate("y ~ x", data, donut=[0, 0.01, 0.02])
|
|
80
|
+
rd_placebo("y ~ x", data, placebo_cutoffs=[-0.2, -0.1, 0.1, 0.2]).plot()
|
|
81
|
+
|
|
82
|
+
# binned means and polynomial fits, as rdplot draws them
|
|
83
|
+
rd_plot("vote ~ margin", "senate.parquet").plot(ci=True)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**Version 0.1.0.**
|
|
87
|
+
|
|
88
|
+
Documentation, with examples on the U.S. Senate elections data, is at
|
|
89
|
+
<https://leetucker.github.io/rd-stream/>.
|
|
90
|
+
|
|
91
|
+
## What it does
|
|
92
|
+
|
|
93
|
+
- `rd_estimate` covers sharp and fuzzy designs, regression kinks (`derivative=1`) and
|
|
94
|
+
covariate adjustment.
|
|
95
|
+
- It offers rdrobust's three kernels, any polynomial and bias order, and all ten
|
|
96
|
+
bandwidth selectors, or bandwidths you give.
|
|
97
|
+
- Variances: nearest-neighbor, HC0 to HC3, or clustered (CRV1, CRV2 or CRV3, written
|
|
98
|
+
as in hdfe-stream: `vcov={"CRV1": "school"}`). It takes sampling weights.
|
|
99
|
+
- It has rdrobust's mass-point adjustment, minimum-support check and bandwidth
|
|
100
|
+
restriction.
|
|
101
|
+
- A list of bandwidths costs little more than one: each side's sums come from block
|
|
102
|
+
power sums, and the nearest-neighbor residuals are computed once.
|
|
103
|
+
- `donut=` leaves out the observations within a distance of the cutoff (a radius, a
|
|
104
|
+
left/right pair, or a list of them for one fit each).
|
|
105
|
+
- `rd_bandwidth` gives rdbwselect's bandwidths, one selector or all ten.
|
|
106
|
+
- `rd_placebo` estimates at placebo cutoffs, each from the observations on its side of
|
|
107
|
+
the true cutoff only, and draws the estimates with their robust intervals.
|
|
108
|
+
- `rd_plot` gives rdplot's binned means, with its eight ways of choosing the bins, and the
|
|
109
|
+
global polynomial fits. `.plot()` draws them with matplotlib. Several outcomes
|
|
110
|
+
(`"y1 + y2 ~ x"`) share one read and sort, and `.plot()` draws them as a grid of panels.
|
|
111
|
+
- Results have hdfe-stream's reporting: `summary()`, `tidy()`, `confint()` and
|
|
112
|
+
`summary_json()`. With several outcomes or bandwidths, `estimates()` gives one row
|
|
113
|
+
per fit.
|
|
114
|
+
- A fit reports rdrobust's three rows (conventional, bias-corrected, robust).
|
|
115
|
+
`etable(fits, rows=...)` and `fit.with_rows(...)` choose which to show, including
|
|
116
|
+
`"conventional_robust"`: the conventional estimate with the robust standard error,
|
|
117
|
+
test and interval, as many papers report it.
|
|
118
|
+
- `memory=` says how the data are held; the default, `"auto"`, picks the first that
|
|
119
|
+
applies:
|
|
120
|
+
- `"by_value"`: when the running variable has mass points (ages, scores, dates) and
|
|
121
|
+
the fit has no clusters or weights, the data are grouped by its value as they are
|
|
122
|
+
read. Memory grows with the number of distinct values, and the fit runs on one row
|
|
123
|
+
per value, with the same results. At 10 million rows it is about 7 times faster than
|
|
124
|
+
holding the rows.
|
|
125
|
+
- `"rows"`: the rows are read and sorted once, when they fit the memory budget.
|
|
126
|
+
- `"window"`: the full sample's statistics come from streaming passes, and only the
|
|
127
|
+
rows near the cutoff are held (`rd_plot` reads its rows a slab at a time from disk,
|
|
128
|
+
since its bins need each side's rows in order). When a step's window doesn't fit either, the rows
|
|
129
|
+
are written to disk in slabs by distance from the cutoff, and each step reads them
|
|
130
|
+
a slab at a time. The results are the same as with `"rows"`. 300 million rows took
|
|
131
|
+
about 4.3 minutes under a 2 GB budget (4 threads).
|
|
132
|
+
- `max_memory_gb=` sets the budget, `workdir=` says where the slabs go, and
|
|
133
|
+
`profile=True` reports the memory and disk a fit would need without running it.
|
|
134
|
+
|
|
135
|
+
## When to use it, and when not
|
|
136
|
+
|
|
137
|
+
Use it when an RD fit is slow or doesn't fit in memory. Two cases stand out:
|
|
138
|
+
- **Large samples.** R's rdrobust and diff-diff take minutes at tens of millions of
|
|
139
|
+
rows.
|
|
140
|
+
- **A running variable with many ties.** rdrobust's nearest-neighbor variance slows
|
|
141
|
+
sharply when many observations share each value.
|
|
142
|
+
|
|
143
|
+
For a few thousand observations, rdrobust is already fast. rd-stream agrees with it but
|
|
144
|
+
won't save you time there. rd-stream covers rdrobust, rdbwselect and rdplot; the rest of
|
|
145
|
+
the rdpackages family (rddensity, rdlocrand, rdmulti, rdpower) isn't covered. Fitting
|
|
146
|
+
from slabs on disk is about four times slower than in memory, so it's worth it only when
|
|
147
|
+
the rows don't fit. The documentation's conventions page maps rdrobust's option names to rd-stream's.
|
|
148
|
+
|
|
149
|
+
## Install
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
pip install -e .
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Python 3.10 or later. It depends on hdfe-stream, stream-limits, Polars,
|
|
156
|
+
NumPy, numba, SciPy and pyarrow. Plots need matplotlib (`pip install -e ".[plot]"`).
|
|
157
|
+
|
|
158
|
+
A sibling of [hdfe-stream](https://github.com/leetucker/hdfe-stream),
|
|
159
|
+
[diff-stream](https://github.com/leetucker/diff-stream),
|
|
160
|
+
[match-stream](https://github.com/leetucker/match-stream) and
|
|
161
|
+
[penalized-stream](https://github.com/leetucker/penalized-stream).
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
MIT.
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# rd-stream
|
|
2
|
+
|
|
3
|
+
Regression discontinuity estimates with robust bias-corrected inference, on
|
|
4
|
+
data with millions to hundreds of millions of rows. It computes what R's
|
|
5
|
+
rdrobust package computes (local-polynomial estimates, the data-driven
|
|
6
|
+
bandwidth selectors, the conventional and robust standard errors, and RD
|
|
7
|
+
plots), and agrees with it to 1e-8 or better.
|
|
8
|
+
|
|
9
|
+
It reads Parquet or a Polars frame and sorts the running variable once. Each
|
|
10
|
+
bandwidth step and the final fit are then compiled, threaded passes over the
|
|
11
|
+
observations within that step's bandwidth. When the running variable takes
|
|
12
|
+
few distinct values (ages, test scores, dates), the fit runs on one row per
|
|
13
|
+
value, which is exact.
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from rd_stream import rd_estimate, rd_placebo, rd_plot
|
|
17
|
+
|
|
18
|
+
fit = rd_estimate("vote ~ margin", "senate.parquet")
|
|
19
|
+
fit.summary()
|
|
20
|
+
|
|
21
|
+
# several outcomes from one sort; a fuzzy design; covariates with clusters
|
|
22
|
+
rd_estimate("y1 + y2 + y3 ~ score", "data/*.parquet", cutoff=60)
|
|
23
|
+
rd_estimate("earnings ~ age", data, cutoff=65, treatment="enrolled")
|
|
24
|
+
rd_estimate("y ~ x", data, covariates=["z1", "z2"], vcov={"CRV2": "school"})
|
|
25
|
+
|
|
26
|
+
# a bandwidth grid, from one sort
|
|
27
|
+
rd_estimate("y ~ x", data, bandwidth=[0.1, 0.15, 0.2, 0.25]).estimates()
|
|
28
|
+
|
|
29
|
+
# robustness: donut holes, and placebo cutoffs on each side of the true one
|
|
30
|
+
rd_estimate("y ~ x", data, donut=[0, 0.01, 0.02])
|
|
31
|
+
rd_placebo("y ~ x", data, placebo_cutoffs=[-0.2, -0.1, 0.1, 0.2]).plot()
|
|
32
|
+
|
|
33
|
+
# binned means and polynomial fits, as rdplot draws them
|
|
34
|
+
rd_plot("vote ~ margin", "senate.parquet").plot(ci=True)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**Version 0.1.0.**
|
|
38
|
+
|
|
39
|
+
Documentation, with examples on the U.S. Senate elections data, is at
|
|
40
|
+
<https://leetucker.github.io/rd-stream/>.
|
|
41
|
+
|
|
42
|
+
## What it does
|
|
43
|
+
|
|
44
|
+
- `rd_estimate` covers sharp and fuzzy designs, regression kinks (`derivative=1`) and
|
|
45
|
+
covariate adjustment.
|
|
46
|
+
- It offers rdrobust's three kernels, any polynomial and bias order, and all ten
|
|
47
|
+
bandwidth selectors, or bandwidths you give.
|
|
48
|
+
- Variances: nearest-neighbor, HC0 to HC3, or clustered (CRV1, CRV2 or CRV3, written
|
|
49
|
+
as in hdfe-stream: `vcov={"CRV1": "school"}`). It takes sampling weights.
|
|
50
|
+
- It has rdrobust's mass-point adjustment, minimum-support check and bandwidth
|
|
51
|
+
restriction.
|
|
52
|
+
- A list of bandwidths costs little more than one: each side's sums come from block
|
|
53
|
+
power sums, and the nearest-neighbor residuals are computed once.
|
|
54
|
+
- `donut=` leaves out the observations within a distance of the cutoff (a radius, a
|
|
55
|
+
left/right pair, or a list of them for one fit each).
|
|
56
|
+
- `rd_bandwidth` gives rdbwselect's bandwidths, one selector or all ten.
|
|
57
|
+
- `rd_placebo` estimates at placebo cutoffs, each from the observations on its side of
|
|
58
|
+
the true cutoff only, and draws the estimates with their robust intervals.
|
|
59
|
+
- `rd_plot` gives rdplot's binned means, with its eight ways of choosing the bins, and the
|
|
60
|
+
global polynomial fits. `.plot()` draws them with matplotlib. Several outcomes
|
|
61
|
+
(`"y1 + y2 ~ x"`) share one read and sort, and `.plot()` draws them as a grid of panels.
|
|
62
|
+
- Results have hdfe-stream's reporting: `summary()`, `tidy()`, `confint()` and
|
|
63
|
+
`summary_json()`. With several outcomes or bandwidths, `estimates()` gives one row
|
|
64
|
+
per fit.
|
|
65
|
+
- A fit reports rdrobust's three rows (conventional, bias-corrected, robust).
|
|
66
|
+
`etable(fits, rows=...)` and `fit.with_rows(...)` choose which to show, including
|
|
67
|
+
`"conventional_robust"`: the conventional estimate with the robust standard error,
|
|
68
|
+
test and interval, as many papers report it.
|
|
69
|
+
- `memory=` says how the data are held; the default, `"auto"`, picks the first that
|
|
70
|
+
applies:
|
|
71
|
+
- `"by_value"`: when the running variable has mass points (ages, scores, dates) and
|
|
72
|
+
the fit has no clusters or weights, the data are grouped by its value as they are
|
|
73
|
+
read. Memory grows with the number of distinct values, and the fit runs on one row
|
|
74
|
+
per value, with the same results. At 10 million rows it is about 7 times faster than
|
|
75
|
+
holding the rows.
|
|
76
|
+
- `"rows"`: the rows are read and sorted once, when they fit the memory budget.
|
|
77
|
+
- `"window"`: the full sample's statistics come from streaming passes, and only the
|
|
78
|
+
rows near the cutoff are held (`rd_plot` reads its rows a slab at a time from disk,
|
|
79
|
+
since its bins need each side's rows in order). When a step's window doesn't fit either, the rows
|
|
80
|
+
are written to disk in slabs by distance from the cutoff, and each step reads them
|
|
81
|
+
a slab at a time. The results are the same as with `"rows"`. 300 million rows took
|
|
82
|
+
about 4.3 minutes under a 2 GB budget (4 threads).
|
|
83
|
+
- `max_memory_gb=` sets the budget, `workdir=` says where the slabs go, and
|
|
84
|
+
`profile=True` reports the memory and disk a fit would need without running it.
|
|
85
|
+
|
|
86
|
+
## When to use it, and when not
|
|
87
|
+
|
|
88
|
+
Use it when an RD fit is slow or doesn't fit in memory. Two cases stand out:
|
|
89
|
+
- **Large samples.** R's rdrobust and diff-diff take minutes at tens of millions of
|
|
90
|
+
rows.
|
|
91
|
+
- **A running variable with many ties.** rdrobust's nearest-neighbor variance slows
|
|
92
|
+
sharply when many observations share each value.
|
|
93
|
+
|
|
94
|
+
For a few thousand observations, rdrobust is already fast. rd-stream agrees with it but
|
|
95
|
+
won't save you time there. rd-stream covers rdrobust, rdbwselect and rdplot; the rest of
|
|
96
|
+
the rdpackages family (rddensity, rdlocrand, rdmulti, rdpower) isn't covered. Fitting
|
|
97
|
+
from slabs on disk is about four times slower than in memory, so it's worth it only when
|
|
98
|
+
the rows don't fit. The documentation's conventions page maps rdrobust's option names to rd-stream's.
|
|
99
|
+
|
|
100
|
+
## Install
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
pip install -e .
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Python 3.10 or later. It depends on hdfe-stream, stream-limits, Polars,
|
|
107
|
+
NumPy, numba, SciPy and pyarrow. Plots need matplotlib (`pip install -e ".[plot]"`).
|
|
108
|
+
|
|
109
|
+
A sibling of [hdfe-stream](https://github.com/leetucker/hdfe-stream),
|
|
110
|
+
[diff-stream](https://github.com/leetucker/diff-stream),
|
|
111
|
+
[match-stream](https://github.com/leetucker/match-stream) and
|
|
112
|
+
[penalized-stream](https://github.com/leetucker/penalized-stream).
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
MIT.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "rd-stream"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Regression discontinuity with robust bias-corrected inference on large data: rdrobust's estimates from one sort and compiled, threaded passes"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE.md"]
|
|
12
|
+
authors = [{ name = "Lee Tucker" }]
|
|
13
|
+
keywords = ["econometrics", "regression discontinuity", "rdrobust", "local polynomial",
|
|
14
|
+
"bandwidth selection", "robust bias correction", "out-of-core"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 2 - Pre-Alpha",
|
|
17
|
+
"Intended Audience :: Science/Research",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Programming Language :: Python :: 3.14",
|
|
25
|
+
"Topic :: Scientific/Engineering",
|
|
26
|
+
"Typing :: Typed",
|
|
27
|
+
]
|
|
28
|
+
requires-python = ">=3.10"
|
|
29
|
+
dependencies = [
|
|
30
|
+
# result base classes, reporting, source handling (hdfe_stream.core's stable names)
|
|
31
|
+
"hdfe-stream>=0.13.0",
|
|
32
|
+
"stream-limits>=0.3.0",
|
|
33
|
+
"polars>=1.37,!=1.41.*",
|
|
34
|
+
"numpy>=2.0",
|
|
35
|
+
"scipy>=1.13",
|
|
36
|
+
"numba>=0.60",
|
|
37
|
+
"pyarrow>=16.0",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Homepage = "https://github.com/leetucker/rd-stream"
|
|
42
|
+
Source = "https://github.com/leetucker/rd-stream"
|
|
43
|
+
Issues = "https://github.com/leetucker/rd-stream/issues"
|
|
44
|
+
Documentation = "https://leetucker.github.io/rd-stream/"
|
|
45
|
+
|
|
46
|
+
[project.optional-dependencies]
|
|
47
|
+
benchmark = ["matplotlib>=3.7", "psutil>=5.9", "rdrobust>=2.1.1", "diff-diff>=4.0"]
|
|
48
|
+
docs = ["sphinx>=8.0", "myst-parser>=4.0", "furo>=2024.8.6", "sphinx-copybutton>=0.5",
|
|
49
|
+
"sphinx-design>=0.6"]
|
|
50
|
+
plot = ["matplotlib>=3.7"]
|
|
51
|
+
test = ["pytest>=7.0"]
|
|
52
|
+
|
|
53
|
+
[tool.setuptools.packages.find]
|
|
54
|
+
include = ["rd_stream*"]
|
|
55
|
+
|
|
56
|
+
[tool.setuptools.package-data]
|
|
57
|
+
rd_stream = ["py.typed"]
|
|
58
|
+
|
|
59
|
+
[tool.pytest.ini_options]
|
|
60
|
+
testpaths = ["tests"]
|
|
61
|
+
|
|
62
|
+
[tool.ruff]
|
|
63
|
+
line-length = 100
|
|
64
|
+
target-version = "py310"
|
|
65
|
+
|
|
66
|
+
[tool.ruff.lint]
|
|
67
|
+
select = ["E", "F", "I", "B", "UP"]
|
|
68
|
+
ignore = ["E501", "E731"]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Regression discontinuity on large data.
|
|
2
|
+
|
|
3
|
+
rdrobust's local-polynomial estimates, bandwidth selectors and robust
|
|
4
|
+
bias-corrected inference, and rdplot's binned means, from one sort of the
|
|
5
|
+
running variable (or one grouping by its values) and compiled, threaded
|
|
6
|
+
passes over each bandwidth's observations.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
# first, before polars is imported: a scheduler's or container's thread and
|
|
10
|
+
# memory limits, which set Polars' out-of-core budget (read when it is imported)
|
|
11
|
+
import stream_limits as _limits
|
|
12
|
+
|
|
13
|
+
_limits.apply_scheduler_defaults()
|
|
14
|
+
# No OpenMP wait policy is set: the bandwidth steps' parallel passes follow one
|
|
15
|
+
# another closely, and run 2-5 times slower when idle threads sleep (passive).
|
|
16
|
+
|
|
17
|
+
from .api import BandwidthResult, rd_bandwidth, rd_estimate # noqa: E402
|
|
18
|
+
from .placebo import PlaceboResult, rd_placebo # noqa: E402
|
|
19
|
+
from .plot import RDPlot, RDPlotMulti, rd_plot # noqa: E402
|
|
20
|
+
from .results import RDMulti, RDResult, etable # noqa: E402
|
|
21
|
+
|
|
22
|
+
__version__ = "0.1.0"
|
|
23
|
+
|
|
24
|
+
__all__ = ["BandwidthResult", "PlaceboResult", "RDMulti", "RDPlot", "RDPlotMulti", "RDResult",
|
|
25
|
+
"etable", "rd_bandwidth", "rd_estimate", "rd_placebo", "rd_plot"]
|