Tutorials › Statistical Learning (Graduate) › Stratified Holdout Validation for Rare Classes

Validation and Data-Driven Tuning · Tutorial 293 of 989

Stratified Holdout Validation for Rare Classes

Separate the population risk a stratified holdout estimates from the class-count choices that control its variability.

Advanced 12 min read

What You'll Learn

  • Distinguish population-weighted classification risk from macro-averaged risk.
  • State when a stratified holdout estimator is unbiased for the intended target.
  • Derive its variance when validation class counts are fixed.
  • Account for a fitted classifier’s randomness separately from validation-sample variability.
  • Compare stratified allocations and identify when Neyman allocation applies.
  • Recognize why absent or very small rare-class validation samples are consequential.

Why Stratify a Holdout?

In a classification problem, an ordinary random holdout can contain few or no observations from a rare class. That makes its estimated error for the class unstable or undefined. Stratifying the split fixes the number of validation observations from each class, ensuring that each selected class is represented. But fixing class counts changes the calculation: the validation set’s class proportions need not match the population proportions. To estimate population classification risk, the class-specific validation errors must be combined with suitable population weights.

This tutorial focuses on the effect of fixed validation counts on the target and variability of estimated classification loss. The central calculations first treat the fitted classifier as fixed, then extend them to a classifier trained on the complementary training set.

Target Risk and the Stratified Estimator

Let \(Y\) take values in \(\{1,\ldots,K\}\), with \(P(Y=k)=\pi_k>0\). For a fixed classifier \(g\), define its class-\(k\) error rate and its population classification risk as follows.

Definition: The class-\(k\) risk of \(g\) is \(R_k(g)=P(g(X)\ne k\mid Y=k)\). Its population classification risk is \(R(g)=P(g(X)\ne Y)=\sum_{k=1}^K\pi_k R_k(g)\). The macro-averaged risk is \(K^{-1}\sum_{k=1}^K R_k(g)\). These are different targets unless the class weights happen to agree.

Suppose the validation set contains exactly \(m_k\geq 1\) observations from class \(k\), with total size \(M=\sum_{k=1}^K m_k\). Let \(\widehat R_k\) be the fraction misclassified among those \(m_k\) observations. The class proportions in this validation set are \(m_k/M\), but those are design choices rather than necessarily the population proportions \(\pi_k\). Thus the unweighted overall fraction of validation errors estimates the risk under the validation-set class mix. A population-risk estimate instead uses the population weights:

$$ \widehat R_{\pi}=\sum_{k=1}^K \pi_k\widehat R_k. $$

If the intended target is macro-averaged risk, use \(K^{-1}\sum_k\widehat R_k\). If the target is risk under a different deployment class distribution, use that distribution’s weights. Stratification itself does not determine which target is appropriate.

Worked Example: One Stratified Split, Two Risk Targets

Suppose there are two classes, with \(\pi_1=0.99\) and \(\pi_2=0.01\). A stratified validation set contains 90 class-1 cases and 10 class-2 cases. It has 3 class-1 errors and 4 class-2 errors. Find the estimated population risk and macro-averaged risk.

The class-specific error estimates are \(\widehat R_1=3/90=1/30\) and \(\widehat R_2=4/10=0.4\). Weighting by population prevalence gives \(0.99(1/30)+0.01(0.4)=0.033+0.004=0.037\).

The macro-averaged estimate is \(\{1/30+0.4\}/2=13/60\), approximately \(0.217\). The difference is not a contradiction: the first estimate targets population error, where class 1 dominates, while the second gives both classes equal weight. The raw validation error fraction, \(7/100=0.07\), targets neither of these in general; it reflects the validation allocation of 90% and 10%.

Unbiasedness and Variance with Fixed Class Counts

For a fixed classifier, suppose observations in the validation sample are independent draws from the class-conditional feature distribution within each stratum. Define \(Z_{ki}=1\{g(X_{ki})\ne k\}\) for validation observation \(i\) in class \(k\). Then \(Z_{ki}\) is Bernoulli with success probability \(R_k(g)\), and \(\widehat R_k=m_k^{-1}\sum_{i=1}^{m_k}Z_{ki}\).

Result: Under the sampling conditions just stated, \(\widehat R_{\pi}\) is unbiased for \(R(g)\), and \(\operatorname{Var}(\widehat R_{\pi})=\sum_{k=1}^K \pi_k^2 R_k(g)\{1-R_k(g)\}/m_k\).

Proof. Within class \(k\), the \(m_k\) error indicators are independent Bernoulli variables. Therefore \(E[\widehat R_k]=R_k(g)\) and \(\operatorname{Var}(\widehat R_k)=R_k(g)\{1-R_k(g)\}/m_k\). The validation observations from distinct strata are independent under the stated sampling scheme, so the class-specific estimates are independent. By linearity of expectation,

$$ E[\widehat R_{\pi}] =\sum_{k=1}^K\pi_k E[\widehat R_k] =\sum_{k=1}^K\pi_k R_k(g) =R(g). $$

Independence also makes the variance of the weighted sum the sum of the weighted variances:

$$ \operatorname{Var}(\widehat R_{\pi}) =\sum_{k=1}^K\pi_k^2\operatorname{Var}(\widehat R_k) =\sum_{k=1}^K\frac{\pi_k^2R_k(g)\{1-R_k(g)\}}{m_k}. $$

This proves both claims. \(\square\)

The result describes validation-sample variability for a fixed classifier. When the classifier is trained on the complementary training data, the same calculation applies conditionally on that training data, provided the validation observations are independent of it and have the stated class-conditional distributions.

Worked Example: Class-Weighted Variance with a Rare Class

Let \(\pi_1=0.95\), \(\pi_2=0.05\), and suppose a fixed classifier has \(R_1=0.05\) and \(R_2=0.30\). A stratified validation set uses \(m_1=m_2=20\). Calculate the variance and standard deviation of the population-weighted risk estimate.

The class-specific Bernoulli variances are \(0.05(0.95)=0.0475\) and \(0.30(0.70)=0.21\). Hence

$$ \begin{aligned} \operatorname{Var}(\widehat R_{\pi}) &=\frac{0.95^2(0.0475)}{20} +\frac{0.05^2(0.21)}{20}\\ &=\frac{0.04286875+0.000525}{20}\\ &=0.0021696875. \end{aligned} $$

The standard deviation is \(\sqrt{0.0021696875}\), approximately \(0.04658\). Although the rare class has only 5% population weight, it still contributes to the variance; its contribution is \(0.000525/20=0.00002625\). The much larger contribution from class 1 reflects its larger population weight and the specified class risk.

Training Randomness and the Validation Target

In practice, \(g\) is usually fitted using the training portion of the split. Write \(D\) for that training data and \(g_D\) for the resulting classifier. Conditional on \(D\), define \(R_k(D)=P(g_D(X)\ne k\mid Y=k,D)\), where the probability is for a new class-\(k\) observation. If validation observations are independent of \(D\), the preceding result gives

$$ E[\widehat R_{\pi}\mid D]=R(D):=\sum_{k=1}^K\pi_kR_k(D), \qquad \operatorname{Var}(\widehat R_{\pi}\mid D) =\sum_{k=1}^K\frac{\pi_k^2R_k(D)\{1-R_k(D)\}}{m_k}. $$

Thus the estimate is conditionally unbiased for the population risk of the fitted classifier. Unconditionally, its variability includes both the validation-sample noise and variation in the fitted classifier’s risk across possible training sets:

Result: If the conditional expectations and variances are finite, then \(\operatorname{Var}(\widehat R_{\pi})=E[\operatorname{Var}(\widehat R_{\pi}\mid D)]+\operatorname{Var}(R(D))\).

Derivation. Write \(A=\widehat R_{\pi}\). Since \(E[A\mid D]=R(D)\), add and subtract this conditional mean: \(A-E[A]=(A-E[A\mid D])+(E[A\mid D]-E[A])\). On squaring and taking expectations, the cross term is zero, because \(E[A-E[A\mid D]\mid D]=0\). The first squared term has expectation \(E[\operatorname{Var}(A\mid D)]\); the second has expectation \(\operatorname{Var}(E[A\mid D])=\operatorname{Var}(R(D))\). This proves the identity. \(\square\)

The training sample size and its class composition matter too: stratifying a split may change how many examples of each class are available for fitting. The conditional target is the risk of the model trained on that particular training data, not automatically the risk of a model trained on a larger data set or with a different class balance.

Allocation and Rare-Class Trade-Offs

When the total validation size \(M\) is fixed, the variance formula makes the allocation trade-off explicit. Set \(a_k=\pi_k^2R_k(1-R_k)\). For positive \(a_k\), assigning more observations to class \(k\) reduces that class’s variance contribution \(a_k/m_k\). In a continuous allocation with no lower-bound requirement on any \(m_k\), the variance-minimizing allocation is proportional to \(\sqrt{a_k}=\pi_k\sqrt{R_k(1-R_k)}\).

To see this, assume first that every \(a_k>0\) and \(m_k>0\), with \(\sum_km_k=M\). The Cauchy–Schwarz inequality gives

$$ \left(\sum_{k=1}^K\sqrt{a_k}\right)^2 =\left(\sum_{k=1}^K\sqrt{\frac{a_k}{m_k}}\sqrt{m_k}\right)^2 \leq \left(\sum_{k=1}^K\frac{a_k}{m_k}\right) \left(\sum_{k=1}^K m_k\right). $$

Therefore the variance is at least \((\sum_k\sqrt{a_k})^2/M\). Equality in Cauchy–Schwarz requires \(\sqrt{a_k/m_k}\) to be proportional to \(\sqrt{m_k}\), or equivalently \(m_k\propto\sqrt{a_k}\). This establishes the stated optimum when all \(a_k\) are positive. In practice, counts must be integers, and a minimum count may be required for reporting or operational reasons, so the continuous allocation is a guide rather than necessarily an exactly attainable design.

If a positive-weight class has \(R_k\in\{0,1\}\), then \(a_k=0\). The variance lower bound remains valid, but allocating counts proportional to \(\sqrt{a_k}\) would give that class zero observations. If every class must receive a positive count, this unconstrained allocation is infeasible. With integer counts and a minimum of one per class, assign the required minimum to zero-variance classes and allocate the remaining counts among positive-\(a_k\) classes according to the variance criterion. If all \(a_k=0\), the classification errors are deterministic within every class under the fixed-classifier model, and the variance is zero for any positive allocation.

Worked Example: Choosing a Variance-Oriented Allocation

Consider two classes with \(\pi_1=0.8\), \(\pi_2=0.2\), \(R_1=0.1\), and \(R_2=0.4\). Allocate a validation size of \(M=35\) to reduce the variance of the population-risk estimate.

The variance coefficients are \(a_1=0.8^2(0.1)(0.9)=0.0576\) and \(a_2=0.2^2(0.4)(0.6)=0.0096\). Their square roots are \(0.24\) and approximately \(0.09798\). The continuous optimum assigns \(35(0.24)/(0.24+0.09798)\approx24.85\) observations to class 1 and about \(10.15\) to class 2. A feasible integer allocation close to this is \((25,10)\).

For \((25,10)\), the variance is \(0.0576/25+0.0096/10=0.002304+0.000960=0.003264\). For the comparison allocation \((17,18)\), it is \(0.0576/17+0.0096/18\), approximately \(0.003922\). The first allocation has lower estimated variance for this particular target and these specified class risks. Other goals, such as ensuring a precise class-specific estimate for the rare class, can justify allocating more observations to class 2 than this population-risk variance criterion recommends.

Common Mistakes and Practical Interpretation

  • Using the validation proportions as risk weights automatically. The raw error fraction estimates risk under the validation mix. For population risk, use \(\pi_k\); for macro risk, use equal class weights.
  • Assuming stratification removes uncertainty. It guarantees class representation, but the class-specific error rates still vary. Their variance depends on both the class risks and the fixed counts.
  • Interpreting an empty class stratum as zero error. If an ordinary random holdout contains no examples from a class, its error rate for that class is unobserved, not zero. For a rare class with prevalence \(\pi\), the probability of no examples in an unstratified validation sample of size \(M\) is \((1-\pi)^M\). For example, at \(\pi=0.01\) and \(M=100\), this probability is \(0.99^{100}\), approximately \(0.366\).
  • Ignoring the training split. A stratified holdout estimates the risk of the classifier produced by its training data. Changing class counts in training can change the fitted classifier as well as the validation estimator’s variability.

Stratification is especially useful when rare classes need reliable representation, but it does not make the holdout automatically representative for every performance target. State the target weights, report class-specific results when they matter, and distinguish uncertainty from the validation sample from variability due to fitting the model.

Check Your Understanding

Use the definitions and results above to answer the following questions.

  1. A validation set contains equal numbers from two classes whose population proportions are 0.9 and 0.1. What target does its unweighted error fraction naturally reflect?
  2. For fixed class-specific risks and fixed counts, what happens to the class-\(k\) contribution to the weighted risk variance as \(m_k\) increases?
  3. Why can a stratified estimate be conditionally unbiased for the fitted classifier’s population risk without being an estimate of the risk of a model trained on the full data set?
  4. State the variance-minimizing continuous allocation when every \(a_k=\pi_k^2R_k(1-R_k)\) is positive.
  5. What is the probability that an unstratified holdout of size \(M\) omits a class with prevalence \(\pi\)?
Clinical Biostats