How it works

forest-stream grows the same trees that other forest packages grow. What differs is how the data are held: the features are reduced to bins and kept in a file on disk, and the trees are grown a batch at a time from samples gathered in passes over that file, so the rows never have to fit in memory together.

Bins

Each feature is cut into at most 256 bins at its quantiles (estimated from a sample of the rows), and the first pass writes every row’s bin numbers, treatment and outcome, and sampling unit to a binned copy on disk: one byte per feature, 24 bytes of the rest. Splits are placed at bin boundaries. With few distinct values in a feature (a dummy, a count, a year) the bins are the values and nothing is lost; with a continuous feature, 256 quantile bins lose little, and the reference tests against rpart agree to 1e-8 on data where the bins are exact (Other implementations). Fewer bins (max_bins=) trade accuracy for speed.

Samples without a record of them

Each tree is grown on a sample of the rows (or of the clusters). Whether a row is in a tree’s sample, and in which half of the sample, is a function of the row’s sampling unit and the tree, computed from a hash. Nothing records which rows a tree has, so a pass over the file can gather the samples of any batch of trees, and the out-of-bag trees of a row can be found again when the predictions are made.

Batches of trees

The trees are grown a batch at a time. A pass over the binned file gathers the splitting sample of each tree in the batch into memory, and the batch’s trees are grown there, in parallel, one tree to a thread. Honest trees need one more step: a second pass sends the other half of each tree’s sample down the tree and fills each leaf’s statistics. That pass is shared with the next batch’s gathering, so a forest of B trees in batches of T makes about B/T + 1 passes.

How many trees are in a batch depends on the memory allowed (max_memory_gb): a larger batch means fewer passes over the file, up to eight trees for each thread. More than that holds memory without saving time, because the time spent on trees does not depend on how they are batched. See Memory, disk and threads.

A tree is grown by splitting the node with the largest gain until a node holds too few rows. For a causal tree the gain is grf’s: the node’s treatment effect is estimated, each row gets a pseudo-outcome from its departure from that estimate, and the split that best separates the pseudo-outcomes wins, subject to each child keeping enough of the parent’s variation in the treatment. For a regression tree it is the reduction in squared error, and for a classification tree the Gini or entropy gain. At each node the histograms of all the candidate features are built in one pass over the node’s rows.

Estimates

A leaf holds weighted sums of the treatment and outcome of the estimation half. A forest averages the leaf means of the trees (all the trees for new rows, and for a training row only the trees whose sample does not hold it). A causal forest solves the local moment equation for the effect from the averaged means. These sums are computed in the compiled kernels, and the trees are read from memory-mapped files, so predicting for many rows costs little memory beyond the rows being predicted.

Variances

Trees come in groups of variance_group_size that share a half-sample. The variance of an estimate is the variation between groups’ average estimates, less the variation within groups that the finite number of trees adds, and a debiasing step keeps the result positive (Wager and Athey 2018; Athey, Tibshirani and Wager 2019). The pieces of this are accumulated across batches, one pass over the trees for the average and one for the variation.

Centering

A causal forest first centers the outcome and treatment with regression forests of each on the features. These are out of bag, so a row is never centered with trees that used it. With fixed effects, they are backfit: a forest and the group effects take turns until the effects stop changing, and the rounds after the first keep the first round’s trees and estimate their leaves again, which needs no growing. See Fixed effects for the options and what to expect.

The average effect

The average is of each row’s doubly robust score, the estimate plus a correction from the row’s centered outcome and treatment. The correction needs the variance of the treatment given the features, from one more regression forest. The standard error is that of the mean of the scores, clustered if the forest is.

References

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

  • Breiman, L. (2001). Random forests. Machine Learning 45(1).

  • Breiman, L., Friedman, J., Olshen, R. and Stone, C. (1984). Classification and Regression Trees. Wadsworth.

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

  • Oza, N. and Russell, S. (2001). Online bagging and boosting. Artificial Intelligence and Statistics 2001.

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