Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

LOO & Generalized Cross-Validation

Leave-one-out evaluates one excluded observation at a time. In least squares, a leverage correction recovers those predictions from a single fit. Generalized cross-validation replaces individual leverages with their average.

Leave-One-Out Cross-Validation

With k=nk=n, each fold contains one observation:

Err⁡^LOO=1n∑i=1nL(Yi,f^(−i)(Xi)).\widehat{\operatorname{Err}}_{\mathrm{LOO}} =\frac1n\sum_{i=1}^nL(Y_i,\widehat f^{(-i)}(X_i)).

A direct implementation fits nn models on n−1n-1 observations each. For a fixed algorithm under iid sampling, its expected score is Err⁡n−1\operatorname{Err}_{n-1}. This is often close to Err⁡n\operatorname{Err}_n when the learning curve changes slowly; it is not exactly unbiased for the full-data model’s error.

The OLS Shortcut

Let XX be a full-rank n×pn\times p fixed design, with rows xi⊤x_i^\top. Set

H=X(X⊤X)−1X⊤,Y^=HY,ei=Yi−Y^i,hii=Hii.H=X(X^\top X)^{-1}X^\top,\qquad \widehat Y=HY,\qquad e_i=Y_i-\widehat Y_i,\qquad h_{ii}=H_{ii}.

If deleting observation ii leaves a full-rank design, then hii<1h_{ii}<1 and

Yi−f^(−i)(Xi)=ei1−hii.\boxed{Y_i-\widehat f^{(-i)}(X_i)=\frac{e_i}{1-h_{ii}}.}

Consequently, for squared loss,

Err⁡^LOO=1n∑i(ei1−hii)2.\boxed{\widehat{\operatorname{Err}}_{\mathrm{LOO}} =\frac1n\sum_i\left(\frac{e_i}{1-h_{ii}}\right)^2.}

The numerator uses ordinary full-fit residuals; the denominator corrects for using the observation in its own fit. hiih_{ii} is leverage, determined by the design. High leverage can amplify a small residual, but leverage alone is not a complete measure of influence.

Derivation via Sherman-Morrison

Write M=X⊤XM=X^\top X, hi=xi⊤M−1xih_i=x_i^\top M^{-1}x_i, and ei=Yi−xi⊤β^e_i=Y_i-x_i^\top\widehat\beta. Removing row ii gives

X−i⊤X−i=M−xixi⊤,X−i⊤Y−i=X⊤Y−xiYi.X_{-i}^\top X_{-i}=M-x_ix_i^\top,\qquad X_{-i}^\top Y_{-i}=X^\top Y-x_iY_i.

Sherman-Morrison yields

(M−xixi⊤)−1=M−1+M−1xixi⊤M−11−hi.(M-x_ix_i^\top)^{-1}=M^{-1}+ \frac{M^{-1}x_ix_i^\top M^{-1}}{1-h_i}.

Multiplying by X⊤Y−xiYiX^\top Y-x_iY_i and collecting terms gives

β^(−i)=β^−M−1xiei1−hi.\widehat\beta^{(-i)}=\widehat\beta-\frac{M^{-1}x_i e_i}{1-h_i}.

Thus

f^(−i)(Xi)=Y^i−hiei1−hi,\widehat f^{(-i)}(X_i)=\widehat Y_i-\frac{h_i e_i}{1-h_i},

and

Yi−f^(−i)(Xi)=ei+hiei1−hi=ei1−hi.Y_i-\widehat f^{(-i)}(X_i) =e_i+\frac{h_i e_i}{1-h_i}=\frac{e_i}{1-h_i}.

Squaring and averaging gives the shortcut.

Scope of the Formula

The identity is exact for the specified OLS refits, and for certain penalized least-squares smoothers with the appropriate fixed penalty convention. Merely writing Y^=HY\widehat Y=HY for an arbitrary full-data linear map is not sufficient: the rule after deletion must have the required relationship to the full fit.

It also does not automatically reproduce a pipeline that repeats feature selection, changes the basis, or retunes a penalty after each deletion. If hii=1h_{ii}=1, the displayed OLS shortcut is undefined and the deleted design loses rank.

Worked Leverage Example

Use x=(−1,0,1)x=(-1,0,1) and y=(1,0,1)y=(1,0,1), fitting an intercept and slope. The fitted line is constant at 2/32/3:

e=(1/3,−2/3,1/3),h=(5/6,1/3,5/6).e=(1/3,-2/3,1/3),\qquad h=(5/6,1/3,5/6).
ObservationTraining residual eie_i1−hii1-h_{ii}LOO residual
x=−1x=-11/31/31/61/62
x=0x=0−2/3-2/32/32/3-1
x=1x=11/31/31/61/62

The training MSE is 2/9≈0.22222/9\approx0.2222, but

Err⁡^LOO=(4+1+4)/3=3.\widehat{\operatorname{Err}}_{\mathrm{LOO}}=(4+1+4)/3=3.

Deleting an endpoint leaves a line fitted through the other two points. Predicting the excluded endpoint is extrapolation, which explains the large LOO errors despite modest training residuals.

Generalized Cross-Validation

LOO: individual correction
GCV: average correction
LOO⁡=1n∑i(ei1−hii)2.\operatorname{LOO}=\frac1n\sum_i\left(\frac{e_i}{1-h_{ii}}\right)^2.

Uses each observation’s leverage. Exact for the specified OLS deleted fits.

Replace each hiih_{ii} in the LOO correction by the average leverage

hˉ=1n∑ihii=tr⁡(H)n.\bar h=\frac1n\sum_i h_{ii}=\frac{\operatorname{tr}(H)}n.

The GCV score is

GCV⁡=1n∑i(ei1−tr⁡(H)/n)2=RSS⁡/n[1−df⁡/n]2.\boxed{\operatorname{GCV} =\frac1n\sum_i\left(\frac{e_i}{1-\operatorname{tr}(H)/n}\right)^2 =\frac{\operatorname{RSS}/n}{[1-\operatorname{df}/n]^2}.}

For OLS, df⁡=p\operatorname{df}=p. Compute the trace without forming the full n×nn\times n hat matrix. The same idea applies to regularized smoothers with noninteger effective degrees of freedom.

LOOGCV
Corrects each residual using its own leverage.Uses a common average-leverage correction.
Exact OLS deleted-fit score when the conditions hold.Approximation to LOO, not literal leave-one-out refitting.
Can be dominated by individual high-leverage cases.Removes individual leverage denominators, but may hide their effect.

For the three-point example, df⁡=2\operatorname{df}=2 and

GCV⁡=2/9(1−2/3)2=2,\operatorname{GCV}=\frac{2/9}{(1-2/3)^2}=2,

which differs from LOO’s value of 3. If all leverages are equal, the two formulas agree. When df approaches nn, the GCV denominator becomes small; at df =n=n the score is undefined.

Why GCV Is Invariant to Orthogonal Rotation

For a fixed linear smoother, transform the observation coordinates using Q⊤Q=QQ⊤=IQ^\top Q=QQ^\top=I:

Y∗=QY,H∗=QHQ⊤,e∗=Qe.Y^*=QY,\qquad H^*=QHQ^\top,\qquad e^*=Qe.

Then

∥e∗∥2=∥e∥2,tr⁡(H∗)=tr⁡(H).\|e^*\|^2=\|e\|^2,\qquad \operatorname{tr}(H^*)=\operatorname{tr}(H).

Both parts of GCV are unchanged. Individual diagonal entries of H∗H^* need not equal those of HH, so the corresponding coordinate-wise LOO expression can change. For OLS, this smoother transformation follows from X∗=QXX^*=QX.

This is an algebraic motivation for GCV. Rotating rows mixes observations and changes what it means to omit one observation; it does not make the original LOO assessment invalid.

A Subtle Point: Estimating the Error of This Fit

In correctly specified fixed-design Gaussian OLS, β^\widehat\beta is independent of e=(I−H)Ye=(I-H)Y. The LOO score with fixed leverages is a function of ee, while the fitted coefficients depend on the projected component HYHY.

This explains an important distinction: an error score need not track how fortunate or unfortunate this particular coefficient estimate was. Estimating average performance across training samples is different from closely predicting the conditional error of one realized fit.