Unit 10, lesson 51

Principal Component Analysis with Centering and Variance

How principal component analysis uses feature means, centering and the sample covariance matrix to find directions of largest projected variance, and how retained variance, reconstruction error and units enter.

  • Video 51
  • Low-Rank Approximation and Principal Components

Four observations of two features sit at the corners of a rectangle, four units wide and two units tall, with center (10,5)(10, 5). You may keep only one number per observation, its coordinate along a line of your choice. Which line loses the least when the points are rebuilt from those numbers? Principal component analysis answers this by measuring spread around the mean, taking the directions of largest variance from the eigenvectors of a covariance matrix, and accounting exactly for what is kept and what is lost. Tuples such as (2,1)(2, 1) stand for column vectors, and each observation is stored as a row of a data matrix, the transpose of such a column.

Key terms

Feature mean and centered data matrix

Definition A data matrix is a real N×pN \times p matrix XX whose NN rows are observations and whose pp columns are features, the quantities recorded for each observation. Row ii is xi⊤\mathbf{x}_i^{\top} with xi\mathbf{x}_i in Rp\mathbb{R}^p. The mean of feature jj is the average xˉj\bar{x}_j of column jj, and the feature means form the mean vector

xˉ=1N∑i=1Nxi=1NX⊤1,\bar{\mathbf{x}} = \frac{1}{N}\sum_{i=1}^{N} \mathbf{x}_i = \frac{1}{N}X^{\top}\mathbf{1},

where every entry of 1\mathbf{1} in RN\mathbb{R}^N equals 11. The centered data matrix XcX_c has rows (xi−xˉ)⊤(\mathbf{x}_i - \bar{\mathbf{x}})^{\top}, that is

Xc=X−1xˉ⊤.X_c = X - \mathbf{1}\bar{\mathbf{x}}^{\top}.

Every column of XcX_c sums to zero, which says Xc⊤1=0X_c^{\top}\mathbf{1} = \mathbf{0}. Adding the same vector a\mathbf{a} to every observation changes the mean to xˉ+a\bar{\mathbf{x}} + \mathbf{a} and leaves XcX_c unchanged. Centering subtracts the means and nothing else. It does not change units or divide by any measure of spread.

In plain words Centering moves the origin to the middle of the cloud. What remains describes how the observations differ from one another, not where the cloud sits.

Example The observations (12,6)(12, 6), (12,4)(12, 4), (8,6)(8, 6) and (8,4)(8, 4) have feature means (12+12+8+8)/4=10(12 + 12 + 8 + 8)/4 = 10 and (6+4+6+4)/4=5(6 + 4 + 6 + 4)/4 = 5, so xˉ=(10,5)\bar{\mathbf{x}} = (10, 5) and

X=[1261248684],Xc=[212−1−21−2−1].\begin{gathered} X = \begin{bmatrix} 12 & 6 \\ 12 & 4 \\ 8 & 6 \\ 8 & 4 \end{bmatrix}, \\[0.4em] X_c = \begin{bmatrix} 2 & 1 \\ 2 & -1 \\ -2 & 1 \\ -2 & -1 \end{bmatrix}. \end{gathered}

Translating the whole rectangle to any other position would produce the same XcX_c.

Sample covariance matrix

Definition Let N>1N > 1 and let XcX_c be the centered N×pN \times p data matrix. The sample covariance matrix is the p×pp \times p matrix

C=1N−1Xc⊤Xc.C = \frac{1}{N - 1}X_c^{\top}X_c.

Multiplying Xc⊤X_c^{\top} by XcX_c column by row shows that (N−1)C(N - 1)C is the sum of the outer products (xi−xˉ)(xi−xˉ)⊤(\mathbf{x}_i - \bar{\mathbf{x}})(\mathbf{x}_i - \bar{\mathbf{x}})^{\top} of the centered observations. The diagonal entry cjjc_{jj} of CC is the sample variance of feature jj, the sum of the squared centered values in column jj divided by N−1N - 1. For j≠kj \ne k, the entry cjkc_{jk} is the sample covariance of features jj and kk, the sum of the products of their centered values divided by N−1N - 1. The matrix CC is symmetric and positive semidefinite, since w⊤Cw=∥Xcw∥2/(N−1)≥0\mathbf{w}^{\top}C\mathbf{w} = \lVert X_c\mathbf{w} \rVert^2/(N - 1) \ge 0 for every w\mathbf{w}. Its trace, the sum of the feature variances, is the total variance. Dividing by NN instead of N−1N - 1 gives mean squared deviations, which equal the sample variances times (N−1)/N(N - 1)/N, and this page uses N−1N - 1 throughout. A covariance of zero says only that the products sum to zero. It does not mean that the features are statistically independent.

In plain words The diagonal says how widely each feature spreads around its mean. An entry off the diagonal is positive when two features tend to lie above or below their means together, and negative when one tends to be high while the other is low.

Example For the centered rectangle, the squared values in the first column add to 4+4+4+4=164 + 4 + 4 + 4 = 16, those in the second column add to 1+1+1+1=41 + 1 + 1 + 1 = 4, and the products add to 2−2−2+2=02 - 2 - 2 + 2 = 0. With N−1=3N - 1 = 3,

C=[16/3004/3],C = \begin{bmatrix} 16/3 & 0 \\ 0 & 4/3 \end{bmatrix},

and the total variance is 163+43=203\tfrac{16}{3} + \tfrac{4}{3} = \tfrac{20}{3}.

Projected variance

Definition Let w\mathbf{w} be a unit vector in Rp\mathbb{R}^p. The score of observation ii along w\mathbf{w} is ti=w⋅(xi−xˉ)t_i = \mathbf{w} \cdot (\mathbf{x}_i - \bar{\mathbf{x}}), and the scores form the vector t=Xcw\mathbf{t} = X_c\mathbf{w} in RN\mathbb{R}^N. They have mean zero, because 1⊤Xcw=0\mathbf{1}^{\top}X_c\mathbf{w} = 0, and their sample variance, the projected variance along w\mathbf{w}, is

1N−1∑i=1Nti2=∥Xcw∥2N−1=w⊤Cw.\begin{aligned} \frac{1}{N - 1}\sum_{i=1}^{N} t_i^2 &= \frac{\lVert X_c\mathbf{w} \rVert^2}{N - 1} \\[0.4em] &= \mathbf{w}^{\top}C\mathbf{w}. \end{aligned}

The point xˉ+tiw\bar{\mathbf{x}} + t_i\mathbf{w} is the orthogonal projection of xi\mathbf{x}_i onto the line through xˉ\bar{\mathbf{x}} with direction w\mathbf{w}. The condition ∥w∥=1\lVert \mathbf{w} \rVert = 1 matters, because replacing w\mathbf{w} by cwc\mathbf{w} multiplies the quadratic form by c2c^2 without choosing a better direction.

In plain words Drop every point perpendicularly onto a line through the mean and measure how spread out the landing positions are. The quadratic form w⊤Cw\mathbf{w}^{\top}C\mathbf{w} gives that spread for every direction at once, from CC alone.

Example For the rectangle and w=(cos⁡θ,sin⁡θ)\mathbf{w} = (\cos\theta, \sin\theta),

w⊤Cw=163cos⁡2θ+43sin⁡2θ=43+4cos⁡2θ.\begin{aligned} \mathbf{w}^{\top}C\mathbf{w} &= \tfrac{16}{3}\cos^2\theta + \tfrac{4}{3}\sin^2\theta \\[0.4em] &= \tfrac{4}{3} + 4\cos^2\theta. \end{aligned}

The horizontal direction gives 163\tfrac{16}{3}, the vertical direction gives 43\tfrac43, and the diagonal direction (1,1)/2(1, 1)/\sqrt{2} gives 103\tfrac{10}{3}. The last value can be checked directly. The centered rows have scores 3/23/\sqrt{2}, 1/21/\sqrt{2}, −1/2-1/\sqrt{2} and −3/2-3/\sqrt{2} along the diagonal, their squares add to 9+1+1+92=10\tfrac{9 + 1 + 1 + 9}{2} = 10, and dividing by 33 gives 103\tfrac{10}{3}.

Principal component

Definition Let CC be the sample covariance matrix. By the spectral theorem it has real eigenvalues and an orthonormal basis of eigenvectors w1,…,wp\mathbf{w}_1, \dots, \mathbf{w}_p with Cwj=λjwjC\mathbf{w}_j = \lambda_j\mathbf{w}_j. The eigenvalues are nonnegative because CC is positive semidefinite, and they are listed here in decreasing order λ1≥λ2≥⋯≥λp≥0\lambda_1 \ge \lambda_2 \ge \cdots \ge \lambda_p \ge 0. Writing a unit vector as w=∑jαjwj\mathbf{w} = \sum_j \alpha_j\mathbf{w}_j with ∑jαj2=1\sum_j \alpha_j^2 = 1 gives

w⊤Cw=∑j=1pλjαj2≤λ1,\mathbf{w}^{\top}C\mathbf{w} = \sum_{j=1}^{p} \lambda_j\alpha_j^2 \le \lambda_1,

with equality at w=w1\mathbf{w} = \mathbf{w}_1. So the first principal direction w1\mathbf{w}_1 has the largest projected variance, namely λ1\lambda_1. In the same way, wk\mathbf{w}_k has the largest projected variance among unit vectors perpendicular to w1,…,wk−1\mathbf{w}_1, \dots, \mathbf{w}_{k-1}, and that variance is λk\lambda_k. The kkth principal component is the new variable measured along wk\mathbf{w}_k, with scores XcwkX_c\mathbf{w}_k and sample variance λk\lambda_k. Many texts also call the direction wk\mathbf{w}_k itself a principal component. A direction can be replaced by its negative, which flips the sign of every score and changes nothing else, and when an eigenvalue is repeated any orthonormal basis of its eigenspace may be used. In terms of a full SVD Xc=UΣV⊤X_c = U\Sigma V^{\top}, the right singular vectors v1,…,vp\mathbf{v}_1, \dots, \mathbf{v}_p serve as principal directions, and the eigenvalues of CC are the numbers σj2/(N−1)\sigma_j^2/(N - 1) for j=1,…,min⁡(N,p)j = 1, \dots, \min(N, p), followed by p−Np - N zeros when p>Np > N. For k≤min⁡(N,p)k \le \min(N, p), the scores of the first kk components are XcVk=UkΣkX_cV_k = U_k\Sigma_k, not UkU_k alone.

In plain words The first principal direction spans a line along which the centered cloud is most spread out. The second is a direction of largest spread perpendicular to the first, and so on.

Example For the rectangle, CC is diagonal with distinct diagonal entries, so w1=e1\mathbf{w}_1 = \mathbf{e}_1 with λ1=163\lambda_1 = \tfrac{16}{3} and w2=e2\mathbf{w}_2 = \mathbf{e}_2 with λ2=43\lambda_2 = \tfrac43. The first component has scores Xce1=(2,2,−2,−2)X_c\mathbf{e}_1 = (2, 2, -2, -2). Since Xc⊤Xc=diag⁡(16,4)X_c^{\top}X_c = \operatorname{diag}(16, 4), the singular values of XcX_c are 44 and 22, and indeed 42/3=1634^2/3 = \tfrac{16}{3} and 22/3=432^2/3 = \tfrac43. Choosing −e1-\mathbf{e}_1 instead gives the scores (−2,−2,2,2)(-2, -2, 2, 2) and describes the same line.

Retained variance and reconstruction error

Definition For an integer kk with 1≤k≤p1 \le k \le p, keep the first kk principal directions as the columns of the p×kp \times k matrix Wk=[w1 ⋯ wk]W_k = [\mathbf{w}_1 \ \cdots \ \mathbf{w}_k], which has orthonormal columns. The reconstruction of observation ii from its kk scores is

x^i=xˉ+WkWk⊤(xi−xˉ),\hat{\mathbf{x}}_i = \bar{\mathbf{x}} + W_kW_k^{\top}(\mathbf{x}_i - \bar{\mathbf{x}}),

the orthogonal projection of xi\mathbf{x}_i onto the affine subspace xˉ+span⁡{w1,…,wk}\bar{\mathbf{x}} + \operatorname{span}\{\mathbf{w}_1, \dots, \mathbf{w}_k\}. The retained variance is λ1+⋯+λk\lambda_1 + \cdots + \lambda_k, the total variance is tr⁡C=λ1+⋯+λp\operatorname{tr} C = \lambda_1 + \cdots + \lambda_p, and when the total is positive the retained fraction is their quotient. The reconstruction error is the total squared distance

∑i=1N∥xi−x^i∥2=∥Xc−XcWkWk⊤∥F2=(N−1)(λk+1+⋯+λp).\begin{aligned} &\sum_{i=1}^{N} \lVert \mathbf{x}_i - \hat{\mathbf{x}}_i \rVert^2 \\[0.4em] &= \lVert X_c - X_cW_kW_k^{\top} \rVert_F^2 \\[0.4em] &= (N - 1)(\lambda_{k+1} + \cdots + \lambda_p). \end{aligned}

Each residual is perpendicular to the kept directions, so the total squared length ∥Xc∥F2=(N−1)tr⁡C\lVert X_c \rVert_F^2 = (N - 1)\operatorname{tr} C splits into the retained part (N−1)(λ1+⋯+λk)(N - 1)(\lambda_1 + \cdots + \lambda_k) plus the reconstruction error. The matrix XcWkWk⊤X_cW_kW_k^{\top} is a truncated SVD of XcX_c of rank at most kk, so by the Eckart-Young theorem no affine subspace of dimension kk through xˉ\bar{\mathbf{x}} gives a smaller reconstruction error. One that misses xˉ\bar{\mathbf{x}} does no better, because sliding it parallel to itself until it contains xˉ\bar{\mathbf{x}} never increases the total squared distance. Maximizing the retained variance and minimizing the reconstruction error select the same directions.

In plain words Retained variance is the spread kept along the chosen directions, and reconstruction error is the spread thrown away, measured as squared distances from the points to their projections. Up to the factor N−1N - 1 the two add up to the total, so making one as large as possible makes the other as small as possible.

Example For the rectangle with k=1k = 1, the centered rows are rebuilt as (2,0)(2, 0), (2,0)(2, 0), (−2,0)(-2, 0) and (−2,0)(-2, 0). The lost second coordinates 1,−1,1,−11, -1, 1, -1 give reconstruction error 44, which equals (N−1)λ2=3⋅43(N - 1)\lambda_2 = 3 \cdot \tfrac43 and also σ22=22\sigma_2^2 = 2^2. The total squared length is 16+4=2016 + 4 = 20, so the first component keeps 1616 of 2020, or 8080 percent, the same fraction as 163\tfrac{16}{3} out of 203\tfrac{20}{3}. Adding the mean (10,5)(10, 5) back gives the reconstructed observations (12,5)(12, 5), (12,5)(12, 5), (8,5)(8, 5) and (8,5)(8, 5), all on the horizontal line through the mean. A large retained fraction does not mean that the useful information was kept. This reconstruction has erased whether each point lay above or below the mean, which is exactly what some task might need.

Effect of centering and units

Definition Principal directions depend on two choices made before any eigenvalue is computed. The first is centering. Without it, the matrix 1NX⊤X\tfrac1N X^{\top}X of second moments about the origin takes the place of CC, and

1NX⊤X=N−1N C+xˉxˉ⊤,\frac{1}{N}X^{\top}X = \frac{N - 1}{N}\,C + \bar{\mathbf{x}}\bar{\mathbf{x}}^{\top},

so a mean far from the origin adds a large rank-one term in the direction of xˉ\bar{\mathbf{x}}, which can dominate the leading eigenvector. The second choice is the units. Measuring feature jj in different units multiplies its centered column by a positive factor djd_j. With D=diag⁡(d1,…,dp)D = \operatorname{diag}(d_1, \dots, d_p), the centered data become XcDX_cD and the covariance matrix becomes DCDDCD, which in general has different eigenvectors from CC. Scaling every feature by the same factor dd multiplies CC by d2d^2 and leaves the directions alone. Standardizing, which divides each centered feature by its sample standard deviation so that every variance becomes 11, is one particular choice of DD and requires every feature variance to be positive. It answers a different question about the data, and it is not an automatic correction.

In plain words Subtracting the mean and choosing units are separate decisions, and each can change which direction counts as most variable. State both before reading meaning into a component.

Example The observations (10,1)(10, 1) and (10,−1)(10, -1) have mean (10,0)(10, 0) and centered rows (0,1)(0, 1) and (0,−1)(0, -1). With N−1=1N - 1 = 1, the covariance matrix is diag⁡(0,2)\operatorname{diag}(0, 2), so the first principal direction is e2\mathbf{e}_2 and it keeps all the variance. Without centering, 12X⊤X=diag⁡(100,1)\tfrac12 X^{\top}X = \operatorname{diag}(100, 1), whose leading direction e1\mathbf{e}_1 only reflects the constant first coordinate 1010. For units, triple the second feature of the rectangle. The centered rows become (2,3)(2, 3), (2,−3)(2, -3), (−2,3)(-2, 3) and (−2,−3)(-2, -3), the covariance matrix becomes diag⁡(163,12)\operatorname{diag}(\tfrac{16}{3}, 12), and the leading direction turns from horizontal to vertical.

Check yourself

  1. Find the feature means, the centered data matrix and the sample covariance matrix of the observations (4,1)(4, 1), (6,5)(6, 5) and (2,3)(2, 3).
Show the answer

The means are (4+6+2)/3=4(4 + 6 + 2)/3 = 4 and (1+5+3)/3=3(1 + 5 + 3)/3 = 3, so xˉ=(4,3)\bar{\mathbf{x}} = (4, 3). Subtracting it from each row gives

Xc=[0−222−20].X_c = \begin{bmatrix} 0 & -2 \\ 2 & 2 \\ -2 & 0 \end{bmatrix}.

The squared values add to 0+4+4=80 + 4 + 4 = 8 in each column, and the products add to 0+4+0=40 + 4 + 0 = 4. With N−1=2N - 1 = 2,

C=[4224].C = \begin{bmatrix} 4 & 2 \\ 2 & 4 \end{bmatrix}.
  1. For the same data, find the first principal direction, the fraction of variance it retains, the reconstructions of the three observations from one component, and the reconstruction error.
Show the answer

The covariance matrix sends (1,1)(1, 1) to (6,6)(6, 6) and (1,−1)(1, -1) to (2,−2)(2, -2), so its eigenvalues are 66 and 22, and the first principal direction is w1=(1,1)/2\mathbf{w}_1 = (1, 1)/\sqrt{2}. It retains 6/(6+2)=346/(6 + 2) = \tfrac34, or 7575 percent, of the total variance. The scores w1⋅(xi−xˉ)\mathbf{w}_1 \cdot (\mathbf{x}_i - \bar{\mathbf{x}}) are −2-\sqrt{2}, 222\sqrt{2} and −2-\sqrt{2}, so the centered reconstructions are (−1,−1)(-1, -1), (2,2)(2, 2) and (−1,−1)(-1, -1). Adding the mean back gives (3,2)(3, 2), (6,5)(6, 5) and (3,2)(3, 2). The residuals are (1,−1)(1, -1), (0,0)(0, 0) and (−1,1)(-1, 1), with squared lengths adding to 44, which matches (N−1)λ2=2⋅2(N - 1)\lambda_2 = 2 \cdot 2.

  1. The centered observations (2,1)(2, 1), (−2,−1)(-2, -1), (1,2)(1, 2) and (−1,−2)(-1, -2) have equal feature variances. A student concludes that every direction retains the same variance, so no principal direction is preferred. Check the claim.
Show the answer

The squared values add to 4+4+1+1=104 + 4 + 1 + 1 = 10 in each column, but the products add to 2+2+2+2=82 + 2 + 2 + 2 = 8, so

C=13[108810].C = \frac{1}{3}\begin{bmatrix} 10 & 8 \\ 8 & 10 \end{bmatrix}.

The variances are both 103\tfrac{10}{3}, yet CC sends (1,1)(1, 1) to (6,6)(6, 6) and (1,−1)(1, -1) to (23,−23)(\tfrac23, -\tfrac23). The eigenvalues are 66 and 23\tfrac23, so the direction (1,1)/2(1, 1)/\sqrt{2} retains variance 66, which is 9090 percent of the total 203\tfrac{20}{3}, while the horizontal direction retains only 103\tfrac{10}{3}. The claim is false. Every direction retains the same variance only when CC is a multiple of the identity, as for the square with corners (±1,±1)(\pm 1, \pm 1), where C=43IC = \tfrac43 I. Equal diagonal entries are not enough, because the covariance off the diagonal also matters.

How this lesson connects

Where this leads in Linear Algebra II