forest-stream¶
Honest causal forests, regression forests, random forests and decision trees for data with millions of rows, with fixed effects removed by hdfe-stream. The features are binned into a file on disk and the trees are grown in memory-budgeted batches from passes over that file, so a fit needs a few gigabytes however many rows there are.
import forest_stream as fs
# how the effect of a treatment varies: a formula, then the features it may vary with
fit = fs.causal_forest("earnings ~ enrolled | age + education + prior_earnings", "jobs.parquet")
fit.average_treatment_effect() # the average effect, with a standard error
fit.oob_predictions() # each row's effect, with a variance
fit.calibration_test() # is the heterogeneity real?
# with worker and firm fixed effects removed
fs.causal_forest("log_wage ~ trained | tenure + hours | worker_id + firm_id", "panel.parquet")
# prediction
fs.regression_forest("price ~ area + rooms + age | region", "sales.parquet") # a fixed effect
fs.random_forest("default ~ income + debt + age", "loans.parquet") # class labels
fs.decision_tree("default ~ income + debt + age", "loans.parquet", max_depth=4).tree()
The data are a Parquet path, a glob, or a Polars frame. The options have spelled-out names
(features_per_split, variance_group_size); Other implementations
maps them to those of other packages. The average effect and the linear projection are
hdfe-stream results, with its summary(), tidy() and confint(), and variance
estimators are spelled as in hdfe-stream (vcov="HC3", {"CRV1": "school"}).
What it does¶
Function |
Estimates |
Guide |
|---|---|---|
|
How the effect of a treatment differs across rows, with variances and the average effect |
|
|
The same, when the treatment is moved by an instrument rather than assigned: the local effects |
|
|
Conditional quantiles of an outcome, at any levels |
|
|
The conditional mean, with variances |
|
|
The conditional mean, or class probabilities |
|
|
A tree you can read |
Fixed effects (| worker + firm) are removed by backfitting or two
cheaper options; rows can be weighted; fits can
predict for new rows in batches, to a file, and plot.
When to use this, and when not to¶
Use forest-stream when:
The data are large. From a few million rows, forests in memory need several copies of the data and the trees; forest-stream holds the binned features once, on disk, and a batch of trees’ samples in memory.
There are fixed effects. A worker-firm panel, a school-year panel: the fixed effects are estimated with the forest rather than ignored or dummied out (a million dummies are not an option).
You need a causal forest’s inference (standard errors for the average effect, and a variance for each estimate) on data that grf cannot hold.
Use something else when:
The data are small and the fit is a one-off. Under about 100,000 rows grf and ranger fit in seconds and hold everything in memory, and forest-stream’s compiled kernels add a few seconds the first time a process uses them. Small data is not a reason to avoid forest-stream (at 10,000 rows it fitted 3 to 5 times faster than grf in the benchmarks, not counting that compile time), only a reason to keep the tools you already use if you have no need of its other features.
You need what grf offers beyond these forests: multi-arm, survival and local-linear forests, and tuning of the parameters. They are not in forest-stream yet.
You want boosting (gradient boosted trees) or a neural network. This package does forests.
How it works¶
The features are binned (at most 256 bins each) and written to disk once. A forest is then grown a batch of trees at a time: one pass over the file gathers each tree’s sample, the trees are grown in memory, and a second pass fills their leaves. Trees are honest, and variances come from groups of trees that share a half-sample. See How it works, and Memory, disk and threads for the settings that control it.