Tutorials › High-Dimensional Genomic Data Analysis › Stratified Folds for Imbalanced Outcomes

Honest Evaluation and Leakage · Tutorial 629 of 795

Stratified Folds for Imbalanced Outcomes

Learn to construct and assess stratified folds for rare genomic outcomes, including the limits of stratification and its effect on cross-validation interpretation.

Intermediate 10 min read

What You'll Learn

  • Allocate cases and controls across folds while keeping fold sizes as balanced as possible.
  • Check when every fold can contain both outcome classes.
  • Calculate the chance that a random fold contains no cases.
  • Explain why stratification can stabilize fold-level metrics without changing the target population.
  • Recognize when stratification is insufficient or inappropriate.

Why Stratify Folds?

In binary genomic prediction, the outcome of interest may be rare: for example, a small fraction of individuals may have a particular molecular subtype or clinical event. In ordinary \(K\)-fold cross-validation, observations are divided into \(K\) validation folds and each fold is used once to evaluate a model trained on the other folds. If few cases are present, a random division can leave some folds with very few cases or none at all. Metrics that rely on both outcome classes may then be unstable or undefined.

Stratified folds address this allocation problem by distributing cases and controls separately across folds. The purpose is to make the validation sets more comparable in their observed outcome composition. Stratification does not create additional cases, guarantee representative data in every other respect, or change the population to which the prediction target refers.

Definition: For a binary outcome, stratified \(K\)-fold cross-validation partitions cases and controls separately into \(K\) groups, then combines one group of cases with one group of controls to form each validation fold. The resulting folds are mutually exclusive, cover all observations, and have case counts and control counts as equal as possible across folds.

Let \(N\) be the total number of observations, \(N_1\) the number of cases, and \(N_0=N-N_1\) the number of controls. The observed case proportion is \(\hat{p}=N_1/N\). If \(N_1\) and \(N_0\) are both at least \(K\), each fold can contain at least one case and one control. If a class has fewer than \(K\) observations, that guarantee is impossible. Fold sizes may differ by one when \(N\) is not divisible by \(K\); stratification balances class counts as closely as the integer totals allow.

Constructing the Folds

A standard construction first shuffles the cases and divides them into \(K\) groups with sizes differing by at most one. It repeats this process for controls. The case group and control group with the same fold index are combined. Randomizing within each class avoids making the folds depend on the order of the data, while fixing a random seed makes the allocation reproducible.

The training set for fold \(k\) is the complement of validation fold \(k\): it contains all observations assigned to the other \(K-1\) folds. Each observation is therefore used for validation once and for training in \(K-1\) fits. The stratification rule concerns fold membership; it does not alter the observed outcomes or the model’s prediction target.

Worked Example: Allocating a Rare Outcome Across Five Folds

Suppose a hypothetical genomic data set contains 100 individuals: 5 cases and 95 controls. With \(K=5\), divide the five cases into five groups of one case each. Divide the controls into five groups of 19 controls each. Combining the corresponding groups gives five folds, each with 20 individuals: 1 case and 19 controls.

Every fold has the same observed case proportion, \(1/20=0.05\), which equals the full data set's case proportion, \(5/100=0.05\). Every validation fold contains both outcome classes, so a fold-level area under the receiver operating characteristic curve can be calculated. This allocation does not make the underlying case prevalence larger; it ensures that the limited cases are represented in every validation fold.

Why Random Folds Can Be Unstable

A random fold of fixed size \(m\) drawn without replacement from a data set with \(N_1\) cases and \(N_0\) controls has a hypergeometric number of cases. In particular, the probability that it contains no cases is the probability that all \(m\) selected observations come from the controls:

$$ P(\text{no cases in a random fold}) =\frac{\binom{N_0}{m}}{\binom{N}{m}}. $$

This calculation makes the problem concrete: when the class is rare, a seemingly substantial fold can still have a meaningful chance of containing no cases. Stratification prevents an empty-case fold when there are at least \(K\) cases, but random allocation does not.

Worked Example: Probability of an Empty-Case Fold

Use the hypothetical data set of 100 individuals, with 5 cases and 95 controls, and consider a random fold of size 20. Under simple random allocation, the probability that all 20 are controls is:

$$ P(\text{no cases}) =\frac{\binom{95}{20}}{\binom{100}{20}} =\frac{80}{100}\frac{79}{99}\frac{78}{98}\frac{77}{97}\frac{76}{96} \approx 0.3193. $$

Thus the probability is about \(31.93\%\), or roughly 32 in 100 random folds of this size under this calculation. The expected number of cases in a fold is \(20(5/100)=1\), but an expected count of one does not mean each fold will contain one case. Stratified five-fold allocation instead places exactly one case in each fold for these data.

The calculation treats a single fold as a simple random sample without replacement. The five folds in a complete cross-validation partition are not independent draws: once an observation is assigned to one fold, it cannot be assigned to another. The single-fold probability illustrates the risk of an empty fold; it should not be used as though it were the probability that all folds independently have a particular composition.

Justification and Interpretation

The key benefit is a reduction in variation due solely to accidental allocation of rare outcomes. Conditional on the observed class totals, stratification controls the number of cases and controls in each validation set. For example, if each of five folds has one case, a fold-level sensitivity is based on one case in every fold rather than being undefined in some folds and based on several cases in others.

This is a design choice for estimating performance on the same target population represented by the data, assuming observations are exchangeable draws from that target and the folds are otherwise formed without using information that would compromise evaluation. The total number of cases and controls is unchanged; no observation is reweighted or duplicated. Under this setup, validation observations still represent the observed data source, and the overall prediction target is not redefined by stratification. The folds are conditionally balanced given the observed outcomes, rather than independent random samples from the population.

This distinction matters for metrics. Sensitivity, specificity, and area under the receiver operating characteristic curve are class-conditional summaries; their estimates can be erratic when a fold contains very few members of a class, and the area under the curve is undefined if a fold contains only one class. Accuracy and positive predictive value depend on class prevalence as well as model behavior. Stratified folds preserve the sample's case proportion more consistently, but they do not make that proportion equal to the prevalence in some different intended population.

If the target population has a different prevalence from the study sample, preserving the sample proportion does not solve that mismatch. In particular, a positive predictive value computed from a validation sample reflects that sample's case prevalence. It should not automatically be interpreted as the positive predictive value in a population with a different prevalence. The target population and performance measure must be stated independently of the fold construction.

Worked Example: Fold Counts When Totals Do Not Divide Evenly

Suppose a hypothetical data set has 47 individuals: 7 cases and 40 controls. For \(K=5\), divide the cases into groups of sizes \(2,2,1,1,1\). Divide the controls into five groups of 8. The combined fold sizes are therefore \(10,10,9,9,9\), and every fold contains at least one case and one control.

The overall case proportion is \(7/47\approx0.1489\). The fold proportions are \(2/10=0.2000\), \(2/10=0.2000\), and \(1/9\approx0.1111\) for each of the remaining three folds. They cannot all equal the overall proportion because case counts are integers and there are only seven cases. The allocation is as balanced as possible: each fold receives one or two cases, with no fold left empty of cases.

If the evaluation metric is sensitivity, the first two folds each estimate it using two cases, while the other folds each use one. These estimates can still vary substantially; stratification prevents a fold with zero cases here, but it cannot make a one-case estimate precise.

Assumptions, Diagnostics, and Limits

Stratified folds are most useful when the observed outcome is binary, cases are scarce enough that random allocation could produce very different fold compositions, and the chosen number of folds is compatible with the class counts. The observations must also be assigned at the appropriate unit for the prediction target. Stratifying individual records does not by itself ensure that a validation design respects other dependence or sampling structures; the split unit must match the scientific question.

Before fitting models, inspect the number of cases, controls, and total observations in every fold. Confirm that each observation appears in exactly one validation fold and that the folds cover the full data set. Check whether every fold has the classes required by the chosen metric. If \(N_1<K\) or \(N_0<K\), reduce \(K\) when feasible, or use an evaluation plan that acknowledges that not every fold can contain both classes. Do not report an undefined fold-level metric as though it were zero.

Stratification has limits. With very few cases, fold estimates remain noisy even when cases are evenly allocated. The training sets also overlap, so fold-specific results are not independent replications. Stratification controls one source of split-to-split variation; it does not guarantee that folds are similar in predictors, clinical characteristics, or other factors. Nor does it remedy an inappropriate prediction target, biased sampling, or a mismatch between the observed and intended deployment populations.

Compared with ordinary random \(K\)-fold assignment, stratified assignment gives more controlled class counts and often makes rare-class metrics more interpretable across folds. Ordinary random assignment is simpler and may be adequate when both classes are abundant relative to \(K\). In either approach, the overall cross-validation estimate should be formed and interpreted consistently with the metric. For example, pooled out-of-fold predictions yield one overall sensitivity by dividing the total true positives by the total number of cases; this is not necessarily equal to an unweighted average of fold-specific sensitivities when fold case counts differ.

Common Mistake

  • Wrong reasoning: “Every stratified fold has the same case proportion, so the performance estimate must represent any population with that outcome.”
  • Correction: Stratification balances the observed cases and controls across validation folds. It neither changes the study sample's overall prevalence nor guarantees that this prevalence matches a different target population.
  • Another diagnostic error: Treating an empty-case fold's sensitivity as zero confuses “no cases available to evaluate sensitivity” with “the model failed on every case.” The metric is undefined for that fold.
Key takeaway: Stratify by the binary outcome when rare cases would otherwise be unevenly distributed across folds. Check class counts before analysis, and interpret the resulting performance as tied to the stated target population and metric—not as a consequence of fold balance alone.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. A data set has 12 cases and 188 controls. Can five-fold stratification place at least one case in every fold? What case counts are possible under a balanced allocation?
  2. A data set has 4 cases and 96 controls, and five-fold cross-validation is proposed. Why can’t every fold contain a case? Give one design adjustment.
  3. Why does a fold with no cases make sensitivity undefined rather than equal to zero?
  4. If validation folds preserve the sample case proportion, does this establish that positive predictive value applies to a population with a different prevalence? Explain.

Solutions. (1) Yes. Since \(12\geq5\), an allocation such as \(3,3,2,2,2\) cases across folds is possible. (2) There are fewer cases than folds, so at least one fold must have no case; reducing the number of folds to four or fewer permits at least one case per fold. (3) Sensitivity is the number of correctly predicted cases divided by the number of observed cases. With zero cases, that denominator is zero, so the fold provides no sensitivity estimate. (4) No. Positive predictive value depends on prevalence; the sample's stratified folds preserve the sample composition but do not establish the prevalence of a different target population.

Clinical Biostats