Tutorials › Statistical Learning (Graduate) › Nested Cross-Validation for Post-Tuning Performance Assessment

Validation and Data-Driven Tuning · Tutorial 281 of 989

Nested Cross-Validation for Post-Tuning Performance Assessment

Build an inner tuning procedure inside each outer training fold, then use the untouched outer fold to assess that complete procedure.

Advanced 12 min read

What You'll Learn

  • Define the procedure and risk that nested cross-validation assesses.
  • Construct inner tuning folds separately within each outer training set.
  • Calculate a pooled outer-fold performance estimate.
  • Prove what the outer estimate averages under independent sampling.
  • Distinguish performance at the outer training size from performance after tuning on all available data.
  • Identify leakage and avoid interpreting outer-fold scores as independent replicates.

Why Nest the Tuning and Evaluation Loops?

A tuning procedure uses data to choose among candidate settings: for example, a penalty level, a tree depth, or a number of neighbors. That choice is part of the learning procedure being assessed. If the same validation results both select the setting and report its performance, the reported score can benefit from having been selected to look favorable. Nested cross-validation separates these jobs: an inner loop selects a setting using only an outer training set, and an outer loop evaluates the resulting tuned fit on observations that played no part in that selection.

The target is important. Nested cross-validation assesses a procedure trained on the outer training portions, including its inner tuning step. It does not directly give an unbiased assessment of the particular model refitted on the entire data set after tuning there. The outer estimate is useful precisely because its evaluation observations are kept apart from the tuning decisions for their fold.

Definition and Construction

Definition: Nested cross-validation uses an outer partition to assess performance and, separately within each outer training set, an inner cross-validation procedure to select a tuning parameter. For each outer fold, the selected procedure is refitted on that fold's full training set and evaluated on its held-out outer fold.

Here is a precise version for independent observations \(Z_i=(X_i,Y_i)\), \(i=1,\ldots,n\), drawn from a common distribution. Let \(L(y,\hat y)\) be a measurable loss with finite expectation for the fitted predictors under consideration. Let \(\Lambda\) be a finite candidate set of tuning values, and let \(A_m(D;\lambda)\) denote the predictor returned by a learning algorithm trained on data set \(D\) of size \(m\) using setting \(\lambda\). The candidate set, loss, and fitting rules are specified before examining outer-fold outcomes.

Partition the observations into outer folds \(I_1,\ldots,I_K\), with sizes \(n_k=|I_k|\), and let \(D_{-k}\) denote the observations outside \(I_k\). For each \(k\), partition \(D_{-k}\) into inner folds \(J_{k1},\ldots,J_{kK'}\). The inner score for \(\lambda\) is the average loss on those inner validation observations, each predicted by a model trained without that inner fold:

$$ \widehat R_{\mathrm{in},k}(\lambda) = \frac{1}{n-n_k} \sum_{r=1}^{K'} \sum_{i\in J_{kr}} L\!\left(Y_i,\, A_{n-n_k-|J_{kr}|}(D_{-k}\setminus J_{kr};\lambda)(X_i)\right). $$

Choose \(\hat\lambda_k\) to minimize this score, using a fixed tie-breaking rule if necessary. Then refit at \(\hat\lambda_k\) using all of \(D_{-k}\), and calculate losses only on \(I_k\):

$$ \hat\lambda_k=\arg\min_{\lambda\in\Lambda}\widehat R_{\mathrm{in},k}(\lambda), \qquad \hat R_{\mathrm{out}} = \frac{1}{n}\sum_{k=1}^{K}\sum_{i\in I_k} L\!\left(Y_i,A_{n-n_k}(D_{-k};\hat\lambda_k)(X_i)\right). $$

All choices that use data must respect this nesting. For instance, if standardization, imputation, feature screening, or class-threshold selection is part of the pipeline, it must be fitted using only the relevant training portion. In an inner split, that means fitting it without the inner validation observations; in an outer split, the complete inner tuning procedure must use only \(D_{-k}\).

Worked Examples

Worked Example: Choosing a Penalty Inside One Outer Fold

Suppose an outer training set contains 12 observations, split into three inner folds of four observations each. Two penalty settings, \(\lambda_a\) and \(\lambda_b\), are compared using squared-error loss. The inner validation sums of squared errors are as follows.

Inner fold\(\lambda_a\) loss sum\(\lambda_b\) loss sum
1 (4 observations)3.22.8
2 (4 observations)2.43.6
3 (4 observations)3.23.2
Total (12 observations)8.89.6

Because each observation is validated once, the inner mean losses are \(8.8/12=0.733\overline{3}\) for \(\lambda_a\) and \(9.6/12=0.8\) for \(\lambda_b\). Thus \(\hat\lambda_k=\lambda_a\). The next step is to refit the model with \(\lambda_a\) on all 12 outer-training observations. The inner scores are used to choose the setting; neither is the outer performance score.

Suppose that refitted model has squared errors \(0.5,1.0,0.5,1.0\) on the four observations in its outer fold. Its outer-fold mean loss is \((0.5+1.0+0.5+1.0)/4=0.75\). Those four observations did not determine \(\hat\lambda_k\), so this score evaluates the tuning-and-refitting procedure rather than the inner winner's validation score.

Worked Example: Why Selecting From Outer Scores Is Leakage

Imagine two candidate pipelines evaluated on the same outer fold, with outer mean losses \(0.18\) and \(0.24\). If the analyst chooses the first pipeline using only the inner data, then its outer loss of \(0.18\) can be included in the nested estimate. But if the analyst compares both outer losses and then reports the smaller one as the performance of the selected pipeline, the outer data have now influenced selection.

For these two observed scores, that post hoc report is \(\min(0.18,0.24)=0.18\). The arithmetic is correct, but the interpretation is not: the reported value is the better of two scores chosen using the evaluation data, not an untouched assessment of a pre-specified tuning procedure. The correction is to move every such choice into the inner loop, or to reserve a further independent test set for evaluating a choice made after the outer comparison.

Worked Example: Pooling Unequal Outer Folds

Suppose \(n=10\), with three outer folds of sizes \(4,3,3\), and their outer mean losses are \(0.20,0.40,0.10\), respectively. The pooled estimate weights each fold by its number of observations:

$$ \hat R_{\mathrm{out}} = \frac{4(0.20)+3(0.40)+3(0.10)}{10} = \frac{0.8+1.2+0.3}{10} = 0.23. $$

The unweighted average of the three fold means is \((0.20+0.40+0.10)/3=0.233\overline{3}\). That is a different summary: it gives each fold equal weight, even though the first fold contains four observations and the others contain three. The pooled form in the definition is the average of the ten individual outer losses.

What the Outer Estimate Targets

For a predictor \(f\), define its risk on a fresh observation \(Z=(X,Y)\) by \(R(f)=E[L(Y,f(X))]\), assuming this expectation exists. Write \(f_k=A_{n-n_k}(D_{-k};\hat\lambda_k)\) for the predictor produced by the complete inner-selection and refitting procedure in outer fold \(k\). The held-out observations estimate the risk of \(f_k\), conditional on the data used to construct it.

Theorem: Suppose observations are independent and identically distributed, the outer fold assignment is fixed independently of their values, and each \(f_k\) is constructed using only \(D_{-k}\) and any independent algorithmic randomization. If the losses are integrable, then $$ E[\hat R_{\mathrm{out}}] = \sum_{k=1}^{K}\frac{n_k}{n}E[R(f_k)]. $$ Conditional on \(D_{-k}\) and the algorithmic randomization used to construct \(f_k\), the mean outer loss in fold \(k\) has expectation \(R(f_k)\).

Proof. Fix a fold \(k\). Conditional on its training data and the randomization used to fit and tune, \(f_k\) is fixed. The observations in \(I_k\) are independent of \(D_{-k}\) and have the common distribution of a fresh \(Z\). Therefore, for every \(i\in I_k\),

$$ E[L(Y_i,f_k(X_i))\mid D_{-k},f_k] = R(f_k). $$

Taking the average over the \(n_k\) observations gives conditional expectation \(R(f_k)\) for the fold mean. For the pooled estimate, apply linearity of expectation to the weighted sum of fold means:

$$ E[\hat R_{\mathrm{out}}] = \sum_{k=1}^{K}\frac{n_k}{n}E\!\left[ \frac{1}{n_k}\sum_{i\in I_k}L(Y_i,f_k(X_i)) \right] = \sum_{k=1}^{K}\frac{n_k}{n}E[R(f_k)]. $$

The fold scores need not be independent for this expectation calculation. In particular, observations used to evaluate one outer fold can appear in the training sets for other folds. This proof establishes the expectation of the pooled score, not an independence claim about fold scores. \(\square\)

This theorem also identifies the training-size qualification. If the folds have equal size and the same inner design, then each \(f_k\) is produced by the same procedure at training size \(m=n-n/K\). Under the iid assumptions, the terms \(E[R(f_k)]\) are equal, so the theorem reduces to \(E[\hat R_{\mathrm{out}}]=E[R(f_1)]\). More generally, the theorem gives a fold-size-weighted average of the risks at the different outer training sizes. Neither statement makes the target the risk of a model tuned and refitted using all \(n\) observations.

Result: With equal-sized outer folds, nested cross-validation assesses the expected risk of the specified tuning-and-refitting procedure trained on \(n(1-1/K)\) observations, under the assumptions above. It does not, by itself, assess the exact final fit trained on all \(n\) observations.

Derivation. When \(n_k=n/K\) for every \(k\), the theorem's weights are all \(1/K\). Each outer training set has size \(n-n/K\); by identical sampling and a common inner design, the resulting predictors have the same risk distribution. Thus each term \(E[R(f_k)]\) equals the expected risk of that procedure at the outer training size \(m=n(1-1/K)\). Substitution gives the stated target. If folds differ in size, their training sizes can differ too, so the weighted expression in the theorem is the appropriate general statement. \(\square\)

Interpretation and Common Pitfalls

Nested cross-validation matters when the scientific or operational question concerns performance after a data-driven choice. The inner loop represents that choice; the outer loop evaluates the whole choice-and-fit process on data withheld from it. After assessment, it is common to run the tuning procedure on all available data to produce a final model. That can be a sensible use of the data, but the nested estimate remains an assessment at the outer training size, rather than a direct score for that final full-data fit.

  • Using outer results to retune. Comparing settings by outer scores and reporting the best score lets evaluation outcomes guide the choice. Keep such comparisons inside the inner loop.
  • Preprocessing before splitting. If information from a validation fold affects a transformation or feature choice, that fold is no longer untouched. Fit each data-dependent step within the corresponding training split.
  • Reporting the inner minimum as performance. The minimum inner score selected the setting; it is not the outer assessment. Report the pooled outer score for the specified nested procedure.
  • Treating outer-fold scores as independent replicates. Their training sets overlap, so an ordinary standard error calculated as if the fold scores were independent is not justified by this construction.
  • Claiming a full-sample guarantee. The outer models train on fewer than \(n\) observations. State the assessed training size and avoid equating the estimate with the unknown risk of the final refit.

A useful report describes the loss, candidate settings, inner and outer fold designs, tie-breaking rule, and any preprocessing included in the pipeline. It should also say whether the reported estimate is pooled across outer observations and identify the approximate outer training size. These details define what procedure was assessed and make the result interpretable.

Check Your Understanding

Use the definitions and results above to check your understanding.

  1. Why is the inner minimum validation loss not the outer estimate of post-tuning performance?
  2. In one outer fold, which observations may be used to select the tuning parameter?
  3. State the risk target of equal-fold nested cross-validation when \(K\) outer folds are used.
  4. Why can an outer-fold score be unbiased for its fold predictor's risk without the outer-fold scores being independent of one another?
  5. If preprocessing is fitted once using the full data before nested cross-validation begins, what part of the evaluation design has been compromised?
  6. What additional caution is needed when someone uses the outer scores themselves to choose among pipelines?
Clinical Biostats