Leave-one-out cross-validation and error correction
Collin Erickson
2026-05-11
Source:vignettes/CrossValidationErrorCorrection.Rmd
CrossValidationErrorCorrection.RmdCross-validation is often used in machine learning to judge how well a model is fit. Instead of using the entire data set to fit the model, it will use one part of the data set to fit a model and then test the model on the remaining data. This gives an idea of how well the model will generalize to indpendent data.
Leave-one-out predictions using Gaussian processes
Leave-one-out prediction uses an entire model fit to all the data except a single point, and then makes a prediction at that point which can be compared to the actual value. It seems like this may be very expensive to do, but it is actually an inexpensive computation for a Gaussian process model, as long as the same parameters are used from the full model. This will bias the predictions to better results than if parameters were re-estimated.
Normally each prediction point requires solving a matrix equation. To predict the output, , at point , given input data in matrix and output , we use the equation For leave-one-out predictions, the matrix will have all the design points except for the one we are predicting at, and thus will be different for each one. However, we will have the correlation matrix for the full data set from estimating the parameters, and there is a shortcut to find the inverse of a matrix leaving out a single row and column.
There is significant speed-up by using a multiplication instead of a matrix solve. The code chunk below shows that solving with a square matrix with 200 rows is over 30 times slower than a matrix multiplication.
n <- 200
m1 <- matrix(runif(n*n),ncol=n)
b1 <- runif(n)
if (requireNamespace("microbenchmark", quietly = TRUE)) {
microbenchmark::microbenchmark(solve(m1, b1), m1 %*% b1)
}## Unit: microseconds
## expr min lq mean median uq max neval
## solve(m1, b1) 592.271 679.9970 725.5333 699.0850 747.5265 1508.915 100
## m1 %*% b1 17.830 21.6325 657.0802 44.3065 946.7010 10418.468 100
Getting the inverse of a submatrix
Suppose we have a matrix and know its inverse . Suppose that has block structure Now we want to find out how to find using instead of doing the full inverse. We can write in block structure
Now we use the fact that
This gives the equations
Solving the first equation gives that or
Leave-one-out covariance matrix inverse for Gaussian processes
For Gaussian processes we can consider the block matrix for the covariance (or correlation) matrix where a single row and its corresponding column is being removed. Let the first rows and columns be the covariance of the points in design matrix , while the last row and column are the covariance for the vector with and . Then we can have
Using the notation from the previous subsection we have and , and and will be submatrices of the full . is a column vector, so I’ll write it as a vector , and is a row vector, so I’ll write it as a vector . So we have So if we want to calculate we still have to invert , which is a large matrix. However this can be done efficiently since it is a rank one matrix using the Sherman-Morrison formula. Thus we have the shortcut for that is only multiplication
To speed this up it should be calculated as
Below demonstrates that we get a speedup of almost twenty by using this shortcut.
set.seed(0)
corr <- function(x,y) {exp(sum(-30*(x-y)^2))}
n <- 200
d <- 2
X <- matrix(runif(n*d),ncol=2)
R <- outer(1:n,1:n, Vectorize(function(i,j) {corr(X[i,], X[j,])}))
Rinv <- solve(R)
A <- R[-n,-n]
Ainv <- solve(A)
E <- Rinv[-n, -n]
b <- R[n,-n]
g <- Rinv[n,-n]
Ainv_shortcut <- E + E %*% b %*% g / (1-sum(g*b))
summary(c(Ainv - Ainv_shortcut))## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -825.28399 -0.00669 0.00000 0.00000 0.00675 880.47203
if (requireNamespace("microbenchmark", quietly = TRUE)) {
microbenchmark::microbenchmark(solve(A), E + E %*% b %*% g / (1-sum(g*b)))
}## Unit: microseconds
## expr min lq mean median
## solve(A) 1107.185 1388.448 5078.824 3699.019
## E + E %*% b %*% g/(1 - sum(g * b)) 115.456 133.419 1426.391 241.451
## uq max neval
## 5565.893 31006.72 100
## 1880.523 28052.19 100
In terms of the covariance matrices already calculated, this is the following, where is the matrix with the ith row and column removed, and is the ith row of the matrix with the value from the ith column removed.