Chapter 13 §10 proved that the -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
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 . The data are points , written as the columns of a matrix, and
is the
Euclidean norm.
Centering
An affine subspace of is a coset of a subspace , and its dimension is
; Chapter 18
§01 calls the
direction space and shows in Proposition 18.1.4 that
every affine hull has this form. The subspaces of are the affine
subspaces containing .
Chapter 13 §10 fitted subspaces, which all pass
through . 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 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 . The data matrix is the
matrix whose -th
column is . The
mean of the data is and the centered data matrix
is whose -th column is .
Two clauses deserve comment. Columns
are the data points, so is the number of
measured quantities and the number of records;
the opposite convention is also in use, and every
formula below would be transposed under it. And is the
matrix
all of whose columns equal , since is the
all-ones vector.
The defining property of the centered matrix is that
its own mean is zero: using . In words, each row of sums to .
Example
(Centering three data sets)
- (a) points in : . Their sum is , so and Both rows sum to .
- (b) . Then and : a single point
carries no shape at all. This degenerate case matters,
because every ratio below has here and the
statements must exclude it or say what they mean.
- (c) Data already
centered: if then and . Centering is idempotent, since 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 by with . 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.
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 be a subspace, let be the
orthogonal projection onto , and let .
- (a) For every , , and the nearest point of exists.
- (b) For points with
mean , In particular the left side is smallest
exactly when .
Idea. Part (a) is the Best
Approximation Theorem after sliding the whole picture by
. Part (b) is
the one-dimensional identity “sum of squares about a
point = sum of squares about the mean + times the squared
displacement”, proved the way it always is: expand the
square and notice that the cross term is a multiple of
, which is by the definition of
the mean.
Proof.
(a) A point of
is with , and . By Theorem 11.3.5 the
quantity over is smallest exactly at , where
its value is . So the minimum
is attained and equals , which is
by Definition 11.3.3
applied after the translation.
(b) Put
and , so that by linearity of . Since is linear and , Therefore, expanding each square, which is the identity. The second term is
and vanishes
exactly when , that is when (Theorem 11.3.2),
that is when . This proves the lemma.
Part (b) is the whole of “the best affine subspace
passes through the mean”. It holds for
every direction space , before any
optimization over
has happened, which is why the affine problem costs
nothing extra.
Theorem
24.10.2 (The Best-Fitting Affine
Subspace)
Let have mean and centered data
matrix (Definition 24.10.1), let
be the singular values of (Definition 13.8.1),
and let be the left singular vectors of a fixed
singular value decomposition of (Theorem 13.8.1). Let
. Among all affine subspaces of
dimension at most , the total squared
distance is smallest for and its smallest value is . Moreover every
minimizing
contains .
Idea. Two optimizations in
sequence, and Lemma 24.10.1 does the
outer one first. ① For a fixed direction space
, the best
translate is the one through , whatever is. ② With the
translate fixed at , the objective
becomes the total squared distance from the
centered points to the subspace — which is exactly the
quantity Chapter 13 §10 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 with , and let
. By
Lemma 24.10.1 (a), and by part (b) of the same lemma this is at
least , with equality
exactly when . So it suffices to minimize over
subspaces with
.
Now
by Definition 11.3.3,
where is the -th column of . Hence which is the quantity of Corollary 13.10.4 for
the matrix . That corollary, applicable
because , says that the minimum over is
and is attained at . Combining, for every admissible
, with equality at
.
Finally let be any minimizer. Then ,
since the right side is admissible and no smaller; by
the equality clause of Lemma 24.10.1 (b) this
forces . This proves the theorem.
Quick check. Is the minimizing
unique? Take
the four centered points in and .
Solution. No. Here and ,
so and the theorem’s minimum is . Every
line through the origin attains it: for the four
squared distances are ; for
each point is at squared distance .
Total in both
cases. What the theorem asserts is that is a
minimizer and that
is the minimum value; uniqueness fails exactly
when , just as the truncation of Definition 13.10.1 is
non-unique then.
The
sample covariance matrix
Fix a direction with and look at the data through it: each point
has the single
coordinate along . The mean of these
numbers is and their spread about that mean is the number
the last step because . The
direction enters
only through a quadratic form, and the matrix of that
form does not depend on . 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
?” for every
at
once.
Definition
24.10.2 (Sample covariance matrix)
Let with centered data matrix (Definition 24.10.1). The
sample covariance matrix of the data is
with entries For with , the variance of the data in the
direction is the number
,
which for a unit is ; and the
total variance of the data is .
Clause by clause. The factor makes an average, so it does
not grow when data are duplicated. The entry is the ordinary
variance of the -th measured quantity,
and for
says
whether quantities and 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 rather than about the
data.
Note also what 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 Both coordinates have variance , and the positive
off-diagonal entry says they rise and fall
together.
- (b) Two points in with , so and . Then
and ,
of rank one: all the spread is in one direction, and
vanishes for every .
- (c) . Then and : no direction has
any spread.
Change the definition in one place and it stops being
a covariance. Replacing by gives , whose
entry is the
mean of
rather than the variance; for the data of (a) it is
, which is not , and its top
eigenvector is a multiple of rather
than of . 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 be a centered data matrix with singular
values (padded with zeros to length ), and let be
a singular value decomposition with columns of
. Put .
Then:
- (a) is symmetric and ;
- (b) for every , so the eigenvalues of
are and the are an orthonormal
eigenbasis;
- (c) for every , the variance
of the data in the direction is the Rayleigh
quotient
of Definition 17.1.1;
- (d) .
Idea. Every part is one line once
is written down: (a) and (b)
because 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) , and for every , ; with symmetry
this is Definition 13.1.1.
(b) Since is orthogonal, , so and by the
shape of in
Theorem 13.8.1.
Multiplying on the right by and using gives
; divide by . The are orthonormal and
there are of
them, so they are an orthonormal basis of , and the list is
decreasing.
(c) By the
computation preceding Definition 24.10.2, the
variance in direction is .
Since is
symmetric, by Definition 17.1.1,
which is the same number.
(d) The -th diagonal entry of
is
, so
; and by Theorem 0.9.6 (3). On
the other hand the display in (b) and the same trace
identity give using . Dividing by 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 largest?”, and Proposition 17.1.1
answers it: the maximum is , attained at . “Which direction
has the most spread among those orthogonal to ?”
is answered the same way by Proposition 17.1.3,
with maximum at . 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 are the principal
directions of the data, and the numbers are the -th principal
components: the coordinates of the centered
data along .
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
, let and let be the orthogonal
projection onto a subspace with . Write Then:
- (a) is
the total variance of the projected points ,
whose mean is ;
- (b) , where is as in Theorem 24.10.2;
- (c) ;
- (d) if , the fraction
of the total variance captured by is
Idea. Everything is Pythagoras
applied times and
then added up. For each centered point, splits into
the part keeps
and the part it throws away; averaging the first over
is ,
averaging the second is the fitting error of Theorem 24.10.2, and
averaging the total is by Proposition 24.10.3 (d).
Once (b) is available, (c) and (d) are just the theorem
already proved, divided by .
Proof.
(a) The mean of
is by linearity of , so the -th centered projected
point is . By Proposition 24.10.3 (d)
applied to these
points, their total variance is .
(b) Fix . The vectors and are
orthogonal, the first lying in and the second in (Theorem 11.3.2), so
Theorem 11.1.3
gives Summing over and dividing by : the left side is by Proposition 24.10.3 (d),
the first term on the right is ,
and the second is , as computed in the
proof of Theorem 24.10.2.
(c) By (b),
maximizing
over
is the same as minimizing , and Theorem 24.10.2 says the
minimum is ,
attained at .
Hence , and by (b) again,
(d) If then some
, so
and the
quotient is defined; dividing the value in (c) by it
cancels the .
This proves the theorem.
So “the first
principal directions explain
of the variance” is a theorem about the 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 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. , and has columns , as computed above.
Covariance. , so
. Since has the form
it sends
to and to , so its
eigenvalues are
and with By Proposition 24.10.3 (b),
and
,
that is and , and the eigenvalues of are and . Check: and .
Total variance, two ways. The squared
distances of the centered points from are , of average
. And , as Proposition 24.10.3 (d)
requires.
Best-fitting line. By Theorem 24.10.2 with it is the line , and the total squared distance is . Directly:
and lie on the line,
contributing ; for
the centered
point is ,
whose projection onto is
, so its squared
distance is ;
likewise for
. Total , as predicted.
Variance explained. , out of : the first
principal direction explains of the
variance, and the remaining is the average
squared distance to the line. The two add to , 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 . Then , so the first principal direction is . Now suppose the
first coordinate was measured in meters and we
re-express it in units three times smaller, which
multiplies row by
. The data are the
same data; the new centered matrix is with , and
so the first principal direction is now . It has turned by a
right angle because of a choice of units. Nothing is
wrong with the theorem — 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 before
starting is one common repair; it is a modeling
decision, and it is not forced by anything proved
here.
Computing
it, and what is not proved
Two closing matters, one computational and one about
scope.
Never form .
Everything above is stated in terms of , and is the natural object
to state it with; but forming and then finding its
eigenvalues is the worst available way to compute the
principal directions. The reason is Theorem 16.8.4: passing from
to squares the
condition number, , and Section 3 of this chapter
turns that into a count of lost digits — Theorem 24.3.2 says the
product route loses about
decimal digits where a backward stable factorization of
itself loses
about . The
same arithmetic applies with — but only
when that matrix has full column rank, which is the
hypothesis of Theorem 16.8.4. That asks
for ,
and
already forces , so it fails whenever : 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
is an
inner product of two rows, each of length , so by Theorem 24.1.2 the
computed product is
with of order
, where is the unit roundoff of
Section 1 and is
a modest dimensional factor. It is a factor and not a
constant because the entrywise bound has to be
aggregated over the whole product, by the same count as in
Section 3, where the corresponding step reads :
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 of size about
.
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 . So
every
below —
that is, up to the factor , every below — is
indistinguishable from in the computed
product, while a backward stable factorization of itself keeps it.
Those are exactly the singular values a low-rank fit has
to decide about. The right computation therefore works
on 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
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 §10’s promise, that a
singular value decomposition is “never” computed by
forming
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 §10 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 columns as a population
of equally likely
outcomes, each of weight . Then “expectation”
means the average , which
needs no measure theory and no limit, and the mean of
the population is . For two
real-valued quantities on it, with averages and , the
covariance of the population is and the variance of is .
With that reading, the variance of is
, which
for a unit is the variance in the
direction of Definition 24.10.2, hence
the Rayleigh quotient of Proposition 24.10.3 (c);
and the covariance of with is , which is the
displayed formula of Definition 24.10.2 read
backwards. So
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
, hence
, unchanged.
The largest entry of the error has no such property —
the matrix has largest
entry , while the
same matrix read in the frame
is , with
largest entry — and Chapter 13
§10’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.10.3); for
the sum of unsquared distances ,
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 points
are drawn from a distribution on , an expectation
defined by an integral rather than a finite sum, and
theorems saying that 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
§10’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.
Exercises
A. Check your
understanding
Exercise
(A1)
- (a) Define the
centered data matrix and the sample covariance matrix,
and say what means for a
unit vector .
- (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 . Justify your
answer.
- (d) True or false:
if points in
are given,
then has rank at
most . Justify
your answer.
- (e) Why should a
computation of the principal directions avoid forming
?
Solution.
(a) ,
the data matrix with the mean column subtracted from
every column, and (Definition 24.10.1, Definition 24.10.2). For a
unit , is the variance
of the numbers
, that is
the spread of the data in the direction .
(b) Among all
affine subspaces of of dimension at
most , the total squared distance is
smallest at , the minimum value is
with the
singular values of , and every minimizer
contains .
(c) False in
general; they are the eigenvectors of . For the four
points of Example (Four points, done exactly),
, whose eigenvalues are
and whose top eigenvector is not , while the top
principal direction is. The two
matrices agree exactly when , since . Their
eigenvectors can agree without that:
for the two points and the mean is , yet and
have the same eigenvectors.
(d) True. —
since , because gives
— and with having columns forces .
Centering costs at most one from the rank and sometimes
nothing: the right inequality because is a product (Theorem 3.5.9), the
left because adds to it a matrix of rank at most
(Exercise (C1: Rank of a sum)).
The drop need not happen: for , , the mean is
, yet .
(e) Because
forming the product destroys the small singular values.
Each entry of is an inner
product, so the computed product is
with of order
(Theorem 24.1.2); if is symmetric, Corollary 17.3.6 moves
every eigenvalue by at most that, so every below about
becomes indistinguishable from , while a backward
stable factorization of itself keeps it —
and those are exactly the singular values a low-rank fit
has to decide about. The conditioning statement says the same thing in digits
(Theorem 16.8.4, Theorem 24.3.2), but it
needs 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 are given in .
- (a) Compute , , and .
- (b) Find the
singular values of 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 , so and so .
(b) sends to and to , so , , that is
, , with and . The eigenvalues of are and , which are (Proposition 24.10.3
(b)).
(c) By Theorem 24.10.2 the best
line is , that is , with total squared
distance . By Theorem 24.10.4 (d) it
explains of the variance. Directly: and lie on ; the centered
points and
both project
to on ,
so each contributes . Total .
Exercise
(B2: Variance direction by direction)
For the data of Exercise B1, compute the variance in
the directions , , and
,
and say which of them is largest and why that was
predictable.
Solution. With and Definition 24.10.2, the
variance in a unit direction is : The largest is , in the direction . 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 ,
whose maximum is and is
attained at a unit eigenvector for . Note also
: 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 . The
second coordinate is then re-expressed in units times smaller, which
multiplies row by
.
- (a) Find the
principal directions before the change.
- (b) Find the value
of at which the
first principal direction switches from one coordinate
axis to the other, and say what happens exactly at that
value.
Solution.
(a) , so the principal directions are (variance
) and (variance
).
(b) After the
change the centered matrix is with , and
. The first principal direction
is when , that is
, and
when . At the two
eigenvalues are equal, , 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 with mean .
- (a) Prove that the
point
minimizing is unique and equals . (This is the case
, which Theorem 24.10.2 excludes,
so it has to be proved rather than quoted.)
- (b) Hence deduce
that for every subspace , the affine subspace
minimizing
over translates
of is .
- (c) Give an example
of data and a subspace for which is
not the best-fitting affine subspace of
its dimension, so that the choice of still matters.
Solution.
(a) Apply Lemma 24.10.1 (b) with
, so that
: The first term does not depend on and the second is
, vanishing
exactly when . So the minimum is attained at and nowhere
else.
(b) By Lemma 24.10.1 (a), , and by part (b) of
that lemma this exceeds unless — in
which case , the two cosets having a common point and the
same direction space. So the minimizing translate is
, and as
a set it is unique.
(c) Take the four
points of Example (Four points, done exactly)
and . Then is the
horizontal line , and the centered points are at squared distances from , total . The best line is
, with total . 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 with sample covariance , and let be
orthogonal.
- (a) Prove that the
data have sample covariance , and deduce
that the total variance is unchanged.
- (b) Prove that for
any orthonormal basis of , the sum of the
variances of the data in the directions equals .
- (c) Deduce that no
orthonormal frame can make every coordinate variance
small: some
always has variance at least . For the data of
Example (Four points, done exactly),
which frames attain that bound?
Solution.
(a) The mean of
the transformed data is , so the transformed centered matrix is
. Hence its sample covariance is . By Theorem 0.9.6, , since .
(b) By Definition 24.10.2 the
variance in the unit direction is . Let be the orthogonal
matrix with columns . Then is the
entry of
, so the
sum over is by
part (a) applied to .
(c) A sum of
numbers equal to
has a term at
least the average , since otherwise
the sum would be . For Example (Four points, done exactly),
and the
bound is ; the
variances in a frame are and , which sum
to , so both equal
exactly when
. Writing with and using
,
, this
reads , and subtracting gives , that is . So the
bound is attained exactly by the frames whose vectors
split their weight equally between and — for instance the
standard frame , since ; its two variances
are indeed
each. The extreme case of the spread is visible only in
the principal frame; an arbitrary frame hides
it.