Tutorials › Multivariate Statistics (Graduate) › Leave-One-Out Error for Linear Discriminant Analysis

Discriminant Inference and Risk · Tutorial 612 of 975

Leave-One-Out Error for Linear Discriminant Analysis

You will derive the leave-one-out LDA fit, apply its rank-one covariance update, and distinguish exact refitting shortcuts from approximations.

Advanced 10 min read

What You'll Learn

  • Define leave-one-out predictions and error for an LDA classifier
  • Update a class mean and within-class scatter after deleting one observation
  • Derive the pooled covariance and its inverse using a rank-one update
  • Reproduce refitted LDA scores, including the treatment of class priors
  • Recognize when shortcuts fail because of singularity, class loss, or data-dependent fitting

Why Leave-One-Out LDA Needs a Refit

A training observation is not an independent test case for the classifier fitted using that same observation: it helped determine the class means and pooled covariance used to classify it. Leave-one-out (LOO) assessment addresses this by deleting each observation in turn, fitting the classification rule to the remaining data, and predicting the deleted observation. For linear discriminant analysis (LDA), the refits have enough algebraic structure that some calculations can be updated exactly rather than repeated from scratch.

The key qualification is that an algebraic shortcut is exact only if it reproduces the entire specified training procedure. Updating the class means while leaving the covariance unchanged, for example, is not the same as refitting ordinary LDA. The pooled covariance, its degrees-of-freedom denominator, and any estimated class priors may also change.

Definition: Let \(x_i\in\mathbb{R}^p\) be observation \(i\), with known training label \(g_i\in\{1,\ldots,K\}\), for \(i=1,\ldots,n\). The LOO prediction for \(x_i\) is the class assigned by the specified classifier fitted to all observations except \(i\). The LOO error is $$ \widehat R_{\mathrm{LOO}}=\frac{1}{n}\sum_{i=1}^n I\{\widehat g_{-i}(x_i)\ne g_i\}, $$ where \(\widehat g_{-i}\) denotes the rule fitted without observation \(i\), and \(I\{\cdot\}\) is the indicator function.

The LDA Fit and Its Deletion Updates

Write \(n_h=\#\{i:g_i=h\}\), and suppose initially that every class has at least two observations. The class mean and within-class scatter for class \(h\) are $$ \bar x_h=\frac{1}{n_h}\sum_{i:g_i=h}x_i,\qquad W_h=\sum_{i:g_i=h}(x_i-\bar x_h)(x_i-\bar x_h)^\mathsf T. $$ The pooled within-class scatter is \(W=\sum_{h=1}^K W_h\). We use the usual pooled covariance estimate \(S=W/(n-K)\), assuming \(n>K\). For a positive-definite \(S\), the LDA score for class \(h\), with prior probability \(\pi_h>0\), is $$ \delta_h(x)=x^\mathsf T S^{-1}\bar x_h-\frac{1}{2}\bar x_h^\mathsf T S^{-1}\bar x_h+\log\pi_h. $$ The assigned class maximizes this score, with a fixed deterministic rule for ties.

Consider deleting \(x_i\) from its class \(h=g_i\), and put \(d=x_i-\bar x_h\). The other class means do not change. The deleted class mean does:

$$ \bar x_{h,-i} =\frac{n_h\bar x_h-x_i}{n_h-1} =\bar x_h-\frac{d}{n_h-1}. $$

The corresponding scatter update is a rank-one downdate. The recentering step matters: after removing \(x_i\), the remaining observations are not generally centered at the old mean.

Theorem: If \(n_h\ge2\), deleting \(x_i\) changes the within-class scatter and pooled scatter to $$ W_{h,-i}=W_h-\frac{n_h}{n_h-1}dd^\mathsf T,\qquad W_{-i}=W-\frac{n_h}{n_h-1}dd^\mathsf T. $$ If the refitted pooled covariance is defined using the usual within-class degrees of freedom, then $$ S_{-i}=\frac{W_{-i}}{n-K-1}, $$ provided \(n-K-1>0\).

To prove the scatter identity, let \(r_j=x_j-\bar x_h\) for observations \(j\) in class \(h\). The residuals sum to zero, so the residuals of the remaining \(n_h-1\) observations sum to \(-d\). Their scatter about the old mean is \(W_h-dd^\mathsf T\). Recentring at their new mean subtracts the outer product of their residual sum divided by their number:

$$ W_{h,-i} =(W_h-dd^\mathsf T)-\frac{(-d)(-d)^\mathsf T}{n_h-1} =W_h-\left(1+\frac{1}{n_h-1}\right)dd^\mathsf T =W_h-\frac{n_h}{n_h-1}dd^\mathsf T. $$

Only class \(h\) changes, giving the stated update for \(W\). After deletion there are \(n-1\) observations and still \(K\) represented classes, so the pooled covariance denominator is \((n-1)-K=n-K-1\), not \(n-K\). This proves the result. \(\square\)

Worked Example: Refit the Mean and Pooled Covariance

Take one-dimensional data with class A equal to \(\{0,2,4\}\) and class B equal to \(\{5,7,9\}\). Delete \(x_i=0\) from A. The original means are \(\bar x_A=2\) and \(\bar x_B=7\), and each class has scatter 8, so \(W=16\).

Here \(n_A=3\) and \(d=0-2=-2\). The refitted A mean is \(2-(-2)/2=3\). Its updated scatter is $$ W_{A,-i}=8-\frac{3}{2}(-2)^2=8-6=2. $$ Class B remains unchanged with scatter 8. Thus \(W_{-i}=2+8=10\). There are five training observations and two represented classes, so the refitted pooled variance is \(S_{-i}=10/(5-2)=10/3\).

With equal fixed priors, the score difference at \(x=0\) is $$ \delta_{A,-i}(0)-\delta_{B,-i}(0) =-\frac{3^2-7^2}{2(10/3)} =\frac{40}{20/3}=6>0. $$ The LOO prediction is A, which is the deleted observation’s actual class. This calculation refits both the mean and covariance; using the original covariance would not reproduce this score.

When the Inverse Shortcut Is Exact

For many observations, directly inverting each refitted covariance can be wasteful. The Sherman–Morrison identity gives the inverse of a rank-one update. In this setting it is an exact computational shortcut, not an approximation, when its invertibility conditions hold.

Theorem: Suppose \(W\) is positive definite and let \(\alpha=n_h/(n_h-1)\). If \(1-\alpha d^\mathsf T W^{-1}d\ne0\), then $$ W_{-i}^{-1} =W^{-1}+ \frac{\alpha W^{-1}dd^\mathsf T W^{-1}} {1-\alpha d^\mathsf T W^{-1}d}. $$ In particular, when both \(W\) and \(W_{-i}\) are positive definite, the denominator is positive. The inverse of the refitted pooled covariance is $$ S_{-i}^{-1}=(n-K-1)W_{-i}^{-1}. $$

For a direct verification, set \(u=W^{-1}d\), \(q=d^\mathsf T W^{-1}d\), and \(c=\alpha/(1-\alpha q)\). Multiplying the proposed inverse by \(W-\alpha dd^\mathsf T\) gives $$ (W-\alpha dd^\mathsf T)(W^{-1}+c uu^\mathsf T) =I+(c-\alpha-\alpha cq)du^\mathsf T. $$ Since \(c(1-\alpha q)=\alpha\), the coefficient \(c-\alpha-\alpha cq\) is zero. The product is therefore \(I\), proving the inverse formula. The pooled covariance relation follows by inverting \(S_{-i}=W_{-i}/(n-K-1)\). \(\square\)

To reproduce the LOO prediction, insert \(\bar x_{h,-i}\) for the deleted class mean, leave the other means unchanged, use \(S_{-i}^{-1}\) in every score, and use the priors prescribed by the fitting procedure. For fixed externally specified priors, the \(\pi_h\) values remain fixed. If priors are estimated as the training class proportions, they must be updated too: after deletion, they are \((n_h-1)/(n-1)\) for class \(h\) and \(n_g/(n-1)\) for each \(g\ne h\). In either case, the shortcut matches refitting only when every changed ingredient is accounted for.

Worked Example: Verify the Rank-One Inverse Update

Continue the previous example. The original pooled scatter is the scalar \(W=16\), so \(W^{-1}=1/16\). For deleting \(0\) from A, \(\alpha=3/2\), \(d=-2\), and $$ q=d^2W^{-1}=\frac{4}{16}=\frac14,\qquad 1-\alpha q=1-\frac{3}{8}=\frac58. $$ The inverse shortcut gives $$ W_{-i}^{-1} =\frac{1}{16}+ \frac{(3/2)(1/16)(4)(1/16)}{5/8} =\frac{1}{16}+\frac{3}{80} =\frac{1}{10}. $$ This agrees with direct refitting, since \(W_{-i}=10\). The deleted-sample covariance inverse is \(3W_{-i}^{-1}=3/10\), also equal to \(1/(10/3)\). Thus the rank-one formula reproduces the refitted covariance inverse exactly.

Worked Example: Calculate the Full LOO Error

Let class A be \(\{0,2,4\}\) and class B be \(\{6,8,10\}\), with equal fixed priors. Every deletion leaves two observations in each class. In one dimension with equal priors, the LDA boundary is the midpoint of the two refitted means: this follows because the common positive \(S_{-i}^{-1}\) multiplies both terms in the score difference and does not change its sign.

For completeness, the refitted scatter is 10 when an endpoint is deleted and 16 when a middle observation is deleted. The refitted covariance denominator is \(n-K-1=3\). The resulting means, covariance, boundary, and prediction are:

Deleted observationRefitted means (A, B)\(S_{-i}\)BoundaryPrediction
A: 03, 810/35.5A
A: 22, 816/35A
A: 41, 810/34.5A
B: 62, 910/35.5B
B: 82, 816/35B
B: 102, 710/34.5B

Each prediction is on the correct side of its boundary: the deleted A values \(0,2,4\) are all below their respective boundaries, and the deleted B values \(6,8,10\) are all above theirs. There are zero LOO errors, so \(\widehat R_{\mathrm{LOO}}=0/6=0\). The example also illustrates why the covariance must be refitted even when, in this particular equal-prior univariate calculation, the boundary happens not to depend on its value.

When Shortcuts Fail—and Why LOO Matters

The update formulas give computational savings, but do not remove the conditions required by the classifier. If \(W\) or \(W_{-i}\) is singular, the ordinary inverse-based LDA score is not defined. The Sherman–Morrison denominator then cannot be used as if it were a valid inverse. One must instead apply the specified alternative fitting rule, such as a stated regularization method, and ensure its deletion update matches that rule. Deleting an observation from a class of size one removes that class entirely; the formulas above do not apply, and the intended prediction problem needs an explicit convention for that case.

A further pitfall is to update means but keep the full-sample covariance, or to update \(W\) but retain the old denominator \(n-K\). Both produce a different classifier from the ordinary refit. Likewise, if priors are estimated from class counts, leaving them at their full-sample values changes the procedure. A valid shortcut must reproduce all these choices, as well as any data-dependent preprocessing or fitting decisions that are part of the classifier.

LOO error estimates the performance of the specified training procedure when trained on \(n-1\) observations, using each observation once as a held-out case. Its value is not the apparent training error, and it is not a guarantee of the risk for a model trained on all \(n\) observations. The held-out predictions also share much of their training data, so the individual error indicators are dependent. These are properties of the assessment, not reasons to alter the refitting calculation. The earlier material on classification risk distinguishes such empirical error estimates from the underlying risk being estimated.

Key takeaway: An exact LOO LDA shortcut updates the deleted class mean, the pooled within-class scatter, its degrees-of-freedom denominator, the covariance inverse, and any estimated priors. If any part of the specified fitting procedure is left at its full-sample value, the shortcut need not equal refitting.

Check Your Understanding

Use the definitions and updates above to answer the following.

  1. For a deleted observation with residual \(d\) in a class of size \(n_h\), what is the refitted class mean?
  2. Why does recentering the remaining residuals subtract an additional \(dd^\mathsf T/(n_h-1)\) from the scatter computed about the old mean?
  3. After one deletion with all \(K\) classes still represented, what is the usual pooled covariance denominator?
  4. Under what condition does the Sherman–Morrison update reproduce the inverse of the refitted pooled scatter?
  5. If LDA priors are estimated from class proportions, how does deleting an observation in class \(h\) change those proportions?
Clinical Biostats