Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Cross-Validation

Repeatedly fit on one part of the data and score on another. Each observation receives a prediction from a model that did not use that observation for training.

kk-Fold Cross-Validation

Partition the indices into disjoint nonempty folds I1,…,IkI_1,\ldots,I_k, with nj=∣Ij∣n_j=|I_j| and ∑jnj=n\sum_jn_j=n. Let κ(i)\kappa(i) identify observation ii’s fold.

  1. For each fold jj, remove IjI_j and fit f^(−j)=A(T∖Tj)\widehat f^{(-j)}=A(T\setminus T_j).

  2. Predict the held-out observations in IjI_j.

  3. Average all held-out losses.

Lj=1nj∑i∈IjL(Yi,f^(−j)(Xi)),L_j=\frac1{n_j}\sum_{i\in I_j}L(Y_i,\widehat f^{(-j)}(X_i)),
Err⁡^CV=∑j=1knjnLj=1n∑i=1nL(Yi,f^(−κ(i))(Xi)).\boxed{\widehat{\operatorname{Err}}_{\mathrm{CV}} =\sum_{j=1}^k\frac{n_j}{n}L_j =\frac1n\sum_{i=1}^nL(Y_i,\widehat f^{(-\kappa(i))}(X_i)).}

If all folds are equally sized, this is k−1∑jLjk^{-1}\sum_j L_j. If they differ in size, weight fold means by their observation counts to obtain average loss per observation.

RoundFold 1Fold 2Fold 3
1Hold outTrainTrain
2TrainHold outTrain
3TrainTrainHold out

Every observation is scored once and used for training in k−1k-1 rounds. The fitted models differ between rounds.

What Does CV Estimate?

For a fixed algorithm and equal folds under iid sampling, each training subset contains m=n(1−1/k)m=n(1-1/k) observations, independent of its held-out fold. Therefore

E[Err⁡^CV]=Err⁡m.E[\widehat{\operatorname{Err}}_{\mathrm{CV}}]=\operatorname{Err}_m.

This is average generalization error for a procedure trained on mm observations. It is not an exact measurement of the conditional error of the final fit on all nn observations. For unequal folds, the expectation is a weighted average of risks at sizes n−njn-n_j.

The folds’ losses are dependent because the training subsets overlap. Their sample standard deviation divided by k\sqrt{k} is not generally a justified standard error for the CV score.

ChoiceTraining size per fitMain tradeoff
Smaller kkFurther below nnMore training-size mismatch; larger held-out folds.
Larger kkCloser to nnLess training-size mismatch; more fits and strongly overlapping training sets.
k=nk=nn−1n-1Leave-one-out; one scored observation per fit.

The familiar “low bias, high variance” description of leave-one-out is a useful heuristic, not a universal ordering of CV variances. Training-size bias depends on the learning curve, and variance depends on the data and procedure.

Worked Two-Fold Example

Use the illustrative observations

ii123456
xix_i012345
yiy_i122445
Fold121212

For a simple intercept-only algorithm, predict the mean of the training responses.

Selecting a Tuning Parameter

Select

Use validation folds to compare model choices. Refit the winner on all development data.

Assess

Use untouched test data or outer CV to evaluate the complete selection procedure. The winning validation score is not an independent assessment.

For candidates λ∈Λ\lambda\in\Lambda, use the same fold assignments and compute

λ^=arg⁡min⁡λ∈Λ1n∑iL(Yi,f^λ(−κ(i))(Xi)).\widehat\lambda=\arg\min_{\lambda\in\Lambda} \frac1n\sum_iL(Y_i,\widehat f_{\lambda}^{(-\kappa(i))}(X_i)).

Then refit Aλ^A_{\widehat\lambda} on all available development data. Candidate choices might be neighbour count, polynomial degree, or a regularization parameter.

The minimum CV score was used to select the winner, so it is generally optimistic as an assessment of the complete selection procedure. A final independent test set or nested CV separates selection from assessment.

Nested CV

For each outer fold, run all tuning using inner folds of the outer training set. Refit the selected procedure on that outer training set and score the untouched outer fold. Aggregate the outer held-out losses to assess the tuning procedure. After assessment, tune again using the full development data and fit the final rule.

Keep the Whole Procedure Inside the Split

StepCorrect location
Imputation and scalingLearn values on the current training fold, then apply to its held-out fold.
Feature screening or dimension reductionFit within the training fold, including supervised selection.
Hyperparameter searchUse validation/inner folds, leaving the assessment set untouched.
Grouped or repeated observationsKeep each dependent group together when evaluating generalization to new groups.
Future prediction from ordered dataRespect time order; train on the past and evaluate later observations.

These are practical extensions of the iid setup. A random split cannot create independence when the observations are dependent.