Tutorials › Exam SRM - Statistics for Risk Modeling › Common Traps In Bagging And Random Forests

Exam SRM · Bagging and Random Forests · Tutorial 634 of 926

Common Traps In Bagging And Random Forests

Learn to check bootstrap, prediction, OOB, tuning, and importance calculations without confusing their distinct roles.

Advanced 13 min read

What You'll Learn

  • Track repeated observations in bootstrap samples and identify which cases are truly out of bag.
  • Distinguish majority-vote predictions from averages of class probabilities.
  • Calculate an OOB prediction only from trees that excluded that observation.
  • Relate tree correlation and ensemble size to the variance of an averaged prediction.
  • Interpret mtry and variable-importance scores without treating them as causal evidence.

Why Small Forest Details Matter

Bagging and random forests combine many trees, but an ensemble calculation is only as sound as its bookkeeping. A duplicated observation still contributes repeatedly to a bootstrap-fitted tree; an OOB prediction uses only trees that omitted the case; and a majority vote need not agree with a mean of class probabilities. These details can change a numerical answer or its interpretation.

The focus here is on diagnosing those errors. A bootstrap sample of size \(n\) is formed by drawing \(n\) observations independently, with replacement, from the \(n\) training cases. A tree is fitted to that resample. In a random forest, a random subset of predictors is also considered at each split; \(mtry\) denotes the number of candidate predictors in that subset. A case is out of bag for a tree only if it was never drawn into that tree’s bootstrap sample.

Definition: For a forest of \(B\) trees, a case’s OOB prediction is aggregated only over the trees for which that case was out of bag. A bootstrap multiplicity is the number of times a training case was drawn into one resample. A case drawn two or more times is still in bag, not OOB.

Worked Example: Bootstrap Multiplicities Are Not Unique-Case Weights

Worked Example: Bootstrap Multiplicities Are Not Unique-Case Weights

A hypothetical regression tree is fitted to five training cases with responses \(2, 5, 11, 14,\) and \(20\), labelled A through E. Its size-five bootstrap sample is A, A, B, D, D. Suppose all five resampled records fall into one terminal node. Find the node prediction and identify the OOB cases. Then average three hypothetical forest-tree predictions, \(7.4, 9,\) and \(12\), for a new case. Use ordinary bootstrap resampling; no interest rate, mortality assumption, or parametric outcome distribution is involved.

Method and multiplicities. The sample contains A twice, B once, D twice, and neither C nor E. The terminal-node prediction is the mean of the resampled responses, counting each draw:

$$ \frac{2+2+5+14+14}{5} =\frac{37}{5} =7.4. $$

A calculation using only the distinct cases would instead give \((2+5+14)/3=7\). That is not the bootstrap-sample mean: it discards the multiplicities. The cases C and E are OOB for this tree because their multiplicities are zero; A, B, and D are in bag.

Forest aggregation. For this regression forest, give each tree prediction equal weight:

$$ \frac{7.4+9+12}{3} =\frac{28.4}{3} =9.466\overline{6}. $$

Rounded to three decimal places, the prediction is \(9.467\). The bootstrap counts determine each tree’s fit; they do not mean that the forest prediction is weighted by how many times a case appeared in a different tree’s resample. The common mistake is to collapse the bootstrap sample to unique records or to treat bootstrap multiplicity as a weight on the final tree average.

Votes and Probabilities Are Different Aggregation Rules

For a classification tree, the class vote is the class the tree predicts, often the class with the largest proportion of training cases in the terminal node. A tree may also report class probabilities, such as the observed class proportions in that node. A forest can aggregate either the tree-level class predictions or the tree-level probability estimates. These operations need not give the same result.

With two classes, majority voting chooses the class receiving more than half of the votes; a tie requires a specified tie rule. Probability averaging instead computes the mean of the trees’ estimated probabilities for a class, then applies a classification threshold if a class label is required. For a threshold of \(0.5\), an averaged probability below \(0.5\) gives the negative class. The threshold and aggregation method should be identified rather than assumed.

Worked Example: Majority Vote Versus Mean Probability

A hypothetical five-tree classification forest predicts whether a policyholder will make a claim during a specified period. The trees’ estimated claim probabilities for one case are \(0.51, 0.51, 0.51, 0.10,\) and \(0.10\). Each tree votes for “claim” if its own probability is at least \(0.5\). Find the majority-vote class and the class obtained by averaging probabilities and using a \(0.5\) threshold. Treat the listed probabilities as the tree outputs; no parametric distribution, interest rate, or mortality assumption is required.

Majority-vote method. The first three probabilities exceed \(0.5\), so those trees vote “claim.” The last two vote “no claim.” The claim-vote fraction is \(3/5=0.6\), and three votes exceed two. Majority voting therefore classifies the case as “claim.”

Mean-probability method. Add the five probabilities and divide by five:

$$ \frac{0.51+0.51+0.51+0.10+0.10}{5} =\frac{1.73}{5} =0.346. $$

Since \(0.346<0.5\), probability averaging with this threshold classifies the case as “no claim.” The two answers differ because majority voting gives each tree’s class label one vote, whereas probability averaging retains the size of each tree’s estimated probability. The common mistake is to call the vote fraction an average probability, or to report one aggregation rule’s result as though it were the other’s. If the question specifies the forest’s probability rule, use that rule; do not silently substitute a vote fraction.

OOB Eligibility and the Correct Error Calculation

An observation is eligible for a tree’s OOB prediction only when its bootstrap multiplicity for that tree is zero. When an observation appears several times in the bootstrap sample, it remains ineligible for that tree’s OOB calculation. Across the forest, each case can have a different number of eligible trees, so it is essential to aggregate only predictions from its eligible trees.

A useful check on the bookkeeping is the probability of being OOB for one size-\(n\) bootstrap sample. Each draw misses a particular case with probability \(1-1/n\). By independence of the draws, the probability that all \(n\) draws miss it is \((1-1/n)^n\), which approaches \(e^{-1}\), about \(0.368\), as \(n\) grows. This is a probability, not a guarantee that every case has the same OOB count in a finite forest.

Worked Example: Forming OOB Predictions Before Computing Error

A hypothetical regression forest is evaluated on four training cases with responses A \(=10\), B \(=20\), C \(=30\), and D \(=40\). Four trees have the following OOB sets: T1 has A and C; T2 has B and C; T3 has A and D; T4 has B, C, and D. Their corresponding OOB predictions are: T1, A \(=12\), C \(=28\); T2, B \(=18\), C \(=34\); T3, A \(=8\), D \(=38\); T4, B \(=22\), C \(=32\), D \(=42\). Find each case’s OOB prediction and the OOB mean squared error. These are fixed hypothetical regression values; no parametric distribution, interest rate, or mortality assumption applies.

Step 1: Average eligible tree predictions case by case. A is OOB for T1 and T3, so its OOB prediction is \((12+8)/2=10\). B is OOB for T2 and T4, giving \((18+22)/2=20\). C is OOB for T1, T2, and T4, giving \((28+34+32)/3=94/3\), or \(31.333\overline{3}\). D is OOB for T3 and T4, giving \((38+42)/2=40\).

Step 2: Compute squared errors from those aggregated predictions. The four squared errors are \((10-10)^2=0\), \((20-20)^2=0\), \((30-94/3)^2=16/9\), and \((40-40)^2=0\).

Step 3: Average over the four cases. The OOB mean squared error is

$$ \operatorname{MSE}_{\mathrm{OOB}} =\frac{0+0+16/9+0}{4} =\frac{4}{9} =0.444\overline{4}. $$

Rounded to four decimal places, it is \(0.4444\). This calculation first averages the OOB tree predictions for each case, then squares the case-level error, then averages over cases. It does not divide by the total number of tree-case predictions, nor does it include predictions from trees for which a case was in bag. The common mistake is to confuse the number of eligible tree predictions with the number of cases in the error average.

Tree Correlation, mtry, and Importance Scores

Averaging more trees usually stabilizes a forest prediction, but the benefit depends on how similar the trees are. The following variance identity explains why increasing the number of trees cannot eliminate variation when tree predictions remain positively correlated.

Derivation: Suppose \(T_1,\ldots,T_B\) are tree predictions for the same case, each with variance \(\sigma^2\), and every distinct pair has correlation \(\rho\). The forest prediction is \(\bar T=B^{-1}\sum_{b=1}^B T_b\). The variance of a sum equals the sum of all variances plus twice the sum of covariances. There are \(B\) variance terms and \(B(B-1)\) ordered covariance terms in the double sum; each covariance is \(\rho\sigma^2\). Therefore:
$$ \operatorname{Var}(\bar T) =\frac{1}{B^2}\left[B\sigma^2+B(B-1)\rho\sigma^2\right] =\sigma^2\left[\rho+\frac{1-\rho}{B}\right]. $$
This identity assumes equal tree variances and equal pairwise correlation. It is a variance calculation, not a complete formula for prediction error, which also depends on bias and the prediction target.

Worked Example: Interpreting mtry and the Correlation Limit

A hypothetical regression forest has \(B=100\) trees, and suppose their predictions for a fixed case have common variance \(\sigma^2\) and pairwise correlation \(\rho=0.20\). There are eight available predictors, and \(mtry=3\) predictors are sampled uniformly without replacement as candidates at each split. Find the ensemble variance as a multiple of \(\sigma^2\), the chance that one specified predictor is a candidate at a given split, and what happens to the variance factor as the number of trees grows. These are model assumptions for the calculation; no interest rate, mortality assumption, or parametric distribution is used.

Variance. Substitution into the derived identity gives

$$ \frac{\operatorname{Var}(\bar T)}{\sigma^2} =0.20+\frac{1-0.20}{100} =0.20+\frac{0.80}{100} =0.208. $$

If the trees were uncorrelated under the same variance assumption, the factor would be \(1/100=0.01\). The difference illustrates that positive correlation limits the variance reduction from averaging.

mtry. A uniformly selected subset of three out of eight predictors includes the specified predictor with probability \(3/8=0.375\). This is the chance at a particular split, not a promise that the predictor will be used, and not the chance that it appears anywhere in the tree. As \(B\) grows without bound, the variance factor approaches \(\rho=0.20\), since \((1-\rho)/B\) approaches zero.

A smaller mtry can change how often strong predictors compete at a split and can reduce similarity among trees, but it can also make individual trees less effective. Thus, lower correlation alone does not prove lower prediction error. The common mistake is to infer that either more trees or smaller mtry must always improve the final model.

Variable importance also requires careful interpretation. Permutation importance typically measures how much a chosen prediction-error measure worsens when one predictor’s values are permuted, often using OOB cases. Mean decrease in impurity instead summarizes reductions in a tree’s split criterion attributed to a predictor. These are different scores, with different scales and potential biases; their numerical values should not be compared as though they were the same unit.

An importance score is not a causal effect, a regression coefficient, or a guarantee that a predictor will be useful in every population. Correlated predictors can share or mask importance: permuting one may have little effect if another supplies similar information. A small or negative permutation score can reflect sampling variation, redundancy, or model behavior; it is not proof that the predictor has a protective or harmful causal effect. Likewise, a larger score means more importance according to that model, data, and scoring procedure—not a larger real-world effect.

Key takeaway: Keep the quantities separate. Bootstrap multiplicities affect tree fitting; OOB eligibility determines which trees may predict a case for OOB assessment; votes and probability averages are distinct classification rules; mtry changes candidate selection; and importance scores describe model behavior under a specified scoring method.

Common Traps to Check

  • Dropping duplicates: a bootstrap resample is a list of draws, not a set of unique cases. Repeated draws count repeatedly in fitting.
  • Calling an in-bag case OOB: even one draw makes a case in bag for that tree; repeated draws do not change that eligibility rule.
  • Swapping aggregation rules: a majority vote counts labels, while probability averaging averages numerical probability estimates.
  • Using every tree for every OOB case: only trees that omitted the case contribute to its OOB prediction.
  • Overstating tuning or importance conclusions: smaller tree correlation does not by itself ensure lower error, and predictor importance is not causality.

Check Your Understanding

For each problem, track the specified aggregation rule and eligibility condition before calculating.

  1. A size-six bootstrap sample is \(A,C,C,D,F,F\), drawn from cases A through F. Which cases are OOB, and what is the multiplicity of C? (Answer: B and E are OOB; C has multiplicity 2.)
  2. Four classification trees give probabilities \(0.80, 0.48, 0.48, 0.48\) for the positive class. What is the majority-vote result if each tree votes positive at probability at least \(0.5\), and what is the probability-average result at threshold \(0.5\)? (Answer: Majority vote is negative, with 1 of 4 votes; the mean probability is \(2.24/4=0.56\), so probability averaging classifies positive.)
  3. A regression case has response 6 and is OOB for three trees whose predictions are 5, 7, and 8. Find its aggregated OOB prediction and squared error. (Answer: OOB prediction \(20/3=6.666\overline{6}\); squared error \(4/9=0.444\overline{4}\).)
  4. A forest has 25 trees with equal prediction variance \(\sigma^2\) and common correlation \(0.12\). Find the variance of the average as a multiple of \(\sigma^2\), using the equal-correlation identity. (Answer: \(\sigma^2[0.12+0.88/25]=0.1552\sigma^2\).)
  5. There are 10 predictors and \(mtry=4\), chosen uniformly at a split. A permutation-importance score for predictor X is near zero when another predictor is strongly correlated with X. State the chance X is a candidate at that split and the valid interpretation of the score. (Answer: \(4/10=0.4\); the score indicates little additional predictive contribution under this model and scoring procedure, possibly because the correlated predictor supplies similar information, not that X has no causal effect.)
Clinical Biostats