2.3.9Tree-Based & Instance Methods

Out-of-bag error estimation

1,878 words9 min readdifficulty · medium1 backlinks

A "free" validation set that falls out of bagging for free — no separate hold-out required.

The Setup: Why does OOB even exist?

WHAT is OOB error?

WHY does it work? For any single training point xix_i, the set of trees that excluded it forms a sub-ensemble that treated xix_i as unseen data. Aggregating predictions over those trees gives an honest, out-of-sample prediction for xix_i.


Deriving the magic number: ~37% are out-of-bag

Let's derive the probability a specific point is NOT chosen in one bootstrap sample.

Probability point ii is not picked in one draw: P(miss in 1 draw)=11nP(\text{miss in 1 draw}) = 1 - \frac{1}{n}

Why this step? Each draw picks exactly one of nn points uniformly; "not ii" covers the other n1n-1, so probability =n1n=11n= \frac{n-1}{n} = 1-\frac1n.

Probability point ii is never picked across all nn independent draws: P(OOB for i)=(11n)nP(\text{OOB for } i) = \left(1 - \frac{1}{n}\right)^{n}

Why this step? Draws are independent, so multiply the per-draw miss probability nn times.

Now take the limit as nn \to \infty: limn(11n)n=e10.368\lim_{n\to\infty}\left(1 - \frac{1}{n}\right)^{n} = e^{-1} \approx 0.368

Why this step? This is the classic definition limn(1+xn)n=ex\lim_{n\to\infty}(1+\frac{x}{n})^n = e^x with x=1x=-1.


HOW to actually compute OOB error (the algorithm)

  1. For each training point xix_i, find the set Si={b:xibootstrap(b)}S_i = \{b : x_i \notin \text{bootstrap}(b)\} — the trees for which xix_i is OOB.
  2. Aggregate the predictions of only those trees:
    • Classification: y^i=majority vote{Tb(xi):bSi}\hat{y}_i = \text{majority vote}\{T_b(x_i) : b \in S_i\}
    • Regression: y^i=1SibSiTb(xi)\hat{y}_i = \frac{1}{|S_i|}\sum_{b \in S_i} T_b(x_i)
  3. Compute the OOB error over all points:

OOB error=1ni=1nL(yi,  y^i)\text{OOB error} = \frac{1}{n}\sum_{i=1}^{n} L\big(y_i,\; \hat{y}_i\big)

where LL is your loss (0/1 loss for classification, squared error for regression).

Why only trees in SiS_i? Using a tree that trained on xix_i would leak the label — the tree could memorize xix_i, making the estimate optimistically biased. Restricting to SiS_i keeps it honest.

Figure — Out-of-bag error estimation

Worked Example 1 — Which points are OOB?

  • In-bag = unique picked = {1,2,4}\{1,2,4\}. Why? These indices appear at least once, so the tree trained on them.
  • OOB for tree bb = {3,5}\{3,5\}. Why? Indices 3 and 5 never appear → the tree never saw them → they test this tree.

Check the fraction: OOB fraction =2/5=0.40= 2/5 = 0.40, close to the theoretical 0.3680.368 (small nn noise).


Worked Example 2 — Computing OOB prediction for one point

T2(x7)=1,T5(x7)=0,T9(x7)=1,T11(x7)=1T_2(x_7)=1,\quad T_5(x_7)=0,\quad T_9(x_7)=1,\quad T_{11}(x_7)=1

  • Majority vote =1= 1 (three 1's vs one 0). Why? We aggregate only the 4 trees that didn't train on x7x_7 — honest sub-ensemble.
  • OOB prediction y^7=1=y7\hat{y}_7 = 1 = y_7 → contributes 00 to 0/1 loss.

We ignore all other trees (say T1,T3,T_1, T_3, \dots) for x7x_7 because they did train on it.


Worked Example 3 — Full OOB error

ii yiy_i y^i\hat y_i (OOB) (yiy^i)2(y_i-\hat y_i)^2
1 10 12 4
2 20 19 1
3 30 27 9

OOB MSE=4+1+93=1434.67\text{OOB MSE} = \frac{4+1+9}{3} = \frac{14}{3} \approx 4.67

Why divide by n=3n=3? Average over all training points, each getting a prediction from its own set of OOB trees.


Common Mistakes (Steel-manned)



Recall Feynman: explain to a 12-year-old

Imagine 100 friends each study for a quiz using a random pile of flashcards drawn from a big box (with repeats). By pure chance, each friend leaves out about 37% of the cards. Now, to test if the friends really learned (not just memorized), you quiz each friend only on the cards they never studied. Averaging how well they do on their unseen cards tells you how good the whole group is at new questions — without buying a separate quiz! That average score-mistake is the OOB error.


Flashcards

What is an out-of-bag (OOB) sample?
A training point NOT selected in a given tree's bootstrap sample; it acts as unseen test data for that tree.
What fraction of data is OOB per tree (large n)?
About 36.8% (e1e^{-1}); ~63.2% is in-bag.
Derive the OOB fraction.
P(miss)=(11/n)ne10.368P(\text{miss}) = (1-1/n)^n \to e^{-1} \approx 0.368 as nn\to\infty.
Which trees predict a point's OOB label?
Only the trees for which that point was out-of-bag (did NOT train on it).
Why exclude in-bag trees from OOB prediction?
They saw the point during training → label leakage → optimistically biased (too low) error.
OOB error vs training error?
Training error uses memorizing trees (too optimistic); OOB uses only unseen-tree votes → estimates test error.
Classification OOB aggregation rule?
Majority vote among trees that had the point out-of-bag.
Regression OOB aggregation rule?
Average prediction over trees that had the point out-of-bag.
When is OOB unreliable?
When the number of trees BB is small (few OOB votes per point, high variance, possibly undefined).
What is the advantage of OOB over cross-validation?
It's essentially free — computed during training, no separate hold-out or extra model fits.

Connections

  • Bootstrap Aggregating (Bagging) — OOB is a byproduct of sampling with replacement.
  • Random Forests — standard consumer of OOB error for model evaluation.
  • Cross-Validation — alternative generalization estimate; OOB approximates it cheaply.
  • Bias-Variance Tradeoff — bagging reduces variance; OOB measures the resulting test error.
  • Bootstrap Sampling — the with-replacement draw that creates the 63/37 split.
  • Decision Trees — the base learners being aggregated.

Concept Map

trains each tree via

leaves out

per-draw miss

multiply n draws, take limit

yields

form

avoids label leakage

majority vote or mean

estimates

each point OOB for ~0.37B trees

Bootstrap sampling with replacement

Random Forest ensemble

Out-of-bag samples

P miss = 1 - 1/n

Limit gives e^-1

~36.8% OOB per tree

Sub-ensemble Si excluding xi

Aggregate predictions

OOB error estimate

Generalization error

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, jab hum Random Forest banate hain, toh har tree ek bootstrap sample par train hota hai — matlab nn data points me se nn points replacement ke saath uthate hain. Kyunki replacement ke saath uthaate hain, kuch points baar-baar aa jaate hain aur kuch points kabhi select hi nahi hote. Jo select nahi hote unhe us tree ke liye out-of-bag (OOB) kehte hain. Yeh points us tree ne kabhi dekhe hi nahi, toh yeh ek free ka test set ban jaate hain — alag se validation data ki zaroorat hi nahi!

Ab maths ka magic: ek point kisi ek tree ke liye OOB hone ka probability (11/n)n(1-1/n)^n hota hai, aur jab nn bada hota hai toh yeh e10.368e^{-1} \approx 0.368, yaani lagbhag 37% ban jaata hai. Iska matlab har tree ke liye ~37% data OOB hota hai, aur baaki ~63% in-bag. Har point ko test karne ke liye hum sirf wahi trees use karte hain jinke liye woh point OOB tha — kyunki agar wahi tree use karein jo point par train hua tha, toh woh cheating ho jaayegi (label leak), aur error galat tarah se kam dikhega.

Kyun important hai? Kyunki OOB error basically muft ka cross-validation hai. Model train karte karte hi generalization error ka estimate mil jaata hai, bina extra data alag rakhe. Bas dhyaan rakho — trees zyada hone chahiye, warna har point ke liye votes kam padte hain aur estimate bharosemand nahi rehta.

Go deeper — visual, from zero

Test yourself — Tree-Based & Instance Methods

Connections