Practice / Linear algebra

PCA and the covariance matrix

Ten problems on principal component analysis worked on four points in the plane: centring and the covariance matrix, its eigen-decomposition, the variance along a direction and why the top eigenvector maximises it, total and explained variance, scores and reconstructions, the reconstruction error as the discarded eigenvalues, why no other subspace does better, PCA from the SVD, decorrelated and whitened scores, and what feature scaling does to the components, with worked solutions and the mistakes that skip the centring, read directions from the wrong singular vectors or take eigenvalues in the wrong order.

Before you start

Principal component analysis is the eigen-decomposition of a covariance matrix, read as geometry: the eigenvectors are the directions along which the data spread most, the eigenvalues are the variances along them, and keeping the top few directions is the projection with the least squared error among all projections of that dimension. Every property of PCA that gets quoted, that the components are uncorrelated, that the explained variance ratios sum to one, that the reconstruction error is the sum of the discarded eigenvalues, that the SVD computes the same thing, is a short calculation with a symmetric matrix. These ten problems do each calculation on four points in the plane, where every number is exact, and prove the general statement alongside: the covariance and its decomposition, the variance along a direction, total and explained variance, scores and reconstructions, the error and its optimality, the SVD route, whitening, and what happens when one feature is rescaled. The five mistakes at the end are the ones that produce components that look fine: the mean left in, directions read from the left singular vectors, explained variance computed from unsquared singular values, reconstructions without the mean added back, and eigenvectors taken in the order the library returns them.

  • Data are X∈RN×dX \in \mathbb{R}^{N\times d} with rows xn⊤x_n^\top (examples). The mean is xˉ=1N∑nxn\bar x = \tfrac1N\sum_n x_n, the centred data are Xc=X−1xˉ⊤X_c = X - \mathbf{1}\bar x^\top with rows (xn−xˉ)⊤(x_n - \bar x)^\top, and the covariance is S=1NXc⊤Xc=1N∑n(xn−xˉ)(xn−xˉ)⊤S = \tfrac1N X_c^\top X_c = \tfrac1N\sum_n(x_n - \bar x)(x_n - \bar x)^\top, the variance page's 1NX⊤HX\tfrac1N X^\top HX. This page divides by NN throughout; dividing by N−1N - 1 instead multiplies every eigenvalue by NN−1\tfrac{N}{N-1} and changes nothing else.
  • SS is symmetric and positive semidefinite (the variance page's Problem 6), so by the eigenvalues page S=QΛQ⊤S = Q\Lambda Q^\top with QQ orthogonal, columns q1,…,qdq_1, \dots, q_d (the principal directions), and Λ=diag⁡(λ1,…,λd)\Lambda = \operatorname{diag}(\lambda_1, \dots, \lambda_d) with λ1≥⋯≥λd≥0\lambda_1 \ge \dots \ge \lambda_d \ge 0. QkQ_k is the first kk columns of QQ.
  • The scores of xnx_n on the first kk components are zn=Qk⊤(xn−xˉ)∈Rkz_n = Q_k^\top(x_n - \bar x) \in \mathbb{R}^k, collected as Z=XcQk∈RN×kZ = X_cQ_k \in \mathbb{R}^{N\times k}; the reconstruction is x^n=xˉ+Qkzn\hat x_n = \bar x + Q_kz_n. The least-squares page's projection facts are used: for WW with orthonormal columns, P=WW⊤P = WW^\top satisfies P2=P=P⊤P^2 = P = P^\top.
  • The thin SVD of the centred data is Xc=UΣV⊤X_c = U\Sigma V^\top with U⊤U=V⊤V=IU^\top U = V^\top V = I and singular values σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \dots \ge 0 on the diagonal of Σ\Sigma (the least-squares page).
  • The four points used throughout are (5,3)(5, 3), (3,5)(3, 5), (−1,1)(-1, 1) and (1,−1)(1, -1), the rows of X∈R4×2X \in \mathbb{R}^{4\times 2}, so N=4N = 4 and d=2d = 2.
  • tr⁡\operatorname{tr} is the trace, with tr⁡(AB)=tr⁡(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA) and tr⁡(vv⊤)=v⊤v=∥v∥2\operatorname{tr}(vv^\top) = v^\top v = \|v\|^2; 1\mathbf{1} is the all-ones vector and e1=(1,0)⊤e_1 = (1, 0)^\top.

Builds on: Eigenvalues and eigenvectors by hand, Least squares, projections and the SVD

Problems

  1. ·

    Compute xˉ\bar x, the centred data XcX_c and the covariance SS for the four points.

  2. ·

    Find the eigenvalues and unit eigenvectors of SS and write S=QΛQ⊤S = Q\Lambda Q^\top.

  3. ··

    For a unit vector uu, show that the variance of the projected data u⊤(xn−xˉ)u^\top(x_n - \bar x) is u⊤Suu^\top Su, and show with a Lagrange multiplier that it is largest at u=q1u = q_1, where it equals λ1\lambda_1. Evaluate it for the four points at u=q1u = q_1 and at u=e1u = e_1.

  4. ··

    Show that tr⁡S=∑jλj=1N∑n∥xn−xˉ∥2\operatorname{tr}S = \sum_j\lambda_j = \tfrac1N\sum_n\|x_n - \bar x\|^2, and compute the fraction of the total variance carried by the first component for the four points.

  5. ··

    Compute the scores zn=q1⊤(xn−xˉ)z_n = q_1^\top(x_n - \bar x) and the one-component reconstructions x^n=xˉ+znq1\hat x_n = \bar x + z_nq_1 for the four points, and show that x^n−xˉ=q1q1⊤(xn−xˉ)\hat x_n - \bar x = q_1q_1^\top(x_n - \bar x) is a projection of the centred point.

  6. ··

    Show that the mean squared reconstruction error with kk components, 1N∑n∥xn−x^n∥2\tfrac1N\sum_n\|x_n - \hat x_n\|^2, equals ∑j>kλj\sum_{j > k}\lambda_j, and compute it for the four points with k=1k = 1.

  7. ···

    Let W∈Rd×kW \in \mathbb{R}^{d\times k} have orthonormal columns and reconstruct by x^n=xˉ+WW⊤(xn−xˉ)\hat x_n = \bar x + WW^\top(x_n - \bar x). Show that the mean squared error is tr⁡S−tr⁡(W⊤SW)\operatorname{tr}S - \operatorname{tr}(W^\top SW), and that tr⁡(W⊤SW)≤∑j≤kλj\operatorname{tr}(W^\top SW) \le \sum_{j \le k}\lambda_j with equality when the columns of WW span q1,…,qkq_1, \dots, q_k. Conclude that the PCA subspace minimises the error.

  8. ··

    Let Xc=UΣV⊤X_c = U\Sigma V^\top. Show that S=1NVΣ2V⊤S = \tfrac1N V\Sigma^2V^\top, so the principal directions are the right singular vectors with λj=σj2/N\lambda_j = \sigma_j^2/N, and that the scores are Z=XcV=UΣZ = X_cV = U\Sigma. Find the singular values of XcX_c for the four points.

  9. ··

    Let Z=XcQZ = X_cQ be the full N×dN\times d matrix of scores. Show that its columns have mean zero and that 1NZ⊤Z=Λ\tfrac1N Z^\top Z = \Lambda, so the scores are uncorrelated with variances λj\lambda_j, and that W=ZΛ−1/2W = Z\Lambda^{-1/2} satisfies 1NW⊤W=I\tfrac1N W^\top W = I when every λj>0\lambda_j > 0.

  10. ···

    Scale the second feature by c=10c = 10: X′=XDX' = XD with D=diag⁡(1,10)D = \operatorname{diag}(1, 10). Show that S′=DSDS' = DSD, find its eigenvalues and top eigenvector to three decimal places, and show that the correlation matrix R=DS−1/2SDS−1/2R = D_S^{-1/2}SD_S^{-1/2}, with DS=diag⁡(S11,S22)D_S = \operatorname{diag}(S_{11}, S_{22}), is unchanged by the scaling. Find RR and its eigen-decomposition for the four points.

Worked solutions

Problem 1

Compute xˉ\bar x, the centred data XcX_c and the covariance SS for the four points.

  1. xˉ=14((5,3)+(3,5)+(−1,1)+(1,−1))⊤=14(8,8)⊤=(2,2)⊤\bar x = \tfrac14\big((5, 3) + (3, 5) + (-1, 1) + (1, -1)\big)^\top = \tfrac14(8, 8)^\top = (2, 2)^\top.Average each coordinate over the four points.
  2. The centred rows are (3,1)(3, 1), (1,3)(1, 3), (−3,−1)(-3, -1) and (−1,−3)(-1, -3).Subtract xˉ⊤\bar x^\top from each row; they sum to zero, as centred rows must.
  3. Xc⊤Xc=(9+1+9+13+3+3+3121+9+1+9)=(20121220)X_c^\top X_c = \begin{pmatrix} 9 + 1 + 9 + 1 & 3 + 3 + 3 + 3 \\ 12 & 1 + 9 + 1 + 9\end{pmatrix} = \begin{pmatrix} 20 & 12 \\ 12 & 20\end{pmatrix}.M⊤MM^\top M sums the outer products of the rows of MM: the (1,1)(1, 1) entry is ∑n(xn1−xˉ1)2\sum_n (x_{n1} - \bar x_1)^2 and the (1,2)(1, 2) entry ∑n(xn1−xˉ1)(xn2−xˉ2)\sum_n (x_{n1} - \bar x_1)(x_{n2} - \bar x_2).
  4. S=14(20121220)=(5335)S = \tfrac14\begin{pmatrix} 20 & 12 \\ 12 & 20\end{pmatrix} = \begin{pmatrix} 5 & 3 \\ 3 & 5\end{pmatrix}.Divide by N=4N = 4.
  5. xˉ=(2,2)⊤\bar x = (2, 2)^\top; XcX_c has rows (3,1)(3, 1), (1,3)(1, 3), (−3,−1)(-3, -1), (−1,−3)(-1, -3); S=(5335)S = \begin{pmatrix} 5 & 3 \\ 3 & 5\end{pmatrix}S11=5S_{11} = 5 is the variance of the first coordinate, S12=3S_{12} = 3 the covariance, and the correlation is 3/5=0.63/5 = 0.6 (Problem 10). This is np.cov(X.T, bias=True); without bias=True every entry is 43\tfrac43 as large. Leaving the mean in is Mistake 1.

Problem 2

Find the eigenvalues and unit eigenvectors of SS and write S=QΛQ⊤S = Q\Lambda Q^\top.

  1. det⁡(S−λI)=(5−λ)2−9=0\det(S - \lambda I) = (5 - \lambda)^2 - 9 = 0, so 5−λ=±35 - \lambda = \pm 3 and λ1=8\lambda_1 = 8, λ2=2\lambda_2 = 2.The characteristic polynomial of a 2×22\times 2 matrix, as on the eigenvalues page.
  2. λ1=8\lambda_1 = 8: (S−8I)v=(−333−3)v=0(S - 8I)v = \begin{pmatrix} -3 & 3 \\ 3 & -3\end{pmatrix}v = 0 gives v1=v2v_1 = v_2, so q1=12(1,1)⊤q_1 = \tfrac{1}{\sqrt2}(1, 1)^\top.Solve the homogeneous system and normalise.
  3. λ2=2\lambda_2 = 2: (3333)v=0\begin{pmatrix} 3 & 3 \\ 3 & 3\end{pmatrix}v = 0 gives v2=−v1v_2 = -v_1, so q2=12(1,−1)⊤q_2 = \tfrac{1}{\sqrt2}(1, -1)^\top.Solve and normalise.
  4. QΛQ⊤=12(111−1)(8002)(111−1)=12(828−2)(111−1)=12(106610)=SQ\Lambda Q^\top = \tfrac12\begin{pmatrix} 1 & 1 \\ 1 & -1\end{pmatrix}\begin{pmatrix} 8 & 0 \\ 0 & 2\end{pmatrix}\begin{pmatrix} 1 & 1 \\ 1 & -1\end{pmatrix} = \tfrac12\begin{pmatrix} 8 & 2 \\ 8 & -2\end{pmatrix}\begin{pmatrix} 1 & 1 \\ 1 & -1\end{pmatrix} = \tfrac12\begin{pmatrix} 10 & 6 \\ 6 & 10\end{pmatrix} = S.Multiply out, with Q=12(111−1)Q = \tfrac{1}{\sqrt2}\begin{pmatrix} 1 & 1 \\ 1 & -1\end{pmatrix} and the two factors of 12\tfrac{1}{\sqrt2} combining into 12\tfrac12.
  5. λ1=8\lambda_1 = 8, λ2=2\lambda_2 = 2; q1=12(1,1)⊤q_1 = \tfrac{1}{\sqrt2}(1, 1)^\top, q2=12(1,−1)⊤q_2 = \tfrac{1}{\sqrt2}(1, -1)^\top; S=QΛQ⊤S = Q\Lambda Q^\top with Q=12(111−1)Q = \tfrac{1}{\sqrt2}\begin{pmatrix} 1 & 1 \\ 1 & -1\end{pmatrix} and Λ=diag⁡(8,2)\Lambda = \operatorname{diag}(8, 2)The eigenvectors are orthogonal because SS is symmetric (the eigenvalues page's Problem 8), and each one's sign is a free choice: −q1-q_1 serves as well, which is why the check compares directions up to sign. λ1+λ2=10=tr⁡S\lambda_1 + \lambda_2 = 10 = \operatorname{tr}S (Problem 4). The top direction is the diagonal, along which the four points visibly spread most; taking the eigenvectors in a library's ascending order is Mistake 5.

Problem 3

For a unit vector uu, show that the variance of the projected data u⊤(xn−xˉ)u^\top(x_n - \bar x) is u⊤Suu^\top Su, and show with a Lagrange multiplier that it is largest at u=q1u = q_1, where it equals λ1\lambda_1. Evaluate it for the four points at u=q1u = q_1 and at u=e1u = e_1.

  1. The projections tn=u⊤(xn−xˉ)t_n = u^\top(x_n - \bar x) have mean 00, so their variance is 1N∑ntn2=1N∑nu⊤(xn−xˉ)(xn−xˉ)⊤u=u⊤Su\tfrac1N\sum_n t_n^2 = \tfrac1N\sum_n u^\top(x_n - \bar x)(x_n - \bar x)^\top u = u^\top Su.∑n(xn−xˉ)=0\sum_n(x_n - \bar x) = 0; tn2=u⊤(xn−xˉ)(xn−xˉ)⊤ut_n^2 = u^\top(x_n - \bar x)(x_n - \bar x)^\top u; uu comes out of the sum on both sides.
  2. Maximise u⊤Suu^\top Su subject to u⊤u=1u^\top u = 1: L=u⊤Su−λ(u⊤u−1)L = u^\top Su - \lambda(u^\top u - 1) and ∇uL=2Su−2λu=0\nabla_u L = 2Su - 2\lambda u = 0, so Su=λuSu = \lambda u.The Lagrange page; ∇u(u⊤Su)=2Su\nabla_u(u^\top Su) = 2Su for symmetric SS, from the matrix-calculus page.
  3. At an eigenvector u=qju = q_j, u⊤Su=λjqj⊤qj=λju^\top Su = \lambda_j q_j^\top q_j = \lambda_j, and the largest of these is λ1\lambda_1 at q1q_1.The candidates are the unit eigenvectors; compare the values.
  4. Directly: write u=∑jcjqju = \sum_j c_jq_j with ∑jcj2=1\sum_j c_j^2 = 1; then u⊤Su=∑jλjcj2≤λ1∑jcj2=λ1u^\top Su = \sum_j\lambda_jc_j^2 \le \lambda_1\sum_j c_j^2 = \lambda_1.Expand in the orthonormal eigenbasis, use Sqj=λjqjSq_j = \lambda_jq_j and qi⊤qj=0q_i^\top q_j = 0 for i≠ji \ne j; a weighted average of the λj\lambda_j is at most the largest.
  5. For the four points, q1⊤Sq1=8q_1^\top Sq_1 = 8 and e1⊤Se1=S11=5e_1^\top Se_1 = S_{11} = 5.Problem 2; the (1,1)(1, 1) entry of SS.
  6. Var⁡(u⊤(x−xˉ))=u⊤Su≤λ1\operatorname{Var}\big(u^\top(x - \bar x)\big) = u^\top Su \le \lambda_1, with equality at u=q1u = q_1; for the four points it is 88 along q1q_1 and 55 along e1e_1The first principal direction is the direction of greatest variance, and the variance along any unit direction is a convex combination of the eigenvalues weighted by the squared cosines with the eigenvectors. Along the diagonal the four points sit at ±22\pm 2\sqrt2 (Problem 5), with variance 88, more than either raw coordinate's 55. The same argument gives the minimum λd\lambda_d at qdq_d. The variance page's a⊤Σaa^\top\Sigma a is this quadratic form for a population covariance.

Problem 4

Show that tr⁡S=∑jλj=1N∑n∥xn−xˉ∥2\operatorname{tr}S = \sum_j\lambda_j = \tfrac1N\sum_n\|x_n - \bar x\|^2, and compute the fraction of the total variance carried by the first component for the four points.

  1. tr⁡S=tr⁡(QΛQ⊤)=tr⁡(ΛQ⊤Q)=tr⁡Λ=∑jλj\operatorname{tr}S = \operatorname{tr}(Q\Lambda Q^\top) = \operatorname{tr}(\Lambda Q^\top Q) = \operatorname{tr}\Lambda = \sum_j\lambda_j.The cyclic property of the trace, then Q⊤Q=IQ^\top Q = I.
  2. tr⁡S=1N∑ntr⁡((xn−xˉ)(xn−xˉ)⊤)=1N∑n∥xn−xˉ∥2\operatorname{tr}S = \tfrac1N\sum_n\operatorname{tr}\big((x_n - \bar x)(x_n - \bar x)^\top\big) = \tfrac1N\sum_n\|x_n - \bar x\|^2.The trace is linear and tr⁡(vv⊤)=∥v∥2\operatorname{tr}(vv^\top) = \|v\|^2.
  3. tr⁡S=∑jSjj\operatorname{tr}S = \sum_j S_{jj} is also the sum of the per-coordinate variances.The diagonal entries of SS are the variances of the coordinates.
  4. For the four points, tr⁡S=5+5=10=8+2\operatorname{tr}S = 5 + 5 = 10 = 8 + 2, and the first component carries λ1/(λ1+λ2)=8/10\lambda_1/(\lambda_1 + \lambda_2) = 8/10.Problems 1 and 2.
  5. tr⁡S=∑jλj=1N∑n∥xn−xˉ∥2\operatorname{tr}S = \sum_j\lambda_j = \tfrac1N\sum_n\|x_n - \bar x\|^2; for the four points the first component explains 8/10=80%8/10 = 80\% of the varianceThe total variance is the mean squared distance from the mean, which a rotation of the data leaves unchanged, so splitting it over the eigenvalues is meaningful: λj\lambda_j is the variance along qjq_j and the fractions λj/tr⁡S\lambda_j/\operatorname{tr}S sum to 11. In terms of the SVD they are ratios of squared singular values (Problem 8), not of singular values (Mistake 3).

Problem 5

Compute the scores zn=q1⊤(xn−xˉ)z_n = q_1^\top(x_n - \bar x) and the one-component reconstructions x^n=xˉ+znq1\hat x_n = \bar x + z_nq_1 for the four points, and show that x^n−xˉ=q1q1⊤(xn−xˉ)\hat x_n - \bar x = q_1q_1^\top(x_n - \bar x) is a projection of the centred point.

  1. zn=12(1,1)⋅(xn−xˉ)z_n = \tfrac{1}{\sqrt2}(1, 1)\cdot(x_n - \bar x): 3+12=22\tfrac{3 + 1}{\sqrt2} = 2\sqrt2, 1+32=22\tfrac{1 + 3}{\sqrt2} = 2\sqrt2, −3−12=−22\tfrac{-3 - 1}{\sqrt2} = -2\sqrt2 and −22-2\sqrt2.Dot product of q1q_1 with each centred row from Problem 1.
  2. znq1=±22⋅12(1,1)⊤=±(2,2)⊤z_nq_1 = \pm 2\sqrt2\cdot\tfrac{1}{\sqrt2}(1, 1)^\top = \pm(2, 2)^\top.The 2\sqrt2 factors cancel.
  3. x^n=(2,2)⊤+(2,2)⊤=(4,4)⊤\hat x_n = (2, 2)^\top + (2, 2)^\top = (4, 4)^\top for n=1,2n = 1, 2 and (2,2)⊤−(2,2)⊤=(0,0)⊤(2, 2)^\top - (2, 2)^\top = (0, 0)^\top for n=3,4n = 3, 4.Add the mean back.
  4. x^n−xˉ=q1(q1⊤(xn−xˉ))=q1q1⊤(xn−xˉ)\hat x_n - \bar x = q_1\big(q_1^\top(x_n - \bar x)\big) = q_1q_1^\top(x_n - \bar x), and P=q1q1⊤P = q_1q_1^\top has P2=q1(q1⊤q1)q1⊤=PP^2 = q_1(q_1^\top q_1)q_1^\top = P and P⊤=PP^\top = P.Regroup the scalar; q1⊤q1=1q_1^\top q_1 = 1. This is the least-squares page's projection onto the line through q1q_1.
  5. Scores (22,22,−22,−22)(2\sqrt2, 2\sqrt2, -2\sqrt2, -2\sqrt2); reconstructions (4,4)⊤(4, 4)^\top, (4,4)⊤(4, 4)^\top, (0,0)⊤(0, 0)^\top, (0,0)⊤(0, 0)^\top; x^n−xˉ=q1q1⊤(xn−xˉ)\hat x_n - \bar x = q_1q_1^\top(x_n - \bar x) is the orthogonal projection of the centred point onto the line through q1q_1Two points reconstruct to (4,4)(4, 4) and two to (0,0)(0, 0): the one-dimensional summary keeps where each point sits along the diagonal and forgets its ±1\pm 1 offset across it. The scores have mean 00 and variance 14⋅4⋅(22)2=8=λ1\tfrac14\cdot 4\cdot(2\sqrt2)^2 = 8 = \lambda_1 (Problem 9). In matrix form X^=1xˉ⊤+Xcq1q1⊤\hat X = \mathbf{1}\bar x^\top + X_cq_1q_1^\top; dropping the first term is Mistake 4.

Problem 6

Show that the mean squared reconstruction error with kk components, 1N∑n∥xn−x^n∥2\tfrac1N\sum_n\|x_n - \hat x_n\|^2, equals ∑j>kλj\sum_{j > k}\lambda_j, and compute it for the four points with k=1k = 1.

  1. xn−x^n=(xn−xˉ)−QkQk⊤(xn−xˉ)=(I−QkQk⊤)(xn−xˉ)x_n - \hat x_n = (x_n - \bar x) - Q_kQ_k^\top(x_n - \bar x) = (I - Q_kQ_k^\top)(x_n - \bar x).Problem 5's form with kk directions; the xˉ\bar x cancels.
  2. I−QkQk⊤=∑j>kqjqj⊤I - Q_kQ_k^\top = \sum_{j > k}q_jq_j^\top.I=QQ⊤=∑j=1dqjqj⊤I = QQ^\top = \sum_{j=1}^{d}q_jq_j^\top because the qjq_j are an orthonormal basis; subtract the first kk terms.
  3. ∥xn−x^n∥2=∑j>k(qj⊤(xn−xˉ))2\|x_n - \hat x_n\|^2 = \sum_{j > k}\big(q_j^\top(x_n - \bar x)\big)^2.A vector in the span of the orthonormal qjq_j, j>kj > k, has squared length equal to the sum of its squared coefficients, and the coefficients are the dot products.
  4. 1N∑n∥xn−x^n∥2=∑j>k1N∑n(qj⊤(xn−xˉ))2=∑j>kqj⊤Sqj=∑j>kλj\tfrac1N\sum_n\|x_n - \hat x_n\|^2 = \sum_{j > k}\tfrac1N\sum_n\big(q_j^\top(x_n - \bar x)\big)^2 = \sum_{j > k}q_j^\top Sq_j = \sum_{j > k}\lambda_j.Swap the sums; Problem 3, step 1 with u=qju = q_j; then qj⊤Sqj=λjq_j^\top Sq_j = \lambda_j.
  5. For the four points with k=1k = 1 the error is λ2=2\lambda_2 = 2; directly, ∥(5,3)−(4,4)∥2=2\|(5, 3) - (4, 4)\|^2 = 2 and likewise for each point, mean 22.Problem 5's reconstructions.
  6. 1N∑n∥xn−x^n∥2=∑j>kλj\tfrac1N\sum_n\|x_n - \hat x_n\|^2 = \sum_{j > k}\lambda_j; for the four points with k=1k = 1 the error is λ2=2\lambda_2 = 2The variance the kept components carry and the error the dropped ones leave are the two halves of tr⁡S\operatorname{tr}S: 88 kept and 22 lost out of 1010. Choosing kk by a threshold on the explained variance is choosing it by this error. Problem 7 shows that no other kk-dimensional subspace does better.

Problem 7

Let W∈Rd×kW \in \mathbb{R}^{d\times k} have orthonormal columns and reconstruct by x^n=xˉ+WW⊤(xn−xˉ)\hat x_n = \bar x + WW^\top(x_n - \bar x). Show that the mean squared error is tr⁡S−tr⁡(W⊤SW)\operatorname{tr}S - \operatorname{tr}(W^\top SW), and that tr⁡(W⊤SW)≤∑j≤kλj\operatorname{tr}(W^\top SW) \le \sum_{j \le k}\lambda_j with equality when the columns of WW span q1,…,qkq_1, \dots, q_k. Conclude that the PCA subspace minimises the error.

  1. With v=xn−xˉv = x_n - \bar x: ∥(I−WW⊤)v∥2=v⊤(I−WW⊤)v=∥v∥2−∥W⊤v∥2\|(I - WW^\top)v\|^2 = v^\top(I - WW^\top)v = \|v\|^2 - \|W^\top v\|^2.I−WW⊤I - WW^\top is a symmetric projection because W⊤W=IkW^\top W = I_k, and for a symmetric projection ∥Pv∥2=v⊤P⊤Pv=v⊤Pv\|Pv\|^2 = v^\top P^\top Pv = v^\top Pv; then v⊤WW⊤v=∥W⊤v∥2v^\top WW^\top v = \|W^\top v\|^2.
  2. Averaging over nn: error =1N∑n∥xn−xˉ∥2−1N∑n∥W⊤(xn−xˉ)∥2=tr⁡S−tr⁡(W⊤SW)= \tfrac1N\sum_n\|x_n - \bar x\|^2 - \tfrac1N\sum_n\|W^\top(x_n - \bar x)\|^2 = \operatorname{tr}S - \operatorname{tr}(W^\top SW).Problem 4 for the first term; for the second, ∥W⊤v∥2=tr⁡(W⊤vv⊤W)\|W^\top v\|^2 = \operatorname{tr}(W^\top vv^\top W) and the average of vv⊤vv^\top is SS.
  3. tr⁡(W⊤SW)=∑jλj∥W⊤qj∥2=:∑jλjwj\operatorname{tr}(W^\top SW) = \sum_j\lambda_j\|W^\top q_j\|^2 =: \sum_j\lambda_jw_j.Insert S=∑jλjqjqj⊤S = \sum_j\lambda_jq_jq_j^\top and use tr⁡(W⊤qjqj⊤W)=∥W⊤qj∥2\operatorname{tr}(W^\top q_jq_j^\top W) = \|W^\top q_j\|^2.
  4. 0≤wj≤10 \le w_j \le 1 and ∑jwj=k\sum_j w_j = k.∥W⊤qj∥2=qj⊤WW⊤qj=∥WW⊤qj∥2≤∥qj∥2=1\|W^\top q_j\|^2 = q_j^\top WW^\top q_j = \|WW^\top q_j\|^2 \le \|q_j\|^2 = 1 because WW⊤WW^\top is a projection; and ∑jwj=tr⁡(W⊤(∑jqjqj⊤)W)=tr⁡(W⊤W)=tr⁡Ik=k\sum_j w_j = \operatorname{tr}\big(W^\top(\sum_j q_jq_j^\top)W\big) = \operatorname{tr}(W^\top W) = \operatorname{tr}I_k = k.
  5. ∑jλjwj≤∑j≤kλj\sum_j\lambda_jw_j \le \sum_{j \le k}\lambda_j, with equality when wj=1w_j = 1 for j≤kj \le k and 00 otherwise.With the λj\lambda_j sorted and weights in [0,1][0, 1] summing to kk, moving weight from a smaller eigenvalue to a larger one that still has room never decreases the sum, so the maximum puts full weight on the kk largest; wj=1w_j = 1 for j≤kj \le k means each top eigenvector lies in the span of WW's columns, which, there being kk of them, is then exactly that span.
  6. Error(W)=tr⁡S−tr⁡(W⊤SW)≥tr⁡S−∑j≤kλj=∑j>kλj(W) = \operatorname{tr}S - \operatorname{tr}(W^\top SW) \ge \operatorname{tr}S - \sum_{j \le k}\lambda_j = \sum_{j > k}\lambda_j, with equality when the columns of WW span q1,…,qkq_1, \dots, q_kPCA is the kk-dimensional affine subspace of least squared perpendicular distance to the data, the flat-fitting version of least squares, and for k=1k = 1 it is Problem 3 again. Only the span matters: WRWR for any orthogonal k×kk\times k matrix RR gives the same projection and the same error, so the components are defined up to a rotation within the subspace, and when λk=λk+1\lambda_k = \lambda_{k+1} even the subspace is not unique.

Problem 8

Let Xc=UΣV⊤X_c = U\Sigma V^\top. Show that S=1NVΣ2V⊤S = \tfrac1N V\Sigma^2V^\top, so the principal directions are the right singular vectors with λj=σj2/N\lambda_j = \sigma_j^2/N, and that the scores are Z=XcV=UΣZ = X_cV = U\Sigma. Find the singular values of XcX_c for the four points.

  1. Xc⊤Xc=VΣU⊤UΣV⊤=VΣ2V⊤X_c^\top X_c = V\Sigma U^\top U\Sigma V^\top = V\Sigma^2V^\top.Transpose the SVD and multiply; U⊤U=IU^\top U = I.
  2. S=1NXc⊤Xc=V(1NΣ2)V⊤S = \tfrac1N X_c^\top X_c = V\big(\tfrac1N\Sigma^2\big)V^\top, an eigen-decomposition with Q=VQ = V and Λ=Σ2/N\Lambda = \Sigma^2/N.VV is orthogonal and Σ2/N\Sigma^2/N is diagonal with nonincreasing entries, which is the form QΛQ⊤Q\Lambda Q^\top; the decomposition is unique up to the signs of the columns when the eigenvalues are distinct.
  3. Z=XcV=UΣV⊤V=UΣZ = X_cV = U\Sigma V^\top V = U\Sigma.V⊤V=IV^\top V = I.
  4. For the four points, σj=Nλj\sigma_j = \sqrt{N\lambda_j}: σ1=32=42\sigma_1 = \sqrt{32} = 4\sqrt2 and σ2=8=22\sigma_2 = \sqrt8 = 2\sqrt2.Step 2 with N=4N = 4 and Problem 2's eigenvalues.
  5. S=1NVΣ2V⊤S = \tfrac1N V\Sigma^2V^\top: qj=vjq_j = v_j and λj=σj2/N\lambda_j = \sigma_j^2/N; the scores are Z=UΣZ = U\Sigma; for the four points σ1=42\sigma_1 = 4\sqrt2 and σ2=22\sigma_2 = 2\sqrt2The SVD computes PCA without forming SS, which matters numerically because squaring the singular values squares the condition number; it is what sklearn.decomposition.PCA does. The columns of UU are the score vectors scaled to unit length, patterns over the examples rather than directions in feature space, which is Mistake 2. The least-squares page's singular values as square roots of the eigenvalues of A⊤AA^\top A is the same statement without the 1N\tfrac1N.

Problem 9

Let Z=XcQZ = X_cQ be the full N×dN\times d matrix of scores. Show that its columns have mean zero and that 1NZ⊤Z=Λ\tfrac1N Z^\top Z = \Lambda, so the scores are uncorrelated with variances λj\lambda_j, and that W=ZΛ−1/2W = Z\Lambda^{-1/2} satisfies 1NW⊤W=I\tfrac1N W^\top W = I when every λj>0\lambda_j > 0.

  1. 1⊤Z=1⊤XcQ=0\mathbf{1}^\top Z = \mathbf{1}^\top X_cQ = 0.The columns of XcX_c sum to zero, so 1⊤Xc=0\mathbf{1}^\top X_c = 0.
  2. 1NZ⊤Z=1NQ⊤Xc⊤XcQ=Q⊤SQ=Q⊤QΛQ⊤Q=Λ\tfrac1N Z^\top Z = \tfrac1N Q^\top X_c^\top X_cQ = Q^\top SQ = Q^\top Q\Lambda Q^\top Q = \Lambda.S=QΛQ⊤S = Q\Lambda Q^\top and Q⊤Q=IQ^\top Q = I.
  3. 1NW⊤W=Λ−1/2(1NZ⊤Z)Λ−1/2=Λ−1/2ΛΛ−1/2=I\tfrac1N W^\top W = \Lambda^{-1/2}\big(\tfrac1N Z^\top Z\big)\Lambda^{-1/2} = \Lambda^{-1/2}\Lambda\Lambda^{-1/2} = I.Diagonal matrices multiply entry by entry.
  4. 1NZ⊤Z=Λ\tfrac1N Z^\top Z = \Lambda: the scores are uncorrelated, with variance λj\lambda_j along the jj-th component; W=XcQΛ−1/2W = X_cQ\Lambda^{-1/2} has identity covariancePCA rotates the data into coordinates whose covariance is diagonal, and dividing each by its standard deviation λj\sqrt{\lambda_j} is the variance page's whitening with QQ and Λ\Lambda taken from the data. In SVD terms Z=UΣZ = U\Sigma and W=N UW = \sqrt N\,U. Whitening a direction with a tiny λj\lambda_j amplifies whatever noise lives there by 1/λj1/\sqrt{\lambda_j}, so in practice it is done after truncating to kk components or with a small constant added to each λj\lambda_j.

Problem 10

Scale the second feature by c=10c = 10: X′=XDX' = XD with D=diag⁡(1,10)D = \operatorname{diag}(1, 10). Show that S′=DSDS' = DSD, find its eigenvalues and top eigenvector to three decimal places, and show that the correlation matrix R=DS−1/2SDS−1/2R = D_S^{-1/2}SD_S^{-1/2}, with DS=diag⁡(S11,S22)D_S = \operatorname{diag}(S_{11}, S_{22}), is unchanged by the scaling. Find RR and its eigen-decomposition for the four points.

  1. xˉ′=Dxˉ\bar x' = D\bar x and Xc′=XcDX'_c = X_cD, so S′=1NDXc⊤XcD=DSD=(53030500)S' = \tfrac1N DX_c^\top X_cD = DSD = \begin{pmatrix} 5 & 30 \\ 30 & 500\end{pmatrix}.Scaling a column of XX scales the same column of XcX_c; DD is symmetric; S12′=10⋅3S'_{12} = 10\cdot 3 and S22′=100⋅5S'_{22} = 100\cdot 5.
  2. The eigenvalues of S′S' are λ=505±5052−4⋅16002=505±2486252≈501.812\lambda = \dfrac{505 \pm\sqrt{505^2 - 4\cdot 1600}}{2} = \dfrac{505 \pm\sqrt{248625}}{2} \approx 501.812 and 3.1883.188.Trace 505505, determinant 2500−900=16002500 - 900 = 1600, and 248625≈498.623\sqrt{248625} \approx 498.623.
  3. (5−501.812)v1+30v2=0(5 - 501.812)v_1 + 30v_2 = 0 gives v1≈0.0604 v2v_1 \approx 0.0604\,v_2, so q1′≈(0.060,0.998)⊤q_1' \approx (0.060, 0.998)^\top, and the first component now carries 501.81/505≈99.4%501.81/505 \approx 99.4\% of the variance.The first row of (S′−λ1I)v=0(S' - \lambda_1I)v = 0; normalise.
  4. DS′=diag⁡(5,500)=DDSDD_{S'} = \operatorname{diag}(5, 500) = DD_SD, so DS′−1/2=D−1DS−1/2D_{S'}^{-1/2} = D^{-1}D_S^{-1/2} and R′=D−1DS−1/2 DSD DS−1/2D−1=DS−1/2SDS−1/2=RR' = D^{-1}D_S^{-1/2}\,DSD\,D_S^{-1/2}D^{-1} = D_S^{-1/2}SD_S^{-1/2} = R.Diagonal matrices commute, and D−1D=ID^{-1}D = I on each side.
  5. For the four points, DS=diag⁡(5,5)D_S = \operatorname{diag}(5, 5) and R=S/5=(10.60.61)R = S/5 = \begin{pmatrix} 1 & 0.6 \\ 0.6 & 1\end{pmatrix}, with eigenvalues 1.61.6 and 0.40.4 and the eigenvectors q1,q2q_1, q_2 of Problem 2.A positive multiple of a matrix has the same eigenvectors and scaled eigenvalues; here the two variances happen to be equal, so RR is a multiple of SS.
  6. S′=DSD=(53030500)S' = DSD = \begin{pmatrix} 5 & 30 \\ 30 & 500\end{pmatrix} with λ≈501.81,3.19\lambda \approx 501.81, 3.19 and q1′≈(0.060,0.998)⊤q_1' \approx (0.060, 0.998)^\top, against (0.707,0.707)⊤(0.707, 0.707)^\top before scaling; R=(10.60.61)R = \begin{pmatrix} 1 & 0.6 \\ 0.6 & 1\end{pmatrix} is unchanged by any positive rescaling of the features, with eigenvalues 1.61.6 and 0.40.4 and Problem 2's eigenvectorsPCA on the covariance is not scale-invariant: measuring the second feature in different units swings the first direction from the diagonal to almost the second axis, and the 99.4%99.4\% is an artefact of the units. When features are in incommensurable units, standardise first, which is PCA on RR, that is on XcDS−1/2X_cD_S^{-1/2}; when they share a unit, as pixels do, the covariance is the right choice, because the scale carries information.

Where this goes wrong

1. PCA on uncentred data

X⊤XX^\top X is the matrix in every normal equation, and the covariance differs from it only by the centring step.

  1. S=1NXc⊤XcS = \tfrac1N X_c^\top X_cRight so far: Problem 1.
  2. “The mean is a constant offset, so the directions of spread are the same with or without it.”The shortcut that causes the mistake: a constant offset is invisible to a variance but not to a second moment; 1NX⊤X=S+xˉxˉ⊤\tfrac1N X^\top X = S + \bar x\bar x^\top, the variance page's last mistake.
  3. S=1NX⊤XS = \tfrac1N X^\top XWith the four points shifted so that xˉ=(2,12)⊤\bar x = (2, 12)^\top (add 1010 to every second coordinate, which changes no variance), 1NX⊤X=S+xˉxˉ⊤=(92727149)\tfrac1N X^\top X = S + \bar x\bar x^\top = \begin{pmatrix} 9 & 27 \\ 27 & 149\end{pmatrix}, whose top eigenvector makes an angle of 79.5∘79.5^\circ with the first axis, within about 1∘1^\circ of the direction of xˉ\bar x, while the data's spread is along the 45∘45^\circ diagonal. The first component points at the mean, not along the data. On the unshifted points the error hides, because xˉ=(2,2)⊤\bar x = (2, 2)^\top happens to lie along q1q_1.

2. Principal directions read from U instead of V

The SVD has two sets of singular vectors, and the data matrix can be written either way round.

  1. Xc=UΣV⊤X_c = U\Sigma V^\top with U∈RN×dU \in \mathbb{R}^{N\times d} and V∈Rd×dV \in \mathbb{R}^{d\times d}Right so far: Problem 8.
  2. “The first singular vector is the first principal component.”The shortcut that causes the mistake: u1u_1 has one entry per example and v1v_1 one per feature, and a direction in the data space is a vector over the features.
  3. q1=u1q_1 = u_1u1=Xcv1/σ1u_1 = X_cv_1/\sigma_1 is the first column of scores scaled to unit length (Problem 8, Z=UΣZ = U\Sigma): a pattern over the NN examples, not a direction in Rd\mathbb{R}^d. The shapes only agree when N=dN = d, which is when the error produces no exception. With examples as columns, as some texts write, the roles swap and the directions are the left singular vectors, so what has to be checked is which side the examples are on; the least-squares page's mistake of building UU from the eigenvectors of A⊤AA^\top A is the same confusion from the other side.

3. Explained variance from singular values instead of their squares

Singular values are what the SVD prints, and they decrease like the eigenvalues do.

  1. λj=σj2/N\lambda_j = \sigma_j^2/NRight so far: Problem 8.
  2. “The share of component 11 is σ1/∑jσj\sigma_1/\sum_j\sigma_j.”The shortcut that causes the mistake: the share is a ratio of variances, and a variance is a squared singular value.
  3. Share of component 11 =σ1σ1+σ2=4262=23= \dfrac{\sigma_1}{\sigma_1 + \sigma_2} = \dfrac{4\sqrt2}{6\sqrt2} = \dfrac23The variance along q1q_1 is σ12/N\sigma_1^2/N, so the share is σ12/∑jσj2=32/40=0.8\sigma_1^2/\sum_j\sigma_j^2 = 32/40 = 0.8 (Problem 4). The unsquared ratio understates dominant components and overstates weak ones, so a threshold such as 95%95\% applied to it keeps too many components. explained_variance_ratio_ in scikit-learn uses the squares.

4. Reconstruction without the mean added back

The scores are computed from centred data, and decoding looks like the transpose of encoding.

  1. zn=Qk⊤(xn−xˉ)z_n = Q_k^\top(x_n - \bar x)Right so far: Problem 5.
  2. “Decode by applying QkQ_k: x^n=Qkzn\hat x_n = Q_kz_n.”The shortcut that causes the mistake: the encoder subtracted xˉ\bar x, so QkznQ_kz_n reconstructs the centred point and the decoder has to add xˉ\bar x back.
  3. x^n=QkQk⊤(xn−xˉ)\hat x_n = Q_kQ_k^\top(x_n - \bar x)For the four points this gives (2,2)(2, 2), (2,2)(2, 2), (−2,−2)(-2, -2), (−2,−2)(-2, -2) in place of (4,4)(4, 4), (4,4)(4, 4), (0,0)(0, 0), (0,0)(0, 0): every reconstruction is off by xˉ\bar x. The mean squared error becomes ∑j>kλj+∥xˉ∥2=2+8=10\sum_{j > k}\lambda_j + \|\bar x\|^2 = 2 + 8 = 10 instead of 22, since the cross term xˉ⊤(I−QkQk⊤)(xn−xˉ)\bar x^\top(I - Q_kQ_k^\top)(x_n - \bar x) averages to zero over the centred points. In matrix form the reconstruction is 1xˉ⊤+XcQkQk⊤\mathbf{1}\bar x^\top + X_cQ_kQ_k^\top, and inverse_transform adds the mean for exactly this reason.

5. Taking the first column of the eigenvector matrix as the top component

np.linalg.eigh returns eigenvalues in ascending order, and the columns of the eigenvector matrix follow the same order.

  1. S=QΛQ⊤S = Q\Lambda Q^\top, computed as lam, Q = np.linalg.eigh(S)Right so far: the eigen-decomposition of Problem 2.
  2. “The first column of QQ is the first principal component.”The habit that causes the mistake: the notation q1q_1 with λ1≥λ2\lambda_1 \ge \lambda_2, while the routine sorts the other way.
  3. q1q_1 = Q[:, 0]For the four points eigh returns λ=(2,8)\lambda = (2, 8) and Q[:, 0] =±12(1,−1)⊤= \pm\tfrac{1}{\sqrt2}(1, -1)^\top, the direction of least variance: the chosen component explains 20%20\% of the variance and the reconstruction error is 88 rather than 22 (Problem 6). The columns have to be reversed, Q[:, ::-1] with lam[::-1], or sorted by λ\lambda descending; np.linalg.svd returns singular values in descending order, which is one more reason to take Problem 8's route.

Print this set: pca-and-the-covariance-matrix.pdf (problems, answers, and worked solutions on separate pages).