KBKnowledge Base
Linear Algebra for ML · 1.29

Generalized Eigenvalue Problems

What LDA and CCA actually solve — Av = λBv, not just Av = λv.

On this page
In plain English — beginner to advanced

Beginner: section 1.7's eigenvalue problem, Av = λv, asks "which directions does A only stretch, never rotate?" The generalized eigenvalue problem, Av = λBv, asks a related but different question: "which directions does A stretch by exactly λ times as much as B stretches them?" — comparing two matrices' behavior against each other, instead of describing just one matrix alone.

Intermediate: this shows up constantly whenever a method wants to maximize one notion of "spread" relative to another. Linear Discriminant Analysis (LDA) wants to find the projection direction that maximizes between-class separation relative to within-class spread — exactly a generalized eigenvalue problem, with A the between-class scatter matrix and B the within-class scatter matrix.

Advanced: Canonical Correlation Analysis (CCA), which finds the most correlated linear combinations of two different sets of variables (used to relate two different views or modalities of the same data), is also a generalized eigenvalue problem underneath. Both LDA and CCA can technically be reduced to an ordinary eigenvalue problem by multiplying through by B⁻¹, but doing that explicitly is numerically worse than using a solver built specifically for the generalized case.

Formula
Av=λBvAv = \lambda Bv

When B is the identity matrix, this is exactly the ordinary eigenvalue problem from section 1.7 — the generalized version is a strict superset, not a different topic.

Derivation: reducing Av = λBv to an ordinary eigenvalue problem

When B is symmetric positive definite (the usual ML case), Cholesky-factor it (section 1.15) as B = LLᵀ. Substitute into the generalized problem and insert L⁻ᵀLᵀ = I in a useful spot:

Av=λLLTvL1Av=λLTvAv = \lambda LL^Tv \quad\Longrightarrow\quad L^{-1}Av = \lambda L^Tv

Now define the change of variables y = Lᵀv, so v = L⁻ᵀy, and substitute on the left:

L1ALTy=λyL^{-1}A L^{-T} y = \lambda y

This is now an ordinary eigenvalue problem (section 1.7) for the matrix C = L⁻¹AL⁻ᵀ, with the exact same eigenvalues λ as the original generalized problem — and if A is symmetric, C is symmetric too, so a standard symmetric eigensolver applies directly. The original eigenvectors are recovered via v = L⁻ᵀy. This is precisely the reduction production solvers use internally, which is why calling a dedicated generalized eigensolver is both correct and no more expensive than doing this transformation by hand.

Where this is used: this exact Cholesky-based reduction is what LDA and CCA solvers do internally, and it's also the standard way vibration analysis software solves Kv = λMv for a structure's natural frequencies and mode shapes.

LDA: find the projection that best separates two classes

Rotate the projection line — the Fisher score (between-class spread over within-class spread) is exactly what the generalized eigenvalue problem solves for directly.

Practical example — solving a generalized eigenvalue problem for LDA

scipy.linalg.eigh(A, B) solves the generalized problem directly — this is the real, standard way LDA is implemented, not by explicitly forming B⁻¹A.

python
import numpy as np
from scipy.linalg import eigh

classA = np.random.randn(50, 2) @ [[3, 1], [1, 1]] + [-3, 0]
classB = np.random.randn(50, 2) @ [[3, 1], [1, 1]] + [3, 0]

mean_diff = (classA.mean(axis=0) - classB.mean(axis=0)).reshape(-1, 1)
between_scatter = mean_diff @ mean_diff.T                       # "A" in Av = lambda Bv
within_scatter = np.cov(classA.T) + np.cov(classB.T)              # "B" in Av = lambda Bv

eigvals, eigvecs = eigh(between_scatter, within_scatter)          # generalized eigensolver
best_direction = eigvecs[:, -1]                                    # largest eigenvalue -> best separation
print(best_direction)
Real-world examples
  • LDA is used both as a classifier and as a supervised dimensionality reduction technique — unlike PCA (section 1.7/1.10), it uses class labels to choose directions that separate categories, not just directions of maximum variance.
  • CCA underlies multi-view learning — relating text and image embeddings of the same concept, or brain-imaging signals to stimulus features, by finding maximally correlated projections of each.
  • Vibration/structural analysis (the mechanical engineering example from section 1.7) is actually a generalized eigenvalue problem in its full form, Kv = λMv, relating stiffness (K) and mass (M) matrices.
Common mistakes
  • Explicitly computing B⁻¹A and then solving an ordinary eigenvalue problem — this works in theory but is markedly less numerically stable than a dedicated generalized eigensolver, especially when B is close to singular.
  • Forgetting that a generalized eigenvalue problem needs B to be invertible (or at least positive definite, section 1.15, for the well-behaved real-eigenvalue case) — an ill-conditioned within-class scatter matrix is a common practical failure mode of LDA on small or collinear datasets.
Going deeper

When B is symmetric positive definite (the common case in ML — scatter and covariance matrices are always PSD, section 1.15), the generalized eigenvalue problem has an elegant reduction: Cholesky-factor B = LLᵀ, then solve the ordinary eigenvalue problem for L⁻¹A(L⁻¹)ᵀ — which is exactly what production-grade generalized eigensolvers do internally, tying this lesson directly back to section 1.15.

At the master level: LDA assumes each class's within-class scatter is well-estimated, which fails badly in high dimensions with few samples per class — regularized/shrinkage LDA adds a small multiple of the identity to the within-class scatter matrix before solving, the exact same "ridge" trick from section 1.9 applied here to keep B safely invertible.

Newsletter

Stay in the loop

Subscribe to get new docs, diagrams, and engineering write-ups by Dharaneesh Boobalan delivered to your inbox.

  • Deep-dive write-ups on ML, inference, and systems.
  • New Draw.io diagrams & interactive canvases.
  • Agentic patterns and rocket-science notes.
  • No spam. One tasteful email when there's something new.

Crafted by Dharaneesh Boobalan

Newsletter

Get new docs, diagrams, and write-ups in your inbox.

We never share your details. Unsubscribe anytime.