Cross-Validation
Study guides: Exam SRM, Exam MAS-I, Exam PCPA
Cross-Validation is a method of estimating the test error of a model by fitting the model to part of the data and computing its error on the observations that were held out of the fit. It is used to choose between models, between values of a tuning parameter, or between procedures such as credibility procedures. The one with the smallest error on the held-out observations, such as the smallest sum of squared errors, is chosen.
Validation set approach
The validation set approach splits the observations at random into a training set and a validation set, often of about equal size. The model is fitted to the training set, and the error on the validation set estimates the test error. A different random split gives a different estimate. The model is fitted to only part of the data, so the estimate tends to overstate the test error of the model fitted to all the data.
Leave-one-out cross-validation
Leave-one-out cross-validation (LOOCV) fits the model n times, each time to all the observations but one, and computes the error on the one observation left out. The LOOCV estimate of the test mean squared error is
\text{CV}_{(n)} = \frac{1}{n}\sum_{i=1}^{n}\left(y_i - \hat{y}_{(i)}\right)^{2}
\hat{y}_{(i)}is the prediction for observationifrom the model fitted without observationi.
The prediction for each observation must come from the fit that left it out. LOOCV involves no random split, so it gives the same estimate every time it is run on the same data. For a least squares linear regression, the n fits are not needed, because the LOOCV estimate can be computed from the one fit to all the data:
\text{CV}_{(n)} = \frac{1}{n}\sum_{i=1}^{n}\left(\frac{y_i - \hat{y}_i}{1 - h_i}\right)^{2}
\hat{y}_iis the fitted value of observationifrom the fit to all the data.h_iis the leverage of observationi, theith diagonal element of the hat matrix.
A least squares line fitted to
x = 1, 2, 3, 4andy = 3, 7, 6, 10is\hat = 1.5 + 2x, and the leverages are 0.7, 0.3, 0.3 and 0.7. What are the training MSE and the LOOCV estimate of the test MSE?
- The fitted values are 3.5, 5.5, 7.5 and 9.5, so the residuals are
-0.5,1.5,-1.5and0.5.- The training MSE is
(0.25 + 2.25 + 2.25 + 0.25)/4 = 1.25.- Dividing each residual by
1 - h_igives-1.667,2.143,-2.143and1.667.- The LOOCV estimate is
\left[2(1.667)^2 + 2(2.143)^2\right]/4 = 3.68.
k-fold cross-validation
k-fold cross-validation divides the observations at random into k folds of about equal size. The model is fitted k times, each time holding out one fold as the validation set and fitting to the other k - 1 folds. Each observation is in exactly one fold, so it is in the validation set once and in the training set k - 1 times. The k-fold estimate of the test mean squared error is
\text{CV}_{(k)} = \frac{1}{k}\sum_{j=1}^{k} \text{MSE}_j
\text{MSE}_jis the mean squared error on foldjof the model fitted to the other folds.
Each training set holds the fraction (k - 1)/k of the data. LOOCV is the special case k = n. The usual choices are k = 5 and k = 10, which need only 5 or 10 fits instead of n.
Five candidate models have 10-fold cross-validated MSEs of 530, 430, 395, 415 and 470, and training MSEs of 520, 410, 350, 320 and 305. Which model is chosen?
- The training MSE falls with every added term, so it cannot choose between the models.
- The cross-validated MSE is smallest for model 3, at 395.
- Model 3 is chosen. Models 4 and 5 have lower training MSEs but higher cross-validated MSEs, because they overfit.
Bias and variance of the estimates
| LOOCV | k-fold, k = 5 or 10 |
|
|---|---|---|
| Model fits | n |
k |
| Bias | Lowest, because each fit uses n - 1 observations |
Larger, because each fit uses (k - 1)/k of the data |
| Variance | Higher, because the n training sets are nearly identical |
Lower, because the training sets overlap less |
A model fitted to fewer observations tends to fit worse, so a smaller k gives an estimate with more upward bias. The n fitted models of LOOCV are nearly the same, so their errors are highly positively correlated, and the mean of highly correlated quantities has a high variance. k-fold cross-validation with k = 5 or 10 balances the two.
Cross-validating a whole procedure
Every step that uses the response must be repeated inside each fold, including the selection of predictors, the choice of a penalty and the encoding of a variable by its response. If a step uses the held-out observations, their response has leaked into the fit, and the cross-validated error is too low. This is called leakage. For example, a target encoding of a categorical variable must be computed for each fold from the other folds only, which is called out-of-fold encoding. Cross-validation can also check the stability of a model, by comparing the coefficient estimates fitted to the different sets of k - 1 folds.
Selecting predictors before cross-validating An analyst who picks the 10 predictors most correlated with the response from all the data, and then cross-validates a model on those 10, has already used the held-out folds. The selection must be repeated within each fold, from that fold's training observations only.
Practice questions
- CAS Exam 8 Fall 2016, Question 6
- CAS Exam 8 Fall 2017, Question 4
- Exam 8 Practice Question 6
- Exam ATPA Practice Question 30
- CAS Exam MAS-I Fall 2018, Question 33
- CAS Exam MAS-I Spring 2018, Question 30
- CAS Exam MAS-I Fall 2019, Question 29
- CAS Exam MAS-I Fall 2019, Question 34
- CAS Exam MAS-I Fall 2019, Question 36
- CAS Exam MAS-I Spring 2019, Question 34
- Exam MAS-I Practice Question 141
- CAS Exam MAS-I Sample Question 3
- CAS Exam MAS-II Fall 2019, Question 24
- Exam PA Practice Question 27
- Exam PA Practice Question 31
- Exam PA Practice Question 36
- SOA Exam SRM Sample Question 73
- Exam SRM Practice Question 1