Tutorials › Exam SRM - Statistics for Risk Modeling › Choosing mtry For A Random Forest

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

Choosing mtry For A Random Forest

Compare smaller and larger mtry values, quantify how often useful predictors are available, and use OOB error to make a practical choice.

Advanced 11 min read

What You'll Learn

  • Define mtry and identify which parts of the forest-fitting process it changes.
  • Calculate the chance that a split considers one or more relevant predictors.
  • Explain the tradeoff between individual tree strength and correlation among trees.
  • Compare candidate mtry values using OOB error and repeated runs.
  • Recognize when a small OOB difference is not a reliable reason to prefer one value.
  • Avoid confusing the expected number of useful candidates with the probability of having any.

Why mtry Is a Tuning Choice

At each split in a random forest, the algorithm considers only a randomly selected subset of the available predictors. The number in that subset is called mtry. This creates a practical question: should each split see only a few predictors, or should it have access to most or all of them?

A larger candidate set gives a split more opportunity to use a predictor that produces a strong split. But when the same strong predictors are available repeatedly, different trees may make similar choices. A smaller candidate set makes the trees less likely to follow the same path, but can also leave a useful predictor out of consideration at a particular split. The best mtry balances these effects for the prediction task and data at hand; there is no universally best value.

Definition: If a data set has \(p\) predictors, mtry is the number of predictors randomly made available to compete at each tree split. Its usual integer range is \(1 \leq \text{mtry} \leq p\). At \(\text{mtry}=p\), every predictor is available at each split; at smaller values, only a random subset is considered. The candidate predictors are typically sampled without replacement within a split.

This choice changes the set of split opportunities, not the number of predictors in the data, the bootstrap sample size, or the number of trees. To compare mtry values fairly, hold the training data and other fitting choices fixed, including the tree-growing rules and number of trees. The random subsets and bootstrap samples also introduce variation, so a small difference in one run should not automatically be treated as decisive.

Availability of Useful Predictors

A useful first calculation is the probability that at least one relevant predictor appears among the candidates at a split. Suppose there are \(p\) predictors in total, \(r\) of which are relevant to the split under consideration, and \(m\) candidates are sampled uniformly without replacement. The probability that none of the relevant predictors is included is the number of all-relevant-free subsets divided by the number of possible subsets.

$$ P(\text{at least one relevant candidate}) = 1 - \frac{\binom{p-r}{m}}{\binom{p}{m}}, $$

This expression applies when \(m \leq p-r\); if \(m>p-r\), at least one relevant predictor must be included, so the probability is 1. The expected number of relevant candidates is a different quantity: it is \(mr/p\). That expectation is useful, but it does not give the probability of including at least one.

Worked Example: How Often Can a Split Consider a Useful Predictor?

A hypothetical claims-screening data set has \(p=10\) predictors. Suppose two predictors are relevant to a particular split, and candidate subsets are drawn uniformly without replacement. Compare mtry values 2 and 5. Calculate the expected number of relevant candidates and the probability of including at least one for each value.

Method. Use \(E[K]=mr/p\) for the expected number \(K\) of relevant candidates, and use the complement of the event that all candidates are irrelevant for the inclusion probability.

For mtry = 2. The expected number of relevant candidates is \(2(2)/10=0.4\). There are \(\binom{10}{2}=45\) possible candidate pairs. Of these, \(\binom{8}{2}=28\) contain no relevant predictor. Therefore, \(P(\text{at least one relevant})=1-28/45=17/45=0.3778\), to four decimal places.

For mtry = 5. The expected number of relevant candidates is \(5(2)/10=1\). There are \(\binom{10}{5}=252\) candidate sets, of which \(\binom{8}{5}=56\) contain no relevant predictor. Therefore, \(P(\text{at least one relevant})=1-56/252=196/252=7/9=0.7778\), to four decimal places.

Increasing mtry from 2 to 5 makes a relevant predictor much more likely to be available. Availability does not guarantee that the predictor will be selected: the split rule still compares the available predictors, and a relevant predictor may not give the best split at that node. Common mistake avoided: \(0.4\) is the expected number of relevant candidates for mtry 2, not the probability of at least one. The probability is \(0.3778\), calculated by counting subsets with and without relevant predictors.

Tree Strength, Tree Correlation, and the Tradeoff

In this context, tree strength describes how well an individual tree predicts the outcome. It is a useful qualitative description, not a single universal score. With more predictors available at each split, a tree has more chances to choose a strong split, so increasing mtry often increases individual tree strength. It need not do so at every split or for every data set: additional candidates can be unhelpful, and trees also depend on their bootstrap samples and random choices.

Tree correlation describes how similarly different trees predict the same cases, especially in their prediction errors. Larger mtry often increases correlation because the same influential predictors are more frequently available, allowing trees to make similar splits. Smaller mtry can reduce this similarity by changing the available choices from split to split and tree to tree.

Averaging trees reduces the effect of their individual variation most when their errors are not too strongly correlated. But if a smaller mtry makes the trees substantially weaker, the reduction in correlation may not compensate. The practical goal is therefore not to minimize correlation by itself, nor to maximize the strength of each tree in isolation, but to obtain good ensemble prediction.

$$ \text{smaller mtry: potentially weaker trees, often lower correlation} \qquad \text{larger mtry: potentially stronger trees, often higher correlation} $$

These are tendencies, not guarantees. If predictors are redundant, changing mtry may have little effect on useful split choices. If a small number of predictors dominate, a small mtry may often omit them, weakening trees. Conversely, when many predictors offer similar information, smaller mtry may give the forest varied trees without losing much split quality.

Key takeaway: mtry controls the balance between split opportunities for individual trees and similarity among trees. Predictive performance reflects both effects, so it should be assessed using an error measure rather than inferred from mtry alone.

Using OOB Error to Compare Values

For an observation, a tree is out-of-bag (OOB) when that observation was not included in the tree’s bootstrap sample. Its OOB prediction is formed using trees for which it was OOB. Across the training observations, an OOB error measure summarizes how well those predictions perform. For classification, a common measure is the fraction of OOB classifications that are incorrect; for regression, a common measure is OOB mean squared error. The measure should match the task and the stated objective.

To tune mtry, fit forests at several candidate values and compare their OOB errors, keeping other settings fixed. Prefer a value with consistently good performance rather than assuming that error must improve monotonically as mtry changes. If the best values have very similar OOB error, consider the size and stability of the difference, computational cost, and the purpose of the analysis. If many choices are tried, the lowest observed OOB error can be optimistic because the tuning procedure has selected the best-looking result from several noisy estimates.

Worked Example: Choosing Among Three Values by OOB Error

A hypothetical classifier uses \(p=9\) predictors. Three forests are fitted with mtry values 1, 3, and 9, using the same training observations and the same tree-growing settings. Each observation receives at least 100 OOB votes in each fit. The respective numbers of incorrect OOB classifications are 55, 39, and 43 among \(n=240\) observations. Which value has the lowest OOB error?

Method. Calculate the incorrect-classification fraction for each forest by dividing its number of OOB errors by 240.

For mtry 1, the OOB error is \(55/240=0.2292\), or 22.92% to two decimal places. For mtry 3, it is \(39/240=0.1625\), or 16.25%. For mtry 9, it is \(43/240=0.1792\), or 17.92% to two decimal places.

The smallest observed OOB error is 0.1625, so mtry 3 is the initial choice among these three settings. The result is consistent with a tradeoff: mtry 1 may make trees less similar but can restrict their split choices, while mtry 9 allows all predictors at each split but does not produce the lowest OOB error here. These error values alone do not prove why the difference occurred.

Check. The denominator is 240 in all three calculations, and the error ordering is \(0.1625 < 0.1792 < 0.2292\). Common mistake avoided: choosing mtry 9 simply because it offers the most predictors at each split. More options can help an individual tree, but the forest’s OOB performance is the relevant comparison.

When Small Differences Need Careful Interpretation

OOB error is a practical internal estimate, not an error-free measurement of future performance. The fitted forests and their OOB predictions are affected by random sampling and feature selection. Comparing several mtry values also creates a selection effect: the minimum among noisy estimates may look better than the selected setting would perform on fresh data. When the choice matters, repeat the comparison under several random seeds or use a separate validation procedure to check whether the apparent advantage persists.

Repeated runs are particularly useful when candidate values have nearly equal errors. Compare both their average error and how much their errors vary across runs. A tiny difference in average error that changes direction across runs is weak evidence for a meaningful predictive advantage. When results are close, a less expensive setting may be reasonable, provided its performance is adequate for the purpose. This is a practical judgment, not a theorem that lower mtry is always preferable.

Worked Example: Interpreting Nearly Tied Repeated OOB Results

A hypothetical risk model is fitted four times at each of three mtry values, with the same training set, tree-growing rules, and number of trees. The four OOB error rates are:

mtry 1: 0.184, 0.196, 0.190, 0.202
mtry 3: 0.162, 0.171, 0.167, 0.164
mtry 8: 0.169, 0.158, 0.176, 0.165

Method. Calculate each mean and range, then distinguish the lowest average from a clearly established advantage.

For mtry 1, the mean is \((0.184+0.196+0.190+0.202)/4=0.772/4=0.193\). Its range is \(0.202-0.184=0.018\).

For mtry 3, the mean is \((0.162+0.171+0.167+0.164)/4=0.664/4=0.166\). Its range is \(0.171-0.162=0.009\).

For mtry 8, the mean is \((0.169+0.158+0.176+0.165)/4=0.668/4=0.167\). Its range is \(0.176-0.158=0.018\).

Mtry 3 has the lowest mean error, 0.166, while mtry 8 is close at 0.167. Mtry 3 also has the smallest range in these four runs. A sensible provisional choice is mtry 3, but the 0.001 difference in mean error is not, by itself, convincing evidence that its predictive performance is materially better than mtry 8. If computation is important, mtry 3 requires fewer candidate evaluations per split; confirm that choice on fresh validation data when a dependable performance comparison is needed.

Check. The means are 0.193, 0.166, and 0.167, and their ordering identifies mtry 3 as the lowest average. The overlapping ranges reinforce the caution against treating a small mean difference as conclusive. Common mistake avoided: reporting the smallest mean as proof of a definite improvement without considering run-to-run variation.

Common Exam Traps

  • Confusing an expected count with a probability. For \(p=10\), \(r=2\), and mtry 2, \(mr/p=0.4\) is the expected number of relevant candidates. It is not the probability of at least one; that probability is \(1-\binom{8}{2}/\binom{10}{2}=17/45=0.3778\).
  • Assuming larger mtry must lower forest error. A larger candidate set can strengthen individual trees, but can also increase their similarity. OOB performance must decide the comparison.
  • Assuming the smallest mtry is best because it decorrelates trees. Lower correlation is useful only in combination with adequate tree strength.
  • Comparing unlike fits. If the bootstrap scheme, tree settings, or number of trees changes along with mtry, the observed error difference cannot be attributed to mtry alone.
  • Overinterpreting the minimum in a large search. Selecting the smallest of many OOB errors can capitalize on random variation; confirm important choices using repeated runs or validation data.

The useful workflow is to choose a reasonable range of mtry values, keep the rest of the fitting procedure controlled, and compare the appropriate OOB error. Interpret the result as evidence about predictive performance, not as a direct measurement of tree strength or correlation. This mtry tradeoff is aligned with the SOA Exam SRM (Statistics for Risk Modeling) syllabus.

Check Your Understanding

For each question, calculate or interpret the stated comparison. Probabilities should be rounded to four decimal places where needed.

  1. A forest has \(p=12\) predictors, of which \(r=3\) are relevant to a particular split. Candidate predictors are sampled uniformly without replacement, with mtry 4. Find the probability that at least one relevant predictor is considered. (Answer: \(1-\binom{9}{4}/\binom{12}{4}=369/495=0.7455\).)
  2. Among 180 observations, three candidate forests have 31, 25, and 28 incorrect OOB classifications at mtry 1, 4, and 10, respectively. Calculate each OOB error and identify the lowest. (Answer: \(31/180=0.1722\), \(25/180=0.1389\), and \(28/180=0.1556\); mtry 4 has the lowest OOB error.)
  3. Across three runs, mtry 2 gives OOB error rates 0.15, 0.17, and 0.16; mtry 6 gives 0.14, 0.16, and 0.18. Find both mean errors and state what the results support. (Answer: both means are 0.160; neither has a lower mean, so these results alone do not establish a predictive-performance preference.)
  4. There are \(p=8\) predictors, exactly one relevant to a split. Compare the probability it is available at mtry 2 and mtry 8. (Answer: \(2/8=0.2500\) at mtry 2 and \(8/8=1.0000\) at mtry 8.)
  5. A practitioner tests 20 mtry values and selects the one with the smallest OOB error, which is only slightly below several other values. What is the main interpretive risk, and what is a useful next check? (Answer: the selected minimum may be unusually low because many noisy estimates were compared; repeat the comparison or assess the selected setting on separate validation data.)
Clinical Biostats