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.
-Fold Cross-Validation¶
Partition the indices into disjoint nonempty folds , with and . Let identify observation ’s fold.
For each fold , remove and fit .
Predict the held-out observations in .
Average all held-out losses.
If all folds are equally sized, this is . If they differ in size, weight fold means by their observation counts to obtain average loss per observation.
| Round | Fold 1 | Fold 2 | Fold 3 |
|---|---|---|---|
| 1 | Hold out | Train | Train |
| 2 | Train | Hold out | Train |
| 3 | Train | Train | Hold out |
Every observation is scored once and used for training in 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 observations, independent of its held-out fold. Therefore
This is average generalization error for a procedure trained on observations. It is not an exact measurement of the conditional error of the final fit on all observations. For unequal folds, the expectation is a weighted average of risks at sizes .
The folds’ losses are dependent because the training subsets overlap. Their sample standard deviation divided by is not generally a justified standard error for the CV score.
| Choice | Training size per fit | Main tradeoff |
|---|---|---|
| Smaller | Further below | More training-size mismatch; larger held-out folds. |
| Larger | Closer to | Less training-size mismatch; more fits and strongly overlapping training sets. |
| Leave-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
| 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 | |
| 1 | 2 | 2 | 4 | 4 | 5 | |
| Fold | 1 | 2 | 1 | 2 | 1 | 2 |
For a simple intercept-only algorithm, predict the mean of the training responses.
Selecting a Tuning Parameter¶
Use validation folds to compare model choices. Refit the winner on all development data.
Use untouched test data or outer CV to evaluate the complete selection procedure. The winning validation score is not an independent assessment.
For candidates , use the same fold assignments and compute
Then refit 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¶
| Step | Correct location |
|---|---|
| Imputation and scaling | Learn values on the current training fold, then apply to its held-out fold. |
| Feature screening or dimension reduction | Fit within the training fold, including supervised selection. |
| Hyperparameter search | Use validation/inner folds, leaving the assessment set untouched. |
| Grouped or repeated observations | Keep each dependent group together when evaluating generalization to new groups. |
| Future prediction from ordered data | Respect 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.