Why Fold Assignment Matters
In \(K\)-fold cross-validation, each observation is held out once while a model is fitted using the observations in the other folds. The resulting prediction errors are combined to estimate how well the fitting procedure predicts cases from a target population. Fold assignment therefore affects which cases are compared with one another and, in a finite data set, how representative each held-out fold is.
With a continuous outcome, a purely shuffled assignment can put many low outcomes in one fold and many high outcomes in another. This can happen by chance, especially in a small data set or when a few values are extreme. Approximate outcome-range balancing is an alternative: it tries to put observations from across the observed outcome range into every fold. It is a way to make fold composition more even, not a way to make the folds identical or to improve the fitted model itself.
These are assignment strategies for the same cross-validation procedure. They do not change which cases are in the data set. They do change the particular training and held-out sets used in each round, so the resulting estimate can vary with the assignment.
Shuffled Random Assignment
A basic approach is to shuffle the case labels and allocate the cases in that random order to folds, cycling through the fold labels. This produces fold sizes that differ by at most one. Each observation is assigned to exactly one fold, and no case is deliberately placed according to its outcome.
When cases are independent and come from the same target population, random assignment is a natural way to form folds: before the assignment, each case has the same chance of being in any fold. It does not guarantee that every fold will look alike. In particular, random assignment cannot guarantee equal proportions of low, middle, and high outcomes in a finite sample.
Worked Example: A Shuffled Assignment
Suppose a hypothetical data set has 12 cases with outcomes, in the original case order, \(10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30,\) and \(50\). For \(K=3\), suppose a random shuffle followed by cycling through three fold labels produces the following assignment.
| Fold | Outcomes assigned to the fold | Fold mean outcome |
|---|---|---|
| 1 | 10, 18, 26, 50 | 26.00 |
| 2 | 12, 20, 24, 30 | 21.50 |
| 3 | 14, 16, 22, 28 | 20.00 |
Each fold has four cases. For Fold 1, the mean outcome is
The other means are \(\bar{y}_2=(12+20+24+30)/4=21.50\) and \(\bar{y}_3=(14+16+22+28)/4=20.00\). All three folds contain several outcome values, but the means differ, and the largest outcome, 50, is in only one fold. This assignment is possible under random shuffling; it is not evidence that the procedure was carried out incorrectly.
Approximate Balancing Across Outcome Ranges
To balance the outcome range, first divide the observed outcomes into ordered groups, often using ranges or ranks. Then distribute observations from each group across the folds, with allocations made as evenly as possible. The groups should generally reflect their observed frequencies: if one range contains more cases than another, its per-fold allocations should usually be correspondingly larger. Randomizing the allocation within groups helps avoid a fixed assignment pattern.
The goal is broad coverage in each fold, not identical outcomes. Exact equality of fold means is neither required nor guaranteed. A rare extreme value, for example, cannot appear in every fold if there is only one such case. Approximate balancing can still prevent all the observations in a broad part of the outcome range from being concentrated in one fold.
Worked Example: Distributing Outcome Ranges Across Folds
Use the same 12 hypothetical outcomes and three folds. Divide them into three ordered groups of four: low (10, 12, 14, 16), middle (18, 20, 22, 24), and high (26, 28, 30, 50). A randomized allocation within these groups gives two low, one middle, and one high outcome to Fold 1; one low, two middle, and one high to Fold 2; and one low, one middle, and two high to Fold 3.
| Fold | Low group | Middle group | High group | Fold mean outcome |
|---|---|---|---|---|
| 1 | 10, 12 | 20 | 30 | 18.00 |
| 2 | 14 | 18, 22 | 50 | 26.00 |
| 3 | 16 | 24 | 26, 28 | 23.50 |
For example, Fold 1 has mean \((10+12+20+30)/4=72/4=18.00\). Every fold now includes outcomes from all three groups. The fold means still differ: range coverage is not a promise of equal means. The unusual value 50 also remains in just one fold because it is a single observation. Its presence can still affect the predictions and errors in the cross-validation run.
This allocation uses group counts close to what is possible with four observations from each group and three folds. In other data sets, group counts may not divide evenly among folds. The allocations then differ by one where necessary, and the resulting folds may only be approximately balanced.
What Balancing Does—and Does Not—Change
A useful distinction is between the target population of the evaluation and the composition of individual folds. Suppose the cases are sampled from the population whose prediction performance is of interest. A fold assignment rearranges these observed cases into training and held-out sets; it does not, by itself, replace the original population with a different one. When outcome groups are represented in each fold in proportion to their observed frequencies, balancing can reduce accidental differences in outcome-range composition while retaining the overall mix represented by the data.
The interpretation depends on how results are summarized. If the evaluation target is average prediction error for cases drawn from the original population, each case should contribute according to that target. If groups are deliberately given equal representation even though their population frequencies differ, an unweighted average across groups may instead describe a population in which those groups are equally common. To target the original population in that situation, group contributions need weights reflecting the target population frequencies.
Balancing is not a correction for a nonrepresentative sample. It cannot recover outcome ranges absent from the data, undo selection bias, or make observations independent when the data have a grouped or time-ordered structure. Nor does it guarantee less variability in every cross-validation result: prediction errors also depend on the predictors, fitted models, and which observations are in the training sets.
The sum includes only groups that have observations in fold \(k\). This restriction matters: when \(n_{hk}=0\), the group mean \(\bar{y}_{hk}\) is undefined, so its product with zero must not be included as though it were an ordinary term.
Why the Weighted Mean Identity Holds
The identity follows by adding the outcomes group by group within a fold. For each group with observations in fold \(k\), the definition of its mean gives \(\sum_{i\in(h,k)}y_i=n_{hk}\bar{y}_{hk}\). Groups with no cases contribute no outcomes and are omitted. Thus the sum of all outcomes in fold \(k\) is the sum of these group totals. Dividing by the fold size gives its mean:
Across all folds, each observation occurs exactly once. Therefore, adding the fold outcome totals recovers the total of all observed outcomes, and dividing by the total sample size recovers the full-data mean. This is an accounting identity, not evidence that fold means are equal or that prediction errors are equal.
Worked Example: Checking the Overall Mean With Unequal Fold Sizes
Suppose ten hypothetical outcomes are \(1,2,3,4,5,6,7,8,9,\) and \(30\), assigned to three folds of sizes four, three, and three. For illustration, form low (1, 2, 3), middle (4, 5, 6), and high (7, 8, 9, 30) groups and allocate them as follows.
| Fold | Low group | Middle group | High group | Fold size | Fold mean |
|---|---|---|---|---|---|
| 1 | 1 | 4 | 7, 30 | 4 | 10.50 |
| 2 | 2 | 5 | 8 | 3 | 5.00 |
| 3 | 3 | 6 | 9 | 3 | 6.00 |
The fold means are \(42/4=10.50\), \(15/3=5.00\), and \(18/3=6.00\). The fold with four cases must receive more weight than either fold with three cases when reconstructing the overall mean:
Directly averaging the ten outcomes also gives \(75/10=7.50\). An unweighted average of the three fold means would instead be \((10.50+5.00+6.00)/3=7.167\), which is not the overall mean because the fold sizes are unequal. In this example, the high group has two observations in Fold 1 and only one in each other fold; the group mean for the high group in Fold 2 is defined, but it would not be defined in a fold receiving no high-group cases.
Choosing Between the Two Approaches
Shuffled random assignment is a clear default when observations are exchangeable and the data set is large enough that each fold is likely to cover the outcome range adequately. Approximate outcome-range balancing can be useful when the sample is modest, the outcome is skewed, or a small number of observations occupy unusual parts of the range. It can make fold-level summaries less dependent on an unlucky concentration of outcomes.
Use balancing as a controlled allocation, not as a search for the assignment that produces the most favorable prediction score. One sound approach is to create outcome groups, randomize cases within each group, and allocate each group across folds as evenly as fold sizes permit. Keep the groups sufficiently broad to avoid creating many tiny groups, and check the final fold sizes and outcome summaries. Exact group boundaries are a practical choice; they do not define new scientific categories.
Outcome-based assignment should also be distinguished from using the outcome as a predictor. The outcome is used here only to organize the held-out sets. It must not be supplied to the model as an input for predicting that outcome. The observed outcome is needed to score predictions, and its use to define a balanced partition does not make it available as a predictor at deployment. However, choosing fold assignments after examining which arrangement gives a preferred model result would compromise an impartial evaluation.
Finally, random case-level assignment is not suitable for every data structure. If several records come from the same person, clinic, or other cluster, splitting those records across folds may make held-out cases too similar to training cases. For time-ordered data, randomizing across time can likewise fail to reflect prediction into the future. In such settings, assignment must respect the relevant grouping or time structure; balancing the continuous outcome does not solve that problem.
Common Mistakes
- Expecting balanced folds to have equal means. Outcome-range balancing aims for coverage, not exact equality. Small samples and extreme observations can still produce noticeably different fold means.
- Equalizing groups that are not equally common. Giving every outcome group the same number of cases in each fold can distort the target mix. Use allocations that reflect observed frequencies when the goal is to represent the original population.
- Interpreting a balanced assignment as a representative sample guarantee. It changes the arrangement of the observed cases; it cannot correct a sample that does not represent the target population.
- Including a nonexistent group mean in a calculation. If a fold has no cases from a group, its group mean is undefined. Omit that group from the weighted sum rather than writing a zero count multiplied by an undefined mean.
- Ignoring dependence in the data. Outcome balance does not justify splitting repeated measurements, clusters, or time series as if every case were independent.
Check Your Understanding
Answer each question using the definitions and examples above.
- In a shuffled assignment, why can two equally sized folds have quite different mean outcomes?
- A continuous outcome has one unusually large value. Can approximate outcome-range balancing ensure that this value appears in every fold? Explain.
- Three folds have sizes 4, 3, and 3, with means 10.50, 5.00, and 6.00. What weighting gives the overall mean, and why is an unweighted average of the fold means inappropriate?
- How could assigning the same number of cases to every outcome group in every fold change the population described by an unweighted group average?
- Why might random observation-level fold assignment be inappropriate when multiple records come from the same person?