Causal forests

causal_forest estimates how the effect of a treatment varies with other characteristics, from observational or experimental data, and gives a standard error for the average effect and a variance for each estimate.

import forest_stream as fs

fit = fs.causal_forest("earnings ~ enrolled | age + education + prior_earnings", "jobs.parquet")
fit.summary()                           # the forest, and how long each step took

fit.average_treatment_effect()          # the average effect, with a standard error
fit.oob_predictions()                   # each row's effect, from the trees that did not use it
fit.predict("new_people.parquet")       # effects (and variances) for other rows
fit.variable_importance()               # which features the trees split on
fit.calibration_test()                  # is the heterogeneity real?
fit.best_linear_projection("jobs.parquet", ["age", "prior_earnings"])
fit.plot("effects")                     # the distribution of the estimates

The formula is outcome ~ treatment | features. The features are the columns the effect may depend on; a column that only affects the outcome belongs there too, because the forest uses it to adjust for who is treated. A third part, after a second |, names fixed effects: "y ~ w | x1 + x2 | worker + firm" (see Fixed effects). The treatment can be binary or continuous. Data are a Parquet path, a glob of paths, or a Polars frame.

When an instrument moves the treatment instead, see Instrumental forests. Quickstart walks through this on simulated data whose true effects are known.

What the forest estimates

The conditional average treatment effect at a row is the difference in the outcome that the treatment makes, for people with that row’s features. The forest estimates it in two steps (Robinson 1988; Nie and Wager 2021):

  1. Centering. The outcome and the treatment are each predicted from the features (and the fixed effects), using only trees that did not see the row, and the predictions are subtracted. What is left in the treatment is the part that is not explained by who the row is, and what is left in the outcome is its variation apart from the features’ own effect. This is what adjusts for selection into treatment on the features.

  2. The causal forest. Trees are grown on the centered values, with splits chosen to separate rows whose treatment effects differ (Athey, Tibshirani and Wager 2019). Each tree is honest: half of its sample places the splits and the other half estimates the effect in each leaf (Wager and Athey 2018). A row’s estimate averages the estimates of the leaves it falls in.

Adjusting for the features removes the bias from selection on them. It does not remove bias from selection on anything that is not a feature: the usual assumption of unconfoundedness is the user’s to defend.

Choosing how to center

nuisance=

What it does

Use it when

"forest" (default)

Two regression forests (outcome, treatment) with out-of-bag predictions; with fixed effects, backfitting

The outcome or treatment depends on the features in nonlinear ways, which it usually does

"linear"

One hdfe-stream regression on the features (and fixed effects)

The relationships are close to linear, and speed matters: it is much faster

With forest centering, nuisance_num_trees sets the trees in each centering forest (default a quarter of num_trees, and at least 200). In a randomized experiment the treatment’s expected value is known; the forest still estimates it, which does no harm and costs little.

The average effect

average_treatment_effect() averages the doubly robust scores of the rows: each row’s estimate plus a correction from its own residual. The average is consistent when either the forest’s estimates or the centering is good, and its standard error accounts for the noise in both (Chernozhukov et al. 2018; Athey, Tibshirani and Wager 2019). With cluster=, the standard error clusters on that column.

Is there heterogeneity?

A forest returns an estimate for every row whether or not the effect varies. Two checks tell them apart:

  • calibration_test() regresses the outcome on the treatment times the average estimate and times the estimate’s departure from the average. The first coefficient should be near one if the average effect is right. The second should be near one if the heterogeneity is right: a positive and significant value means the forest found differences that are there, and a value near zero means it found none. A positive estimate is tested one-sided: halve its p-value.

  • best_linear_projection(data, terms) regresses the doubly robust scores on chosen columns. It reports how the average effect differs with each, without assuming that the effect is linear in them; the data must be the rows the forest was fitted on, with the same order.

variable_importance() counts how often the trees split on each feature, weighting splits near the root most. It says which features the forest used, not that they matter for the effect.

Clusters and weights

cluster="school" samples whole clusters into each tree (so a cluster’s rows are all in or all out), and the average effect’s standard error, the calibration test and the linear projection all cluster on it. Use it when rows within a unit are related, for example the several years of a worker. weights="w" weights the splits, the leaves and the inference, and the regressions on the fixed effects: see Sample weights.

Options that matter

Option

Default

What it does

num_trees

2000

More trees lower the forest’s own noise; the variance estimate needs a few thousand

min_node_size

5

The fewest rows of a tree’s splitting half in a leaf

features_per_split

grf’s rule

The mean number of features tried at a split

min_treatment_variation

0.05

A split must keep this share of the parent’s variation in the treatment in each child

sample_fraction

0.5

The share of rows in each tree’s sample; at most 0.5 with variances

honesty_fraction

0.5

The share of a sample used to place splits

variance_group_size

2

Trees sharing a half-sample, from which variances come; 1 skips variances

The variance estimate is noisier than the forest’s predictions: it averages over groups of trees, and it errs on the high side when there are few groups relative to the sample. With a large sample and the default 2,000 trees, intervals come out wider than they need to be, and their coverage is above the nominal level. More trees narrow them toward the nominal level. In a simulation with a million rows, 2,000 trees gave 98.6 percent coverage for a 95 percent interval and 8,000 trees gave 96.1 percent, with about the same point estimates. The larger forest took four times as long. If you rely on intervals with a million rows or more, raise num_trees to several thousand more.

Other implementations maps these to the options of grf and others. Predicting and plotting covers predict on new data and plot.

References

  • Athey, S., Tibshirani, J. and Wager, S. (2019). Generalized random forests. Annals of Statistics 47(2).

  • Chernozhukov, V., Chetverikov, D., Demirer, M., Duflo, E., Hansen, C., Newey, W. and Robins, J. (2018). Double/debiased machine learning for treatment and structural parameters. The Econometrics Journal 21(1).

  • Nie, X. and Wager, S. (2021). Quasi-oracle estimation of heterogeneous treatment effects. Biometrika 108(2).

  • Robinson, P. (1988). Root-N-consistent semiparametric regression. Econometrica 56(4).

  • Wager, S. and Athey, S. (2018). Estimation and inference of heterogeneous treatment effects using random forests. Journal of the American Statistical Association 113(523).