Refit a deep decision tree after changing a few training rows, and its splits and predictions can change a lot. Averaging many such trees should steady the prediction, but every one of them comes from the same training set, so how much can averaging help? This lesson builds bagged trees from bootstrap samples, adds the random choice of features that makes them a random forest, shows why the correlation between trees sets a floor under the variance of their average, and explains when the rows each tree never saw give an honest estimate of error.
Key terms
Bootstrap sample
Definition Let a training set hold rows. A bootstrap sample draws row indices independently and uniformly from , that is, with replacement, and contains the rows with those indices, repeats included. The number of copies of row has a binomial distribution with trials and success probability , so it averages , and the probability that row is missing from the sample is
which increases with toward . On average a sample therefore contains the fraction of the distinct rows, about for large . These probabilities come from the index draws alone, whatever the rows contain. A bootstrap sample reuses the same data with random multiplicities and is not new data. In bagging it is part of how the model is trained, which is a different job from the bootstrap in Bootstrap Uncertainty and Model Comparisons, where resampling measures how much a statistic varies.
In plain words Draw rows from the training table one at a time, put each one back before the next draw, and stop after as many draws as the table has rows. Some rows come out twice or more, and on average about a third never come out at all.
Example The constructed data in this lesson have four rows with inputs and targets units. Writing each row by its input, three bootstrap samples are , and . The first misses the row with , the second misses the row with and the third misses the row with , while the row with appears in all three. For a given row is missing from a sample with probability , and a sample contains on average distinct rows. Each of these three samples happens to contain exactly .
Bagging
Definition Bagging, short for bootstrap aggregating, takes a learning procedure, a training set and a number of models. It draws independent bootstrap samples, fits the procedure to each one, counting a repeated row as many times as it appears, and combines the fitted models . For regression the combination is the average
For classification it is either a majority vote of the predicted classes, with a rule for ties, or an average of the estimated class probabilities followed by choosing the most probable class, and the two can disagree. In regression the fits have the same distribution, so their average has the same expected prediction as a single bootstrap fit and never a larger variance. Bagging works on variance, not on bias. It helps most for unstable procedures such as deep trees, whose fits change a lot when a few rows change, and it changes little for stable procedures.
In plain words Fit the same kind of model to many resampled versions of the training data and let them vote or average. Each fit overreacts to its own sample in its own way, and the average keeps the part of the pattern they share.
Example Fit a regression stump, a tree with one split chosen by squared error, to each sample above, with the rule that goes left. The first sample has targets and splits at , predicting on the left and on the right. The second, with targets , splits at and predicts and . The third, with targets , splits at and predicts and . At the query the three stumps predict , and , so
One stump fitted to all four rows splits at and predicts there. On three constructed validation rows, with targets , the bagged predictions are , and , a mean absolute error of , while the single stump predicts with mean absolute error . The average has steps at , and instead of one large jump. Three validation rows show how averaging works here, not that an ensemble always beats one tree, and once they are used to choose between the two models they are no longer an untouched test. For classification, three trees that give a case the probabilities , and for class vote , so the majority says class , while their average probability says class .
Random forest
Definition A random forest is bagging applied to decision trees with one more source of randomness, random feature subsets. Each tree is fitted to its own bootstrap sample, and at every node, before the split search, a random subset of of the features is drawn without replacement, independently of all other nodes and trees. The best split is searched only among the features in that subset. The size is a hyperparameter with , and gives plain bagged trees. Common defaults are about for classification, while libraries differ for regression, some using about and some all features, so it is worth tuning on validation data. The trees are usually grown deep and left unpruned, and their predictions are combined as in bagging.
In plain words Bootstrap samples make the trees see different rows, and random feature subsets make them ask different questions. When one feature is much stronger than the rest, plain bagging tends to put it at the top of every tree, while a forest makes many trees look for structure in the other features too.
Example With features and , each node draws one of possible subsets, and a given feature is available with probability . So at the root, on average half the trees cannot use even the strongest feature and must split on another one. In the four-row example there is only one feature, so every subset is that feature, and a random forest on those rows is the same as bagging. Feature randomization changes something only when .
Ensemble variance
Definition Fix an input , and let be the predictions of trees at , random through the training sample and through the random draws of the algorithm. Assume each has variance and every pair of distinct trees has correlation . The variance of the average is the sum of the variances and the covariances , divided by , which gives
These variances and covariances are possible only when . If , the variance falls as grows but never below the floor . If , averaging does not help at all, and if , the variance is . This is a model with equal variances and equal correlations, a way to see what correlation does, and not a measured property of a fitted forest. Averaging identically distributed predictions does not change their mean, so whatever bias the trees have, the average keeps.
In plain words Averaging cancels the part of the trees’ errors that differs from tree to tree and keeps the part they share. Adding trees shrinks only the first part. Once there are many trees, the shared part is nearly all that is left, so making the trees less alike matters more than adding more of them.
Example With and , one tree has variance . Ten trees give , a hundred trees give , and no number of trees goes below . Going from to trees removes only , while lowering to with the same and trees would give .
Correlation between trees
Definition For a fixed input and two distinct trees of an ensemble, with predictions and at , the correlation between trees is
taken over the randomness of the training sample and of the bootstrap and feature draws. It concerns the predictions of different trees at the same input, not the correlation between features. It is usually positive, because every tree is fitted to the same training set and strong features tend to win the same splits. A smaller usually lowers , but it can raise the variance and the bias of each tree, because the trees must often split on weaker features. Lower correlation therefore lowers the ensemble variance only when it is not outweighed by worse trees, and when most features are noise a small can hurt. Within one fitted forest the trees share the training set, so the spread of their predictions at one input shows only the variation that comes from the random draws, and it cannot reveal by itself.
In plain words Trees that make the same mistakes cannot correct each other. Random feature subsets make the trees disagree more, and that helps the average only as long as each tree stays reasonably accurate.
Example Suppose, as an assumption for illustration, that forest A with has trees with and , and forest B with a smaller has noisier trees with and . With trees the formula gives for A and for B, close to their floors and . Forest B has the lower ensemble variance despite its worse trees. Whether it also has the lower prediction error depends on bias as well, which is why is chosen on validation data.
Out-of-bag error
Definition In a bagged ensemble or random forest with trees, row of the training set is out of bag for tree when it is missing from that tree’s bootstrap sample. Let be the set of such trees. When is not empty, the out-of-bag prediction for row combines only the trees in , and the out-of-bag error is the average loss of these predictions over the rows that have one, reported together with how many rows that is. Because the samples are independent, a row has no out-of-bag prediction with probability , which shrinks quickly as grows, and with many trees each out-of-bag prediction uses about percent of them. When the rows are independent draws from the population the model will serve, and no preprocessing step such as imputation or feature selection was fitted using all the rows, the out-of-bag error estimates the error on new rows without a separate validation set. It is not valid when rows come in related groups, such as several rows from one customer, since trees that saw the customer’s other rows predict the omitted one too easily, or when the rows are ordered in time and the model must predict the future. Once the out-of-bag error has been used to choose settings such as depth or , it is selection evidence and no longer an independent final test.
In plain words Each tree skips about a third of the rows, so every row can be predicted by the trees that never saw it, which works like a validation set built into the forest. It is honest only when skipping a row really hides that row’s information, and only until the score has been used to make choices.
Example In the four-row example the row with and is missing only from the third sample, whose stump predicts , an absolute error of . The row with and is predicted by the second stump as , an error of , and the row with and by the first stump as , an error of . The row with is in every sample and has no out-of-bag prediction, which happens to a given row with probability when and . The out-of-bag mean absolute error over the three covered rows is . Each of these predictions comes from a single stump, so the figure describes something much weaker than the three-tree average.
Check yourself
- A forest of three trees is fitted to five training rows. Written by row number, the bootstrap samples are , and . Which trees can give each row an out-of-bag prediction? For samples of size , what is the probability that a given row is missing from one sample, and that it has no out-of-bag prediction with trees and with trees?
Show the answer
Row is missing only from the second sample, so only tree can predict it out of bag. Row is missing only from the first sample, so tree predicts it, and row is missing only from the third sample, so tree predicts it. Rows and appear in all three samples and have no out-of-bag prediction. An out-of-bag error for this forest covers three of the five rows and should say so, rather than quietly filling in predictions from trees that trained on rows and .
A given row is missing from one sample with probability . It is in a sample with probability about , and the three samples are independent, so it has no out-of-bag prediction with probability about . With trees that probability is about , so in practice every row is covered.
- A regression data set has features, one strong and nine weak, and a random forest uses . How likely is the strong feature to be available at a given node, and what does that change compared with ? Suppose that with the trees at some input have and , and with they have and . Compare the ensemble variances for trees. Does this prove that is better?
Show the answer
Each of the features is equally likely to be in the subset of , so the strong feature is available with probability . Counting subsets gives the same value, . With every tree can split on the strong feature at the root and most will, so the trees look alike. With about percent of the trees must start with a weak feature, so the trees differ more, and each is somewhat worse.
Under the assumed values, the ensemble variances are
close to the floors and . Adding trees cannot bring the first forest below , while lower correlation brings the second well under it.
This does not prove that is better. The values of and are assumptions, they describe one input, and prediction error also depends on bias, which can grow when the trees are forced onto weak features. The choice of is made with validation data, or with the out-of-bag error when the rows are independent, and the chosen forest is then assessed once on a test set.
- A hospital fits a random forest to visits from patients, with several visits per patient, to predict a lab value for new patients. The team tries combinations of depth and , keeps the one with the lowest out-of-bag error, and reports that error as the error to expect on new patients. What is wrong, and what is an honest plan?
Show the answer
There are two problems. First, the visits of one patient are related. When a visit is out of bag for a tree, that tree has usually trained on other visits of the same patient, so the out-of-bag prediction is easier than a prediction for a patient the forest has never seen, and the out-of-bag error is likely to be too optimistic. Second, the reported value is the smallest of out-of-bag errors and it made the choice of settings, so it tends to understate the chosen forest’s error even with independent rows. After tuning it is selection evidence, not a final test.
An honest plan splits by patient. Put all visits of a set of test patients aside. On the remaining patients, tune depth and with cross-validation in which every patient’s visits fall in a single fold, and fit any preprocessing inside each fold. Refit the chosen settings on all training patients and evaluate once on the test patients. If the model will be used later in time on new patients, the test patients should also come from a later period than the training data.