TL;DR
Bagging, short for bootstrap aggregating, builds an ensemble by training the same kind of model many times, each time on a different random resample of the training set. The final prediction is the average of the individual predictions (or a majority vote for classification).
Averaging does not make any single model more accurate on average. What it does is shrink the part of the error that comes from each model reacting to the particular training sample it saw. That makes bagging most useful for flexible, unstable models such as deep decision trees.
Where you'll see it: in random forests, which are bagged decision trees with an extra trick, and in any setting where a single high-variance model gives good answers on average but jumps around from one training set to the next.
Intuition
Many noisy opinions
Suppose you want to estimate the length of a coastline and you give the job to ten people, each with a different ruler and each measuring a slightly different stretch of the shore. Each estimate is unreliable, but their errors point in different directions, so the average is usually much better than a typical single guess. Bagging applies the same logic to models. Each tree is an unreliable estimator, and their mistakes are partly independent, so the average is steadier.
The catch is that the trees must not all make the same mistakes. If every tree were trained on exactly the same data, they would agree, and averaging would change nothing. Bagging creates diversity by giving each tree a different slice of reality, which is what the bootstrap provides.
Why trees benefit most
A deep decision tree fits its training data almost perfectly. Change a handful of points and the splits near the top can move, which changes the predictions a lot. This is high variance. A straight-line model changes much less when the data changes, but it may miss a curve entirely, which is bias. Bagging is designed for the first problem. It leaves the bias of the tree roughly where it is and reduces the wobble. That is why bagging is paired with low-bias, high-variance learners and rarely helps a model that is already stable.
flowchart LR D["Training data (n points)"] --> S1["Bootstrap sample 1"] D --> S2["Bootstrap sample 2"] D --> S3["Bootstrap sample B"] S1 --> M1["Tree 1"] S2 --> M2["Tree 2"] S3 --> M3["Tree B"] M1 --> A["Average of predictions"] M2 --> A M3 --> A
The math
Bootstrap samples
Given a training set of points, a bootstrap sample is a new set of points drawn with replacement from the original. Each draw picks one of the original points, each with probability , and every draw is independent. So a bootstrap sample has the same size as the original, but some points appear several times and others not at all. The resampling idea is covered in Bootstrap Sampling.
How many distinct original points should we expect in one bootstrap sample? A given point is missed by one draw with probability . It is missed by all draws with probability
So on average about of the original points appear in each bootstrap sample. The remaining roughly are "out of bag" for that sample, and they can be used as a free validation set for that model.
The bagged predictor
Let be the prediction at input from the model trained on bootstrap sample , for . The bagged predictor is
For classification, the average is replaced by a majority vote over the predicted labels, or by averaging predicted class probabilities.
What averaging does to variance
Assume each model has the same variance , and every pair of models has the same correlation in their predictions at . The variance of the average is
Two things follow. First, the second term shrinks as , so more models always help, with diminishing returns. Second, the first term does not shrink with . It is the floor set by how correlated the models are. Bagging reduces variance only as far as the bootstrap samples decorrelate the models, and this is why random forests add an extra step to lower .
The expected error decomposes in the usual way, as in the Bias-Variance Tradeoff:
Bagging leaves the bias term essentially unchanged, because the average of the models has about the same mean as each model. It shrinks the variance term.
- rho = 0.5, sigma^2 = 4
- rho = 0.2, sigma^2 = 4
Worked examples
Take a dataset of points, labelled 1 to 5.
- Draw five indices with replacement: .
- The distinct points are . Point 4 is missing, so it is out of bag for this sample.
- Four of five points are present, which is . For the expected fraction is (see step 4), so this draw is a little above average by chance; with so few points the fraction is noisy.
- The chance a fixed point is missing is for , so the expected fraction present is . For the chance is , and as grows the fraction present converges to .
- Interpretation: each tree sees about two thirds of the data, and the rest can score it without a separate validation split.
Each tree has variance at some input. Take trees.
- Case A, correlation : , and . Total variance .
- Case B, correlation : , and . Total variance .
- A single tree has variance , so bagging cut the variance by almost half in case A and by almost three quarters in case B.
- Even with trees, case A cannot go below . Only lower correlation helps further.
- Interpretation: the number of trees is not the only lever. Decorrelating the trees matters as much.
Five bagged regression trees each predict the price of one house, in hundreds of thousands of dollars: .
- The bagged prediction is the mean: .
- Deviations from the mean: .
- Their average square is . This is the spread the average is averaging over, and the bagged prediction sits in the middle of it.
- Suppose the true price is . The bagged error is . The individual errors are , which average , so the bagged error is smaller than the typical single tree's, though one tree (the last) happened to be exact.
- Interpretation: the average smooths out individual trees, but if all five trees had been trained on data that systematically underpriced houses, the average would also be low. Bagging fixes instability, not a bad model.
Tradeoffs and failure modes
Bagging is a strong default for high-variance learners, and it is simple to parallelise because the models do not depend on each other. Its costs are a larger model and slower prediction, since each query runs models. It also loses some interpretability: one tree is readable, a hundred averaged trees are not.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Stable, low-variance model such as linear regression | Little variance to remove, so the gain is small | Use the model directly, or add L2 Regularization |
| Strongly correlated trees, for example one dominant feature | The variance floor stays high | Random forests, which subsample features at each split |
| Very few training points | Bootstrap samples repeat points heavily and miss many | Cross-validation with a careful split |
| Model with high bias, such as a shallow tree | Averaging does not reduce the bias | Boosting, which reduces bias step by step |
| Strong outliers | Each sample that includes an outlier produces a distorted model | Robust losses or outlier removal before bagging |
A common mistake is to bag a model that is already regularised heavily. Each copy is then nearly identical, the correlation is close to one, and the ensemble costs many times more compute for almost no gain.
History
The bootstrap itself was introduced by Bradley Efron in 1979 as a way to estimate the uncertainty of a statistic by resampling the data. Leo Breiman proposed bagging in a technical report in 1994 and published it in the journal Machine Learning in 1996. His argument was simple: averaging predictors built on bootstrap replicates lowers error for unstable methods, and the method needs no change to the underlying learner.
Breiman later combined bagging with random feature selection at each split and called the result a random forest, published in 2001. That combination became one of the most widely used general-purpose learners, and it remains the reference point for bagging today. Bagging also showed a broader pattern that later ensemble methods built on, which is that many modest models can beat one carefully tuned model when their errors are not too alike.
Check yourself
QuestionRecall: what does bagging average over, and why is it called bootstrap aggregating?Show answer
Bagging averages the predictions of many models, each trained on a bootstrap sample. The bootstrap is the random resampling with replacement that creates each training set, and aggregating means combining the predictions by averaging or voting.
QuestionComputation: with training points, about what fraction of the original points appear in a bootstrap sample, and what fraction are out of bag?Show answer
The chance a fixed point is missed by all draws is , which is very close to . So about of the original points appear in each bootstrap sample, and about are out of bag. The out-of-bag points give a validation estimate for each tree without a separate held-out set.
QuestionWhy: why does bagging reduce variance but not bias, and why does it help a deep decision tree more than a linear regression?Show answer
Each model's prediction is an average over the same kind of learner, so the average has roughly the same mean as the individual models. That leaves bias unchanged. A deep tree's predictions swing a lot when the training sample changes, so averaging many of them removes much of that swing. A linear regression already changes little between samples, so there is little variance to remove.
QuestionApplied: you bag 200 trees and find the ensemble barely beats one tree. The trees are almost identical. What would you change, and why?Show answer
Identical trees mean the correlation is near one, so the variance floor stays high no matter how many trees you add. Reduce the correlation: subsample features at each split as random forests do, or draw smaller samples (a smaller fraction of the rows) for each tree. Then recheck the out-of-bag error, since the extra trees will only help once they disagree with each other.
Sources
- Breiman, Bagging predictors, Machine Learning, 1996: https://link.springer.com/article/10.1007/BF00058655
- Bootstrap aggregating, Wikipedia: https://en.wikipedia.org/wiki/Bootstrap_aggregating
- Hastie, Tibshirani, Friedman, The Elements of Statistical Learning, Chapter 8: https://hastie.su.domains/ElemStatLearn/