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.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Bootstrap Aggregation (Bagging)
- Variance Reduction Analysis
- Random Forests: Decorrelation via Feature Subsampling
- Out-of-Bag Error Estimation
- Feature Importance
- Theoretical Guarantees
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive why averaging reduces variance but not bias.
- Explain the bootstrap and its role in generating diverse base learners.
- Derive the variance of bagged estimators in terms of pairwise correlation.
- Explain how random feature subsampling reduces correlation between trees.
- Derive the out-of-bag error and its relationship to leave-one-out cross-validation.
Notation
- — number of bootstrap samples (trees)
- — prediction of -th tree
- — bagged prediction
- — average pairwise correlation between tree predictions
- — number of features randomly selected at each split ()
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 . 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
Bootstrap Aggregation (Bagging)
Algorithm:
- For :
- Draw a bootstrap sample of size with replacement from .
- Fit a decision tree on (no pruning — grow to full depth).
- Predict: (regression) or majority vote (classification).
Variance Reduction Analysis
Let each tree have variance and pairwise correlation . The variance of the average of correlated estimators:
Derivation. Expand:
As : . The irreducible floor is determined by correlation .
Random Forests: Decorrelation via Feature Subsampling
Key modification: At each split, only consider a random subset of features (out of ). This reduces without increasing individual tree variance much.
Typical choices:
- Classification:
- Regression:
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 → lower ensemble variance.
Out-of-Bag Error Estimation
Each bootstrap sample includes approximately of the original data. The remaining form the out-of-bag (OOB) set for that tree.
OOB prediction for : Average predictions from all trees whose bootstrap sample did not include :
OOB error .
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 , randomly permute values of in the OOB data and measure increase in OOB error:
Gini importance: Sum of impurity reductions across all splits on feature across all trees.
Theoretical Guarantees
Theorem (Breiman, 2001). The generalization error of a random forest satisfies:
where is the strength (margin) of individual trees and 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 –; there is no overfitting from increasing (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 = ; the floor is set by inter-tree correlation.
- Random forests reduce 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 ).
Exercises
Exercise 1. Derive equation (2) for the variance of the average of correlated estimators.
Exercise 2. Show that the probability of a sample being OOB for a given tree converges to as .
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 from to dramatically reduces ensemble variance.