Cross-Validation

Today

  • Training error
  • Estimating test error
    • Validation set approach
    • Leave-one-out cross-validation (LOOCV)
    • \(k\)-fold cross-validation
  • Classification

Polynomial fits

Training MSE

Validation set approach

Validation set algorithm

  1. Split the \(n\) observations at random into a training set and a validation set
  2. Fit the model to the training set
  3. Predict the validation set and average the squared errors

\[\text{MSE}_{\mathcal{V}} = \frac{1}{n_{\mathcal{V}}}\sum_{i \in \mathcal{V}}\left[y_i - \widehat f(x_i)\right]^2\]

  • \(\mathcal{V}\) — validation set, \(n_{\mathcal{V}}\) observations
  • \(\widehat f\) — fit to the training set only

Split variability

Effect of training set size

Leave-one-out cross-validation

Held-out car

LOOCV estimate

\[\text{CV}_{(n)} = \frac{1}{n}\sum_{i=1}^n\left[y_i - \widehat f^{(-i)}(x_i)\right]^2\]

  • \(\widehat f^{(-i)}\) — fit to the \(n - 1\) observations other than observation \(i\)
  • \(\widehat f^{(-i)}(x_i)\) — its prediction for the held-out observation

\(n\) fits per model

Degree selection

k-fold cross-validation

Folds

k-fold estimate

\[\text{MSE}_\ell = \frac{1}{n_\ell}\sum_{i \in \mathcal{F}_\ell}\left[y_i - \widehat f^{(-\ell)}(x_i)\right]^2, \qquad \text{CV}_{(k)} = \sum_{\ell=1}^k \frac{n_\ell}{n}\,\text{MSE}_\ell\]

  • \(k\) — number of folds (unrelated to \(K\), the number of neighbors)
  • \(\mathcal{F}_\ell\) — set of observations in fold \(\ell\), \(n_\ell\) of them
  • \(\widehat f^{(-\ell)}\) — fit to the observations outside fold \(\ell\)

LOOCV is the special case \(k = n\)

Partition variability

Fold count

Computation and bias

  • Computation: \(k\) fits per model, LOOCV \(n\) fits
  • Bias: each fit uses \(n(k - 1)/k\) observations
    • 196 cars at \(k = 2\)
    • about 314 at \(k = 5\)
    • about 353 at \(k = 10\)
    • 391 at LOOCV
  • Smaller \(k\): more upward bias; LOOCV nearly unbiased

Variance

\[Var\left[\frac{1}{k}\sum_{\ell=1}^k\text{MSE}_\ell\right] = \frac{1}{k^2}\sum_{\ell=1}^k\sum_{\ell'=1}^k Cov\left[\text{MSE}_\ell, \text{MSE}_{\ell'}\right] = \tau^2\left(\rho + \frac{1-\rho}{k}\right)\]

  • \(\tau^2\) — variance of each fold MSE
  • \(\rho\) — correlation of each pair of fold MSEs

Averaging more folds shrinks \((1 - \rho)/k\) but not \(\rho\tau^2\)

Usual choice: \(k = 5\) or \(10\)

Classification

Credit card default data

Cross-validation error rate

\[\text{Err}_\ell = \frac{1}{n_\ell}\sum_{i \in \mathcal{F}_\ell}\mathrm{I}\left(y_i \ne \widehat y_i^{(-\ell)}\right), \qquad \text{CV}_{(k)} = \sum_{\ell=1}^k\frac{n_\ell}{n}\,\text{Err}_\ell\]

  • \(\widehat y_i^{(-\ell)}\) — class predicted by the classifier fit outside fold \(\ell\)
  • \(\text{Err}_\ell\) — test error rate on fold \(\ell\)

Neighbor count

Conclusion

  • Training error
    • Always falls with flexibility, so it cannot choose a model
  • Estimating test error
    • Validation set approach
      • Varies by split and overestimates the test error
    • Leave-one-out cross-validation (LOOCV)
      • Nearly unbiased, but needs \(n\) fits
    • \(k\)-fold cross-validation
      • Far less variable than one split; \(k = 5\) or \(10\) is the usual choice
  • Classification
    • Cross-validation chooses the number of neighbors \(K\)