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.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Generalization Problem
- Hold-Out Estimation
- K-Fold Cross-Validation
- Leave-One-Out Cross-Validation
- Bias-Variance of CV Estimators
- Nested Cross-Validation
- Information Criteria
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Formalize the distinction between model assessment (estimating test error) and model selection (choosing among candidates).
- Derive the bias of hold-out estimation due to reduced training set size.
- Prove that LOO-CV is approximately unbiased for test error.
- Analyze the bias-variance tradeoff in choosing for -fold CV.
- Derive AIC as an asymptotic estimate of out-of-sample KL divergence.
- Explain why nested CV is needed when both selecting and evaluating models.
Notation
- — estimated risk (test error estimate)
- — true (population) risk
- — training data with fold removed
- — model trained on
- — size of fold
- — number of parameters in the model
- — 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/5The Generalization Problem
We want to estimate:
where is trained on .
The training error is optimistically biased:
The gap (called "optimism") depends on model complexity.
Hold-Out Estimation
Split data into training () and validation () sets:
Problem: This estimates the test error of a model trained on 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 into disjoint folds :
Each model is trained on samples.
Key properties:
- : Leave-one-out (LOO), nearly unbiased but high variance.
- or : Standard choices, good bias-variance tradeoff.
- : High bias (training on only samples).
Leave-One-Out Cross-Validation
Theorem. LOO-CV is approximately unbiased for the expected test error of a model trained on samples:
For linear models with squared loss, LOO-CV has a closed-form (no refitting needed):
where is the hat matrix.
Bias-Variance of CV Estimators
Bias: -fold CV trains on samples. Since test error decreases with training size, CV overestimates the error of the full- model. Bias is — smaller for larger .
Variance: LOO has high variance because the training sets overlap heavily (each pair shares samples), producing highly correlated estimates. -fold with smaller reduces this correlation.
The tradeoff:
- Large (e.g., LOO): low bias, high variance.
- Small (e.g., 5): higher bias, lower variance.
- Empirically, or works well.
Nested Cross-Validation
When using CV both to select hyperparameters and assess final performance:
- Outer loop (-fold): splits data into assessment train/test.
- Inner loop (-fold): on the outer training set, selects best hyperparameters.
- 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):
where is the maximized log-likelihood and is the number of parameters.
Derivation. AIC estimates . Under regularity conditions and large , the optimism (difference between training and test log-likelihood) is approximately , giving the penalty.
Bayesian Information Criterion (BIC):
BIC penalizes complexity more heavily for large . It approximates the log marginal likelihood 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 ).
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 -fold).
Summary
- Hold-out is simple but wastes data and overestimates error.
- -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 parameters fit by MLE, the training log-likelihood overestimates the test log-likelihood by approximately (motivating AIC).
Exercise 3. Prove that BIC is consistent: as , BIC selects the true model (assuming it's among candidates).
Exercise 4. For fold CV, compute the bias in estimating test error for a linear model with features.
Exercise 5. Explain why repeated -fold CV (averaging over multiple random partitions) reduces variance without affecting bias.