Why the Algorithm Has Two Levels
A classifier’s performance can look better when the same validation results are used both to choose its tuning parameters and to report how well it performs. Nested cross-validation separates those jobs. Inner folds select a classifier configuration; outer folds evaluate the selection process on observations that played no part in that selection. This distinction is especially important in genomic prediction, where the number of candidate predictors and analysis choices can be large relative to the number of individuals.
The algorithm estimates the performance of a specified tuning-and-fitting procedure, not the performance of a model chosen once using all available outcomes. Each outer training set gets its own inner tuning step and may select a different configuration. The resulting outer-fold scores summarize how that procedure performs when trained on a subset of the data.
Set the Evaluation Rules Before Fitting
Let the data be \(D=\{(x_i,y_i):i=1,\ldots,n\}\), where \(x_i\) denotes the predictor information for observational unit \(i\) and \(y_i\) is its outcome. Specify the prediction target and unit of cross-validation, the classifier, the candidate configurations, the performance metric, and the fold construction before running the algorithm. For example, a configuration \(\theta\) might specify a regularization strength and a class-weighting choice. The candidate set \(\Theta\) is the finite collection of configurations that will be compared.
The folds must reflect the intended prediction setting. The units assigned to different folds should be separated in the way required by the target: for example, related individuals or observations from the same site may need to remain together. Use an appropriate fold construction, such as stratification when required by the design, rather than assuming that ordinary random folds are always valid. The split determines what is being evaluated; nested tuning does not repair an unsuitable split.
The performance metric must be defined in advance, including which direction is better. For accuracy, larger is better; for a loss such as classification error, smaller is better. All candidate configurations must be scored using the same metric and validation observations within a given outer training set. If a metric cannot be computed for a particular fold, that must be addressed in the design or specified scoring rule rather than handled ad hoc after seeing results.
The usual interpretation assumes that the folds represent the target population or sampling design, and that the observations used for evaluation are not used to fit or select the model being evaluated. Any learned operation belongs inside the fitting process: imputation, normalization, screening, feature selection, dimension reduction, and classifier fitting must be learned from the training portion at each stage. A fixed candidate list and a prespecified rule for resolving ties also make the procedure reproducible. If model fitting is stochastic, control or record its randomness so that differences in scores are not silently caused by unrelated random seeds.
The Nested Cross-Validation Algorithm
Write the outer folds as \(O_1,\ldots,O_K\), with corresponding outer training sets \(T_k=D\setminus O_k\). Within each \(T_k\), create \(J\) inner validation folds \(V_{k1},\ldots,V_{kJ}\); the training data for inner fold \(j\) are \(T_k\setminus V_{kj}\). For each candidate configuration, fit on each inner training set and score on its inner validation fold. Average the \(J\) scores using the prespecified rule, then select the candidate with the best average.
Partition the observations into \(K\) outer folds according to the specified validation design. Do not use outer-fold outcomes to make modeling or tuning choices.
For each \(k\), partition \(T_k\) into inner folds. For every \(\theta\in\Theta\), fit the full analysis procedure on each inner training set and score it on the corresponding inner validation fold. Average the scores and select \(\hat{\theta}_k\).
Apply the analysis procedure, with \(\hat{\theta}_k\), to all of \(T_k\). Learned transformations are fitted on \(T_k\), not on \(O_k\).
Use the fitted procedure to predict outcomes for \(O_k\), then calculate the prespecified metric \(M_k\) from those predictions and the outer-fold outcomes.
Combine the outer-fold metrics by the prespecified summary. For equal-sized folds, the arithmetic mean is a common summary: \(\widehat{M}_{\mathrm{outer}}=K^{-1}\sum_{k=1}^{K}M_k\).
The outer fold is not a second tuning set. Once its observations have been used to calculate \(M_k\), changing the candidate set, preprocessing, or selection rule in response to that result turns it into part of model development. The same outer outcomes cannot then serve as an untouched evaluation of the revised procedure.
Worked Example: Selecting Between Two Classifiers
Consider a hypothetical data set of 12 unrelated individuals with a binary genomic outcome. Use three outer folds of four individuals each, and within each outer training set use two inner folds. Compare configurations A and B by accuracy, defined as the proportion of correctly classified individuals. Higher accuracy is better; ties are resolved in favor of A, as specified before fitting.
In outer iteration 1, the two inner-fold accuracies are 0.75 and 0.50 for A, and 0.625 and 0.75 for B. Thus,
B is selected for outer iteration 1. In outer iteration 2, suppose A scores 0.75 and 0.75, while B scores 0.625 and 0.625. The inner averages are 0.75 for A and 0.625 for B, so A is selected. In outer iteration 3, A scores 0.625 and 0.75, while B scores 0.75 and 0.625. Both averages are 0.6875, so the prespecified tie rule selects A.
Now refit the selected configuration on each complete outer training set and evaluate its predictions on that iteration’s four held-aside individuals. Suppose the numbers correctly classified are 3, 2, and 3. The outer-fold accuracies are \(3/4=0.75\), \(2/4=0.50\), and \(3/4=0.75\). Their mean is
The result is an estimated accuracy of about 66.7% for this nested selection-and-fitting procedure under the specified fold design. It is not the inner average, nor the accuracy of a single configuration selected using all 12 outcomes.
Why the Outer Evaluation Is Separate
The separation can be seen by following the information used at each stage. For a fixed outer iteration \(k\), the selected configuration \(\hat{\theta}_k\) is a function of the data in \(T_k\): every inner fit and validation score uses only observations from \(T_k\). The refitted classifier is also based only on \(T_k\). Thus the outcomes in \(O_k\) do not determine which candidate is selected or how the classifier is fit; they are used only to calculate its evaluation score.
This is the reason for nesting. If one instead tunes on all observations and reports the best cross-validation score from that same tuning, the reported score has participated in choosing the winner. The outer procedure evaluates the entire rule—“tune by this inner procedure, then fit”—on observations kept aside from that rule’s selection step.
This separation does not make the outer estimate exactly unbiased for every possible target. Each outer model is trained on less data than the final model that might later be fit to all \(n\) observations. The estimate therefore pertains most directly to the specified procedure trained on outer-training-set-sized samples, under the data-generating and fold design assumptions. It also has sampling variability, and the fold scores should not be treated as independent observations simply because they are listed separately.
Worked Example: Keeping the Analysis Pipeline Inside the Folds
Suppose each outer training set contains 80 individuals, and the candidate classifier has two regularization settings. Before classification, the analysis uses median imputation, centering and scaling, and a screen that retains the 50 predictors with the largest training-set associations with the outcome.
For one inner split, suppose 60 individuals are used for inner training and 20 for inner validation. Calculate imputation values, scaling constants, and the screening list using only the 60 inner-training individuals and their outcomes. Fit each candidate classifier on those processed training data, then apply the learned imputation, scaling, and selected predictors to the 20 validation individuals to score the candidate. Repeat for every inner split and candidate.
After selecting a regularization setting, recompute the imputation values, scaling constants, and screening list using all 80 individuals in the outer training set. Fit the selected classifier on those 80 individuals, apply that fitted pipeline to the outer test individuals, and calculate the outer score. No statistic learned from an outer test individual—including a missing-value replacement or a predictor ranking—enters either the fitting or the selection.
The counts clarify the boundary: the inner validation set is held out from the fit for its inner score, while the outer test set is held out from both inner tuning and final fitting within that outer iteration. Fitting a screen once on all 80 outer-training individuals before inner cross-validation would also be invalid for inner scoring, because each inner validation outcome would have influenced the screen.
Implementation Checks and Common Failure Modes
A practical implementation should record, for every outer iteration, the outer-fold membership, inner-fold membership, candidate scores, selected configuration, and outer score. This makes it possible to check that the reported result follows the algorithm rather than a convenient but different sequence of operations. In particular, verify that:
- every outer observation is evaluated only in its held-out outer fold, and never enters that iteration’s inner tuning;
- each candidate is scored on the same inner folds within an outer iteration;
- the selected configuration is refit on the full outer training set before outer predictions are made;
- all data-adaptive steps are refitted within each relevant training split; and
- the scoring rule and tie rule are applied consistently across outer iterations.
A common mistake is to run cross-validation once on the full data, select the configuration with the best score, and call that same best score the nested estimate. The reasoning is wrong because the score helped choose the configuration; it is not an outer evaluation. The correction is to repeat the complete inner selection separately inside every outer training set and score only the resulting refit on that iteration’s untouched outer fold.
Other failures include fitting feature selection before making inner folds, using outer results to revise the candidate set and still reporting those results as untouched, or allowing observations that should be grouped together to cross fold boundaries. Nested cross-validation does not fix leakage caused by a poorly defined observational unit, inappropriate folds, or an analysis step performed outside the folds. Nor does it guarantee that all possible sources of uncertainty are represented: it estimates performance for the chosen data, design, candidate set, and metric.
How to Read and Use the Estimate
The outer estimate answers a defined predictive question: how well does the specified procedure perform when its tuning is carried out using inner cross-validation and its fitted model is applied to observations held out by the outer design? State the metric, units or scale, and validation design alongside the numerical estimate. For example, “mean outer-fold accuracy was 66.7% under three-fold cross-validation” is more informative than “the model was 66.7% accurate.”
After evaluation, a final model intended for deployment can be trained by applying the prespecified tuning procedure to all available development data and then fitting the selected configuration on those data. That final fit is not itself evaluated by the outer folds as a model trained on all \(n\) observations. If its performance must be assessed independently, evaluation data not used for development provide a distinct source of evidence.
Worked Example: A Prespecified Loss-Minimizing Rule
Suppose the metric is mean classification error, the fraction of incorrect predictions, so smaller is better. In one outer iteration, the two inner-fold errors for configuration C are 0.20 and 0.30; for configuration D they are 0.25 and 0.20. The inner averages are
The rule selects D because 0.225 is smaller than 0.25. Suppose its refitted outer classifier makes 2 errors among 10 held-out individuals. Its outer error is \(2/10=0.20\). This is a 20% error rate for that outer fold, not a statement that D will have precisely that error rate in every new cohort. The example also shows why “choose the largest score” is not a generic instruction: the selection direction must match the metric.
Check Your Understanding
Answer each question using the nested algorithm and its stated boundaries.
- In one outer iteration, should the outer-fold outcomes be used to choose between two candidates? Why or why not?
- A candidate has inner-fold accuracies 0.70, 0.80, and 0.60. What is its mean inner accuracy?
- In the pipeline example, may the predictor screen be calculated once using all individuals before inner folds are formed? Explain.
- Three equal-sized outer folds have accuracies 0.60, 0.80, and 0.70. What is their mean outer-fold accuracy?
- If two candidates have the same prespecified inner score, what determines which is selected?
Solutions. (1) No. The outer fold is reserved for evaluation; using its outcomes for selection would make it part of model development. (2) \((0.70+0.80+0.60)/3=0.70\), or 70.0%. (3) No. The screen uses outcome associations, so calculating it before inner folds would use inner-validation outcomes; it must be learned from each inner training set and then recomputed on the complete outer training set for outer evaluation. (4) \((0.60+0.80+0.70)/3=0.70\), or 70.0%. (5) The tie is resolved by a rule specified before evaluating the outer folds, such as choosing the simpler configuration; it should not be decided by examining outer scores.