Simple Algorithm
Machine Learning · Convergence · 28 min

Random Forest Theory

Strength, correlation, out-of-bag error and consistency.

Why do random forests work so well with so little tuning? Breiman's own analysis ties their error to two numbers, the strength of the individual trees and the correlation between them. The theory since then has proved consistency, first for stylized forests and then for Breiman's own in additive models, recast forests as adaptive nearest neighbors, and turned honest forests into estimators with valid confidence intervals.

What you’ll learn

  • Define the margin, strength and correlation of a forest, show that its error converges as trees are added, and prove Breiman's bound PE∗≤ρˉ(1−s2)/s2PE^*\le\bar\rho(1-s^2)/s^2.
  • Explain out-of-bag estimation of error, strength and correlation, and the infinitesimal-jackknife variance of forest predictions.
  • State the consistency results for averaging classifiers, purely random forests and Breiman's forests, and interpret their conditions.
  • Write forest predictions as adaptive nearest-neighbor weights, use them for quantile regression forests, and state the asymptotic normality of honest forests.

Strength, correlation and Breiman’s bound

Breiman (2001) treats a forest as a collection of classifiers h(x,Θ1),…,h(x,ΘB)h(\mathbf x,\Theta_1),\dots,h(\mathbf x,\Theta_B), where the Θb\Theta_b are i.i.d. random vectors (the bootstrap draws and feature choices of each tree) and the training data are held fixed. The margin of the forest at (x,y)(\mathbf x,y) is its vote fraction for the right class minus the largest vote fraction for any wrong class:

mg(x,y)=1B∑b=1B1{h(x,Θb)=y}−max⁡j≠y1B∑b=1B1{h(x,Θb)=j}\mathrm{mg}(\mathbf x,y)=\frac1B\sum_{b=1}^B\mathbf 1\{h(\mathbf x,\Theta_b)=y\}-\max_{j\ne y}\frac1B\sum_{b=1}^B\mathbf 1\{h(\mathbf x,\Theta_b)=j\}
The forest misclassifies (x,y)(\mathbf x,y) when the margin is negative, so its generalization error is PE∗=Pr⁡x,y(mg(x,y)<0)PE^*=\Pr_{\mathbf x,y}\big(\mathrm{mg}(\mathbf x,y)<0\big).

Define the strength s=Ex,y mr(x,y)s=\E_{\mathbf x,y}\,\mathrm{mr}(\mathbf x,y). Let ȷ^(x,y)=arg max⁡j≠yPr⁡Θ(h(x,Θ)=j)\hat\jmath(\mathbf x,y)=\argmax_{j\ne y}\Pr_\Theta(h(\mathbf x,\Theta)=j) be the strongest wrong class, and define a single tree's raw margin rmg(Θ,x,y)=1{h(x,Θ)=y}−1{h(x,Θ)=ȷ^(x,y)}\mathrm{rmg}(\Theta,\mathbf x,y)=\mathbf 1\{h(\mathbf x,\Theta)=y\}-\mathbf 1\{h(\mathbf x,\Theta)=\hat\jmath(\mathbf x,y)\}, so that mr=EΘ[rmg]\mathrm{mr}=\E_\Theta[\mathrm{rmg}]. Write sd(Θ)\mathrm{sd}(\Theta) for the standard deviation of rmg(Θ,⋅)\mathrm{rmg}(\Theta,\cdot) over (x,y)(\mathbf x,y), ρ(Θ,Θ′)\rho(\Theta,\Theta') for the correlation between two trees' raw margins over (x,y)(\mathbf x,y), and ρˉ=E[ρ sd sd′]/E[sd sd′]\bar\rho=\E[\rho\,\mathrm{sd}\,\mathrm{sd}']/\E[\mathrm{sd}\,\mathrm{sd}'] for its mean weighted by the standard deviations, over independent Θ,Θ′\Theta,\Theta'.

DerivationProof
  1. PE∗=Pr⁡(mr<0) ≤ Var⁡x,y(mr)s2PE^*=\Pr(\mathrm{mr}<0)\ \le\ \frac{\Var_{\mathbf x,y}(\mathrm{mr})}{s^2}

    Chebyshev: with s>0s>0, mr<0\mathrm{mr}<0 forces ∣mr−s∣>s\abs{\mathrm{mr}-s}>s, so

  2. Var⁡(mr)=EΘ,Θ′[Cov⁡x,y(rmg(Θ),rmg(Θ′))]=EΘ,Θ′[ρ(Θ,Θ′) sd(Θ) sd(Θ′)]=ρˉ (EΘ sd(Θ))2\Var(\mathrm{mr})=\E_{\Theta,\Theta'}\big[\Cov_{\mathbf x,y}\big(\mathrm{rmg}(\Theta),\mathrm{rmg}(\Theta')\big)\big]=\E_{\Theta,\Theta'}\big[\rho(\Theta,\Theta')\,\mathrm{sd}(\Theta)\,\mathrm{sd}(\Theta')\big]=\bar\rho\,\big(\E_\Theta\,\mathrm{sd}(\Theta)\big)^2

    Since ȷ^\hat\jmath depends on (x,y)(\mathbf x,y) but not on Θ\Theta, mr2=EΘ,Θ′[rmg(Θ) rmg(Θ′)]\mathrm{mr}^2=\E_{\Theta,\Theta'}\big[\mathrm{rmg}(\Theta)\,\mathrm{rmg}(\Theta')\big] for independent Θ,Θ′\Theta,\Theta'. Subtracting the squared mean turns the variance into an average covariance, and independence gives E[sd(Θ) sd(Θ′)]=(EΘsd(Θ))2\E[\mathrm{sd}(\Theta)\,\mathrm{sd}(\Theta')]=(\E_\Theta\mathrm{sd}(\Theta))^2:

  3. EΘ sd(Θ)2=EΘEx,y[rmg2]−EΘ[(Ex,yrmg)2] ≤ 1−s2\E_\Theta\,\mathrm{sd}(\Theta)^2=\E_\Theta\E_{\mathbf x,y}\big[\mathrm{rmg}^2\big]-\E_\Theta\Big[\big(\E_{\mathbf x,y}\mathrm{rmg}\big)^2\Big]\ \le\ 1-s^2

    By Jensen, (EΘsd)2≤EΘsd2(\E_\Theta\mathrm{sd})^2\le\E_\Theta\mathrm{sd}^2. A raw margin takes values in {−1,0,1}\{-1,0,1\}, so its second moment is at most 1, and EΘ[(Ex,yrmg)2]≥s2\E_\Theta\big[(\E_{\mathbf x,y}\mathrm{rmg})^2\big]\ge s^2:

  4. PE∗ ≤ ρˉ (1−s2)s2PE^*\ \le\ \frac{\bar\rho\,(1-s^2)}{s^2}

    Combine the three steps.

Predict before you look

A forest has strength s=0.8s=0.8 and mean correlation ρˉ=0.1\bar\rho=0.1. What does Breiman's bound give for its error, in percent?

Show the answer

5.6 %

0.1×(1−0.64)/0.64≈0.0560.1\times(1-0.64)/0.64\approx0.056, about 5.6%. With ρˉ=0.3\bar\rho=0.3 the bound triples to about 17%, and with s=0.5s=0.5, ρˉ=0.3\bar\rho=0.3 it is 90%, nearly useless. That is typical: the bound explains the trade-off better than it predicts the error.

The bound is loose, but its shape is the lesson: accuracy needs strong trees (large ss) that are weakly correlated (small ρˉ\bar\rho). Reducing mtrym_{\text{try}}, the number of features tried per split, lowers ρˉ\bar\rho and ss together, which is why its best value sits in the middle. Breiman estimated ss and ρˉ\bar\rho from out-of-bag votes to watch this trade-off as mtrym_{\text{try}} varied.

Out-of-bag estimation and the variance of predictions

For each training example ii, the out-of-bag classifier votes only with the trees whose bootstrap samples excluded ii, about B/eB/e of them. Its error over the training set is the out-of-bag (OOB) error, the leave-one-out bootstrap error of the ensemble (The Bootstrap); its votes also give out-of-bag estimates of the strength and the correlation. Each OOB prediction uses only about 37% of the trees, so for small BB the OOB error overstates the full forest's error, and the gap closes as BB grows.

The same bookkeeping estimates the sampling variance of a regression forest's prediction μ^(x)\hat\mu(\mathbf x). With NbiN_{bi} the number of times example ii appears in tree bb's sample and Tb(x)T_b(\mathbf x) that tree's prediction, the infinitesimal jackknife estimate is Var⁡^IJ(x)=∑iCov⁡^b(Nbi,Tb(x))2\widehat{\Var}_{\mathrm{IJ}}(\mathbf x)=\sum_i\widehat{\Cov}_b\big(N_{bi},T_b(\mathbf x)\big)^2. Wager, Hastie & Efron (2014) studied it and the jackknife-after-bootstrap for random forests, including corrections for the Monte Carlo noise of a finite number of trees, which otherwise inflates the estimate.

Consistency

Is a random forest consistent, approaching the Bayes risk as n→∞n\to\infty? The answer depends on which forest.

Purely random forests isolate the two conditions of the partitioning theorem (Trees as Adaptive Partitions): cells shrink, yet each holds many points. Breiman's own algorithm, whose splits depend on the labels through the CART criterion, resisted analysis until Scornet, Biau & Vert (2015) proved its consistency for additive regression models, y=∑jmj(xj)+εy=\sum_jm_j(x_j)+\varepsilon with uniform inputs and Gaussian noise, for forests grown on subsamples under conditions on the subsample and tree sizes; their analysis also shows how forests adapt to sparsity. In a stylized "centered" forest model, Biau (2012) showed that the rate of convergence depends only on the number of strong features, not on how many noise features there are. Consistency of Breiman's forests for general regression functions, without structural assumptions such as additivity, remains open as of the mid-2020s (Biau & Scornet, 2016, survey the results up to then).

Check your understanding

Starting from bagging (mtry=dm_{\text{try}}=d), you lower mtrym_{\text{try}}. In Breiman's bound, what typically happens?

  1. The strength ss rises and the correlation ρˉ\bar\rho falls, so the bound always improves
  2. Both ss and ρˉ\bar\rho fall, so the bound first improves and then worsens
  3. Neither changes, because each tree is still grown to full depth
  4. The correlation rises, because the trees use fewer features
Show the answer

B. Both ss and ρˉ\bar\rho fall, so the bound first improves and then worsens

Restricting splits makes trees more diverse (lower ρˉ\bar\rho) but individually weaker (lower ss). The bound ρˉ(1−s2)/s2\bar\rho(1-s^2)/s^2 falls while the drop in correlation dominates, and rises once the trees become too weak. Mentch & Zhou (2020) argue that this extra randomness acts like a regularization penalty, helping most when the signal-to-noise ratio is low.

Forests as adaptive nearest neighbors

A regression forest predicts with a weighted average of training responses. With Lb(x)L_b(\mathbf x) the leaf of tree bb that contains x\mathbf x (counting each training point with its multiplicity in tree bb's sample):

μ^(x)=∑i=1nαi(x) yi,αi(x)=1B∑b=1B1{xi∈Lb(x)}∣Lb(x)∣\hat\mu(\mathbf x)=\sum_{i=1}^n\alpha_i(\mathbf x)\,y_i,\qquad\alpha_i(\mathbf x)=\frac1B\sum_{b=1}^B\frac{\mathbf 1\{\mathbf x_i\in L_b(\mathbf x)\}}{\abs{L_b(\mathbf x)}}
The weights are nonnegative and sum to 1, and they are large for training points that often share a leaf with x\mathbf x.
Think first A forest has two trees. In tree 1, the leaf containing x\mathbf x holds training points {1,2}\{1,2\}; in tree 2 it holds {2,3,4,5}\{2,3,4,5\}. What weights αi(x)\alpha_i(\mathbf x) do the five points receive?

α2=12(12+14)=0.375\alpha_2=\tfrac12\big(\tfrac12+\tfrac14\big)=0.375, α1=12⋅12=0.25\alpha_1=\tfrac12\cdot\tfrac12=0.25, and α3=α4=α5=12⋅14=0.125\alpha_3=\alpha_4=\alpha_5=\tfrac12\cdot\tfrac14=0.125; they sum to 1. Point 2 shares a leaf with x\mathbf x in both trees, which makes it the closest neighbor in the forest's learned sense.

Lin & Jeon (2006) observed that this makes random forests adaptively weighted potential nearest neighbor methods. Unlike kk-nearest neighbors (knn), the neighborhoods are learned: they stretch along directions where the response is flat and shrink where it changes, and they are unaffected by monotone rescaling of any feature. Two powerful generalizations reuse the weights αi(x)\alpha_i(\mathbf x):

F^(t∣x)=∑i=1nαi(x) 1{yi≤t},q^τ(x)=inf⁡{t:F^(t∣x)≥τ}\hat F(t\mid\mathbf x)=\sum_{i=1}^n\alpha_i(\mathbf x)\,\mathbf 1\{y_i\le t\},\qquad\hat q_\tau(\mathbf x)=\inf\big\{t:\hat F(t\mid\mathbf x)\ge\tau\big\}
Quantile regression forests (Meinshausen, 2006) estimate the whole conditional distribution of yy given x\mathbf x and read off conditional quantiles, which give prediction intervals; Meinshausen proved them consistent. Generalized random forests (Athey, Tibshirani & Wager, 2019) use forest weights to solve local estimating equations ∑iαi(x)ψθ(Oi)=0\sum_i\alpha_i(\mathbf x)\psi_\theta(O_i)=0 for any target defined by moment conditions, such as quantiles, local regression coefficients or treatment effects, with consistency and asymptotic normality.

Honest forests and confidence intervals

The proof treats the forest as a U-statistic, the average of one honest tree's prediction over all subsamples of size sns_n, and shows with a Hájek projection that its linear part dominates; honesty and the regularity conditions keep the bias below the standard deviation and the linear part non-degenerate. Mentch & Hooker (2016) had used the U-statistic view earlier to obtain asymptotic normality for subsampled ensembles. The same machinery gives confidence intervals for heterogeneous treatment effects in causal forests (Causal Inference with Machine Learning).

Key takeaways

  • A forest's error converges as trees are added (Breiman, 2001), so more trees never cause overfitting.
  • With strength s=E mr>0s=\E\,\mathrm{mr}>0 and weighted mean correlation ρˉ\bar\rho of the raw margins, PE∗≤ρˉ(1−s2)/s2PE^*\le\bar\rho(1-s^2)/s^2, from Chebyshev plus Var⁡(mr)=ρˉ(E sd)2≤ρˉ(1−s2)\Var(\mathrm{mr})=\bar\rho(\E\,\mathrm{sd})^2\le\bar\rho(1-s^2). Lowering mtrym_{\text{try}} lowers both ρˉ\bar\rho and ss.
  • Out-of-bag votes estimate the error, strength and correlation, and the infinitesimal jackknife estimates the variance of predictions from the same resamples.
  • Voting preserves consistency; purely random forests are consistent when leaves grow in number but more slowly than nn; Breiman's forests are consistent in additive models (Scornet, Biau & Vert, 2015); the general case is open.
  • Forest predictions are adaptive nearest-neighbor averages ∑iαi(x)yi\sum_i\alpha_i(\mathbf x)y_i. The same weights give quantile regression forests and generalized random forests, and honest subsampled forests are asymptotically normal with jackknife-based confidence intervals.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A forest has strength s=0.6s=0.6 and mean correlation ρˉ=0.2\bar\rho=0.2. What is Breiman's upper bound on its generalization error?

  1. 0.13
  2. 0.36
  3. 0.56
  4. 1.78
Show the answer

B. 0.36

ρˉ(1−s2)/s2=0.2×0.64/0.36≈0.356\bar\rho(1-s^2)/s^2=0.2\times0.64/0.36\approx0.356.

Question 2

What does Breiman's convergence theorem say about adding trees to a forest?

  1. The error converges to the Bayes error
  2. The error falls to zero
  3. The error converges almost surely to a limit set by the limiting margin, so more trees do not cause overfitting
  4. The error eventually rises, so the number of trees must be tuned
Show the answer

C. The error converges almost surely to a limit set by the limiting margin, so more trees do not cause overfitting

By the strong law of large numbers, the vote fractions converge, so PE∗→Pr⁡(mr<0)PE^*\to\Pr(\mathrm{mr}<0). The limit is the infinite forest's error, which can be well above the Bayes error, but adding trees never pushes the error up in the limit.

Question 3

In a regression forest, which training points receive positive weight αi(x)\alpha_i(\mathbf x) in the prediction at x\mathbf x?

  1. Exactly the kk nearest points in Euclidean distance
  2. All points, equally
  3. Only the points with the largest residuals
  4. Exactly the points that share a leaf with x\mathbf x in at least one tree
Show the answer

D. Exactly the points that share a leaf with x\mathbf x in at least one tree

αi(x)=1B∑b1{xi∈Lb(x)}/∣Lb(x)∣\alpha_i(\mathbf x)=\frac1B\sum_b\mathbf 1\{\mathbf x_i\in L_b(\mathbf x)\}/\abs{L_b(\mathbf x)} is positive exactly when xi\mathbf x_i falls in x\mathbf x's leaf for some tree. These "potential nearest neighbors" are chosen adaptively by the splits, not by a fixed metric.

Question 4

Why does Wager and Athey's asymptotic normality theorem require honest trees grown on subsamples?

  1. Honesty keeps the leaf estimates free of the bias of splits chosen on the same responses, and subsampling with sn≪ns_n\ll n makes the forest a U-statistic whose linear part dominates
  2. Subsampling makes training faster, and honesty makes the trees deeper
  3. Both are needed only so that the out-of-bag error can be computed
  4. They make every tree identical, so the forest's variance is that of one tree
Show the answer

A. Honesty keeps the leaf estimates free of the bias of splits chosen on the same responses, and subsampling with sn≪ns_n\ll n makes the forest a U-statistic whose linear part dominates

Adaptive leaf estimates are biased toward the splits that made them look good; honesty removes this. Viewing the forest as an average over subsamples lets a Hájek projection show the estimator is approximately a sum of independent terms, hence asymptotically normal, with variance estimable by the infinitesimal jackknife.

Further reading

  • L. Breiman, "Random forests," Machine Learning 45(1), 5–32 (2001).
  • G. Biau, L. Devroye & G. Lugosi, "Consistency of random forests and other averaging classifiers," Journal of Machine Learning Research 9, 2015–2033 (2008).
  • E. Scornet, G. Biau & J.-P. Vert, "Consistency of random forests," Annals of Statistics 43(4), 1716–1741 (2015).
  • S. Wager & S. Athey, "Estimation and inference of heterogeneous treatment effects using random forests," Journal of the American Statistical Association 113(523), 1228–1242 (2018).
  • G. Biau & E. Scornet, "A random forest guided tour," TEST 25(2), 197–227 (2016).