Bagging & Random Forests

The variance-reduction principle behind bootstrap aggregation, derivation of the out-of-bag error estimate, the random subspace method, and a formal analysis of how decorrelation between trees improves generalization.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Bootstrap Aggregation (Bagging)
  5. Variance Reduction Analysis
  6. Random Forests: Decorrelation via Feature Subsampling
  7. Out-of-Bag Error Estimation
  8. Feature Importance
  9. Theoretical Guarantees
  10. Common Pitfalls
  11. Summary
  12. Exercises

Learning Objectives

  1. Derive why averaging reduces variance but not bias.
  2. Explain the bootstrap and its role in generating diverse base learners.
  3. Derive the variance of bagged estimators in terms of pairwise correlation.
  4. Explain how random feature subsampling reduces correlation between trees.
  5. Derive the out-of-bag error and its relationship to leave-one-out cross-validation.

Notation

  • BB — number of bootstrap samples (trees)
  • f^b(x)\hat{f}_b(\mathbf{x}) — prediction of bb-th tree
  • f^bag(x)=1Bb=1Bf^b(x)\hat{f}_{\text{bag}}(\mathbf{x}) = \frac{1}{B}\sum_{b=1}^B \hat{f}_b(\mathbf{x}) — bagged prediction
  • ρ\rho — average pairwise correlation between tree predictions
  • mm — number of features randomly selected at each split (mdm \leq d)

Core Intuition

Decision trees are high-variance estimators: small data perturbations lead to very different trees. If we could train many trees on independent datasets and average them, variance would decrease by a factor of 1/B1/B. Since we only have one dataset, we approximate independent datasets via bootstrap resampling. Random forests add feature subsampling at each split to further decorrelate trees, dramatically improving the variance reduction.

Random Forest Voting

Trees
5
Single treeEnsemble vote
Explore: Each tree has random axis-aligned splits (faint). More trees → smoother ensemble boundary as votes average out noise.

Bootstrap Aggregation (Bagging)

Algorithm:

  1. For b=1,,Bb = 1, \ldots, B:
    • Draw a bootstrap sample Db\mathcal{D}_b of size nn with replacement from D\mathcal{D}.
    • Fit a decision tree f^b\hat{f}_b on Db\mathcal{D}_b (no pruning — grow to full depth).
  2. Predict: f^bag(x)=1Bb=1Bf^b(x)\hat{f}_{\text{bag}}(\mathbf{x}) = \frac{1}{B}\sum_{b=1}^B \hat{f}_b(\mathbf{x}) (regression) or majority vote (classification).

Variance Reduction Analysis

Let each tree have variance σ2\sigma^2 and pairwise correlation ρ\rho. The variance of the average of BB correlated estimators:

Var(1Bb=1Bf^b)=ρσ2+1ρBσ2.(1)\text{Var}\left(\frac{1}{B}\sum_{b=1}^B \hat{f}_b\right) = \rho\sigma^2 + \frac{1-\rho}{B}\sigma^2. \tag{1}

Derivation. Expand:

Var(1Bbf^b)=1B2(bVar(f^b)+bbCov(f^b,f^b))\text{Var}\left(\frac{1}{B}\sum_b \hat{f}_b\right) = \frac{1}{B^2}\left(\sum_b\text{Var}(\hat{f}_b) + \sum_{b \neq b'}\text{Cov}(\hat{f}_b, \hat{f}_{b'})\right) =1B2(Bσ2+B(B1)ρσ2)=σ2B+B1Bρσ2.(2)= \frac{1}{B^2}(B\sigma^2 + B(B-1)\rho\sigma^2) = \frac{\sigma^2}{B} + \frac{B-1}{B}\rho\sigma^2. \tag{2}

As BB \to \infty: Varρσ2\text{Var} \to \rho\sigma^2. The irreducible floor is determined by correlation ρ\rho.


Random Forests: Decorrelation via Feature Subsampling

Key modification: At each split, only consider a random subset of mm features (out of dd). This reduces ρ\rho without increasing individual tree variance much.

Typical choices:

  • Classification: m=dm = \lfloor\sqrt{d}\rfloor
  • Regression: m=d/3m = \lfloor d/3\rfloor

Effect: If a few features are very strong, bagged trees will all split on them first → high correlation. Random subsampling forces trees to find different structures → lower ρ\rho → lower ensemble variance.


Out-of-Bag Error Estimation

Each bootstrap sample includes approximately 1(11/n)n11/e63.2%1 - (1-1/n)^n \approx 1 - 1/e \approx 63.2\% of the original data. The remaining 36.8%\sim 36.8\% form the out-of-bag (OOB) set for that tree.

OOB prediction for xix_i: Average predictions from all trees whose bootstrap sample did not include xix_i:

f^OOB(xi)=1BibBif^b(xi),Bi={b:(xi,yi)Db}.(3)\hat{f}_{\text{OOB}}(\mathbf{x}_i) = \frac{1}{|B_i|}\sum_{b \in B_i}\hat{f}_b(\mathbf{x}_i), \quad B_i = \{b : (\mathbf{x}_i, y_i) \notin \mathcal{D}_b\}. \tag{3}

OOB error =1ni(f^OOB(xi),yi)= \frac{1}{n}\sum_i\ell(\hat{f}_{\text{OOB}}(\mathbf{x}_i), y_i).

This provides an unbiased estimate of test error without requiring a held-out set (equivalent to leave-one-out CV asymptotically).


Feature Importance

Permutation importance: For feature jj, randomly permute values of xjx_j in the OOB data and measure increase in OOB error:

Imp(j)=1Bb=1B[errb(permj)errb(OOB)].(4)\text{Imp}(j) = \frac{1}{B}\sum_{b=1}^B \left[\text{err}_b^{(\text{perm}_j)} - \text{err}_b^{(\text{OOB})}\right]. \tag{4}

Gini importance: Sum of impurity reductions across all splits on feature jj across all trees.


Theoretical Guarantees

Theorem (Breiman, 2001). The generalization error of a random forest satisfies:

PEρˉ(1s2s2),(5)\text{PE}^* \leq \bar{\rho}\left(\frac{1 - s^2}{s^2}\right), \tag{5}

where ss is the strength (margin) of individual trees and ρˉ\bar{\rho} is mean correlation. Low correlation and high individual strength guarantee low error.


Common Pitfalls

Pitfall 1. Using too few trees. The OOB error stabilizes around B=500B = 50010001000; there is no overfitting from increasing BB (variance monotonically decreases).

Pitfall 2. Expecting random forests to extrapolate. Trees partition the observed feature space; predictions outside the training range default to the nearest leaf value.

Pitfall 3. Interpreting Gini importance as causal. Correlated features share importance; permutation importance on OOB data is more reliable.


Summary

  • Bagging reduces variance by averaging bootstrap-trained trees.
  • Ensemble variance = ρσ2+(1ρ)σ2/B\rho\sigma^2 + (1-\rho)\sigma^2/B; the floor is set by inter-tree correlation.
  • Random forests reduce ρ\rho via random feature subsampling at each split.
  • OOB error provides a free estimate of generalization without a separate test set.
  • More trees never hurts (no overfitting from larger BB).

Exercises

Exercise 1. Derive equation (2) for the variance of the average of BB correlated estimators.

Exercise 2. Show that the probability of a sample being OOB for a given tree converges to 1/e1/e as nn \to \infty.

Exercise 3. For a 1D regression problem where the true function is linear, show that a single decision tree has high variance but averaging many trees reduces this.

Exercise 4. Prove that the bagged predictor has the same bias as a single tree (bias is unchanged by averaging).

Exercise 5. Design an example where reducing mm from dd to d\sqrt{d} dramatically reduces ensemble variance.