Out-of-bag error estimation
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 , the set of trees that excluded it forms a sub-ensemble that treated as unseen data. Aggregating predictions over those trees gives an honest, out-of-sample prediction for .
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 is not picked in one draw:
Why this step? Each draw picks exactly one of points uniformly; "not " covers the other , so probability .
Probability point is never picked across all independent draws:
Why this step? Draws are independent, so multiply the per-draw miss probability times.
Now take the limit as :
Why this step? This is the classic definition with .
HOW to actually compute OOB error (the algorithm)
- For each training point , find the set — the trees for which is OOB.
- Aggregate the predictions of only those trees:
- Classification:
- Regression:
- Compute the OOB error over all points:
where is your loss (0/1 loss for classification, squared error for regression).
Why only trees in ? Using a tree that trained on would leak the label — the tree could memorize , making the estimate optimistically biased. Restricting to keeps it honest.

Worked Example 1 — Which points are OOB?
- In-bag = unique picked = . Why? These indices appear at least once, so the tree trained on them.
- OOB for tree = . Why? Indices 3 and 5 never appear → the tree never saw them → they test this tree.
Check the fraction: OOB fraction , close to the theoretical (small noise).
Worked Example 2 — Computing OOB prediction for one point
- Majority vote (three 1's vs one 0). Why? We aggregate only the 4 trees that didn't train on — honest sub-ensemble.
- OOB prediction → contributes to 0/1 loss.
We ignore all other trees (say ) for because they did train on it.
Worked Example 3 — Full OOB error
| (OOB) | |||
|---|---|---|---|
| 1 | 10 | 12 | 4 |
| 2 | 20 | 19 | 1 |
| 3 | 30 | 27 | 9 |
Why divide by ? 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?
What fraction of data is OOB per tree (large n)?
Derive the OOB fraction.
Which trees predict a point's OOB label?
Why exclude in-bag trees from OOB prediction?
OOB error vs training error?
Classification OOB aggregation rule?
Regression OOB aggregation rule?
When is OOB unreliable?
What is the advantage of OOB over cross-validation?
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
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 data points me se 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 hota hai, aur jab bada hota hai toh yeh , 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.