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.

Principal Component Analysis

Problem Setup

Given data {x(n)}n=1N\{x^{(n)}\}_{n=1}^N with x(n)∈RDx^{(n)} \in \mathbb{R}^D, find a KK-dimensional subspace S\mathcal{S} (K<DK < D) spanned by orthonormal basis {u1,…,uK}\{u_1, \dots, u_K\} that preserves the important structure.

Let U=[u1⋯uK]∈RD×KU = [u_1 \cdots u_K] \in \mathbb{R}^{D \times K} with U⊤U=IKU^\top U = I_K.

The projection of a centered point onto the subspace is:

z=U⊤(x−μ)∈RKz = U^\top (x - \mu) \in \mathbb{R}^K

The reconstruction is:

x~=μ+Uz=μ+UU⊤(x−μ)\tilde{x} = \mu + Uz = \mu + UU^\top(x - \mu)

Orthogonality of projection error: U⊤(x−x~)=0U^\top(x - \tilde{x}) = 0, so the error is orthogonal to the subspace.


Derivation 1: Maximum Variance

Choose the subspace such that the variance of the reconstructions is maximized. Since μ~=μ\tilde{\mu} = \mu:

max⁡U:U⊤U=I1N∑n=1N∥x~(n)−μ∥2\max_{U: U^\top U = I} \frac{1}{N} \sum_{n=1}^N \|\tilde{x}^{(n)} - \mu\|^2

Since x~(n)−μ=Uz(n)\tilde{x}^{(n)} - \mu = Uz^{(n)} and ∥Uz(n)∥2=∥z(n)∥2\|Uz^{(n)}\|^2 = \|z^{(n)}\|^2 (because U⊤U=IU^\top U = I):

max⁡U:U⊤U=I1N∑n=1N∥z(n)∥2=max⁡U:U⊤U=ITr(U⊤SU)\max_{U: U^\top U = I} \frac{1}{N} \sum_{n=1}^N \|z^{(n)}\|^2 = \max_{U: U^\top U = I} \text{Tr}(U^\top S U)

where SS is the sample covariance matrix:

S=1N∑n=1N(x(n)−μ)(x(n)−μ)⊤S = \frac{1}{N} \sum_{n=1}^N (x^{(n)} - \mu)(x^{(n)} - \mu)^\top

Derivation 2: Minimum Reconstruction Error

Minimize the average squared reconstruction error:

min⁡U:U⊤U=I1N∑n=1N∥x(n)−x~(n)∥2\min_{U: U^\top U = I} \frac{1}{N} \sum_{n=1}^N \|x^{(n)} - \tilde{x}^{(n)}\|^2

Equivalence of the Two Derivations

For each sample, decompose:

x(n)−μ=(x~(n)−μ)+(x(n)−x~(n))x^{(n)} - \mu = (\tilde{x}^{(n)} - \mu) + (x^{(n)} - \tilde{x}^{(n)})

The two terms are orthogonal. By Pythagoras:

∥x(n)−μ∥2=∥x~(n)−μ∥2+∥x(n)−x~(n)∥2\|x^{(n)} - \mu\|^2 = \|\tilde{x}^{(n)} - \mu\|^2 + \|x^{(n)} - \tilde{x}^{(n)}\|^2

Averaging over all samples:

1N∑n∥x(n)−μ∥2⏟constant=1N∑n∥x~(n)−μ∥2⏟projected variance+1N∑n∥x(n)−x~(n)∥2⏟reconstruction error\underbrace{\frac{1}{N}\sum_n \|x^{(n)} - \mu\|^2}_{\text{constant}} = \underbrace{\frac{1}{N}\sum_n \|\tilde{x}^{(n)} - \mu\|^2}_{\text{projected variance}} + \underbrace{\frac{1}{N}\sum_n \|x^{(n)} - \tilde{x}^{(n)}\|^2}_{\text{reconstruction error}}

Since the left side is independent of UU:

max⁡projected variance  ⟺  min⁡reconstruction error\max \text{projected variance} \iff \min \text{reconstruction error}

Lagrangian and Eigendecomposition

The PCA objective is:

max⁡UTr(U⊤SU)subject to U⊤U=I\max_U \text{Tr}(U^\top S U) \quad \text{subject to } U^\top U = I

Lagrangian with multiplier matrix Λ\Lambda:

L(U,Λ)=Tr(U⊤SU)−Tr(Λ(U⊤U−I))\mathcal{L}(U, \Lambda) = \text{Tr}(U^\top S U) - \text{Tr}(\Lambda(U^\top U - I))

Stationary condition:

∂L∂U=2SU−U(Λ+Λ⊤)=0\frac{\partial \mathcal{L}}{\partial U} = 2SU - U(\Lambda + \Lambda^\top) = 0

Let M=Λ+Λ⊤M = \Lambda + \Lambda^\top (symmetric). Then SU=UMSU = UM.

Since MM is symmetric, it has an eigendecomposition M=RDR⊤M = RDR^\top. Let U0=URU_0 = UR. Then U0U_0 is still feasible and satisfies SU0=U0DSU_0 = U_0 D, meaning the columns of U0U_0 are eigenvectors of SS.

Let S=QΛQ⊤=∑i=1Dλiqiqi⊤S = Q\Lambda Q^\top = \sum_{i=1}^D \lambda_i q_i q_i^\top with λ1≥λ2≥⋯≥λD≥0\lambda_1 \geq \lambda_2 \geq \cdots \geq \lambda_D \geq 0.

If we choose U=[qt1⋯qtK]U = [q_{t_1} \cdots q_{t_K}], then:

Tr(U⊤SU)=∑j=1Kλtj\text{Tr}(U^\top S U) = \sum_{j=1}^K \lambda_{t_j}

To maximize, choose the top-KK eigenvectors:

U∗=[q1  q2  ⋯  qK]U^* = [q_1 \; q_2 \; \cdots \; q_K]

Interpretation

ConceptMeaning
Eigenvector qkq_kPrincipal direction (basis vector of PCA subspace)
Eigenvalue λk\lambda_kVariance of the data along that direction
Larger λk\lambda_kPreserves more information about the original data

In many datasets, the first few principal components explain most of the total variance.


PCA Algorithm

  1. Compute the sample mean: μ=1N∑n=1Nx(n)\mu = \frac{1}{N}\sum_{n=1}^N x^{(n)}

  2. Compute the covariance matrix: S=1N∑n=1N(x(n)−μ)(x(n)−μ)⊤S = \frac{1}{N}\sum_{n=1}^N (x^{(n)} - \mu)(x^{(n)} - \mu)^\top

  3. Compute eigendecomposition of SS and sort eigenvalues in descending order

  4. Form U=[q1⋯qK]U = [q_1 \cdots q_K] using the top-KK eigenvectors

  5. Compute low-dimensional representation: z(n)=U⊤(x(n)−μ)z^{(n)} = U^\top(x^{(n)} - \mu)