Principal Component Analysis (PCA) finds a lower-dimensional linear subspace that preserves as much variance in the data as possible.
Given centered data X (mean-subtracted, n × d), PCA seeks a unit vector w1 maximizing the variance of the projection Xw1:
maximize w^T Σ w subject to ‖w‖ = 1, where Σ = (1/n) X^T X is the covariance matrix
Derivation via Lagrange multipliers. The Lagrangian is L(w, λ) = w^TΣw - λ(w^Tw - 1). Setting ∇_w L = 0 gives Σw = λw — meaning w must be an eigenvector of Σ, with the corresponding eigenvalue λ equal to the variance captured (w^TΣw = w^T(λw) = λ, using ‖w‖=1). Since Σ is symmetric positive semi-definite, it has an orthonormal set of real eigenvectors with non-negative eigenvalues; the variance-maximizing direction is the eigenvector with the largest eigenvalue. Subsequent principal components are the remaining eigenvectors in decreasing eigenvalue order, each orthogonal to all previous ones (a consequence of Σ's symmetric eigendecomposition), capturing the most remaining variance at each step.
Connection to SVD. If X = UDV^T is the singular value decomposition of the centered data matrix, then X^TX = VD²V^T, so the columns of V are exactly the eigenvectors of Σ (up to the 1/n scaling), and the singular values D relate to the eigenvalues by λ_i = d_i²/n. In practice PCA is computed via SVD directly rather than explicitly forming Σ, both for numerical stability and because SVD is efficient even when d ≫ n.
The derivation uses covariance scaled by 1/n. The SVD example reports sample variances using 1/(n−1); the change rescales the eigenvalues but leaves the principal directions unchanged.
Continue with the book
Explore the full treatment, worked examples, and exercises in The AI Algorithms Compendium.
Companion article for The AI Algorithms Compendium, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
