Skip to content

Principal components

Turn a line through a cloud of points to find the direction of most spread, get it from the covariance matrix or the SVD, and see what it can’t tell you.

Helps to know first
This topic

First, the picture

Which way does a cloud of points spread the most? Watch a line turn through the cloud while the spread of the points’ shadows is traced.

The direction of most spread

Turn a line through six points and watch how far their shadows spread.

Six points, centred on the origin, and a line through it. Each point casts a shadow on the line.

angle
0°
shadow variance
15.2
share of the total
69.1 %
0.00 / 11.40 s
Describe this picture

Two panels (stacked on a phone). In the first, six points on a grid, (3,4)(3,4), (−3,−4)(-3,-4), (5,0)(5,0), (−5,0)(-5,0), (2,1)(2,1) and (−2,−1)(-2,-1), and a line through the origin. The line turns once through 180°, and each point drops a short dashed segment onto it at a right angle, ending in a dot: its shadow. In the second panel, the spread of the six shadows, their variance, is traced against the line’s angle as it turns. It rises and falls once, peaking at 18 at 26.57°, where the line points along (2,1)(2,1), and falling to 4 at 116.57°. The peak is then marked on the curve, and the line turns on to settle at the peak. Readouts give the angle, the shadow variance and its share of the total, 22. One control after the clip: drag the end of the line round, or turn it with the arrow keys, one degree a step; the shadows, the readouts and a dot on the curve follow.

The spread along a direction

The six points in the picture are already centred: their average is the origin. That is the first step of every analysis here, since spread is measured from the middle.

Pick a direction, a unit vector w\mathbf{w}. Each point y\mathbf{y} casts a shadow on the line of w\mathbf{w} of signed length y⋅w\mathbf{y}\cdot\mathbf{w}, as on Length and the dot product. The spread of those shadows is their variance. Here and below, the variance divides the sum of squares by n−1=5n-1=5, one less than the number of points, as in Covariance and correlation.

Along the horizontal axis the shadows are the xx values, with variance 76/5=15.276/5=15.2; along the vertical axis the yy values, with variance 34/5=6.834/5=6.8. Neither is the largest. The line in the picture finds 18, along (2,1)(2,1).

The covariance matrix

Stack the centred points as the rows of a data matrix Y\mathbf{Y}, one row per point. The covariance matrix is

S=Y⊤Yn−1.\mathbf{S}=\frac{\mathbf{Y}^\top\mathbf{Y}}{n-1}.

Its diagonal holds the variance of each coordinate, and its off-diagonal entry the covariance of the two. For the six points, ∑x2=76\sum x^2=76, ∑y2=34\sum y^2=34 and ∑xy=28\sum xy=28, so

S=15[76282834]=[15.25.65.66.8].\mathbf{S}=\frac15\begin{bmatrix}76&28\\28&34\end{bmatrix}=\begin{bmatrix}15.2&5.6\\5.6&6.8\end{bmatrix}.

The variance of the shadows along a unit w\mathbf{w} is the energy of Positive definite matrices:

variance along w=w⊤S w.\text{variance along}\ \mathbf{w}=\mathbf{w}^\top\mathbf{S}\,\mathbf{w}.

S\mathbf{S} is symmetric, so by Symmetric matrices that is λ1u12+λ2u22\lambda_1u_1^2+\lambda_2u_2^2 in its eigenvector coordinates. The largest it can be, for a unit w\mathbf{w}, is the top eigenvalue λ1\lambda_1, reached along the top eigenvector. That direction is the first principal component. The next one, perpendicular to it, is the second.

Here tr S=22\mathrm{tr}\,\mathbf{S}=22 and det⁡S=103.36−31.36=72\det\mathbf{S}=103.36-31.36=72, so the eigenvalues are 18 and 4. The eigenvectors are (2,1)(2,1), since S(2,1)=(36,18)=18 (2,1)\mathbf{S}(2,1)=(36,18)=18\,(2,1), and (−1,2)(-1,2), since S(−1,2)=(−4,8)=4 (−1,2)\mathbf{S}(-1,2)=(-4,8)=4\,(-1,2). The two components carry 18/22=81.8 %18/22=81.8\,\% and 4/22=18.2 %4/22=18.2\,\% of the total variance.

By the SVD

You don’t need to form S\mathbf{S}. Take the SVD of the data matrix itself, Y=UΣV⊤\mathbf{Y}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top, from The singular value decomposition. Then Y⊤Y=VΣ⊤ΣV⊤\mathbf{Y}^\top\mathbf{Y}=\mathbf{V}\boldsymbol{\Sigma}^\top\boldsymbol{\Sigma}\mathbf{V}^\top, so:

  • the columns of V\mathbf{V} are the principal directions;
  • the eigenvalues of S\mathbf{S} are λk=σk2/(n−1)\lambda_k=\sigma_k^2/(n-1);
  • each point’s coordinate along direction kk, its score, is entry kk of Yvk\mathbf{Y}\mathbf{v}_k.

For the six points, σ1=90=9.487\sigma_1=\sqrt{90}=9.487 and σ2=20=4.472\sigma_2=\sqrt{20}=4.472, and 90/5=1890/5=18, 20/5=420/5=4. Along (2,1)/5(2,1)/\sqrt5, the point (3,4)(3,4) scores 10/5=4.47210/\sqrt5=4.472, (5,0)(5,0) also 4.472, and (2,1)(2,1) scores 2.236. Keep only the first component and each point is replaced by its shadow; the squared error left over is σ22=20\sigma_2^2=20, as in Low-rank approximation.

Programs use the SVD route: forming Y⊤Y\mathbf{Y}^\top\mathbf{Y} squares the sensitivity to rounding, as Conditioning explains.

Two traps

The method finds the direction of most spread. It can’t find anything else, and the next picture shows two ways that matters.

Two traps

Two clouds where the principal directions mislead.

Eight points on a circle: S = 0.5714 I, the same spread in every direction, though the points lie exactly on a curve. PCA sees no direction.

0.00 / 7.20 s
Describe this picture

Two clouds side by side (stacked on a phone), drawn one after the other. On the left, eight points on a circle of radius 1, every 45°, with the covariance ellipse drawn: a circle, the same spread in every direction, although the points lie exactly on a curve; its label gives S=0.571 I\mathbf{S}=0.571\,\mathbf{I}, that is 47I\tfrac47\mathbf{I}. On the right, the six points of the first picture with xx multiplied by 10, stretched far sideways at one scale both ways, with their covariance ellipse and their first principal direction drawn almost flat, at 2.12°; the caption adds that it carries 99.7 % of the variance. There is no control.

Curves are invisible. The eight points on the ring have ∑x2=∑y2=4\sum x^2=\sum y^2=4 and ∑xy=0\sum xy=0, so S=47I\mathbf{S}=\tfrac47\mathbf{I}: no direction stands out. The covariance only sees spread, not shape.

Units decide. Measure xx in millimetres instead of centimetres, multiplying it by 10, and the top direction swings to 2.12°, almost along xx, with 99.7 % of the variance. Nothing about the data changed except the units. The usual fix is to standardise first: divide each coordinate by its own spread. For the six points the standardised covariance is the correlation matrix, with correlation ρ^xy=28/76×34=0.5508\hat\rho_{xy}=28/\sqrt{76\times34}=0.5508, and its eigenvalues are 1+ρ^xy=1.55081+\hat\rho_{xy}=1.5508 and 1−ρ^xy=0.44921-\hat\rho_{xy}=0.4492, along (1,1)(1,1) and (1,−1)(1,-1).

Where this leads · regression is different

The first principal component is not the line that best predicts yy from xx. Regression, in the probability and statistics course, makes the vertical gaps small; the principal component makes the perpendicular gaps small, treating both coordinates alike. For the same cloud they give different lines.

Worked example

1. The covariance route. Sums 76, 34 and 28; S=[15.25.65.66.8]\mathbf{S}=\begin{bmatrix}15.2&5.6\\5.6&6.8\end{bmatrix}; eigenvalues 18 and 4 along (2,1)(2,1) and (−1,2)(-1,2); shares 81.8 % and 18.2 %.

2. The SVD route. σ=9.487\sigma=9.487 and 4.472, and σ2/5=18\sigma^2/5=18 and 4. The squared size of Y\mathbf{Y} is 110=90+20110=90+20.

3. Scores. On the first component: 4.472, 4.472 and 2.236 for (3,4)(3,4), (5,0)(5,0) and (2,1)(2,1), and the negatives for the other three.

4. Standardised. ρ^xy=0.5508\hat\rho_{xy}=0.5508, eigenvalues 1.5508 and 0.4492.

Try these before you read the answers.

  • The variance along (2,1)/5(2,1)/\sqrt5? 18.
  • If every point lies on one line through the origin, what is the second eigenvalue? 0.
  • Measure xx in millimetres instead of centimetres. Does the top direction change? Yes: it swings towards xx. Standardise first.

In signal processing · waves as principal components

For smooth signals, such as audio frames or rows of an image, the principal components come out close to the cosine waves of The discrete cosine transform, in the signal processing course. That is one reason the cosine transform compresses so well: it is nearly the best basis, without having to compute one from the data.

Where you’ll meet this

Cleaning brain recordings. An eye blink shows up on many electrodes at once, in one fixed mix. In the many-channel recording it is one strong principal direction, which can be found and removed.

Faces and genes. Pictures of faces, and the genetic data of many people, have a few principal directions that carry most of their variation; maps of human populations have been drawn from the first two.

Finance. A few principal components describe how a whole set of interest rates moves: up together, steeper or flatter.

In every case the first component is the direction of most spread, not of most meaning. The meaning is something the analyst has to supply.

Reference card

QuantityFormulaNotes
Centred datarows of Y\mathbf{Y}subtract the average first
Covariance matrixS=Y⊤Y/(n−1)\mathbf{S}=\mathbf{Y}^\top\mathbf{Y}/(n-1)symmetric
Spread along w\mathbf{w}w⊤Sw\mathbf{w}^\top\mathbf{S}\mathbf{w}w\mathbf{w} of length 1
Principal directionseigenvectors of S\mathbf{S}the columns of V\mathbf{V} in Y=UΣV⊤\mathbf{Y}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top
Variancesλk=σk2/(n−1)\lambda_k=\sigma_k^2/(n-1)largest first
Share explainedλk/(λ1+λ2+⋯ )\lambda_k/(\lambda_1+\lambda_2+\cdots)
ScoresYvk\mathbf{Y}\mathbf{v}_keach point’s coordinate along direction kk
Limitssees spread, not shape; depends on unitsstandardise mixed units

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look