Quantile forests

quantile_forest estimates the conditional quantiles of an outcome: not only where the outcome is on average for rows like this one, but how far it spreads, and how likely it is to be low or high. It returns any quantile you ask for, for any rows.

import forest_stream as fs

fit = fs.quantile_forest(
    "wage ~ age + education + experience",
    "workers.parquet",
    quantiles=[0.1, 0.5, 0.9],      # the levels the trees are grown for
)
fit.predict("new_workers.parquet")                  # the 10th, 50th and 90th percentiles
fit.predict("new_workers.parquet", [0.05, 0.95])    # other levels, from the same forest
fit.oob_predictions()                               # each row's quantiles, from the trees that did not use it
fit.oob_score()                                     # pinball loss and coverage at each level
fit.variable_importance()

The formula is outcome ~ features. Data are a Parquet path, a glob of paths, or a Polars frame. cluster= samples whole clusters into each tree and weights= weights the splits and the quantiles, as in regression_forest. Fixed effects are not available.

What the forest estimates

A regression forest estimates the mean of the outcome given the features. A quantile forest estimates the whole conditional distribution, as a weighted set of the training outcomes: for a query row, each tree finds the leaf the row falls in, and gives the outcomes of that leaf’s estimation rows equal weight, summing to one; the forest averages those weights over its trees (Meinshausen 2006). The estimate at level q is the smallest outcome whose weight reaches q. It is always one of the training outcomes, and the estimates at higher levels are never lower.

Trees are grown to separate rows whose distributions differ (Athey, Tibshirani and Wager 2019). At each node the rows are labeled by which interval their outcome falls in, the intervals being cut at the node’s own quantiles of the levels in quantiles=, and the split is the one that best separates those labels (the Gini criterion). A forest grown for [0.1, 0.9] therefore looks for splits that move the tails, and one grown for [0.5] looks for splits that move the median. Choose the levels you will use. Other levels can be predicted from the same forest, but the trees were not grown to separate them.

Trees are honest, as in the other forests: half of a tree’s sample places the splits and the other half supplies the outcomes the leaves hold (Wager and Athey 2018).

There is no variance for a quantile, and no confidence interval. oob_score() says how well the quantiles fit the outcomes instead: for each level the mean pinball loss (which the true quantile minimizes) and the share of outcomes at or below the estimate, which should be near the level.

Memory and disk

A quantile forest keeps the outcomes of every tree’s estimation rows on disk, 8 bytes each, so the forest grows with num_trees * n * sample_fraction * (1 - honesty_fraction) (plus the trees’ splits, which the other forests keep too). The outcomes are not held in memory after a batch of trees is finished.

Predicting reads the leaves of every tree for each row, so it costs more than a regression forest’s mean, in proportion to the leaf sizes. Rows are predicted in blocks whose pooled outcomes take up to a quarter of the memory left (max_memory_gb): a larger budget means fewer, larger blocks and a faster prediction, so a prediction of many rows uses more memory than the fit does, up to that share, and a smaller max_memory_gb lowers it at some cost in time.

Options that matter

Option

Default

What it does

quantiles

(0.1, 0.5, 0.9)

The levels the trees are grown to separate

num_trees

2000

More trees smooth the weights, especially in the tails

min_node_size

5

The fewest rows of a tree’s splitting half in a leaf; larger leaves give smoother, less local quantiles

min_child_fraction

0.05

A split must leave each child at least this share of the parent’s rows

sample_fraction, honesty_fraction

0.5, 0.5

The share of rows in each tree, and the share of that used to place splits

Extreme levels need many rows: a quantile at 0.01 rests on the lowest one percent of the weight, so a leaf of five estimation rows rarely holds one of them. Use larger min_node_size and more trees there, and check oob_score()’s coverage.

References

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

  • Meinshausen, N. (2006). Quantile regression forests. Journal of Machine Learning Research 7.

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