24.10  Principal Components

Chapter 13 §11 proved that the k-dimensional subspace through the origin closest to a cloud of points is spanned by the leading left singular vectors, and then said plainly what it had not proved: the centering, the covariance matrix, and the reading of σi2/∑lσl2 as a fraction of variance. This section supplies all three — and the variance fraction turns out to be a theorem, not a convention — and then marks exactly where linear algebra stops and statistics begins. The honest answer is: more is provable than one might expect, and less than the phrase “explained variance” usually suggests.

Throughout, the field is R. The data are N≥1 points x1,…,xN∈Rn, written as the columns of a matrix, and ‖⋅‖ is the Euclidean norm.

24.10.1. Centering

An affine subspace of Rn is a coset A=p+U={p+z:z∈U} of a subspace U, and its dimension is dim⁡U; Chapter 18 §01 calls U the direction space and shows in Proposition 18.1.4 that every affine hull has this form. The subspaces of Rn are the affine subspaces containing 0.

Chapter 13 §11 fitted subspaces, which all pass through 0. That is the wrong family for data: a cloud of points far from the origin is badly described by any line through the origin, and the quantity minimized is then dominated by the distance from 0 to the cloud rather than by its shape. Widening the competition to all affine subspaces fixes this, and it costs nothing: the extra freedom is used up in one place, and we can say in advance where.

Subtract the mean once, and the affine problem becomes the linear problem Chapter 13 solved.

Definition 24.10.1 (Data matrix, mean, and centered data matrix)

Let x1,…,xN∈Rn. The data matrix is the matrix X∈Mn×N(R) whose j-th column is xj. The mean of the data is x¯:=1N∑j=1Nxj∈Rn, and the centered data matrix is Xc:=X−x¯1⊤∈Mn×N(R), whose j-th column is cj:=xj−x¯.

Two clauses deserve comment. Columns are the data points, so n is the number of measured quantities and N the number of records; the opposite convention is also in use, and every formula below would be transposed under it. And x¯1⊤ is the n×N matrix all of whose columns equal x¯, since 1∈RN is the all-ones vector.

The defining property of the centered matrix is that its own mean is zero: Xc1=X1−x¯(1⊤1)=Nx¯−Nx¯=0, using 1⊤1=N. In words, each row of Xc sums to 0.

Example (Centering three data sets)
  • (a) N=4 points in R2: (5,4),(4,1),(2,3),(1,0). Their sum is (12,8), so x¯=(3,2) and Xc=(21−1−22−11−2). Both rows sum to 0.
  • (b) N=1. Then x¯=x1 and Xc=O: a single point carries no shape at all. This degenerate case matters, because every ratio below has 0/0 here and the statements must exclude it or say what they mean.
  • (c) Data already centered: if X1=0 then x¯=0 and Xc=X. Centering is idempotent, since Xc1=0 always.

Change one clause and the object changes. Suppose one subtracts the mean of each column instead of forming the mean of the columns, replacing xj by xj−(1n1n⊤xj)1n with 1n∈Rn. That removes the average of the measured quantities inside each record, which is a meaningful operation for some data and a meaningless one for most, and in any case is not the one below. The clause that matters is which index is averaged over.

24.10.2. The best-fitting affine subspace

Distances to an affine subspace reduce to distances to its direction space, and the reduction is Chapter 11’s Best Approximation Theorem.

Lemma 24.10.1 (Distance to a flat, and where its translate wants to be)

Let U⊆Rn be a subspace, let Q=In−PU be the orthogonal projection onto U⊥, and let p∈Rn.

  • (a) For every x∈Rn,  d(x,p+U)=‖Q(x−p)‖, and the nearest point of p+U exists.
  • (b) For points x1,…,xN with mean x¯, ∑j=1N‖Q(xj−p)‖2=∑j=1N‖Q(xj−x¯)‖2+N‖Q(x¯−p)‖2. In particular the left side is smallest exactly when x¯∈p+U.

Idea. Part (a) is the Best Approximation Theorem after sliding the whole picture by −p. Part (b) is the one-dimensional identity “sum of squares about a point = sum of squares about the mean + N times the squared displacement”, proved the way it always is: expand the square and notice that the cross term is a multiple of ∑j(xj−x¯), which is 0 by the definition of the mean.

Proof.

  • (a) A point of p+U is p+z with z∈U, and ‖x−(p+z)‖=‖(x−p)−z‖. By Theorem 11.3.5 the quantity ‖(x−p)−z‖ over z∈U is smallest exactly at z=PU(x−p), where its value is ‖(x−p)−PU(x−p)‖=‖Q(x−p)‖. So the minimum is attained and equals ‖Q(x−p)‖, which is d(x,p+U) by Definition 11.3.3 applied after the translation.

  • (b) Put yj=Q(xj−x¯) and d=Q(x¯−p), so that Q(xj−p)=yj+d by linearity of Q. Since Q is linear and ∑j(xj−x¯)=Nx¯−Nx¯=0, ∑jyj=Q(∑j(xj−x¯))=0. Therefore, expanding each square, ∑j‖yj+d‖2=∑j(‖yj‖2+2⟨yj,d⟩+‖d‖2)=∑j‖yj‖2+2⟨∑jyj,d⟩+N‖d‖2=∑j‖yj‖2+N‖d‖2, which is the identity. The second term is ≥0 and vanishes exactly when d=0, that is when x¯−p∈kerQ=U (Theorem 11.3.2), that is when x¯∈p+U. This proves the lemma.

Part (b) is the whole of “the best affine subspace passes through the mean”. It holds for every direction space U, before any optimization over U has happened, which is why the affine problem costs nothing extra.

Theorem 24.10.2 (The Best-Fitting Affine Subspace)

Let x1,…,xN∈Rn have mean x¯ and centered data matrix Xc (Definition 24.10.1), let σ1≥σ2≥⋯≥0 be the singular values of Xc (Definition 13.9.1), and let u1,u2,… be the left singular vectors of a fixed singular value decomposition of Xc (Theorem 13.9.1). Let 1≤k≤min(n,N). Among all affine subspaces A⊆Rn of dimension at most k, the total squared distance D(A):=∑j=1Nd(xj,A)2 is smallest for A0=x¯+span(u1,…,uk), and its smallest value is ∑i>kσi2. Moreover every minimizing A contains x¯.

Idea. Two optimizations in sequence, and Lemma 24.10.1 does the outer one first. ① For a fixed direction space U, the best translate is the one through x¯, whatever U is. ② With the translate fixed at x¯, the objective becomes the total squared distance from the centered points to the subspace U — which is exactly the quantity Chapter 13 §11 minimized. So nothing new has to be proved about subspaces; the work is entirely in noticing that step ① does not interact with step ②.

Proof. Write A=p+U with dim⁡U≤k, and let Q=In−PU. By Lemma 24.10.1 (a), D(p+U)=∑j‖Q(xj−p)‖2, and by part (b) of the same lemma this is at least ∑j‖Q(xj−x¯)‖2=D(x¯+U), with equality exactly when x¯∈p+U. So it suffices to minimize D(x¯+U) over subspaces U with dim⁡U≤k.

Now ‖Q(xj−x¯)‖=‖cj−PUcj‖=d(cj,U) by Definition 11.3.3, where cj=xj−x¯ is the j-th column of Xc. Hence D(x¯+U)=∑j=1Nd(cj,U)2, which is the quantity D(U) of Corollary 13.11.4 for the matrix Xc∈Mn×N(R). That corollary, applicable because 1≤k≤min(n,N), says that the minimum over dim⁡U≤k is ∑i>kσi2 and is attained at U0=span(u1,…,uk). Combining, D(A)≥∑i>kσi2 for every admissible A, with equality at A0=x¯+U0.

Finally let A=p+U be any minimizer. Then D(p+U)=D(x¯+U), since the right side is admissible and no smaller; by the equality clause of Lemma 24.10.1 (b) this forces x¯∈p+U=A. This proves the theorem.

Quick check. Is the minimizer unique? Take the four centered points (1,0), (−1,0), (0,1), (0,−1) in R2 and k=1.

Solution. No. Here x¯=0 and XcXc⊤=diag(2,2), so σ1=σ2=2 and the theorem’s minimum is σ22=2. Every line through the origin attains it: for span(e1) the four squared distances are 0,0,1,1; for span((1,1)) each point is at squared distance 1−12=12. Total 2 in both cases. What the theorem asserts is that A0 is a minimizer and that ∑i>kσi2 is the minimum value; uniqueness fails exactly when σk=σk+1, just as the truncation of Definition 13.11.1 is nonunique then.

24.10.3. The sample covariance matrix

Fix a direction w∈Rn with w≠0 and look at the data through it: each point xj has the single coordinate tj=w⊤xj along w. The mean of these numbers is 1N∑jw⊤xj=w⊤x¯, and their spread about that mean is the number 1N∑j=1N(w⊤xj−w⊤x¯)2=1N∑j=1N(w⊤cj)2=1Nw⊤XcXc⊤w, the last step because ∑j(w⊤cj)2=‖Xc⊤w‖2=w⊤XcXc⊤w. The direction w enters only through a quadratic form, and the matrix of that form does not depend on w. That is a recurring expression, so it gets a name.

The sample covariance matrix is the single matrix that answers “how spread out are the data in direction w?” for every w at once.

Definition 24.10.2 (Sample covariance matrix)

Let x1,…,xN∈Rn with centered data matrix Xc (Definition 24.10.1). The sample covariance matrix of the data is S:=1NXcXc⊤∈Mn(R), with entries sab=1N∑j=1N(xaj−x¯a)(xbj−x¯b)(1≤a,b≤n). For w∈Rn with w≠0, the variance of the data in the direction w is the number w⊤Sw/‖w‖2, which for a unit w is w⊤Sw; and the total variance of the data is trS.

Clause by clause. The factor 1/N makes S an average, so it does not grow when data are duplicated. The entry saa is the ordinary variance of the a-th measured quantity, and sab for a≠b says whether quantities a and b tend to deviate from their means in the same direction (positive) or in opposite directions (negative). The centering is inside the definition, not optional: without it the same formula would measure spread about 0 rather than about the data.

Note also what S does not depend on: the order of the columns, since the defining sum runs over all of them. It does depend on the units of each coordinate, which is the subject of the warning below.

Example (Three covariance matrices)
  • (a) The four points of Example (Centering three data sets) (a). Then XcXc⊤=(106610),S=(5/23/23/25/2). Both coordinates have variance 5/2, and the positive off-diagonal entry says they rise and fall together.
  • (b) Two points ±v in Rn with v≠0, so N=2 and x¯=0. Then XcXc⊤=2vv⊤ and S=vv⊤, of rank one: all the spread is in one direction, and w⊤Sw=(w⊤v)2 vanishes for every w⊥v.
  • (c) N=1. Then Xc=O and S=O: no direction has any spread.

Change the definition in one place and it stops being a covariance. Replacing Xc by X gives 1NXX⊤, whose (a,a) entry is the mean of xaj2 rather than the variance; for the data of (a) it is (46/430/430/426/4), which is not S, and its top eigenvector is a multiple of (1,0.72…) rather than of (1,1). The clause that fails is the subtraction of the mean, and with it every statement below.

Proposition 24.10.3 (The Covariance Matrix and the Singular Value Decomposition)

Let Xc∈Mn×N(R) be a centered data matrix with singular values σ1≥σ2≥… (padded with zeros to length n), and let Xc=UΣV⊤ be a singular value decomposition with columns u1,…,un of U. Put S=1NXcXc⊤. Then:

  • (a) S is symmetric and S⪰O;
  • (b) Sui=σi2Nui for every i, so the eigenvalues of S are σ12/N≥⋯≥σn2/N and the ui are an orthonormal eigenbasis;
  • (c) for every w≠0, the variance of the data in the direction w is the Rayleigh quotient RS(w) of Definition 17.1.1;
  • (d) trS=1N∑j=1N‖xj−x¯‖2=1N∑iσi2.

Idea. Every part is one line once Xc=UΣV⊤ is written down: (a) and (b) because XcXc⊤=U(ΣΣ⊤)U⊤ is already a diagonalization, (c) because the computation before Definition 24.10.2 was the Rayleigh quotient, and (d) because the trace does not notice the unitary factors and reads the same quantity off the rows and off the columns.

Proof.

  • (a) S⊤=1N(XcXc⊤)⊤=1NXcXc⊤=S, and for every w, w⊤Sw=1N‖Xc⊤w‖2≥0; with symmetry this is Definition 13.1.1.

  • (b) Since V is orthogonal, V⊤V=IN, so XcXc⊤=UΣV⊤VΣ⊤U⊤=U(ΣΣ⊤)U⊤, and ΣΣ⊤=diag(σ12,…,σn2) by the shape of Σ in Theorem 13.9.1. Multiplying on the right by ui and using U⊤ui=ei gives XcXc⊤ui=σi2ui; divide by N. The ui are orthonormal and there are n of them, so they are an orthonormal basis of Rn, and the list σi2/N is decreasing.

  • (c) By the computation preceding Definition 24.10.2, the variance in direction w is w⊤Sw/‖w‖2. Since S is symmetric, RS(w)=⟨Sw,w⟩/⟨w,w⟩=w⊤Sw/‖w‖2 by Definition 17.1.1, which is the same number.

  • (d) The j-th diagonal entry of Xc⊤Xc is ‖cj‖2, so tr(Xc⊤Xc)=∑j‖cj‖2; and tr(XcXc⊤)=tr(Xc⊤Xc) by Theorem 0.9.6 (3). On the other hand the display in (b) and the same trace identity give tr(XcXc⊤)=tr(U(ΣΣ⊤)U⊤)=tr((ΣΣ⊤)U⊤U)=∑iσi2, using U⊤U=In. Dividing by N gives both equalities. This proves the proposition.

Part (c) connects this section to Chapter 17. The question “which direction has the most spread?” is the question “where is RS largest?”, and Proposition 17.1.1 answers it: the maximum is λ1(S)=σ12/N, attained at u1. “Which direction has the most spread among those orthogonal to u1,…,uk−1?” is answered the same way by Proposition 17.1.3, with maximum σk2/N at uk. So the leading left singular vectors are not only the best-fitting directions of Theorem 24.10.2; they are also, one at a time, the directions of greatest remaining spread. The two descriptions agree because they are the two halves of one Pythagorean identity, which is the next theorem.

Definition 24.10.3 (Principal directions and components)

In the situation of Proposition 24.10.3, the unit vectors u1,u2,… are the principal directions of the data, and the numbers ui⊤c1,…,ui⊤cN are the i-th principal components: the coordinates of the centered data along ui.

24.10.4. How much variance a subspace explains

Theorem 24.10.4 (Variance Captured by the Principal Directions)

In the situation of Proposition 24.10.3, let 1≤k≤min(n,N), let U0=span(u1,…,uk), and let P be the orthogonal projection onto a subspace U with dim⁡U≤k. Write var(U):=1N∑j=1N‖P(xj−x¯)‖2. Then:

  • (a) var(U) is the total variance of the projected points Px1,…,PxN, whose mean is Px¯;
  • (b) var(U)+1ND(x¯+U)=trS, where D is as in Theorem 24.10.2;
  • (c) var(U)≤var(U0)=1N∑i≤kσi2;
  • (d) if Xc≠O, the fraction of the total variance captured by U0 is var(U0)trS=σ12+⋯+σk2σ12+σ22+….

Idea. Everything is Pythagoras applied N times and then added up. For each centered point, ‖cj‖2 splits into the part P keeps and the part it throws away; averaging the first over j is var(U), averaging the second is the fitting error of Theorem 24.10.2, and averaging the total is trS by Proposition 24.10.3 (d). Once (b) is available, (c) and (d) are just the theorem already proved, divided by N.

Proof.

  • (a) The mean of Px1,…,PxN is 1N∑jPxj=Px¯ by linearity of P, so the j-th centered projected point is Pxj−Px¯=Pcj. By Proposition 24.10.3 (d) applied to these N points, their total variance is 1N∑j‖Pcj‖2=var(U).

  • (b) Fix j. The vectors Pcj and cj−Pcj are orthogonal, the first lying in U and the second in U⊥ (Theorem 11.3.2), so Theorem 11.1.3 gives ‖cj‖2=‖Pcj‖2+‖cj−Pcj‖2. Summing over j and dividing by N: the left side is trS by Proposition 24.10.3 (d), the first term on the right is var(U), and the second is 1N∑jd(cj,U)2=1ND(x¯+U), as computed in the proof of Theorem 24.10.2.

  • (c) By (b), maximizing var(U) over dim⁡U≤k is the same as minimizing D(x¯+U), and Theorem 24.10.2 says the minimum is ∑i>kσi2, attained at U0. Hence var(U)≤var(U0), and by (b) again, var(U0)=trS−1N∑i>kσi2=1N∑iσi2−1N∑i>kσi2=1N∑i≤kσi2.

  • (d) If Xc≠O then some σi>0, so trS=1N∑iσi2>0 and the quotient is defined; dividing the value in (c) by it cancels the 1/N. This proves the theorem.

So “the first k principal directions explain (∑i≤kσi2)/(∑iσi2) of the variance” is a theorem about the N given points, not a convention: the numerator is the variance of the projected cloud, the denominator is the variance of the original cloud, and part (b) is what makes the two add up with nothing left over.

Example (Four points, done exactly)

Take the four points (5,4),(4,1),(2,3),(1,0) of Example (Centering three data sets) (a). Find the mean, the sample covariance matrix, the principal directions, the best-fitting line, the total squared distance to it, and the fraction of variance it explains.

Solution. Mean and centering. x¯=(3,2), and Xc has columns (2,2),(1,−1),(−1,1),(−2,−2), as computed above.

Covariance. XcXc⊤=(106610), so S=(5/23/23/25/2). Since XcXc⊤ has the form aI+bJ it sends (1,1) to 16(1,1) and (1,−1) to 4(1,−1), so its eigenvalues are 16 and 4 with u1=12(1,1),u2=12(1,−1). By Proposition 24.10.3 (b), σ12=16 and σ22=4, that is σ1=4 and σ2=2, and the eigenvalues of S are 4 and 1. Check: trS=5 and 4+1=5.

Total variance, two ways. The squared distances of the centered points from 0 are 8,2,2,8, of average 5. And 1N∑iσi2=14(16+4)=5, as Proposition 24.10.3 (d) requires.

Best-fitting line. By Theorem 24.10.2 with k=1 it is A0=(3,2)+span((1,1)), the line y=x−1, and the total squared distance is σ22=4. Directly: (5,4) and (1,0) lie on the line, contributing 0; for (4,1) the centered point is (1,−1), whose projection onto span((1,1)) is 0, so its squared distance is 2; likewise 2 for (2,3). Total 4, as predicted.

Variance explained. var(U0)=14σ12=4, out of trS=5: the first principal direction explains 16/20=80% of the variance, and the remaining 20% is the average squared distance 4/4=1 to the line. The two add to 5, which is Theorem 24.10.4 (b).

Warning. Principal directions are not “the important directions”: they change when you change the units. Take the four centered points (1,0),(−1,0),(0,2),(0,−2). Then XcXc⊤=diag(2,8), so the first principal direction is e2. Now suppose the first coordinate was measured in meters and we re-express it in units three times smaller, which multiplies row 1 by 3. The data are the same data; the new centered matrix is DXc with D=diag(3,1), and (DXc)(DXc)⊤=Ddiag(2,8)D=diag(18,8), so the first principal direction is now e1. It has turned by a right angle because of a choice of units. Nothing is wrong with the theorem — DXc really is a different matrix — but “the direction of greatest variance” is a statement about a coordinate system, not about the data alone. Rescaling each coordinate to variance 1 before starting is one common repair; it is a modeling decision, and it is not forced by anything proved here.

24.10.5. Computing it, and what is not proved

Two closing matters, one computational and one about scope.

Never form XcXc⊤. Everything above is stated in terms of S, and S is the natural object to state it with; but forming S and then finding its eigenvalues is the worst available way to compute the principal directions. The reason is Theorem 16.8.4: passing from A to A∗A squares the condition number, κ2(A∗A)=κ2(A)2, and Section 3 of this chapter turns that into a count of lost digits — Theorem 24.3.2 says the product route loses about 2log10⁡κ2 decimal digits where a backward stable factorization of A itself loses about log10⁡κ2. The same arithmetic applies with A=Xc⊤ — but only when that matrix has full column rank, which is the hypothesis of Theorem 16.8.4. That asks for rankXc=n, and Xc1=0 already forces rankXc≤N−1, so it fails whenever N≤n: for three records of ten measured quantities there is nothing to apply. The count that follows needs no hypothesis on the rank, and it is the one to remember.

Here is the count in this setting. Each entry of XcXc⊤ is an inner product of two rows, each of length N, so by Theorem 24.1.2 the computed product is XcXc⊤+Δ with ‖Δ‖2 of order c(n,N)uσ12, where u is the unit roundoff of Section 1 and c is a modest dimensional factor. It is a factor and not a constant because the entrywise bound has to be aggregated over the whole n×n product, by the same count as in Section 3, where the corresponding step reads ‖C−A∗A‖≤γm‖A‖F2≤nγm‖A‖2: the length of the inner products sits inside γ, and passing from the Frobenius norm to the spectral norm puts their number in front. Here that makes c(n,N) of size about nN.

If the implementation forms only the upper triangle and reflects it — an assumption about the computation, not something proved here — then Δ is symmetric, and Corollary 17.3.6 says a symmetric perturbation of that size moves every eigenvalue by at most ‖Δ‖2. So every σi2 below c(n,N)uσ12 — that is, up to the factor c, every σi below uσ1 — is indistinguishable from 0 in the computed product, while a backward stable factorization of Xc itself keeps it. Those are exactly the singular values a low-rank fit has to decide about. The right computation therefore works on Xc directly and never builds the product.

What this chapter does not do is assemble that computation. The ingredients are here — the Householder reductions of Section 3, the shifted QR iteration of Section 5 — but no algorithm for the singular values of a given matrix is written down or analyzed anywhere in this book. So the paragraph above pays the parenthesis of the last clause of Chapter 13 §11’s promise, that a singular value decomposition is “never” computed by forming A∗A and finding its eigenvalues; the clause itself, how one is actually computed for a large matrix, is left unpaid.

The finite population, and the boundary. Chapter 13 §11 said that the statistics needs “a probabilistic model, a reason to prefer squared error, and an argument that the sample answer says something about the population”. Here is exactly what has been delivered.

A probabilistic model, for the finite population of the data themselves. Regard the N columns as a population of N equally likely outcomes, each of weight 1/N. Then “expectation” means the average 1N∑j, which needs no measure theory and no limit, and the mean of the population is x¯. For two real-valued quantities f,g on it, with averages f¯=1N∑jf(xj) and g¯=1N∑jg(xj), the covariance of the population is Cov(f,g):=1N∑j=1N(f(xj)−f¯)(g(xj)−g¯), and the variance of f is Cov(f,f). With that reading, the variance of x↦w⊤x is w⊤Sw, which for a unit w is the variance in the direction w of Definition 24.10.2, hence the Rayleigh quotient of Proposition 24.10.3 (c); and the covariance of x↦xa with x↦xb is sab, which is the displayed formula of Definition 24.10.2 read backwards. So S is the covariance matrix of that population, and Theorem 24.10.4 is a statement about it. This much is a theorem, and it is what was proved above.

A reason to prefer squared error. Three, none of them a proof that squared error is the right criterion, which is a modeling question. (i) It does not depend on the coordinate frame: an orthogonal change of coordinates is an isometry, so it leaves every distance d(xj,A), hence D(A), unchanged. The largest entry of the error has no such property — the matrix diag(1,0) has largest entry 1, while the same matrix read in the frame 12(1,±1) is 12(1111), with largest entry 12 — and Chapter 13 §11’s warning shows that it really does prefer a different answer. (ii) It splits: Theorem 24.10.4 (b) says captured plus lost equals total, exactly, which is what makes a fraction of variance meaningful at all. (iii) It has a closed-form minimizer, computable by a single factorization (Theorem 13.11.3); for the sum of unsquared distances ∑jd(xj,A), nothing proved in this book produces a minimizer at all.

An argument that the sample says something about a larger population. This is not proved here, and it cannot be with the tools of this book. Such an argument requires a probability model in which the N points are drawn from a distribution on Rn, an expectation defined by an integral rather than a finite sum, and theorems saying that S converges to the distribution’s covariance and that its eigenvectors converge to the distribution’s principal directions. Every one of those belongs to probability and statistics, which this book does not develop. Chapter 13 §11’s sentence promised it; the honest correction is that linear algebra supplies the finite-population model and the reasons for squared error, and hands the inference to a subject this book does not enter.

Remark. The divisor N versus N−1. Statistics usually defines the sample covariance with 1/(N−1), which presupposes N≥2, because that makes it an unbiased estimator of a population covariance — a statement inside the probability model just excluded, and one we therefore cannot prove. With the finite-population reading the divisor is N, and that is the convention of Definition 24.10.2, which makes sense for every N≥1. For N≥2 the choice changes nothing here: it multiplies S by N/(N−1), which scales every eigenvalue by the same factor, changes no eigenvector, and leaves every ratio such as Theorem 24.10.4 (d) exactly as it was.

24.10.6. Exercises

A. Check your understanding

Exercise (A1)
  • (a) Define the centered data matrix and the sample covariance matrix, and say what w⊤Sw means for a unit vector w.
  • (b) State Theorem 24.10.2, including the value of the minimum and the claim about the mean.
  • (c) True or false: the principal directions of a data set are the eigenvectors of XX⊤. Justify your answer.
  • (d) True or false: if N=3 points in R10 are given, then S has rank at most 2. Justify your answer.
  • (e) Why should a computation of the principal directions avoid forming XcXc⊤?

Solution.

  • (a) Xc=X−x¯1⊤, the data matrix with the mean column subtracted from every column, and S=1NXcXc⊤ (Definition 24.10.1, Definition 24.10.2). For a unit w, w⊤Sw is the variance of the N numbers w⊤xj, that is the spread of the data in the direction w.

  • (b) Among all affine subspaces of Rn of dimension at most k≤min(n,N), the total squared distance ∑jd(xj,A)2 is smallest at A0=x¯+span(u1,…,uk), the minimum value is ∑i>kσi2 with σi the singular values of Xc, and every minimizer contains x¯.

  • (c) False in general; they are the eigenvectors of XcXc⊤. For the four points of Example (Four points, done exactly), XX⊤=(46303026), whose eigenvalues are 36±1010 and whose top eigenvector is not (1,1), while the top principal direction is. The two matrices agree exactly when x¯=0, since XX⊤=XcXc⊤+Nx¯x¯⊤. Their eigenvectors can agree without that: for the two points (0,0) and (2,0) the mean is (1,0)≠0, yet XX⊤=diag(4,0) and XcXc⊤=diag(2,0) have the same eigenvectors.

  • (d) True. rankS=rankXc — since nul(Xc⊤)=nul(XcXc⊤), because XcXc⊤w=0 gives ‖Xc⊤w‖2=w⊤XcXc⊤w=0 — and Xc1=0 with Xc having 3 columns forces rankXc≤2. Centering costs at most one from the rank and sometimes nothing: rankX−1 ≤ rankXc ≤ rankX, the right inequality because Xc=X(IN−1N11⊤) is a product (Theorem 3.5.9), the left because X=Xc+x¯1⊤ adds to it a matrix of rank at most 1 (Exercise (C1: Rank of a sum)). The drop need not happen: for x1=(1,0), x2=(2,0), x3=(0,1) the mean is (1,13)≠0, yet rankX=rankXc=2.

  • (e) Because forming the product destroys the small singular values. Each entry of XcXc⊤ is an inner product, so the computed product is XcXc⊤+Δ with ‖Δ‖2 of order c(n,N)uσ12 (Theorem 24.1.2); if Δ is symmetric, Corollary 17.3.6 moves every eigenvalue by at most that, so every σi below about uσ1 becomes indistinguishable from 0, while a backward stable factorization of Xc itself keeps it — and those are exactly the singular values a low-rank fit has to decide about. The conditioning statement κ2(XcXc⊤)=κ2(Xc)2 says the same thing in digits (Theorem 16.8.4, Theorem 24.3.2), but it needs Xc⊤ to have full column rank, which fails in part (d) above; the Weyl count needs no hypothesis on the rank.

B. Practice

Exercise (B1: A four-point cloud)

The points (1,1),(3,5),(5,3),(7,7) are given in R2.

  • (a) Compute x¯, Xc, XcXc⊤, and S.
  • (b) Find the singular values of Xc and the principal directions.
  • (c) Find the best-fitting line, the total squared distance to it, and the fraction of variance it explains. Verify the total squared distance directly.

Solution.

  • (a) The sum of the four points is (16,16), so x¯=(4,4) and Xc=(−3−113−31−13),XcXc⊤=(20161620), so S=(5445).

  • (b) XcXc⊤ sends (1,1) to 36(1,1) and (1,−1) to 4(1,−1), so σ12=36, σ22=4, that is σ1=6, σ2=2, with u1=12(1,1) and u2=12(1,−1). The eigenvalues of S are 9 and 1, which are σi2/4 (Proposition 24.10.3 (b)).

  • (c) By Theorem 24.10.2 the best line is (4,4)+span((1,1)), that is y=x, with total squared distance σ22=4. By Theorem 24.10.4 (d) it explains 36/40=90% of the variance. Directly: (1,1) and (7,7) lie on y=x; the centered points (−1,1) and (1,−1) both project to 0 on span((1,1)), so each contributes 2. Total 4.

Exercise (B2: Variance direction by direction)

For the data of Exercise B1, compute the variance in the directions e1, e2, 12(1,1), and 12(1,−1), and say which of them is largest and why that was predictable.

Solution. With S=(5445) and Definition 24.10.2, the variance in a unit direction w is w⊤Sw: e1↦5,e2↦5,(1,1)2↦9,(1,−1)2↦1. The largest is 9, in the direction u1. This was predictable from Proposition 24.10.3 (c) and Proposition 17.1.1: the variance in a unit direction is the Rayleigh quotient of S, whose maximum is λ1(S)=9 and is attained at a unit eigenvector for λ1. Note also 5+5=9+1=trS: the total variance is the same however the orthonormal frame is chosen.

Exercise (B3: Changing the units)

The centered data matrix of a two-coordinate data set is Xc=(3−300004−4). The second coordinate is then re-expressed in units c times smaller, which multiplies row 2 by c>0.

  • (a) Find the principal directions before the change.
  • (b) Find the value of c at which the first principal direction switches from one coordinate axis to the other, and say what happens exactly at that value.

Solution.

  • (a) XcXc⊤=diag(18,32), so the principal directions are u1=e2 (variance 32/4=8) and u2=e1 (variance 18/4=4.5).

  • (b) After the change the centered matrix is DXc with D=diag(1,c), and (DXc)(DXc)⊤=diag(18,32c2). The first principal direction is e2 when 32c2>18, that is c>3/4, and e1 when c<3/4. At c=3/4 the two eigenvalues are equal, σ1=σ2, and every direction is a direction of greatest variance: the principal directions are not determined at all, and the best-fitting line of Theorem 24.10.2 is not unique, exactly as in the Quick check above.

C. Going deeper

Exercise (C1: The mean is forced, and the projection is not a shortcut)

Let x1,…,xN∈Rn with mean x¯.

  • (a) Prove that the point p∈Rn minimizing ∑j‖xj−p‖2 is unique and equals x¯. (This is the case k=0, which Theorem 24.10.2 excludes, so it has to be proved rather than quoted.)
  • (b) Hence deduce that for every subspace U, the affine subspace p+U minimizing D over translates of U is x¯+U.
  • (c) Give an example of data and a subspace U for which x¯+U is not the best-fitting affine subspace of its dimension, so that the choice of U still matters.

Solution.

  • (a) Apply Lemma 24.10.1 (b) with U={0}, so that Q=In: ∑j‖xj−p‖2=∑j‖xj−x¯‖2+N‖x¯−p‖2. The first term does not depend on p and the second is ≥0, vanishing exactly when p=x¯. So the minimum is attained at x¯ and nowhere else.

  • (b) By Lemma 24.10.1 (a), D(p+U)=∑j‖Q(xj−p)‖2, and by part (b) of that lemma this exceeds D(x¯+U) unless x¯∈p+U — in which case p+U=x¯+U, the two cosets having a common point and the same direction space. So the minimizing translate is x¯+U, and as a set it is unique.

  • (c) Take the four points of Example (Four points, done exactly) and U=span(e1). Then x¯+U is the horizontal line y=2, and the centered points (2,2),(1,−1),(−1,1),(−2,−2) are at squared distances 4,1,1,4 from U, total 10. The best line is x¯+span((1,1)), with total 4. Both pass through the mean; only one has the right direction. Choosing the translate is free, choosing the direction space is the whole problem.

Exercise (C2: Total variance is a trace, and what that buys)

Let x1,…,xN∈Rn with sample covariance S, and let Q∈Mn(R) be orthogonal.

  • (a) Prove that the data Qx1,…,QxN have sample covariance QSQ⊤, and deduce that the total variance trS is unchanged.
  • (b) Prove that for any orthonormal basis (q1,…,qn) of Rn, the sum of the variances of the data in the n directions qi equals trS.
  • (c) Deduce that no orthonormal frame can make every coordinate variance small: some qi always has variance at least trS/n. For the data of Example (Four points, done exactly), which frames attain that bound?

Solution.

  • (a) The mean of the transformed data is 1N∑jQxj=Qx¯, so the transformed centered matrix is QX−Qx¯1⊤=QXc. Hence its sample covariance is 1N(QXc)(QXc)⊤=QSQ⊤. By Theorem 0.9.6, tr(QSQ⊤)=tr(SQ⊤Q)=trS, since Q⊤Q=In.

  • (b) By Definition 24.10.2 the variance in the unit direction qi is qi⊤Sqi. Let Q be the orthogonal matrix with columns q1,…,qn. Then qi⊤Sqi is the (i,i) entry of Q⊤SQ, so the sum over i is tr(Q⊤SQ)=trS by part (a) applied to Q⊤.

  • (c) A sum of n numbers equal to trS has a term at least the average trS/n, since otherwise the sum would be <trS. For Example (Four points, done exactly), trS=5 and the bound is 5/2; the variances in a frame (q1,q2) are q1⊤Sq1 and q2⊤Sq2, which sum to 5, so both equal 5/2 exactly when q1⊤Sq1=5/2. Writing q1=au1+bu2 with a2+b2=1 and using Su1=4u1, Su2=u2, this reads 4a2+b2=5/2, and subtracting a2+b2=1 gives 3a2=3/2, that is a2=b2=1/2. So the bound is attained exactly by the frames whose vectors split their weight equally between u1 and u2 — for instance the standard frame (e1,e2), since e1=12(u1+u2); its two variances are indeed 5/2 each. The extreme case of the spread is visible only in the principal frame; an arbitrary frame hides it.

Found a typo or a mistake? Report it, or read this page’s source

The text of this book is licensed under CC BY-NC-SA 4.0.