Behavioral Science Dictionary

Cross-validation

Methods & Evidence

Testing a model on data it wasn't trained on to see if it really generalizes.

What it means

Cross-validation is a resampling procedure for estimating how well a model will predict new data, by repeatedly partitioning the dataset into parts used for fitting and parts held out for testing. In k-fold cross-validation the data are split into k groups, each serving once as the test set while the rest train the model, and the results are averaged. It guards against overfitting by measuring out-of-sample performance rather than the misleadingly optimistic in-sample fit. Beyond gauging accuracy, it is widely used to tune model complexity and to choose among competing models in a principled, data-driven way.

Choosing k, and the bias-variance trade

The number of folds is not a free lunch. Leave-one-out trains each model on n-1 points, so every fit closely resembles the model built on all the data and the error estimate carries little bias, but the n fitted models are nearly identical and their errors highly correlated, which makes the average unstable from one dataset to the next. Fewer folds, five or ten, train on smaller subsets and bias the estimate slightly pessimistic, but the held-out sets overlap less and the average is steadier. Ten-fold is the usual compromise, with five-fold preferred when refitting is expensive. Repeating the whole split-and-average procedure under different random partitions further tames the sampling noise. No single k is optimal everywhere; the right choice depends on sample size and the cost of a fit.

What it actually estimates

A subtle point, formalized only recently, is that cross-validation does not estimate the error of the particular model you fit to your data. Bates, Hastie and Tibshirani proved that for linear models the k-fold figure tracks the average error of models fit to other datasets drawn from the same population, not the one in hand, and the two can differ in ways that matter when you want to know how your deployed model will behave. Worse, because every observation serves as both trainer and tester across folds, the fold errors are correlated and the naive standard error understates the true variability. Confidence intervals built the obvious way can have coverage far below their nominal level, so a gap between two models that looks decisive on one run may not survive a fresh sample.

The double-dipping trap

The most common way practitioners fool themselves is to use one cross-validation both to tune a model and to report its accuracy. Once the folds have guided the choice of features, hyperparameters or the winning algorithm, the resulting score is no longer an honest out-of-sample estimate, because the selection has quietly peeked at the test folds. Varma and Simon showed the size of the problem in genomics: tuning a classifier by cross-validation and then reporting that same CV error gave markedly optimistic results even when the features carried no real signal at all. Cawley and Talbot documented the same selection bias more broadly. The remedy is nested cross-validation, where an inner loop tunes the model and an outer loop, which never sees the tuning, measures how well it generalizes.

Where the i.i.d. assumption fails

The guarantee rests on observations being independent draws from one distribution, then shuffled at random into folds. When they are not independent, random splits leak information from test to train and the estimate turns optimistic. Time series are the clearest case: shuffling lets the model train on the future and predict the past, so analysts use blocked or forward-chaining schemes, sometimes with a gap between train and test, to respect the arrow of time. Spatially or hierarchically clustered data, such as repeated measurements on one patient or images from a single hospital, need grouped folds that keep each cluster wholly in or wholly out. Otherwise the model is rewarded for memorizing the group rather than generalizing beyond it, and the reported accuracy flatters what real new cases will show.

Examples

Splitting data into ten folds, training on nine and testing on the tenth in turn, then averaging the prediction errors.

A bank's loan-default model predicts the customers it was built on almost perfectly but stumbles badly on held-out ones — the folds expose the overfitting before real applicants ever do.

Deciding how many variables belong in a churn model: each candidate version is scored on data it never saw, and the one with the lowest averaged out-of-sample error wins.

A radiology model scores well when scans are shuffled into folds, but keeping each patient's images wholly inside one fold makes accuracy fall: the shuffle had let it recognize patients, not detect disease.

To pick a demand-forecasting model, a retailer trains on the first twelve months, tests on the thirteenth, rolls the window forward, and averages the errors, grading each candidate only on months in its future.

First described in Stone (1974); Geisser (1975).

Key references

  1. Bates, S., Hastie, T., & Tibshirani, R. (2024). Cross-validation: what does it estimate and how well does it do it? Journal of the American Statistical Association, 119(546), 1434-1445. doi.org/10.1080/01621459.2023.2197686
  2. Arlot, S., & Celisse, A. (2010). A survey of cross-validation procedures for model selection. Statistics Surveys, 4, 40-79. doi.org/10.1214/09-SS054
  3. Cawley, G. C., & Talbot, N. L. C. (2010). On over-fitting in model selection and subsequent selection bias in performance evaluation. Journal of Machine Learning Research, 11, 2079-2107. www.jmlr.org/papers/v11/cawley10a.html
  4. Varma, S., & Simon, R. (2006). Bias in error estimation when using cross-validation for model selection. BMC Bioinformatics, 7, 91. doi.org/10.1186/1471-2105-7-91
  5. Geisser, S. (1975). The predictive sample reuse method with applications. Journal of the American Statistical Association, 70(350), 320-328. doi.org/10.1080/01621459.1975.10479865
  6. Stone, M. (1974). Cross-validatory choice and assessment of statistical predictions. Journal of the Royal Statistical Society: Series B (Methodological), 36(2), 111-147. doi.org/10.1111/j.2517-6161.1974.tb00994.x

← All 1001 terms