One-Line Summary: Training multiple models on bootstrapped samples and averaging predictions -- reducing variance through diversity.

Prerequisites: Bias-variance tradeoff, decision trees, sampling theory, expected value and variance.

What Is Bagging?

Imagine you want to estimate the temperature outside. Instead of trusting a single thermometer that might be miscalibrated, you place ten thermometers around your yard and average their readings. Each thermometer has its own quirks (variance), but averaging them cancels out individual errors, giving you a more stable estimate.

Bagging -- short for Bootstrap AGGregating -- applies this same logic to machine learning. Introduced by Leo Breiman in 1996, bagging trains multiple instances of a base learner on different random subsets of the training data, then combines their predictions through averaging (regression) or majority voting (classification). The random subsets are generated via bootstrap sampling, a statistical resampling technique that draws samples with replacement from the original dataset.

How It Works

Bootstrap Sampling

Given a training set , a bootstrap sample is formed by drawing examples uniformly at random with replacement from . Because sampling is done with replacement, some examples appear multiple times while others are omitted entirely.

The probability that a specific example is not selected in any single draw is:

Over independent draws, the probability of never being selected is:

Therefore, each bootstrap sample contains approximately 63.2% of the unique training examples. The remaining ~36.8% form the out-of-bag (OOB) set for that particular bootstrap iteration.

The Bagging Algorithm

  1. For :
    • Draw a bootstrap sample of size from .
    • Train a base learner on .
  2. Combine predictions:
    • Regression:
    • Classification:

Variance Reduction Proof

The core theoretical justification for bagging rests on a fundamental property of averaging. Let be identically distributed random variables each with variance , and let be the pairwise correlation between any two variables. The variance of their average is:

When predictors are perfectly uncorrelated (), averaging reduces variance by exactly . In practice, bagged models are trained on overlapping data, so . The second term shrinks with more models, but the first term sets a floor. This is precisely why Random Forests introduce additional randomization to reduce further.

Out-of-Bag Estimation

Since each bootstrap sample leaves out ~36.8% of the data, we get a free validation mechanism. For each training example , we collect predictions only from models whose bootstrap sample did not include :

where . The OOB error computed across all training examples is nearly identical to leave-one-out cross-validation error, but comes at no additional computational cost.

Why It Matters

Bagging is one of the most reliable variance-reduction techniques in machine learning. It transforms unstable learners (those with high variance) into stable ensembles without increasing bias appreciably. In practice, bagging is the backbone of Random Forests, one of the most successful algorithms in machine learning history. It also provides built-in error estimation through OOB, eliminating the need for a separate validation set in many scenarios.

Key Technical Details

  • Base learner choice: Bagging benefits models with high variance and low bias -- deep, unpruned decision trees are the canonical choice. Linear models gain little from bagging because they are already low-variance.
  • Number of models : Unlike boosting, bagging does not overfit as increases. Performance monotonically improves and plateaus. Typical values range from 100 to 500.
  • Computational cost: Each bootstrap model is independent, making bagging embarrassingly parallel. Training scales linearly with the number of processors.
  • Bias preservation: Bagging does not reduce bias. If each base learner has bias , the ensemble also has bias because .
  • Bootstrap sample size: While the standard approach draws samples, sub-bagging (using smaller samples without replacement) can sometimes achieve comparable results with less computation.

Common Misconceptions

  • "Bagging always improves any model." Bagging primarily reduces variance. For high-bias models (e.g., shallow trees, linear regression), bagging provides negligible improvement because averaging biased estimators does not remove the bias. The model must be sufficiently complex (low bias, high variance) for bagging to help.

  • "More bagged models always means better performance." Performance plateaus once variance has been reduced to its floor (the term). Beyond that point, additional models increase computation without improving accuracy.

  • "Bootstrap samples are independent." The bootstrap samples overlap substantially (~63.2% unique overlap on average), which introduces correlation between the trained models. This correlation limits the variance reduction achievable by averaging.

  • "OOB error is just an approximation." OOB error has been shown to be asymptotically equivalent to leave-one-out cross-validation. For sufficiently large , it is a highly reliable estimate of generalization error.

Connections to Other Concepts

  • random-forests.md: Extend bagging by adding random feature selection at each split, further decorrelating the ensemble members and pushing closer to zero.
  • adaboost.md: Represents the opposite philosophy -- sequential rather than parallel, focusing on bias reduction rather than variance reduction.
  • gradient-boosting.md: Also sequential, but frames the ensemble as gradient descent in function space. Bagging's subsampling idea appears in stochastic gradient boosting.
  • stacking-and-blending.md: While bagging combines identical model types, stacking combines diverse model families through a learned meta-model.
  • bias-variance-tradeoff.md: Bagging is the textbook intervention for the variance side of the tradeoff without increasing bias.

Further Reading

  • Breiman, "Bagging Predictors" (1996) -- The foundational paper introducing the bagging framework and OOB estimation.
  • Efron and Tibshirani, "An Introduction to the Bootstrap" (1993) -- Comprehensive treatment of bootstrap methods in statistics.
  • Bühlmann and Yu, "Analyzing Bagging" (2002) -- Theoretical analysis proving bagging's variance reduction for unstable learners.
  • Hastie, Tibshirani, and Friedman, "The Elements of Statistical Learning" (2009), Chapter 8 -- Rigorous treatment of bagging within the ensemble learning framework.