Cross-Validation & Model Selection

The theory behind model evaluation and selection: hold-out estimation, K-fold cross-validation bias-variance tradeoff, leave-one-out CV, nested CV for hyperparameter tuning, and information criteria (AIC, BIC) as asymptotic approximations.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Generalization Problem
  5. Hold-Out Estimation
  6. K-Fold Cross-Validation
  7. Leave-One-Out Cross-Validation
  8. Bias-Variance of CV Estimators
  9. Nested Cross-Validation
  10. Information Criteria
  11. Common Pitfalls
  12. Summary
  13. Exercises

Learning Objectives

  1. Formalize the distinction between model assessment (estimating test error) and model selection (choosing among candidates).
  2. Derive the bias of hold-out estimation due to reduced training set size.
  3. Prove that LOO-CV is approximately unbiased for test error.
  4. Analyze the bias-variance tradeoff in choosing KK for KK-fold CV.
  5. Derive AIC as an asymptotic estimate of out-of-sample KL divergence.
  6. Explain why nested CV is needed when both selecting and evaluating models.

Notation

  • R^(f)\hat{R}(f) — estimated risk (test error estimate)
  • R(f)R(f) — true (population) risk
  • D(k)\mathcal{D}^{(-k)} — training data with fold kk removed
  • f^(k)\hat{f}^{(-k)} — model trained on D(k)\mathcal{D}^{(-k)}
  • nkn_k — size of fold kk
  • pp — number of parameters in the model
  • ^\hat{\ell} — maximized log-likelihood

Core Intuition

Training error underestimates test error because the model was fit to the same data. We need an honest estimate of how the model will perform on unseen data. Cross-validation simulates this by repeatedly training on subsets and testing on held-out portions, rotating through all data.

K-Fold Cross-Validation

Fold 1/5
Full Dataset12345Iteration 1: Train / Test SplitTESTtraintraintraintrainTrain 80.0%Test 20.0%Click dots to switch fold iteration
Train = 80.0%
Test = 20.0%
K folds
5
Fold #
1
TrainingTest (held out)
Explore: K-fold CV rotates which slice is held out for testing. Each fold gets tested once; final performance is averaged over all K runs — a reliable estimate with limited data.

The Generalization Problem

We want to estimate:

R(f^)=E(X,Y)P[(f^(X),Y)],(1)R(\hat{f}) = \mathbb{E}_{(X,Y) \sim P}[\ell(\hat{f}(X), Y)], \tag{1}

where f^\hat{f} is trained on D={(xi,yi)}i=1n\mathcal{D} = \{(x_i, y_i)\}_{i=1}^n.

The training error R^n(f^)=1ni(f^(xi),yi)\hat{R}_n(\hat{f}) = \frac{1}{n}\sum_i\ell(\hat{f}(x_i), y_i) is optimistically biased:

E[R^n(f^)]R(f^).(2)\mathbb{E}[\hat{R}_n(\hat{f})] \leq R(\hat{f}). \tag{2}

The gap (called "optimism") depends on model complexity.


Hold-Out Estimation

Split data into training (ntrainn_{\text{train}}) and validation (nvaln_{\text{val}}) sets:

R^holdout=1nvalival(f^(xi),yi).(3)\hat{R}_{\text{holdout}} = \frac{1}{n_{\text{val}}}\sum_{i \in \text{val}} \ell(\hat{f}(x_i), y_i). \tag{3}

Problem: This estimates the test error of a model trained on ntrain<nn_{\text{train}} < n samples, not the full dataset. The estimate is pessimistically biased (overestimates error) because less training data leads to worse models.


K-Fold Cross-Validation

Partition {1,,n}\{1, \ldots, n\} into KK disjoint folds I1,,IK\mathcal{I}_1, \ldots, \mathcal{I}_K:

R^CV(K)=1Kk=1K1IkiIk(f^(k)(xi),yi).(4)\hat{R}_{\text{CV}}^{(K)} = \frac{1}{K}\sum_{k=1}^K \frac{1}{|\mathcal{I}_k|}\sum_{i \in \mathcal{I}_k}\ell\bigl(\hat{f}^{(-k)}(x_i), y_i\bigr). \tag{4}

Each model f^(k)\hat{f}^{(-k)} is trained on n(11/K)n(1 - 1/K) samples.

Key properties:

  • K=nK = n: Leave-one-out (LOO), nearly unbiased but high variance.
  • K=5K = 5 or 1010: Standard choices, good bias-variance tradeoff.
  • K=2K = 2: High bias (training on only n/2n/2 samples).

Leave-One-Out Cross-Validation

R^LOO=1ni=1n(f^(i)(xi),yi).(5)\hat{R}_{\text{LOO}} = \frac{1}{n}\sum_{i=1}^n \ell\bigl(\hat{f}^{(-i)}(x_i), y_i\bigr). \tag{5}

Theorem. LOO-CV is approximately unbiased for the expected test error of a model trained on n1n-1 samples:

E[R^LOO]EPn[R(f^n1)].(6)\mathbb{E}[\hat{R}_{\text{LOO}}] \approx \mathbb{E}_{P^n}[R(\hat{f}_{n-1})]. \tag{6}

For linear models with squared loss, LOO-CV has a closed-form (no refitting needed):

R^LOO=1ni=1n(yiy^i1Hii)2,(7)\hat{R}_{\text{LOO}} = \frac{1}{n}\sum_{i=1}^n \left(\frac{y_i - \hat{y}_i}{1 - H_{ii}}\right)^2, \tag{7}

where H=X(XTX)1XT\mathbf{H} = \mathbf{X}(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T is the hat matrix.


Bias-Variance of CV Estimators

Bias: KK-fold CV trains on n(11/K)n(1-1/K) samples. Since test error decreases with training size, CV overestimates the error of the full-nn model. Bias is O(1/(nK))\mathcal{O}(1/(nK)) — smaller for larger KK.

Variance: LOO has high variance because the nn training sets overlap heavily (each pair shares n2n-2 samples), producing highly correlated estimates. KK-fold with smaller KK reduces this correlation.

The tradeoff:

  • Large KK (e.g., LOO): low bias, high variance.
  • Small KK (e.g., 5): higher bias, lower variance.
  • Empirically, K=5K = 5 or K=10K = 10 works well.

Nested Cross-Validation

When using CV both to select hyperparameters and assess final performance:

  1. Outer loop (KoutK_{\text{out}}-fold): splits data into assessment train/test.
  2. Inner loop (KinK_{\text{in}}-fold): on the outer training set, selects best hyperparameters.
  3. Train final model with selected hyperparameters on outer training set; evaluate on outer test.

This avoids the optimistic bias from evaluating on the same data used for selection.


Information Criteria

Akaike Information Criterion (AIC):

AIC=2^+2p,(8)\text{AIC} = -2\hat{\ell} + 2p, \tag{8}

where ^\hat{\ell} is the maximized log-likelihood and pp is the number of parameters.

Derivation. AIC estimates 2E[logp(Ynewθ^)]-2\mathbb{E}[\log p(Y_{\text{new}} \mid \hat{\boldsymbol\theta})]. Under regularity conditions and large nn, the optimism (difference between training and test log-likelihood) is approximately p/np/n, giving the 2p2p penalty.

Bayesian Information Criterion (BIC):

BIC=2^+plogn.(9)\text{BIC} = -2\hat{\ell} + p\log n. \tag{9}

BIC penalizes complexity more heavily for large nn. It approximates the log marginal likelihood logp(DM)\log p(\mathcal{D} \mid \mathcal{M}) up to a constant, enabling Bayesian model comparison.

When to use:

  • AIC: optimal prediction (minimizes KL divergence to true model).
  • BIC: model identification (consistent — selects true model as nn \to \infty).

Common Pitfalls

Pitfall 1. Data leakage: preprocessing (normalization, feature selection) on the full dataset before CV introduces bias. All transformations must occur inside each fold.

Pitfall 2. Using CV error for model selection and then reporting the same CV error as the test error estimate. This is optimistic; use nested CV.

Pitfall 3. Ignoring stratification for classification. Folds should preserve class proportions to reduce variance (stratified KK-fold).


Summary

  • Hold-out is simple but wastes data and overestimates error.
  • KK-fold CV balances bias (from reduced training size) and variance (from fold correlation).
  • LOO-CV is nearly unbiased but high-variance; has closed form for linear models.
  • Nested CV separates model selection from assessment.
  • AIC/BIC provide asymptotic alternatives that avoid refitting; AIC targets prediction, BIC targets model identification.

Exercises

Exercise 1. Derive the LOO formula for linear regression (equation 7) using the Sherman-Morrison-Woodbury identity.

Exercise 2. Show that for a model with pp parameters fit by MLE, the training log-likelihood overestimates the test log-likelihood by approximately p/np/n (motivating AIC).

Exercise 3. Prove that BIC is consistent: as nn \to \infty, BIC selects the true model (assuming it's among candidates).

Exercise 4. For K=2K = 2 fold CV, compute the bias in estimating test error for a linear model with dd features.

Exercise 5. Explain why repeated KK-fold CV (averaging over multiple random partitions) reduces variance without affecting bias.