Principal Component Analysis
Problem Setup ¶ Given data { x ( n ) } n = 1 N \{x^{(n)}\}_{n=1}^N { x ( n ) } n = 1 N with x ( n ) ∈ R D x^{(n)} \in \mathbb{R}^D x ( n ) ∈ R D , find a K K K -dimensional subspace S \mathcal{S} S (K < D K < D K < D ) spanned by orthonormal basis { u 1 , … , u K } \{u_1, \dots, u_K\} { u 1 , … , u K } that preserves the important structure.
Let U = [ u 1 ⋯ u K ] ∈ R D × K U = [u_1 \cdots u_K] \in \mathbb{R}^{D \times K} U = [ u 1 ⋯ u K ] ∈ R D × K with U ⊤ U = I K U^\top U = I_K U ⊤ U = I K .
The projection of a centered point onto the subspace is:
z = U ⊤ ( x − μ ) ∈ R K z = U^\top (x - \mu) \in \mathbb{R}^K z = U ⊤ ( x − μ ) ∈ R K The reconstruction is:
x ~ = μ + U z = μ + U U ⊤ ( x − μ ) \tilde{x} = \mu + Uz = \mu + UU^\top(x - \mu) x ~ = μ + U z = μ + U U ⊤ ( x − μ ) Orthogonality of projection error: U ⊤ ( x − x ~ ) = 0 U^\top(x - \tilde{x}) = 0 U ⊤ ( x − 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 = I 1 N ∑ n = 1 N ∥ x ~ ( n ) − μ ∥ 2 \max_{U: U^\top U = I} \frac{1}{N} \sum_{n=1}^N \|\tilde{x}^{(n)} - \mu\|^2 U : U ⊤ U = I max N 1 n = 1 ∑ N ∥ x ~ ( n ) − μ ∥ 2 Since x ~ ( n ) − μ = U z ( n ) \tilde{x}^{(n)} - \mu = Uz^{(n)} x ~ ( n ) − μ = U z ( n ) and ∥ U z ( n ) ∥ 2 = ∥ z ( n ) ∥ 2 \|Uz^{(n)}\|^2 = \|z^{(n)}\|^2 ∥ U z ( n ) ∥ 2 = ∥ z ( n ) ∥ 2 (because U ⊤ U = I U^\top U = I U ⊤ U = I ):
max U : U ⊤ U = I 1 N ∑ n = 1 N ∥ z ( n ) ∥ 2 = max U : U ⊤ U = I Tr ( U ⊤ S U ) \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) U : U ⊤ U = I max N 1 n = 1 ∑ N ∥ z ( n ) ∥ 2 = U : U ⊤ U = I max Tr ( U ⊤ S U ) where S S S is the sample covariance matrix:
S = 1 N ∑ n = 1 N ( x ( n ) − μ ) ( x ( n ) − μ ) ⊤ S = \frac{1}{N} \sum_{n=1}^N (x^{(n)} - \mu)(x^{(n)} - \mu)^\top S = N 1 n = 1 ∑ N ( x ( n ) − μ ) ( x ( n ) − μ ) ⊤ Derivation 2: Minimum Reconstruction Error ¶ Minimize the average squared reconstruction error:
min U : U ⊤ U = I 1 N ∑ n = 1 N ∥ x ( n ) − x ~ ( n ) ∥ 2 \min_{U: U^\top U = I} \frac{1}{N} \sum_{n=1}^N \|x^{(n)} - \tilde{x}^{(n)}\|^2 U : U ⊤ U = I min N 1 n = 1 ∑ N ∥ x ( n ) − 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)}) x ( n ) − μ = ( x ~ ( n ) − μ ) + ( x ( n ) − 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 ∥ x ( n ) − μ ∥ 2 = ∥ x ~ ( n ) − μ ∥ 2 + ∥ x ( n ) − x ~ ( n ) ∥ 2 Averaging over all samples:
1 N ∑ n ∥ x ( n ) − μ ∥ 2 ⏟ constant = 1 N ∑ n ∥ x ~ ( n ) − μ ∥ 2 ⏟ projected variance + 1 N ∑ 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}} constant N 1 n ∑ ∥ x ( n ) − μ ∥ 2 = projected variance N 1 n ∑ ∥ x ~ ( n ) − μ ∥ 2 + reconstruction error N 1 n ∑ ∥ x ( n ) − x ~ ( n ) ∥ 2 Since the left side is independent of U U U :
max projected variance ⟺ min reconstruction error \max \text{projected variance} \iff \min \text{reconstruction error} max projected variance ⟺ min reconstruction error Lagrangian and Eigendecomposition ¶ The PCA objective is:
max U Tr ( U ⊤ S U ) subject to U ⊤ U = I \max_U \text{Tr}(U^\top S U) \quad \text{subject to } U^\top U = I U max Tr ( U ⊤ S U ) subject to U ⊤ U = I Lagrangian with multiplier matrix Λ \Lambda Λ :
L ( U , Λ ) = Tr ( U ⊤ S U ) − Tr ( Λ ( U ⊤ U − I ) ) \mathcal{L}(U, \Lambda) = \text{Tr}(U^\top S U) - \text{Tr}(\Lambda(U^\top U - I)) L ( U , Λ ) = Tr ( U ⊤ S U ) − Tr ( Λ ( U ⊤ U − I )) Stationary condition:
∂ L ∂ U = 2 S U − U ( Λ + Λ ⊤ ) = 0 \frac{\partial \mathcal{L}}{\partial U} = 2SU - U(\Lambda + \Lambda^\top) = 0 ∂ U ∂ L = 2 S U − U ( Λ + Λ ⊤ ) = 0 Let M = Λ + Λ ⊤ M = \Lambda + \Lambda^\top M = Λ + Λ ⊤ (symmetric). Then S U = U M SU = UM S U = U M .
Since M M M is symmetric, it has an eigendecomposition M = R D R ⊤ M = RDR^\top M = R D R ⊤ . Let U 0 = U R U_0 = UR U 0 = U R . Then U 0 U_0 U 0 is still feasible and satisfies S U 0 = U 0 D SU_0 = U_0 D S U 0 = U 0 D , meaning the columns of U 0 U_0 U 0 are eigenvectors of S S S .
Let S = Q Λ Q ⊤ = ∑ i = 1 D λ i q i q i ⊤ S = Q\Lambda Q^\top = \sum_{i=1}^D \lambda_i q_i q_i^\top S = Q Λ Q ⊤ = ∑ i = 1 D λ i q i q i ⊤ with λ 1 ≥ λ 2 ≥ ⋯ ≥ λ D ≥ 0 \lambda_1 \geq \lambda_2 \geq \cdots \geq \lambda_D \geq 0 λ 1 ≥ λ 2 ≥ ⋯ ≥ λ D ≥ 0 .
If we choose U = [ q t 1 ⋯ q t K ] U = [q_{t_1} \cdots q_{t_K}] U = [ q t 1 ⋯ q t K ] , then:
Tr ( U ⊤ S U ) = ∑ j = 1 K λ t j \text{Tr}(U^\top S U) = \sum_{j=1}^K \lambda_{t_j} Tr ( U ⊤ S U ) = j = 1 ∑ K λ t j To maximize, choose the top-K K K eigenvectors :
U ∗ = [ q 1 q 2 ⋯ q K ] U^* = [q_1 \; q_2 \; \cdots \; q_K] U ∗ = [ q 1 q 2 ⋯ q K ] Interpretation ¶ Concept Meaning Eigenvector q k q_k q k Principal direction (basis vector of PCA subspace) Eigenvalue λ k \lambda_k λ k Variance of the data along that direction Larger λ k \lambda_k λ k Preserves more information about the original data
In many datasets, the first few principal components explain most of the total variance.
PCA Algorithm ¶ Compute the sample mean: μ = 1 N ∑ n = 1 N x ( n ) \mu = \frac{1}{N}\sum_{n=1}^N x^{(n)} μ = N 1 ∑ n = 1 N x ( n )
Compute the covariance matrix: S = 1 N ∑ n = 1 N ( x ( n ) − μ ) ( x ( n ) − μ ) ⊤ S = \frac{1}{N}\sum_{n=1}^N (x^{(n)} - \mu)(x^{(n)} - \mu)^\top S = N 1 ∑ n = 1 N ( x ( n ) − μ ) ( x ( n ) − μ ) ⊤
Compute eigendecomposition of S S S and sort eigenvalues in descending order
Form U = [ q 1 ⋯ q K ] U = [q_1 \cdots q_K] U = [ q 1 ⋯ q K ] using the top-K K K eigenvectors
Compute low-dimensional representation: z ( n ) = U ⊤ ( x ( n ) − μ ) z^{(n)} = U^\top(x^{(n)} - \mu) z ( n ) = U ⊤ ( x ( n ) − μ )