Trees and Ensembles of Them
What you will be able to do
The learner can construct a regression tree by recursive binary splitting, read the prediction it makes for a given input, explain why a single deep tree has high variance, and say what bagging and random forests each change about that variance and at what cost.
Orientation
A model with no equation
Every form in the previous unit fixed a shape and estimated coefficients. A tree does neither. It asks a sequence of yes-or-no questions about the predictors and answers with the average of whatever training observations survive the questions.
That supplies two things. There is no functional form to get wrong, so no assumption about curvature or multiplicative growth has to be checked. And the result can be drawn and read aloud, which is why trees appear wherever a prediction has to be explained to someone who will act on it.
It costs two things. The fitted function is a staircase, constant within each region and jumping at the boundaries, so a smooth relationship is approximated by steps. And the structure is unstable: because each split is chosen by a comparison that a handful of different observations can reverse, and because every later split sits beneath an earlier one, a small change in the sample can rebuild the whole tree.
That instability is variance in the sense the evaluation unit defined, and it is what the second half of this unit is about. Averaging many trees reduces it, but not without limit. The trees are built from the same data and tend to make the same choices, and the part of the variance they share is the part averaging cannot remove.
Definition
What the splitting criterion evaluates
The canonical statements above give the splitting rule and the variance identity. What follows is what each leaves out.
The criterion is evaluated on the node's own observations only. Scoring a candidate split at one node takes no account of what could be achieved beneath it. A split that scores second-best now might permit two excellent splits afterwards and is nonetheless discarded. This is what greedy means, and it is why a tree is not the best tree of its size, finding that is computationally infeasible, and the greedy construction is the price of building one at all.
Candidate cut points are boundaries between observed values. There are at most
A leaf's prediction is the mean, because the criterion is squared error. Minimising
The variance identity assumes exchangeable trees. Writing the variance of the average as
| Quantity | What moves it | What does not |
|---|---|---|
| tree depth, sample size | number of trees | |
| restricting split candidates | number of trees | |
| variance of the average | both of the above, and | — |
Bagging changes variance and not bias. Each tree is fitted to a resample of the same data by the same procedure, so they share whatever systematic error the procedure makes. Averaging removes disagreement, and there is no disagreement about a region where every tree is wrong in the same direction.
Out-of-bag observations come free. A bootstrap resample of size
Intuition
Why the structure is less stable than the predictions
A tree drawn on a page invites a particular reading: this predictor matters most, because it is at the root; that one matters next. The reading is often unreliable, and the reason is worth understanding because it is the whole argument for ensembles.
Near-ties at the root. The first split is chosen by comparing scores across candidates. When two candidates score
The consequence propagates downward. Every subsequent split is chosen within the groups the first split created. Change the root and the two groups change, so different candidates are evaluated against different observations, and the entire subtree is rebuilt. One reversed comparison at the top can produce a tree that shares no split with the original.
Predictions move less than structure. Two trees that look nothing alike can predict nearly the same values, because both are approximating the same underlying relationship and the regions they carve, though differently shaped, cover similar ground. This is why the instability is easy to miss: the model keeps working while the explanation it offers changes completely.
What averaging can and cannot fix. If the
The arithmetic makes the limit concrete. With
Which is why the random forest handicaps its trees. Forbidding most predictors at each split forces different trees to open differently, lowering
Example
Relationships a tree handles well, and badly
A tree's fitted function is piecewise constant. That single property decides which relationships it represents cheaply and which it represents only by spending many splits.
A step. Cheap. A response that is one value below a threshold and another above it is exactly what a single split expresses. One comparison, two leaves, and the fit is essentially exact. No form had to be guessed, and no transformation applied.
A straight line. Expensive. A tree approximates
An interaction. Cheap, and automatic. Suppose the effect of dose reverses above a certain age. A linear model represents this only if somebody writes down an age-by-dose product term. A tree splits on age and then chooses different dose splits in each branch, so the interaction is discovered rather than specified. This is the main reason trees are used on data whose structure is unknown.
A relationship depending on a sum of predictors. Expensive. If the response depends on
A smooth curve. Middling, and improved by averaging. A single tree gives a coarse staircase. Averaging many trees whose steps fall in different places produces a fit with many small steps rather than a few large ones, which is closer to smooth. This is a case where the ensemble changes the shape of the fit and not merely its stability.
---
Trees are cheap where the truth has boundaries aligned with individual predictors, and expensive where it is smooth or depends on predictors in combination. Neither is a defect: it is what choosing a partition instead of a formula gains and costs. The complementary observation is that a linear model's costs are the mirror image, which is why the two appear together in practice and why comparing them on held-out error is the routine move.
Procedure
Growing a tree, and growing a forest of them
To grow a regression tree.
- Start with every observation at the root.
- At the current node, enumerate candidates. For each predictor, take the boundaries between adjacent distinct observed values, at most
per predictor. - Score each candidate by the total residual sum of squares of the two groups it creates, using each group's own mean.
- Take the minimising candidate and split. Record the runner-up's score: a near-tie is a warning that the split is not determined by the data as firmly as the drawn tree suggests.
- Repeat within each group until a stopping condition is met. A minimum node size, a maximum depth, or no split that improves the score.
- Predict at a leaf with the mean of its training responses.
To choose the depth. Depth is a flexibility parameter, so it is selected by held-out error rather than by the training criterion, which falls with every split by construction. The usual practice is to grow deliberately too far and then prune back, since a split that looks worthless can enable a valuable one beneath it and stopping early never discovers that.
To bag.
- Draw
bootstrap resamples, each of size , with replacement. - Fit a tree on each, grown deep and left unpruned. The variance that depth introduces is what the averaging is there to remove.
- Average the
predictions for a new input. - Estimate error out-of-bag: for each observation, average the predictions of only those trees whose resample omitted it, then compare with the observed value.
To grow a random forest. Follow the bagging procedure with one change at step 2: at every split, draw a random subset of
Choosing
Checks.
Compare the ensemble's out-of-bag error against a single tree's held-out error. If averaging has not improved matters, the trees are probably too shallow to have had much variance to remove.
Refit the single tree on a resample and compare the two structures. Splits that survive are the ones worth describing to anyone; splits that move were never as determined as the picture implied.
Worked example
Every candidate split, scored
Ten observations,
Step 1: the score before splitting. The mean of all ten is
This is what any split must improve on.
Step 2: enumerate the candidates. Boundaries between adjacent observed values, conventionally at the midpoints:
Step 3: score each. For a candidate, split the observations, take each group's own mean, and add the two sums of squared deviations.
| Split | Total RSS | ||
|---|---|---|---|
| 1 | 9 | ||
| 2 | 8 | ||
| 3 | 7 | ||
| 4 | 6 | ||
| 5 | 5 | ||
| 6 | 4 | ||
| 7 | 3 | ||
| 8 | 2 | ||
| 9 | 1 |
Step 4: take the minimum. The split at
The score falls to the winner and rises symmetrically away from it, because every other candidate leaves some low observations grouped with high ones. The nearest competitors,
It says the boundary lies between
Step 5: what a second split would buy. Within the left leaf the values are
The averaging arithmetic, on the same footing. If
At
Contrast
Changes that look similar and do different work
More trees against less correlated trees.
| raise | lower | |
|---|---|---|
| affects | ||
| at | variance | — |
| at | variance | — |
| at | — | variance |
Going from a hundred trees to infinitely many recovers
Bagging against pruning.
Both address a deep tree's poor test error and they work in opposite directions. Pruning removes splits, lowering variance and raising bias, and leaves one readable tree. Bagging keeps every tree deep and averages, lowering variance while leaving bias alone, and leaves no single tree to read. Choose pruning when the explanation is the deliverable; choose bagging when the prediction is.
Out-of-bag error against cross-validated error.
| out-of-bag | ||
|---|---|---|
| extra fits required | none | |
| estimates the error of | an ensemble of about | an ensemble of |
| bias | mildly pessimistic | mildly pessimistic |
Both are honest held-out estimates and neither is free of bias; out-of-bag is far cheaper and estimates a slightly smaller ensemble than the one being shipped.
A tree's variance against a tree's bias.
A deep tree has low bias and high variance: it can represent almost any partition, and which partition it chooses depends heavily on the sample. A stump has the reverse. Bagging addresses only the first, so bagging stumps accomplishes very little. There is little disagreement to average away. Recognising which term dominates decides whether an ensemble is the right instrument at all.
A split's score against a split's stability.
The reported tree shows which candidate won. It does not show by how much. A split winning by
Warning
The drawn tree claims more than the data support
A fitted tree is unusually legible, and that legibility invites three claims it does not license.
The threshold is not estimated to the precision it is printed with. A split reported at
The order of the splits is not a ranking of importance. The root is whichever candidate scored best, by whatever margin. A win by
A split absent from the tree is not a predictor shown to be irrelevant. Greedy construction commits to the best split now, and a predictor useful only in combination with another may never be chosen. Its absence records that it never won a comparison, not that it carries no information.
---
Two failure modes on the ensemble side.
More trees is the lever people reach for and the one that moves least. The variance of the average is
Bagging does not repair a biased procedure. Averaging removes disagreement. Where every tree is wrong in the same direction, extrapolating past the data, or approximating a smooth trend by steps, they agree, and the average is wrong in exactly that direction. Adding trees to fix a systematic error is work that cannot succeed.
And where there is no disagreement, averaging can make matters worse. A simulation over 300 datasets, bagging 50 replicates of a depth-1 tree fitted to a clean step at
| Query point | 2 | 3 | 5 | 6 | 8 | 9 |
|---|---|---|---|---|---|---|
| sd ratio |
Away from the step the ratio is essentially 1: a stump on a clean step is already near-optimal and has almost no variance to remove. Adjacent to the step the ratio rises above 2, and the bagged prediction is also displaced, at
The same simulation on the case bagging is actually for, a depth-3 tree on a smooth function, reduces the spread at every query point, with ratios
---
What a tree does outside the observed range. It predicts the nearest leaf's mean, forever. A model fitted to machines run up to 80 hours predicts the 80-hour leaf's value at 500 hours, with no indication that the input is far outside anything seen. A linear extrapolation at least fails visibly; a tree fails flat, and a flat prediction reads as a considered answer.
Application
When a tree structure is the deliverable
Clinical triage rules. A protocol that decides which head-injury patients need imaging is a short sequence of yes-or-no questions, because it has to be applied at a bedside by someone who cannot run a model. The tree structure is the deliverable, and the shallow depth is chosen for usability rather than accuracy. Such rules are validated on separate cohorts precisely because a tree fitted once is unstable, and a rule that changed with the sample could not be published as a protocol.
Credit decisions under an explanation requirement. Where a declined applicant is entitled to the reasons, a model whose prediction cannot be stated as a path through conditions is difficult to deploy. A single tree gives that path directly. A random forest does not, which is why lenders using ensembles report feature attributions computed after the fact rather than the model's own structure.
Tabular prediction where accuracy is the only requirement. For customer churn, demand forecasting, fraud scoring and similar problems on mixed-type tabular data, ensembles of trees remain the routine first choice. They need no transformation of the predictors, discover interactions without being told, and tolerate irrelevant columns. No explanation is being sold, so nothing is lost by giving up the readable structure.
Genomics and other wide data. With thousands of predictors and few observations, a single tree's first split is chosen from an enormous pool and is close to arbitrary. The random-forest restriction matters most here: without it, the handful of predictors that happen to correlate with the response in this sample would open nearly every tree, and the correlation floor would be high.
Where a tree is the wrong instrument. Smooth physical relationships, extrapolation beyond the observed range, a tree predicts the nearest leaf's mean forever, so it is flat outside the data rather than wrong in an interesting direction, and any problem where the response depends on a sum or ratio of predictors rather than on each separately.
---
The recurring decision. Each case turns on whether the structure or the prediction is the product. When the structure is the product, a single tree is grown shallow, validated on fresh data, and its instability treated as the central risk. When the prediction is the product, the trees are grown deep, decorrelated, averaged, and no one reads them.