Motivation
A feature can vary substantially without helping us distinguish labelled groups. Suppose two classes have different horizontal centres, while both spread widely in the vertical direction. PCA may retain that vertical variation because it accounts for much of the total variance. A direction designed to distinguish the classes should instead compare separation of their centres with variation inside the classes.
Fisher discriminant analysis makes this comparison explicit. It uses class labels to identify two kinds of variation, then asks for a linear score whose between-class variance is large relative to its within-class variance. The score is still a linear combination of the original features. What changes from PCA is the criterion that selects the combination.
This distinction also changes the geometry. PCA normalizes a direction using its Euclidean length. Fisher directions are naturally normalized by within-class variation. Whitening that variation converts the problem into an ordinary symmetric eigenvalue problem and explains both the maximum ratio and the appropriate orthogonality between several directions.
Definitions
Let be column vectors with known labels. The index sets partition the observations into nonempty classes. Write and . A data matrix has row equal to .
Definition
Class means and empirical class covariance
The class means, overall mean, and class covariance matrices are
Every covariance here uses the empirical denominator, rather than the degrees-of-freedom correction. Class size is therefore part of the weighting: a class containing twice as many observations contributes twice as much when each observation has equal weight. A singleton class has zero empirical covariance because its only observation equals its mean.
Definition
Within-class, between-class, and total variation
Define
The corresponding unnormalized scatter matrices are and .
For a direction , the centred score of observation is . Its class mean is . Consequently, measures the average variance of scores inside their classes, while measures the weighted variance of the class score means. Multiplying both scatter matrices by the same constant leaves their ratio unchanged.
For comparison with PCA, using in the overall sample covariance multiplies by and preserves its eigenvectors. The decomposition below uses consistently in all three matrices.
Theorem/Proposition
Theorem
Variation decomposes, and class means limit the dimension
The matrices and are positive semidefinite, and
Thus between-class variation contains at most independent directions, and can contain fewer if the class means occupy a smaller affine subspace.
Assume now that is positive definite. This means every nonzero feature direction has positive pooled within-class variance. The Fisher ratio is
It is invariant under replacing by any nonzero multiple of itself. We may therefore impose and maximize the numerator. This normalization fixes a scale for the score without changing its direction.
Theorem
Whitening gives the optimal Fisher directions
For positive-definite , define the symmetric matrix
Let be orthonormal eigenvectors of , with eigenvalues . Set
Then
The maximum of is . Successive directions maximize the ratio subject to being -orthogonal to the preceding directions. The number of positive discriminant eigenvalues is .
For selected directions, put . The reduced representation is , or for the whole data matrix. These are score coordinates. The matrix is generally not a Euclidean orthogonal projector, because the columns of are normalized in the geometry.
The normalization has a direct interpretation in score space:
Each selected score has unit pooled within-class variance. Its eigenvalue then measures the between-class variance relative to that common baseline. The cross-covariances between different selected scores vanish in both parts of the decomposition. This is why the normalization is more than a convenient way to shorten a formula: it gives comparable units for different discriminant scores. A larger eigenvalue means a larger standardized contrast, rather than simply a longer coefficient vector.
The sign of a direction remains arbitrary. Changing it reverses scores, class score means, and axis labels together, while preserving every variance and ratio. A repeated eigenvalue permits an orthogonal change of basis inside its whitened eigenspace as well. Thus equivalent calculations can produce different individual coefficient vectors. The appropriate comparison is the normalized discriminant geometry and its eigenvalues, including the selected subspace when the basis inside that subspace is not unique.
Proof sketch or proof idea
For each observation in class , split its displacement as
Expand the outer product and sum within the class. Both cross terms vanish because . Summing the remaining terms over classes gives . Each quadratic form is a weighted sum of squares, which proves positive semidefiniteness.
Every column of lies in the span of the centred class means. Those vectors satisfy the dependence
Since every is positive, their span has dimension at most . Their span can be smaller still: increasing the number of classes does not automatically create new independent directions between their means.
For the optimization, make the change of variable . Then
The ordinary Rayleigh quotient reaches its maximum at a leading eigenvector of . Transforming that vector back gives . Orthogonality of the becomes , proving the stated metric orthogonality and the successive constrained maximizations.
The same stationary equation follows by differentiating :
Although one can write , that matrix is generally not symmetric in the Euclidean inner product. The whitened matrix is symmetric. It supplies an orthonormal eigenbasis before transforming back, including a consistent choice inside a repeated-eigenvalue subspace.
When within-class variation can be nonsingular
The amount of data also constrains the within-class covariance. Form a residual matrix whose row for observation subtracts the mean of its class . Then
Within each class, its residual rows sum to zero. The class-indicator vectors therefore give independent linear constraints on the rows, because their supports are disjoint. This proves the rank bound. If there are fewer than residual degrees of freedom, positive definiteness is impossible. Having at least is necessary, but the residuals must also span the feature space for the covariance to be positive definite.
This explains a common high-dimensional situation without introducing an extra distributional model. Many measured features can coexist with relatively few independent within-class residual directions. The singular example below makes that geometry explicit and then specifies exactly which regularized objective is solved.
Two classes give one mean-contrast direction
For two classes, let and suppose . The mean formulas imply
Write and . Cauchy–Schwarz gives
Equality holds when is proportional to , hence
This is the difference of class means adjusted for within-class variation. A feature with a large mean difference can contribute less after that adjustment if it also varies greatly inside the classes. If the two means coincide, and every defined Fisher ratio is zero.
Worked examples
Worked example
1. Large vertical variance hides horizontal separation from PCA
Take the two classes
The means are and . The classes have equal size, so . Within either class, the centred horizontal values are and the vertical values are , with the cross products cancelling. Therefore
The leading PCA direction is vertical because . Both classes have the same vertical score values, so those scores do not express their different centres. The leading Fisher direction is horizontal: has , while the vertical ratio is zero. The horizontal class scores are and . Here the distinction is visible directly in the data, rather than merely in the eigenvalues. Replacing the overall covariance denominator by preserves PCA's vertical direction.
The following NumPy calculation constructs the three covariances from the observations and solves the symmetric whitened problem. Its sign choice makes the horizontal direction point to the right; reversing that sign would reverse every score and preserve the Fisher ratio.
import numpy as np
X = np.array([
[-3, -3], [-3, 3], [-1, -3], [-1, 3],
[1, -3], [1, 3], [3, -3], [3, 3]
], dtype=float)
labels = np.repeat([0, 1], 4)
n = len(X)
mu = X.mean(axis=0)
W = np.zeros((2, 2))
B = np.zeros((2, 2))
for label in np.unique(labels):
group = X[labels == label]
mean = group.mean(axis=0)
residual = group - mean
W += residual.T @ residual / n
difference = mean - mu
B += len(group) / n * np.outer(difference, difference)
eigenvalues_W, V = np.linalg.eigh(W)
inverse_root = (V / np.sqrt(eigenvalues_W)) @ V.T
C = inverse_root @ B @ inverse_root
eigenvalues, directions = np.linalg.eigh(C)
u = inverse_root @ directions[:, -1]
if u[0] < 0:
u = -u
T = (X - mu).T @ (X - mu) / n
assert np.allclose(T, W + B)
assert np.allclose(B @ u, eigenvalues[-1] * (W @ u))
print("W =", W.tolist())
print("B =", B.tolist())
print("direction =", u.tolist())
print("ratio =", float((u @ B @ u) / (u @ W @ u)))
print("W_norm =", float(u @ W @ u))
The output is
W = [[1.0, 0.0], [0.0, 9.0]]
B = [[4.0, 0.0], [0.0, 0.0]]
direction = [1.0, 0.0]
ratio = 4.0
W_norm = 1.0
Worked example
2. Unequal class sizes determine the weights
Now take
The class means are and , but the overall mean is , since and . The class covariances are and . Thus
For example, the horizontal between-class entry is . The maximum Fisher ratio is , reached horizontally. Giving the two class means equal weight instead would produce mean and between-class entry . That is a class-balanced weighting of a different empirical problem. Equal weighting of all six observations requires the factors and throughout the calculation.
Worked example
3. A singular within-class covariance leads to a new regularized objective
Take and . Then
There is no horizontal variation within either class, although their horizontal means differ. The ratio at is undefined because its denominator is zero. For , , the ratio is , so it is unbounded as approaches zero.
Choose and define the regularized criterion
For , the denominator matrix is , which is positive definite. The maximum is , attained horizontally; a normalized direction is . This solves the displayed regularized problem. The added term penalizes the Euclidean size of the direction in the denominator, so it changes the criterion rather than merely implementing an inverse for the original one. With , the maximum becomes .
Interpreting a reduced representation
The informative dimension is controlled by the class centres, not just by the ambient number of features. Two classes supply a single mean contrast even if observations have hundreds of features. With several classes, the first direction captures the strongest standardized contrast; later directions capture additional contrasts subject to the within-class orthogonality constraint. A zero eigenvalue adds no between-class variance.
The labels enter when estimating the means and within-class residuals. Once the directions are fixed, a new observation is mapped by the same linear score formula. The fitted mean and the chosen direction scales belong to that map. Recentring a new observation separately would instead erase its coordinates. Subtracting the single fitted global mean shifts all class scores consistently and preserves all differences between them.
PCA and Fisher analysis therefore answer different questions about the same table. The first preserves overall variation with Euclidean orthogonality. The second preserves labelled mean contrasts relative to within-class variation. The covariance decomposition shows exactly where their criteria overlap and where they differ.
Common mistakes
Common mistake
Using ordinary Euclidean orthogonality for every Fisher direction
The constraint is , rather than generally . Whitening gives Euclidean-orthogonal vectors ; transforming them back changes the metric. The symmetric eigenproblem belongs to , not generally to .
Common mistake
Mixing covariance denominators or class weights
The identity follows from one consistent empirical weighting. Replacing only the class covariance denominators by , or averaging unequal classes without their factors, changes the matrices. Write each weighted sum before simplifying it.
Summary
Within-class variation describes dispersion around each labelled class mean, and between-class variation describes weighted differences between those means. Their sum is total variation. A positive-definite within-class covariance turns the Fisher ratio into a symmetric Rayleigh quotient after whitening. The resulting directions are orthogonal in the within-class metric, with at most positive discriminant eigenvalues. For two distinct class means, the direction is their difference adjusted by the inverse within-class covariance. A ridge term defines an explicit modified criterion when that covariance is singular.
Exercises
Checkpoint
1. Let , , , , and . Find , , the maximum Fisher ratio, and a normalized maximizing direction.
Use the empirical weights and impose .
Checkpoint
2. Take , , and . Compute and . How can both vectors be normalized in the within-class metric?
Calculate both squared within-class lengths as well.
Checkpoint
3. There are classes in . What is the largest possible number of positive discriminant eigenvalues when is positive definite? What if all four class means lie on one affine line?
Count the span of the centred class means.
Checkpoint
4. For the singular covariance in Example 3, use . Find the maximum of and a normalized maximizing direction. What term distinguishes this objective from the original Fisher ratio?
Use the matrix .
Solutions
Solution · 1. Weight the means before normalizing the direction
The overall mean is and . The horizontal ratio is and the vertical numerator is zero, so the maximum is . One maximizing direction with unit within-class length is .
Solution · 2. Orthogonality changes with the metric
The Euclidean inner product is . Since , the within-class inner product is zero. Both squared within-class lengths are : . Thus and are within-class orthonormal, while their Euclidean inner product remains .
Solution · 3. The mean geometry can sharpen the class-count bound
The general bound is . On one affine line, all centred class means are multiples of a single vector, so and at most one discriminant eigenvalue is positive. It is one if the class means are not all identical, and zero if they are.
Solution · 4. The ridge changes the denominator and the optimal value
For , . Equality holds for and ; one normalized direction is . The denominator now includes , an added penalty that makes this a different objective from the unregularized ratio.
Continue with SVD and principal components, classical and nonmetric MDS, centred kernels and kernel PCA, or the course reading list.