Unit 7, lesson 35

Principal Component Analysis

Centering a data matrix, the sample covariance matrix, principal directions from the SVD, why maximum projected variance and minimum reconstruction error agree, and how centering and units change the answer.

  • Video 35
  • Optimal Approximation and Principal Components

Four observations sit at (4,2)(4, 2), (0,2)(0, 2), (2,3)(2, 3) and (2,1)(2, 1), and each one must be replaced by a point on a single line. Which line loses the least total squared distance, and is it the line along which the data spread out the most? Principal component analysis answers both questions with one singular value decomposition, taken after the data are centered. This lesson builds the centered data matrix and the sample covariance matrix, proves that maximum projected variance and minimum reconstruction error choose the same subspaces, and shows how the answer changes when the center or the units change. Everything here is linear algebra on real data, with no probability model needed.

Key terms

Centered data matrix

Definition Let x1,…,xN∈Rdx_1, \dots, x_N \in \mathbb{R}^d be N>1N > 1 observations, each a column of dd real measurements. Their mean is the vector

xˉ=1N(x1+⋯+xN),\bar{x} = \frac{1}{N}(x_1 + \cdots + x_N),

computed coordinate by coordinate. The centered data matrix is the real N×dN \times d matrix XX whose iith row is (xi−xˉ)T(x_i - \bar{x})^{\mathsf{T}}. So rows are observations and columns are measured coordinates. If XrawX_{\text{raw}} has rows xiTx_i^{\mathsf{T}} and 1∈RN\mathbf{1} \in \mathbb{R}^N is the vector of ones, then X=Xraw−1xˉTX = X_{\text{raw}} - \mathbf{1}\bar{x}^{\mathsf{T}}. Every column of XX sums to zero, 1TX=0\mathbf{1}^{\mathsf{T}}X = 0, so the rows are dependent and rank⁡X≤min⁡(N−1,d)\operatorname{rank} X \le \min(N - 1, d). The mean is the best single center. For every c∈Rdc \in \mathbb{R}^d,

∑i=1N∥xi−c∥2=∑i=1N∥xi−xˉ∥2+N∥xˉ−c∥2,\begin{aligned} \sum_{i=1}^{N} \lVert x_i - c \rVert^2 &= \sum_{i=1}^{N} \lVert x_i - \bar{x} \rVert^2 \\[0.4em] &\quad + N\lVert \bar{x} - c \rVert^2 , \end{aligned}

because the cross term 2∑i(xi−xˉ)T(xˉ−c)2\sum_i (x_i - \bar{x})^{\mathsf{T}}(\bar{x} - c) vanishes. So c=xˉc = \bar{x} is the unique minimizer of the total squared distance.

In plain words Subtract the average observation from every observation. The data then sit around the origin, and every later question is about variation around the mean rather than about distance from an arbitrary origin.

Example For the four observations above the mean is (2,2)(2, 2), and

X=[20−20010−1].X = \begin{bmatrix} 2 & 0 \\ -2 & 0 \\ 0 & 1 \\ 0 & -1 \end{bmatrix}.

Both columns sum to zero. In the first coordinate alone, the values 4,0,2,24, 0, 2, 2 have total squared distance 8+4(2−c)28 + 4(2 - c)^2 from a number cc, which is smallest at the mean c=2c = 2.

Sample covariance matrix

Definition For a centered data matrix X∈RN×dX \in \mathbb{R}^{N \times d} with N>1N > 1, the sample covariance matrix is the d×dd \times d matrix

C=1N−1XTX=1N−1∑i=1N(xi−xˉ)(xi−xˉ)T.\begin{aligned} C &= \frac{1}{N - 1}X^{\mathsf{T}}X \\[0.4em] &= \frac{1}{N - 1}\sum_{i=1}^{N} (x_i - \bar{x})(x_i - \bar{x})^{\mathsf{T}} . \end{aligned}

It is real symmetric and positive semidefinite, since vTCv=∥Xv∥2/(N−1)≥0v^{\mathsf{T}}Cv = \lVert Xv \rVert^2/(N - 1) \ge 0, and rank⁡C=rank⁡X≤N−1\operatorname{rank} C = \operatorname{rank} X \le N - 1. The diagonal entry CjjC_{jj} is the sample variance of coordinate jj, and for j≠lj \ne l the entry CjlC_{jl} is the sample covariance of coordinates jj and ll. The divisor N−1N - 1 is a convention. Dividing by NN instead multiplies CC by the positive constant (N−1)/N(N - 1)/N, which scales every variance by the same factor and leaves every eigenvector and every comparison unchanged.

In plain words The covariance matrix collects the variance of every coordinate and the covariance of every pair of coordinates in one symmetric table. Each entry is an inner product of two centered columns, divided by N−1N - 1.

Example For the matrix XX above, the squared horizontal deviations add to 4+4=84 + 4 = 8, the squared vertical ones to 1+1=21 + 1 = 2, and every product of a horizontal and a vertical deviation is zero. So XTX=diag⁡(8,2)X^{\mathsf{T}}X = \operatorname{diag}(8, 2), and with N=4N = 4

C=[8/3002/3].C = \begin{bmatrix} 8/3 & 0 \\ 0 & 2/3 \end{bmatrix}.

The two coordinates are uncorrelated, and the horizontal one varies more.

Projected variance

Definition Let v∈Rdv \in \mathbb{R}^d be a unit vector. The scores of the centered observations along vv are the scalar projections (xi−xˉ)Tv(x_i - \bar{x})^{\mathsf{T}}v, the entries of Xv∈RNXv \in \mathbb{R}^N. They sum to zero, because 1TXv=0\mathbf{1}^{\mathsf{T}}Xv = 0, so their sample variance is

∥Xv∥2N−1=vTCv,\frac{\lVert Xv \rVert^2}{N - 1} = v^{\mathsf{T}}Cv ,

the projected variance along vv. This is the Rayleigh quotient of the symmetric matrix CC at a unit vector. In this lesson the eigenvalues of CC are listed in decreasing order, λ1≥λ2≥⋯≥λd\lambda_1 \ge \lambda_2 \ge \cdots \ge \lambda_d, to match the order of the singular values. By the bounds on the Rayleigh quotient, the maximum of the projected variance is λ1\lambda_1, attained exactly at the unit vectors of the eigenspace of λ1\lambda_1. For a kk-dimensional subspace spanned by the orthonormal columns of Vk∈Rd×kV_k \in \mathbb{R}^{d \times k}, the retained variance is

∥XVk∥F2N−1=tr⁡(VkTCVk),\frac{\lVert XV_k \rVert_F^2}{N - 1} = \operatorname{tr}(V_k^{\mathsf{T}}CV_k),

the sum of the projected variances along the columns of VkV_k. It depends only on the subspace, and its largest possible value is λ1+⋯+λk\lambda_1 + \cdots + \lambda_k, as the reconstruction error below shows.

In plain words Project every centered point onto a line and measure how spread out the projections are. The direction with the widest spread is a top eigenvector of the covariance matrix.

Example Along v=(cos⁡θ,sin⁡θ)v = (\cos\theta, \sin\theta) the four centered points have scores 2cos⁡θ2\cos\theta, −2cos⁡θ-2\cos\theta, sin⁡θ\sin\theta and −sin⁡θ-\sin\theta, so the projected variance is

8cos⁡2θ+2sin⁡2θ3=2+6cos⁡2θ3.\begin{aligned} &\frac{8\cos^2\theta + 2\sin^2\theta}{3} \\[0.4em] &\quad = \frac{2 + 6\cos^2\theta}{3}. \end{aligned}

It is largest, 8/38/3, for the horizontal direction θ=0\theta = 0, and smallest, 2/32/3, for the vertical direction.

Principal components

Definition Let X∈RN×dX \in \mathbb{R}^{N \times d} be a centered data matrix with N>1N > 1 and real full singular value decomposition X=UΣVTX = U\Sigma V^{\mathsf{T}}, with orthogonal UU of size NN, orthogonal VV of size dd and singular values σ1≥⋯≥σp≥0\sigma_1 \ge \cdots \ge \sigma_p \ge 0, where p=min⁡(N,d)p = \min(N, d). Since XTX=VΣTΣVTX^{\mathsf{T}}X = V\Sigma^{\mathsf{T}}\Sigma V^{\mathsf{T}}, the right singular vectors v1,…,vdv_1, \dots, v_d are orthonormal eigenvectors of CC, with eigenvalues σi2/(N−1)\sigma_i^2/(N - 1) for i≤pi \le p and 00 for any remaining ones. The principal directions, or principal axes, are v1,v2,…v_1, v_2, \dots in this order, and the iith principal component score vector is Xvi∈RNXv_i \in \mathbb{R}^N, whose entries are the coordinates of the centered observations along viv_i. For i≤pi \le p the variance along viv_i is σi2/(N−1)\sigma_i^2/(N - 1) and Xvi=σiuiXv_i = \sigma_iu_i. The first direction maximizes projected variance, and each later vjv_j maximizes it among unit vectors orthogonal to v1,…,vj−1v_1, \dots, v_{j-1}. The directions live in the measurement space Rd\mathbb{R}^d and the scores in the observation space RN\mathbb{R}^N. Books differ on whether “principal component” names the direction or the score vector, so say which one you mean. The directions are not unique. Replacing viv_i by −vi-v_i flips the sign of its scores but leaves the reconstruction XviviTXv_iv_i^{\mathsf{T}} unchanged, and when singular values tie, any orthonormal basis of the tied eigenspace of CC serves equally well.

In plain words The principal directions are perpendicular axes for the data cloud, ordered by how much spread each one carries. One singular value decomposition of the centered matrix gives the axes, the spreads and the coordinates of every observation along the axes.

Example For the four points, XTX=diag⁡(8,2)X^{\mathsf{T}}X = \operatorname{diag}(8, 2), so σ1=22\sigma_1 = 2\sqrt{2}, σ2=2\sigma_2 = \sqrt{2}, v1=e1v_1 = e_1 and v2=e2v_2 = e_2. The first score vector is

Xv1=[2−200]=σ1u1,u1=12[1−100],\begin{gathered} Xv_1 = \begin{bmatrix} 2 \\ -2 \\ 0 \\ 0 \end{bmatrix} = \sigma_1u_1, \\[0.4em] u_1 = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 \\ -1 \\ 0 \\ 0 \end{bmatrix}, \end{gathered}

and the variances along the principal directions are 8/38/3 and 2/32/3.

Reconstruction error

Definition Let Vk∈Rd×kV_k \in \mathbb{R}^{d \times k} have orthonormal columns spanning a subspace SS of dimension kk, and let P=VkVkTP = V_kV_k^{\mathsf{T}} be the orthogonal projector onto SS. The reconstruction of the centered data in SS is XPXP, whose rows are the orthogonal projections of the centered observations onto SS. Its reconstruction error is

∥X−XP∥F2=∑i=1N∥(I−P)(xi−xˉ)∥2.\begin{aligned} &\lVert X - XP \rVert_F^2 \\[0.4em] &\quad = \sum_{i=1}^{N} \lVert (I - P)(x_i - \bar{x}) \rVert^2 . \end{aligned}

Each row splits into perpendicular parts, so

∥X∥F2=∥XP∥F2+∥X−XP∥F2.\begin{aligned} \lVert X \rVert_F^2 &= \lVert XP \rVert_F^2 \\[0.4em] &\quad + \lVert X - XP \rVert_F^2 . \end{aligned}

The left side does not depend on SS, and ∥XP∥F2=∥XVk∥F2\lVert XP \rVert_F^2 = \lVert XV_k \rVert_F^2 is N−1N - 1 times the retained variance. So maximizing retained variance and minimizing reconstruction error are the same problem. When k≤pk \le p and SS is spanned by the top kk principal directions, XP=∑i≤kσiuiviT=XkXP = \sum_{i \le k} \sigma_iu_iv_i^{\mathsf{T}} = X_k is the truncated SVD, with error σk+12+⋯+σp2\sigma_{k+1}^2 + \cdots + \sigma_p^2. Every XPXP has rank at most kk, so the Eckart-Young theorem in the Frobenius norm shows that no kk-dimensional subspace does better. The largest retained variance is (σ12+⋯+σk2)/(N−1)(\sigma_1^2 + \cdots + \sigma_k^2)/(N - 1), and when k<pk < p and σk>σk+1\sigma_k > \sigma_{k+1} the best subspace is unique. For the raw observations and an affine subspace a+Sa + S, the same splitting around the mean gives

∑i=1N∥(I−P)(xi−a)∥2=∥X−XP∥F2+N∥(I−P)(xˉ−a)∥2,\begin{aligned} &\sum_{i=1}^{N} \lVert (I - P)(x_i - a) \rVert^2 \\[0.4em] &\quad = \lVert X - XP \rVert_F^2 \\[0.4em] &\qquad + N\lVert (I - P)(\bar{x} - a) \rVert^2 , \end{aligned}

so a best affine fit of dimension kk can pass through the mean, and xˉ+span⁡(v1,…,vk)\bar{x} + \operatorname{span}(v_1, \dots, v_k) is one.

In plain words Replace every centered point by its projection on a kk-dimensional subspace and add up the squared distances lost. A point’s squared length is the squared length of its projection plus the squared loss, so keeping the most variance and losing the least are one and the same goal.

Example For the four points and the line through the mean in the direction (cos⁡θ,sin⁡θ)(\cos\theta, \sin\theta), the total squared length is 1010 and the retained part is 8cos⁡2θ+2sin⁡2θ8\cos^2\theta + 2\sin^2\theta, so the reconstruction error is

10−(8cos⁡2θ+2sin⁡2θ)=2+6sin⁡2θ.\begin{aligned} &10 - (8\cos^2\theta + 2\sin^2\theta) \\[0.4em] &\quad = 2 + 6\sin^2\theta . \end{aligned}

It is smallest at θ=0\theta = 0, with value 2=σ222 = \sigma_2^2. For the raw observations the best line is y=2y = 2, through the mean (2,2)(2, 2). It keeps (4,2)(4, 2) and (0,2)(0, 2) exactly and moves both (2,3)(2, 3) and (2,1)(2, 1) to (2,2)(2, 2), a squared loss of 1+1=21 + 1 = 2.

Effect of centering and units

Definition The answer of principal component analysis depends on two choices made before any computation, the center and the inner product on the measurement space, which the units of the coordinates fix. Without centering, the SVD of XrawX_{\text{raw}} finds best subspaces through the origin, and since 1TX=0\mathbf{1}^{\mathsf{T}}X = 0,

XrawTXraw=XTX+NxˉxˉT.X_{\text{raw}}^{\mathsf{T}}X_{\text{raw}} = X^{\mathsf{T}}X + N\bar{x}\bar{x}^{\mathsf{T}} .

A nonzero mean adds a rank-one term that can dominate the leading direction. Centered principal component analysis, by contrast, is unchanged when every observation is translated by the same vector. Changing units multiplies each coordinate jj by a positive constant cjc_j, which replaces XX by XDXD and CC by DCDDCD, where D=diag⁡(c1,…,cd)D = \operatorname{diag}(c_1, \dots, c_d), and this generally changes the principal directions. When every coordinate has nonzero variance, standardizing divides each coordinate by its sample standard deviation. That is the choice cj=1/Cjjc_j = 1/\sqrt{C_{jj}}, and it turns CC into the correlation matrix. Standardizing is a modeling decision with mathematical consequences, not a step the mathematics makes for you. The optimality theorem guarantees a best subspace for the chosen center, units and dimension, and nothing more. A direction of large variance is not automatically important, causal or relevant to a prediction.

In plain words The method measures spread around a chosen center in chosen units. Move the center or rescale an axis and you are asking a different question, which can have a different answer.

Example Translate the four centered points up by 1010, to (2,10)(2, 10), (−2,10)(-2, 10), (0,11)(0, 11) and (0,9)(0, 9). Centering returns the same XX, so centered analysis still picks the horizontal direction. The raw Gram matrix is diag⁡(8,402)\operatorname{diag}(8, 402), since 100+100+121+81=402=2+4⋅102100 + 100 + 121 + 81 = 402 = 2 + 4 \cdot 10^2, so the leading uncentered direction is vertical and mostly describes the offset. Units matter too. Multiplying the vertical coordinate of the centered points by 1010 gives (2,0)(2, 0), (−2,0)(-2, 0), (0,10)(0, 10) and (0,−10)(0, -10), with XTX=diag⁡(8,200)X^{\mathsf{T}}X = \operatorname{diag}(8, 200), and the leading direction becomes vertical.

Check yourself

  1. Center the observations (1,1)(1, 1), (3,1)(3, 1), (2,4)(2, 4) and (2,−2)(2, -2). Find XTXX^{\mathsf{T}}X, the sample covariance matrix, the first principal direction, the variances along both principal directions, the best line and its reconstruction error.
Show the answer

The mean is (2,1)(2, 1), so the centered rows are (−1,0)(-1, 0), (1,0)(1, 0), (0,3)(0, 3) and (0,−3)(0, -3). The squared deviations add to 22 horizontally and 1818 vertically, and every cross product is zero, so

XTX=[20018],C=[2/3006].\begin{gathered} X^{\mathsf{T}}X = \begin{bmatrix} 2 & 0 \\ 0 & 18 \end{bmatrix}, \\[0.4em] C = \begin{bmatrix} 2/3 & 0 \\ 0 & 6 \end{bmatrix}. \end{gathered}

The first principal direction is vertical, v1=e2v_1 = e_2, with variance 66, and the second is horizontal with variance 2/32/3. The best line is the vertical line x=2x = 2 through the mean. It loses the horizontal deviations, a reconstruction error of 1+1=21 + 1 = 2, which is σ22\sigma_2^2.

  1. The centered rows are (2,2)(2, 2), (−2,−2)(-2, -2), (1,−1)(1, -1) and (−1,1)(-1, 1). Find the sample covariance matrix, the principal directions and their variances, the scores along the first direction, and the reconstruction error of the best line.
Show the answer

The columns sum to zero, so the data are centered. Adding squares and products gives

XTX=[106610],C=[10/32210/3].\begin{gathered} X^{\mathsf{T}}X = \begin{bmatrix} 10 & 6 \\ 6 & 10 \end{bmatrix}, \\[0.4em] C = \begin{bmatrix} 10/3 & 2 \\ 2 & 10/3 \end{bmatrix}. \end{gathered}

The vector (1,1)(1, 1) is an eigenvector of XTXX^{\mathsf{T}}X with eigenvalue 1616 and (1,−1)(1, -1) one with eigenvalue 44. So v1=(1,1)/2v_1 = (1, 1)/\sqrt{2} with variance 16/316/3, and v2=(1,−1)/2v_2 = (1, -1)/\sqrt{2} with variance 4/34/3. The scores along v1v_1 are Xv1=(22,−22,0,0)Xv_1 = (2\sqrt{2}, -2\sqrt{2}, 0, 0). The best line is y=xy = x. It reconstructs the rows as (2,2)(2, 2), (−2,−2)(-2, -2), (0,0)(0, 0) and (0,0)(0, 0), with error 4=σ224 = \sigma_2^2, and the total ∥X∥F2=20\lVert X \rVert_F^2 = 20 splits as 16+416 + 4.

  1. The observations are (−1,3)(-1, 3), (1,3)(1, 3), (−2,3)(-2, 3) and (2,3)(2, 3). Someone skips centering and takes the leading right singular vector of the raw data matrix. Which line through the origin do they get, and what is its squared error? What does centered principal component analysis give?
Show the answer

The raw Gram matrix is diag⁡(10,36)\operatorname{diag}(10, 36), because the squared first coordinates add to 1010, the squared second coordinates to 4⋅9=364 \cdot 9 = 36, and the products add to 3(−1+1−2+2)=03(-1 + 1 - 2 + 2) = 0. The leading raw direction is vertical, so the line is the yy-axis, and it loses all the horizontal coordinates, a squared error of 1010. Centering subtracts the mean (0,3)(0, 3) and leaves the rows (−1,0)(-1, 0), (1,0)(1, 0), (−2,0)(-2, 0) and (2,0)(2, 0), so XTX=diag⁡(10,0)X^{\mathsf{T}}X = \operatorname{diag}(10, 0). The first principal direction is horizontal with variance 10/310/3, and the best line is y=3y = 3 through the mean, with reconstruction error 00, because every point lies on it. The uncentered answer describes the offset of the data from the origin, not their variation.

How this lesson connects

It also uses the arithmetic mean of a list of numbers.