Exercises — Out-of-bag error estimation
Ten problems, easy to hard. Every one has a full solution hidden inside a collapsible callout — read the problem, try it, then expand. Levels run L1 Recognition → L2 Application → L3 Analysis → L4 Synthesis → L5 Mastery.
This page drills the ideas built in the parent note: the 63/37 split from Bootstrap Sampling, the honest sub-ensemble used to score each point, and how OOB compares to Cross-Validation as a generalization estimate for Random Forests.
Before we start, here is the one picture the whole page leans on: what "in-bag" and "out-of-bag" actually mean for a single tree.

The bag on the left holds the original points. A bootstrap sample (right) is drawn by reaching in times, putting the point back each time (with replacement). Points that got drawn at least once are in-bag (violet) — the tree trains on them. Points never drawn are out-of-bag (orange) — the tree never sees them, so they can honestly test it.
Level 1 — Recognition
Exercise 1.1
Dataset indices (). A tree's bootstrap sample is . List the in-bag set and the out-of-bag (OOB) set for this tree.
Recall Solution
In-bag = the unique indices that appear at least once. Scanning : the values present are . So in-bag . OOB = every original index not in that list. From , remove → OOB . Sanity check: in-bag () + OOB () . ✓ Every point is exactly one of the two.
Exercise 1.2
True or false: "A point that is drawn twice in a bootstrap sample is out-of-bag." Explain in one sentence.
Recall Solution
False. Out-of-bag means drawn zero times. Being drawn twice still means the tree trained on that point (it just weighted it more heavily), so it is firmly in-bag. OOB is about absence, not count.
Level 2 — Application
Exercise 2.1
For , compute the exact probability that a specific point is OOB for one tree, and compare it to the large- limit .
Recall Solution
Probability of missing point in one draw is (the draw lands on one of the other points). Draws are independent, so missing it in all draws: Compare to . Already within percentage points at — the limit converges fast.
Exercise 2.2
Binary classification. Point has true label . It is OOB for trees with predictions Give the OOB prediction and its 0/1 loss.
Recall Solution
Aggregate only these five OOB trees (ignore all trees that trained on ). Votes: three 's () vs two 's (). Majority = , so . Since , the 0/1 loss (correct → contributes nothing to the error).
Level 3 — Analysis
Exercise 3.1
Regression, , squared-error loss. OOB predictions:
| 1 | 5 | 6 |
| 2 | 8 | 8 |
| 3 | 12 | 9 |
| 4 | 20 | 22 |
Compute the OOB mean squared error (MSE).
Recall Solution
Per-point squared errors :
Sum . Average over all points: We divide by , not by "number of trees" — OOB error is an average over training points, each scored by its own OOB sub-ensemble.
Exercise 3.2
A forest has trees. Roughly how many trees is a given point OOB for, on average? If a point turns out to be OOB for only trees, why is its OOB prediction untrustworthy?
Recall Solution
Each tree independently leaves a point out with probability . Across trees the expected number of OOB trees is A point with only OOB trees is in the unlucky tail. Its prediction is a vote/average over just estimators, which is high variance — one weak tree can flip a majority vote, and the squared-error average is noisy. With enough trees ( large) this per-point count grows and the estimate stabilizes.
Level 4 — Synthesis
Exercise 4.1
As the OOB fraction is . But at small it differs. Compute the exact OOB probability for and , and state whether the fraction increases or decreases toward the limit as grows.
Recall Solution
Use .
Values: . The sequence increases monotonically toward from below. So small datasets have fewer OOB points per tree than the famous .

The orange curve is climbing toward the navy dashed line . Notice how quickly it flattens — by you are essentially at the limit, which is why the "" rule of thumb is safe for any real dataset.
Exercise 4.2
You train a Random Forest and observe: training error , OOB error , 10-fold CV error . Interpret each number and explain why training error is so much lower than the other two.
Recall Solution
- Training error : each point is scored partly by trees that memorized it (it was in-bag for ~ of trees). Those trees reproduce the label perfectly → optimistically zero. This measures fit, not generalization.
- OOB error : each point is scored only by trees that never saw it → an honest out-of-sample estimate. This tracks test error.
- CV error : an independent generalization estimate from refitting on folds. It agrees with OOB (), which is the expected behavior for bagged models.
The gap ( vs ) is exactly the label leakage of training error. OOB CV confirms the model generalizes to about – error, and OOB got there for free during training.
Level 5 — Mastery
Exercise 5.1
A dataset has points. You build trees with these bootstrap samples:
| Tree | Bootstrap sample |
|---|---|
For each point, list which trees have it OOB. Then flag any point whose OOB prediction would be undefined.
Recall Solution
First find each tree's in-bag (unique) set:
- in-bag → OOB
- in-bag → OOB
- in-bag → OOB
- in-bag → OOB
Now invert — for each point, collect the trees that have it OOB:
- Point : OOB in
- Point : OOB in
- Point : OOB in
- Point : OOB in
- Point : OOB in
Every point has OOB tree, so no prediction is undefined here — but points rest on a single tree's vote each (very high variance). Only point gets two votes. This is exactly why OOB needs large : with most points are barely covered.
Exercise 5.2
Show algebraically that the per-draw miss probability raised to the -th power decreases toward from above for... wait — check the direction. Using and , determine whether approaches from above or below, and reconcile with Exercise 4.1.
Recall Solution
Evaluate the endpoints:
- Limit:
The sequence is : strictly increasing, approaching from below. So the claim "from above" is false — it converges from below. This matches Exercise 4.1. (Note: it is that approaches from below too, while approaches from above — sign of the exponent flips the direction.)
Exercise 5.3 — capstone
You have and trees. Estimate (a) the fraction of points OOB per tree, (b) the expected number of OOB trees per point, and (c) the expected number of points that are OOB for zero trees (undefined predictions). Use .
Recall Solution
(a) OOB fraction per tree (since is huge, we're at the limit). (b) Each point is OOB for a tree with prob , independently across trees: (c) A point is OOB for zero trees with probability . That number is astronomically small (). Expected count of such points .
Takeaway: with and healthy , essentially every point gets ~ honest OOB votes → the OOB estimate is stable and trustworthy. The "undefined prediction" worry only bites at tiny .
Connections
- Bootstrap Sampling — the with-replacement draw behind every "who's OOB?" exercise.
- Bootstrap Aggregating (Bagging) — OOB falls out of bagging for free.
- Random Forests — the ensemble consuming these OOB scores.
- Cross-Validation — the estimate OOB is compared against in Exercise 4.2.
- Bias-Variance Tradeoff — why small gives high-variance OOB (L3, L5).
- Decision Trees — the base learners casting the votes.