Metadata-Version: 2.4
Name: forest-stream
Version: 0.1.0
Summary: Decision trees, random forests and causal forests on data that do not fit in memory, with fixed effects removed by hdfe-stream
Author: Lee Tucker
License-Expression: MIT
Project-URL: Homepage, https://github.com/leetucker/forest-stream
Project-URL: Source, https://github.com/leetucker/forest-stream
Project-URL: Issues, https://github.com/leetucker/forest-stream/issues
Project-URL: Documentation, https://leetucker.github.io/forest-stream/
Project-URL: Changelog, https://github.com/leetucker/forest-stream/blob/main/CHANGELOG.md
Keywords: econometrics,causal forest,generalized random forest,random forest,decision tree,heterogeneous treatment effects,fixed effects,out-of-core
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Scientific/Engineering
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE.md
Requires-Dist: hdfe-stream[formula]>=0.13.0
Requires-Dist: stream-limits>=0.3.0
Requires-Dist: polars!=1.41.*,>=1.37
Requires-Dist: numpy>=2.0
Requires-Dist: scipy>=1.13
Requires-Dist: numba>=0.60
Requires-Dist: pyarrow>=16.0
Provides-Extra: benchmark
Requires-Dist: matplotlib>=3.7; extra == "benchmark"
Requires-Dist: psutil>=5.9; extra == "benchmark"
Requires-Dist: econml>=0.15; extra == "benchmark"
Provides-Extra: docs
Requires-Dist: sphinx>=8.0; extra == "docs"
Requires-Dist: myst-parser>=4.0; extra == "docs"
Requires-Dist: furo>=2024.8.6; extra == "docs"
Requires-Dist: sphinx-copybutton>=0.5; extra == "docs"
Requires-Dist: sphinx-design>=0.6; extra == "docs"
Provides-Extra: plot
Requires-Dist: matplotlib>=3.7; extra == "plot"
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == "test"
Requires-Dist: stream-limits[testing]>=0.3.0; extra == "test"
Dynamic: license-file

# forest-stream

Honest causal and instrumental forests, quantile forests, regression forests, random forests and decision trees for data with
millions of rows, with fixed effects removed by [hdfe-stream](https://github.com/leetucker/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.

```python
import forest_stream as fs

# how the effect of a treatment varies: outcome ~ treatment | the features it may vary with
fit = fs.causal_forest("earnings ~ enrolled | age + education + prior_earnings", "jobs.parquet")
fit.average_treatment_effect().summary()   # the average effect, with a standard error
fit.oob_predictions()                      # each row's effect and its variance
fit.calibration_test()                     # is the heterogeneity real?
fit.predict("new_people.parquet")          # effects for other rows, in batches

# the same when an instrument moves the treatment rather than assigning it
fs.instrumental_forest("earnings ~ enrolled | age + education", "jobs.parquet", instrument="offered")

# worker and firm fixed effects, removed by backfitting
fs.causal_forest("log_wage ~ trained | tenure + hours | worker_id + firm_id", "panel.parquet")

# prediction: a numeric outcome (with a fixed effect) or class labels, and one readable tree
fs.regression_forest("price ~ area + rooms + age | region", "sales.parquet")
fs.random_forest("default ~ income + debt + age", "loans.parquet").oob_score()
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. Rows can be weighted, clustered, and
predicted in batches to a file; the fixed effects can be removed by backfitting (the default),
by demeaning the outcome, or by adding group means as features.

## What it does

| Function | Estimates |
|---|---|
| `causal_forest` | How the effect of a treatment differs across rows, with a variance for each estimate and the average effect's standard error; calibration test and best linear projection |
| `instrumental_forest` | The local effects of a treatment that an instrument moves, with the same variances and average |
| `quantile_forest` | Conditional quantiles of an outcome, at any levels |
| `regression_forest` | The conditional mean, with variances for the predictions |
| `random_forest` | The conditional mean, or class probabilities |
| `decision_tree` | One tree, as a table of nodes |

The methods are those of Breiman (2001), Wager and Athey (2018) and Athey, Tibshirani and
Wager (2019); the documentation compares the options with those of grf, ranger, rpart and
EconML, and says where the results differ.

## When to use it, and when not

Use it when the data are too large for a forest in memory, when there are fixed effects
too numerous to enter as dummies, or when you need a causal forest's inference on data that
grf cannot hold. Use grf, ranger or scikit-learn when the data are small (under about 100,000
rows), when you need multi-arm, survival or quantile forests, or when
you want boosting.

## Install

```bash
pip install -e .
```

Python 3.10 or later. It depends on hdfe-stream, stream-limits, Polars, NumPy, numba, SciPy
and pyarrow. See the [documentation](https://leetucker.github.io/forest-stream/).

A sibling of [hdfe-stream](https://github.com/leetucker/hdfe-stream),
[diff-stream](https://github.com/leetucker/diff-stream),
[match-stream](https://github.com/leetucker/match-stream),
[penalized-stream](https://github.com/leetucker/penalized-stream) and
[rd-stream](https://github.com/leetucker/rd-stream).

## License

MIT.
