2.3.6Tree-Based & Instance Methods

Bagging and bootstrap aggregating

1,761 words8 min readdifficulty · medium3 backlinks

WHAT is Bagging?

The word literally decomposes: Bootstrap aggregatingBagging.


WHY does averaging help? (Derivation from scratch)

Suppose each base model bb produces a prediction f^b(x)\hat f_b(x) that we treat as a random variable (randomness comes from the resampled training data). Write its mean and variance:

E[f^b(x)]=μ,Var(f^b(x))=σ2.\mathbb{E}[\hat f_b(x)] = \mu, \qquad \mathrm{Var}(\hat f_b(x)) = \sigma^2.

The bagged predictor is the average:

fˉ(x)=1Bb=1Bf^b(x).\bar f(x) = \frac{1}{B}\sum_{b=1}^{B}\hat f_b(x).

Step 1 — the mean is unchanged. Why? Expectation is linear: E[fˉ(x)]=1BbE[f^b(x)]=1BBμ=μ.\mathbb{E}[\bar f(x)] = \frac{1}{B}\sum_b \mathbb{E}[\hat f_b(x)] = \frac{1}{B}\cdot B\mu = \mu. So bagging does not change bias. (This is crucial: it can only touch variance.)

Step 2 — the variance shrinks. Assume each model has variance σ2\sigma^2 and pairwise correlation ρ\rho between any two models. Using Var(aiXi)=iai2Var(Xi)+ijaiajCov(Xi,Xj)\mathrm{Var}\big(\sum a_i X_i\big)=\sum_i a_i^2\mathrm{Var}(X_i)+\sum_{i\ne j}a_ia_j\mathrm{Cov}(X_i,X_j) with ai=1/Ba_i=1/B:

Var(fˉ)=1B2[Bσ2diagonal+B(B1)ρσ2off-diagonal].\mathrm{Var}(\bar f) = \frac{1}{B^2}\Big[\underbrace{B\sigma^2}_{\text{diagonal}} + \underbrace{B(B-1)\rho\sigma^2}_{\text{off-diagonal}}\Big].

Step 3 — simplify.   Var(fˉ)=ρσ2+1ρBσ2  \boxed{\;\mathrm{Var}(\bar f) = \rho\sigma^2 + \frac{1-\rho}{B}\sigma^2\;}


HOW to do it (the algorithm)

Figure — Bagging and bootstrap aggregating

The Bootstrap: WHY resampling works

Each bootstrap sample has size nn but is drawn with replacement, so some points appear multiple times and some are missing.

How many points are left out? The probability a specific point is not chosen in one draw is 11n1-\frac1n. Over nn independent draws:

P(point i absent)=(11n)nne10.368.P(\text{point } i \text{ absent}) = \left(1-\frac1n\right)^n \xrightarrow{n\to\infty} e^{-1}\approx 0.368.


Worked examples


Common mistakes (steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine one weather-guesser who's often wrong in random ways — sometimes too high, sometimes too low. Now ask many friends the same question, but give each friend a slightly shuffled copy of yesterday's weather facts. Each friend still guesses randomly-wrongly, but when you average all their guesses, the too-highs and too-lows cancel out and you land near the truth. The trick: shuffle the facts by picking cards from a deck and putting each card back before the next pick — so every friend gets a different deck. Some cards never get picked (about a third) — you use those to secretly grade each friend for free.


Flashcards

What does "bagging" stand for?
Bootstrap AGGregating — train on bootstrap resamples, then aggregate.
A bootstrap sample of size n is drawn how?
n points sampled with replacement from the n-point dataset.
Does bagging reduce bias or variance?
Variance only; bias is unchanged because E[fˉ]=μ\mathbb{E}[\bar f]=\mu.
Variance formula for a bagged ensemble?
Var(fˉ)=ρσ2+1ρBσ2\mathrm{Var}(\bar f)=\rho\sigma^2+\frac{1-\rho}{B}\sigma^2.
As B→∞ the ensemble variance approaches what?
ρσ2\rho\sigma^2 — the floor set by inter-model correlation.
What fraction of data is out-of-bag per model, and why?
~37%, since (11/n)ne1(1-1/n)^n\to e^{-1}.
What is OOB error and why is it useful?
Predict each point using only trees that didn't train on it — free validation without a held-out set.
Why bag unstable learners like deep trees?
They change a lot across resamples → low correlation ρ → bigger variance reduction.
What single quantity must you lower to beat the ρσ2\rho\sigma^2 floor?
The pairwise correlation ρ (motivates Random Forests).
Can increasing B (number of trees) cause overfitting?
No — larger B just converges to the averaging limit; it never overfits by itself.

Connections

  • Decision Trees — the canonical high-variance base learner bagging fixes.
  • Random Forests — bagging plus feature subsampling to reduce ρ\rho.
  • Bias-Variance Tradeoff — bagging attacks the variance term.
  • Bootstrap (statistics) — the resampling engine behind bagging.
  • Boosting — contrast: reduces bias sequentially, can overfit.
  • Out-of-Bag Error — the free validation estimate.
  • Ensemble Methods — the umbrella family.

Concept Map

creates

aggregate

averages

mean unchanged

variance shrinks

formula

B to infinity

more trees

lower correlation

motivates

Bootstrap resampling

Many base models

Bagged predictor

Ensemble prediction

Bias stays equal to mu

Variance reduction

rho sigma2 plus 1 minus rho over B times sigma2

Floor equals rho sigma2

Kills 1 minus rho over B term

Decorrelate models

Random Forests

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Bagging ka core idea bahut simple hai: agar tumhare paas ek model hai jo sahi average deta hai par bahut wobble karta hai (high variance), to us jaisa model bahut baar train karo aur sab ke predictions ka average le lo. Averaging se random errors cancel ho jaate hain, aur signal bacha rehta hai. Problem yeh hai ki data to ek hi hai — to hum bootstrap karte hain: same dataset se n points replacement ke saath uthate hain, taaki har baar thoda alag dataset mile. Isse har tree thoda alag banta hai.

Maths se do cheezein clear hoti hain. Pehli: E[fˉ]=μ\mathbb{E}[\bar f]=\mu, matlab bagging bias nahi badalta — sirf variance kam karta hai. Isliye bagging deep, unstable trees pe lagao (jo low-bias high-variance hote hain), stable models jaise linear regression pe nahi. Doosri: variance ρσ2+1ρBσ2\rho\sigma^2 + \frac{1-\rho}{B}\sigma^2 hota hai. Jitne zyada trees (BB bada), utna doosra term chhota — par ek floor ρσ2\rho\sigma^2 pe ruk jaata hai. Yeh floor tabhi kam hoga jab models ka aapas ka correlation ρ\rho kam ho — yahi se Random Forest ka idea aata hai (feature randomness daal ke ρ\rho girate hain).

Ek bonus: kyunki har bootstrap sample me lagbhag 37% points chhoot jaate hain ((11/n)ne1(1-1/n)^n \to e^{-1}), wo "out-of-bag" points se hum free me validation error nikaal sakte hain — alag test set ki zaroorat nahi. Yaad rakho: bagging me zyada trees se overfit nahi hota, bas averaging limit tak pahunchte ho. Boosting se ulta — wahan zyada rounds overfit kar sakte hain.

Go deeper — visual, from zero

Test yourself — Tree-Based & Instance Methods

Connections