Unit 8, lesson 58

Covariance, Eigenvectors, and PCA

How centering and the sample covariance matrix lead to principal component analysis, why a principal direction is an eigenvector with the largest eigenvalue, and how projected variance, units, sign and ties enter.

  • Video 58
  • Principal Components and Dimensionality Reduction

Two sensors on the same machine tend to rise and fall together, so a pair of their readings carries less separate information than two numbers suggest. Along which single direction in the plane of readings do the data vary the most? Principal component analysis answers with an eigenvector of the covariance matrix of the centered data. This lesson centers a small constructed data set, builds its sample covariance matrix, defines eigenvectors and eigenvalues, and shows why the direction of largest projected variance is an eigenvector with the largest eigenvalue.

Key terms

Centering

Definition Let XX be an n×pn \times p data matrix whose rows x1⊤,…,xn⊤\mathbf{x}_1^{\top}, \dots, \mathbf{x}_n^{\top} are observations and whose columns are features. The mean vector is xˉ=1n∑i=1nxi\bar{\mathbf{x}} = \frac{1}{n} \sum_{i=1}^{n} \mathbf{x}_i, and centering replaces each row xi\mathbf{x}_i by xi−xˉ\mathbf{x}_i - \bar{\mathbf{x}}. The centered matrix XcX_c has column sums 0. For every vector v\mathbf{v} in Rp\mathbb{R}^p,

∑i=1n(xi⊤v)2=∑i=1n((xi−xˉ)⊤v)2+n (xˉ⊤v)2,\begin{aligned} &\sum_{i=1}^{n} (\mathbf{x}_i^{\top}\mathbf{v})^2 \\[0.4em] &\quad = \sum_{i=1}^{n} \bigl((\mathbf{x}_i - \bar{\mathbf{x}})^{\top}\mathbf{v}\bigr)^2 \\[0.4em] &\qquad + n\,(\bar{\mathbf{x}}^{\top}\mathbf{v})^2 , \end{aligned}

so squared projections of uncentered rows mix the spread of the data with the position of the mean. In a predictive pipeline xˉ\bar{\mathbf{x}} is computed on the training rows only, and the same xˉ\bar{\mathbf{x}} is subtracted from validation, test and new rows. Centering is distinct from standardizing, which also divides each centered feature by its standard deviation, and a PCA should state which of the two was done.

In plain words Centering moves the origin to the middle of the cloud of points, so what remains is variation around the average rather than the location of the average. Without it, the direction with the largest squared projections tends to point toward the mean instead of along the spread.

Example Three constructed observations of two features on a common dimensionless scale are (1,1)(1, 1), (2,2)(2, 2) and (3,3)(3, 3). Their mean is (2,2)(2, 2), and the centered rows are (−1,−1)(-1, -1), (0,0)(0, 0) and (1,1)(1, 1). On this page the features are centered and not standardized unless a question says otherwise. To see what skipping the centering does, take the points (4,−1)(4, -1), (4,0)(4, 0) and (4,1)(4, 1), which vary only in the second feature. Their sum of squared projections is 48 along (1,0)(1, 0) and only 2 along (0,1)(0, 1), because the mean (4,0)(4, 0) lies far out along the first axis. After centering, the rows (0,−1)(0, -1), (0,0)(0, 0) and (0,1)(0, 1) vary along (0,1)(0, 1) alone.

Sample covariance matrix

Definition For n≥2n \ge 2 rows, the sample covariance matrix is the p×pp \times p matrix

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

Its diagonal entry CjjC_{jj} is the sample variance of feature jj, and its entry CjkC_{jk} off the diagonal is the sample covariance of features jj and kk, the sum of the products (xij−xˉj)(xik−xˉk)(x_{ij} - \bar{x}_j)(x_{ik} - \bar{x}_k) over the rows divided by n−1n - 1. The matrix is symmetric, and it is positive semidefinite, because for every v\mathbf{v}

v⊤C v=∥Xcv∥22n−1≥0.\mathbf{v}^{\top} C\, \mathbf{v} = \frac{\lVert X_c \mathbf{v} \rVert_2^2}{n - 1} \ge 0 .

Dividing by nn instead of n−1n - 1 multiplies every entry by (n−1)/n(n - 1)/n, which rescales all eigenvalues by that factor and leaves every eigenvector unchanged. Each entry carries the product of the units of its two features. When the rows are independent draws from one distribution with finite variances, CC is an unbiased estimate of that distribution’s covariance matrix Σ\Sigma. The matrix CC is computed from the sample, while Σ\Sigma belongs to the population.

In plain words The covariance matrix is a table of how each feature varies and how each pair of features varies together, computed from data as the sample counterpart of the covariance matrix of a random vector. A positive entry off the diagonal says that the two features tend to sit on the same side of their means. It describes a linear association in these data and says nothing about cause.

Example For the centered rows (−1,−1)(-1, -1), (0,0)(0, 0) and (1,1)(1, 1), each feature’s squared deviations sum to 1+0+1=21 + 0 + 1 = 2, the cross products also sum to 2, and n−1=2n - 1 = 2, so

C=[1111].C = \begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix} .

Both variances are 1 and the covariance is 1, so the correlation is 1, as it must be for points on a rising line.

Eigenvector and eigenvalue

Definition Let AA be a p×pp \times p matrix. A vector v\mathbf{v} is an eigenvector of AA with eigenvalue λ\lambda when

Av=λv,v≠0.A\mathbf{v} = \lambda \mathbf{v}, \qquad \mathbf{v} \ne \mathbf{0} .

The eigenvalues are the roots of det⁡(A−λI)=0\det(A - \lambda I) = 0. Every nonzero multiple of an eigenvector is again an eigenvector with the same eigenvalue, so eigenvectors are usually scaled to length 1, which still leaves a choice of sign. For a real symmetric matrix the spectral theorem gives real eigenvalues λ1≥λ2≥⋯≥λp\lambda_1 \ge \lambda_2 \ge \dots \ge \lambda_p and an orthonormal basis e1,…,ep\mathbf{e}_1, \dots, \mathbf{e}_p of eigenvectors, so that

A=∑j=1pλj ejej⊤.A = \sum_{j=1}^{p} \lambda_j\, \mathbf{e}_j \mathbf{e}_j^{\top} .

For a covariance matrix every eigenvalue is at least 0, since λ=e⊤C e≥0\lambda = \mathbf{e}^{\top} C\, \mathbf{e} \ge 0 for a unit eigenvector e\mathbf{e}. When an eigenvalue of a symmetric matrix is repeated rr times, its eigenvectors together with 0\mathbf{0} form a subspace of dimension rr, its eigenspace, and every orthonormal basis of that subspace serves equally well.

In plain words An eigenvector is a direction that the matrix only stretches, shrinks or reverses, without turning it, and the eigenvalue is the stretch factor. An eigenvalue of 0 is allowed. The matrix then sends that direction to the zero vector.

Example For the covariance matrix of the three points,

C[11]=[22]=2[11],C[1−1]=[00],\begin{gathered} C \begin{bmatrix} 1 \\ 1 \end{bmatrix} = \begin{bmatrix} 2 \\ 2 \end{bmatrix} = 2 \begin{bmatrix} 1 \\ 1 \end{bmatrix}, \\[0.4em] C \begin{bmatrix} 1 \\ -1 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix} , \end{gathered}

so (1,1)(1, 1) is an eigenvector with eigenvalue 2 and (1,−1)(1, -1) is an eigenvector with eigenvalue 0. The characteristic equation (1−λ)2−1=λ(λ−2)=0(1 - \lambda)^2 - 1 = \lambda(\lambda - 2) = 0 confirms that there are no other eigenvalues. The unit eigenvectors (1,1)/2(1, 1)/\sqrt{2} and (1,−1)/2(1, -1)/\sqrt{2} are orthogonal, and −(1,1)/2-(1, 1)/\sqrt{2} would serve as well as (1,1)/2(1, 1)/\sqrt{2}. At the other extreme, the identity matrix has every nonzero vector as an eigenvector with eigenvalue 1.

Projected variance

Definition Let v\mathbf{v} be a unit vector, ∥v∥2=1\lVert \mathbf{v} \rVert_2 = 1. The projection score of row ii along v\mathbf{v} is zi=(xi−xˉ)⊤vz_i = (\mathbf{x}_i - \bar{\mathbf{x}})^{\top}\mathbf{v}, the signed coordinate of the centered row along the line through the origin in direction v\mathbf{v}. The scores have mean 0, and their sample variance, the projected variance along v\mathbf{v}, is

1n−1∑i=1nzi2=∥Xcv∥22n−1=v⊤C v.\begin{aligned} \frac{1}{n - 1} \sum_{i=1}^{n} z_i^2 &= \frac{\lVert X_c \mathbf{v} \rVert_2^2}{n - 1} \\[0.4em] &= \mathbf{v}^{\top} C\, \mathbf{v} . \end{aligned}

The unit length matters. Replacing v\mathbf{v} by cvc\mathbf{v} multiplies every score by cc and the variance by c2c^2, so without the constraint the variance would have no maximum. Writing v=a1e1+⋯+apep\mathbf{v} = a_1 \mathbf{e}_1 + \dots + a_p \mathbf{e}_p in an orthonormal eigenvector basis of CC, with a12+⋯+ap2=1a_1^2 + \dots + a_p^2 = 1, gives

v⊤C v=∑j=1pλjaj2,\mathbf{v}^{\top} C\, \mathbf{v} = \sum_{j=1}^{p} \lambda_j a_j^2 ,

a weighted average of the eigenvalues with the weights aj2a_j^2.

In plain words Drop each centered point perpendicularly onto a line through the origin and record where it lands. The spread of those landing positions is the projected variance. It changes as the line turns, and the quadratic form v⊤C v\mathbf{v}^{\top} C\, \mathbf{v} gives it for every direction at once from the covariance matrix.

Example For the three points, take v=(cos⁡θ,sin⁡θ)\mathbf{v} = (\cos\theta, \sin\theta). Then

v⊤C v=(cos⁡θ+sin⁡θ)2=1+sin⁡2θ.\begin{aligned} \mathbf{v}^{\top} C\, \mathbf{v} &= (\cos\theta + \sin\theta)^2 \\[0.4em] &= 1 + \sin 2\theta . \end{aligned}

Along the first feature axis, θ=0\theta = 0, the scores are −1-1, 0 and 1, and the projected variance is 1. At θ=45∘\theta = 45^\circ it reaches its largest value 2, and at θ=135∘\theta = 135^\circ, the direction (−1,1)/2(-1, 1)/\sqrt{2}, it is 0, because every centered point projects onto the origin.

Principal direction

Definition Let CC be a sample covariance matrix with eigenvalues λ1≥λ2≥⋯≥λp≥0\lambda_1 \ge \lambda_2 \ge \dots \ge \lambda_p \ge 0. A first principal direction is a unit vector v1\mathbf{v}_1 that maximizes v⊤C v\mathbf{v}^{\top} C\, \mathbf{v} over all unit vectors v\mathbf{v}. Since the projected variance is a weighted average of eigenvalues,

v⊤C v=∑j=1pλjaj2≤λ1,\mathbf{v}^{\top} C\, \mathbf{v} = \sum_{j=1}^{p} \lambda_j a_j^2 \le \lambda_1 ,

the maximum equals λ1\lambda_1, and it is attained exactly at the unit vectors of the eigenspace of λ1\lambda_1. The Lagrangian v⊤C v−λ(v⊤v−1)\mathbf{v}^{\top} C\, \mathbf{v} - \lambda(\mathbf{v}^{\top}\mathbf{v} - 1) leads to the same answer. Its gradient with respect to v\mathbf{v} is 2Cv−2λv2C\mathbf{v} - 2\lambda\mathbf{v} for symmetric CC, which vanishes exactly when Cv=λvC\mathbf{v} = \lambda\mathbf{v}, so every constrained stationary point is a unit eigenvector, with projected variance equal to its eigenvalue. For k=2,…,pk = 2, \dots, p, a kkth principal direction maximizes v⊤C v\mathbf{v}^{\top} C\, \mathbf{v} over unit vectors orthogonal to v1,…,vk−1\mathbf{v}_1, \dots, \mathbf{v}_{k-1}. It can be taken as a unit eigenvector for λk\lambda_k, and the maximum is λk\lambda_k. The principal directions are mutually orthogonal. Each is fixed only up to sign, and when an eigenvalue is repeated only the subspace spanned by its directions is fixed.

In plain words The first principal direction points along the line through the mean on which the cloud of points is most spread out. The next is the most spread out direction among those perpendicular to the first, and so on. Reversing a direction changes nothing that matters, because all its scores reverse too, so each product of score and direction stays the same.

Example For the three points, λ1=2\lambda_1 = 2 and λ2=0\lambda_2 = 0, so v1=(1,1)/2\mathbf{v}_1 = (1, 1)/\sqrt{2}, or its negative, and v2=(1,−1)/2\mathbf{v}_2 = (1, -1)/\sqrt{2}. The scores along v1\mathbf{v}_1 are −2-\sqrt{2}, 0 and 2\sqrt{2}. Their squares sum to 4, so the projected variance is 4/2=2=λ14/2 = 2 = \lambda_1. All scores along v2\mathbf{v}_2 are 0. Adding score times direction back to the mean recovers each point, for example

(2,2)+(−2) (1,1)2=(1,1),\begin{aligned} &(2, 2) + (-\sqrt{2}) \, \frac{(1, 1)}{\sqrt{2}} \\[0.4em] &\quad = (1, 1), \end{aligned}

because the centered points lie on one line. With −v1-\mathbf{v}_1 the first score becomes 2\sqrt{2} and the product is unchanged.

Principal component analysis

Definition Principal component analysis (PCA) takes an n×pn \times p training matrix with n≥2n \ge 2 and a number of components qq with 1≤q≤p1 \le q \le p, and computes every statistic from the training rows only.

  1. Compute the training mean xˉ\bar{\mathbf{x}} and center every row with it.
  2. If the features are to be standardized, divide each centered feature by its training standard deviation, which must be positive.
  3. Form CC from the prepared training rows and find orthonormal eigenvectors v1,…,vq\mathbf{v}_1, \dots, \mathbf{v}_q for its qq largest eigenvalues.
  4. Give any row x\mathbf{x}, from training or new, the scores zj=x~⊤vjz_j = \tilde{\mathbf{x}}^{\top}\mathbf{v}_j for j=1,…,qj = 1, \dots, q, where x~\tilde{\mathbf{x}} is x\mathbf{x} prepared by steps 1 and 2 with the training statistics.

The scores of the training rows have sample variances λ1,…,λq\lambda_1, \dots, \lambda_q and are uncorrelated, since vj⊤C vk=λkvj⊤vk=0\mathbf{v}_j^{\top} C\, \mathbf{v}_k = \lambda_k \mathbf{v}_j^{\top}\mathbf{v}_k = 0 for j≠kj \ne k. For PCA on centered features without standardization, the reconstruction of x\mathbf{x} from its qq scores is the point x^=xˉ+z1v1+⋯+zqvq\hat{\mathbf{x}} = \bar{\mathbf{x}} + z_1 \mathbf{v}_1 + \dots + z_q \mathbf{v}_q. PCA uses no target values, so it is an unsupervised method. Its directions change with the units of the features unless the features are standardized, each direction is fixed only up to sign, and when λq=λq+1\lambda_q = \lambda_{q+1} the choice of the qqth direction is not unique.

In plain words PCA replaces pp correlated features by a few new coordinates along the directions of greatest spread. Large variance is not the same as importance. A high-variance direction can be noise or an effect of the units, and a low-variance direction can carry the signal for a target that PCA never sees. Fitting the mean, the scales and the directions on all rows, test rows included, leaks information into an evaluation even though no labels are used.

Example PCA with q=1q = 1 fitted on the three points has xˉ=(2,2)\bar{\mathbf{x}} = (2, 2) and v1=(1,1)/2\mathbf{v}_1 = (1, 1)/\sqrt{2}. A new point (1,3)(1, 3) lies off the fitted line. Centered with the training mean it becomes (−1,1)(-1, 1), and its score is (−1+1)/2=0(-1 + 1)/\sqrt{2} = 0. Its one-component reconstruction is therefore the mean (2,2)(2, 2), and the discarded part (−1,1)(-1, 1) has squared length 2. A score of 0 says only that the point projects onto the mean along v1\mathbf{v}_1, not that it equals the mean. Its whole deviation lies along the discarded direction v2\mathbf{v}_2, where its score is −2-\sqrt{2}, and whether that deviation is noise, an anomaly or useful information is a question PCA cannot answer.

Check yourself

  1. Use the fitted mean (2,2)(2, 2) and first principal direction v1=(1,1)/2\mathbf{v}_1 = (1, 1)/\sqrt{2} of this lesson. Find the score, the one-component reconstruction and the squared length of the discarded part for the new point (4,2)(4, 2). What changes if the software reports −v1-\mathbf{v}_1 instead?
Show the answer

Centering with the training mean gives (4,2)−(2,2)=(2,0)(4, 2) - (2, 2) = (2, 0). The score is

z1=2+02=2.z_1 = \frac{2 + 0}{\sqrt{2}} = \sqrt{2} .

The reconstruction is (2,2)+2⋅(1,1)/2=(3,3)(2, 2) + \sqrt{2} \cdot (1, 1)/\sqrt{2} = (3, 3), so the discarded part is (4,2)−(3,3)=(1,−1)(4, 2) - (3, 3) = (1, -1), with squared length 1+1=21 + 1 = 2. With −v1-\mathbf{v}_1 the score becomes −2-\sqrt{2}, but the product of score and direction is again (1,1)(1, 1), so the reconstruction (3,3)(3, 3) and the discarded part stay the same. Only the sign convention of the coordinate changes.

  1. Centered training data with two features on a common scale have the sample covariance matrix
C=[5222].C = \begin{bmatrix} 5 & 2 \\ 2 & 2 \end{bmatrix} .

Show that (2,1)(2, 1) is an eigenvector, find both eigenvalues and the first principal direction, and compare the projected variances along (1,0)(1, 0), along (1,1)/2(1, 1)/\sqrt{2} and along the first principal direction.

Show the answer

The product C(2,1)C(2, 1) is (10+2,4+2)=(12,6)=6 (2,1)(10 + 2, 4 + 2) = (12, 6) = 6\,(2, 1), so (2,1)(2, 1) is an eigenvector with eigenvalue 6. The characteristic equation is (5−λ)(2−λ)−4=λ2−7λ+6=0(5 - \lambda)(2 - \lambda) - 4 = \lambda^2 - 7\lambda + 6 = 0, with roots 6 and 1. As a check, C(1,−2)=(1,−2)C(1, -2) = (1, -2), so (1,−2)(1, -2) is an eigenvector with eigenvalue 1, orthogonal to (2,1)(2, 1). The first principal direction is v1=(2,1)/5\mathbf{v}_1 = (2, 1)/\sqrt{5}, or its negative.

For a unit vector (a,b)(a, b) the projected variance is 5a2+2⋅2ab+2b25a^2 + 2 \cdot 2ab + 2b^2. It is C11=5C_{11} = 5 along (1,0)(1, 0), and (5+4+2)/2=5.5(5 + 4 + 2)/2 = 5.5 along (1,1)/2(1, 1)/\sqrt{2}. Along v1\mathbf{v}_1, with (a,b)=(2,1)/5(a, b) = (2, 1)/\sqrt{5}, it is

v1⊤C v1=20+8+25=6=λ1.\begin{aligned} \mathbf{v}_1^{\top} C\, \mathbf{v}_1 &= \frac{20 + 8 + 2}{5} \\[0.4em] &= 6 = \lambda_1 . \end{aligned}

All three values are at most 6, as the weighted-average bound requires. The feature with the larger variance does not give the principal direction by itself, because the positive covariance tilts the direction of greatest spread toward the second feature.

  1. Four centered training points are (2,0)(2, 0), (−2,0)(-2, 0), (0,1)(0, 1) and (0,−1)(0, -1), with both features in meters. Find CC and the first principal direction. Then record the second feature in centimeters and repeat. Finally standardize the features with their training standard deviations. What does PCA conclude in each case?
Show the answer

In meters, the first feature’s squared values sum to 8 and the second’s to 2, the cross products are all 0, and n−1=3n - 1 = 3, so

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

The eigenvalues are 8/38/3 and 2/32/3, and the first principal direction is (1,0)(1, 0), the first feature. In centimeters the second feature’s values become 0, 0, 100 and −100-100, its variance becomes 20000/3≈6666.720000/3 \approx 6666.7, and the first principal direction switches to (0,1)(0, 1). No point moved, only a unit changed.

Standardizing divides the first feature by 8/3≈1.633\sqrt{8/3} \approx 1.633 and the second by its own standard deviation in whichever unit it was recorded. Both standardized features then have variance 1 and covariance 0 in either unit, so CC is the identity matrix. Every unit vector has projected variance 1, the eigenvalue 1 is repeated, and PCA has no preferred direction at all. Software would still return some orthonormal pair, which reflects its arithmetic and not the data. PCA cannot decide which variation should count. That choice of units or standardization belongs to the analyst, and the standard deviations used for it come from the training data.

How this lesson connects