Hexa Scholar

Principal components: the directions of most variance

Gold beads scattered in a long tilted cloud on a brown clay table, with a long wooden rod lying through the cloud and a shorter rod crossing it at the center.
Two rods laid across a cloud of beads.

The question

Twelve points, and a line through their center. Which way should the line point so that the points, projected onto it, spread out the most?

Turn the line yourself; the page keeps score.

  1. 1 SeeTurn a line to catch the most spread.
  2. 2 TryPredict where the best line points.
  3. 3 ProveBuild the proof, line by line.
  4. 4 ApplyProblems to check yourself.
Before you start · 3 quick checks on projections, variance and eigenvectors, about 3 min

Two ideas before you start

2 ideas

A direction turns points into numbers

Figure: the four points (−3, −1), (−1, −1), (1, 1) and (3, 1), a dashed line through their center, and the projection of each point onto the line. It needs JavaScript.

Line at

Scores: −3, −1, 1, 3

Variance along the line = (9 + 1 + 1 + 9)/4 = 5

Project each point onto a line through the center of the cloud. Its signed position along the line is a number, its score. The variance of the scores measures how much the cloud spreads in that direction.

Exact definition

Deviations. Take \(n\) points \(p_1, \dots, p_n\) in the plane. Their center is \(\bar{p} = \tfrac{1}{n}\sum_i p_i\), and the deviations are \(d_i = p_i - \bar{p}\), written as columns. Sums \(\sum_i\) run over \(i = 1, \dots, n\).

Scores and the variance along a direction. For a unit vector \(u\), the score of point \(i\) is \(z_i = u^{\mathsf{T}}d_i\), and the variance along \(u\) is

\[V(u) = \frac{1}{n}\sum_i (u^{\mathsf{T}}d_i)^2.\]

The scores average to 0, because the deviations do. A direction is written by its angle \(\theta\) from the positive \(x\) axis, \(u = (\cos\theta, \sin\theta)\); the angles \(\theta\) and \(\theta + 180^\circ\) give the same line and the same variance.

Covariance matrix.

\[\begin{aligned} S &= \frac{1}{n}\sum_i d_id_i^{\mathsf{T}} \\ &= \begin{pmatrix} S_{xx} & S_{xy} \\ S_{xy} & S_{yy} \end{pmatrix}. \end{aligned}\]

Because \((u^{\mathsf{T}}d_i)^2 = u^{\mathsf{T}}d_id_i^{\mathsf{T}}u\), \(V(u) = u^{\mathsf{T}}Su\), as in the lesson on the covariance ellipse.

Principal axes. A first principal axis is a direction \(u\) with the largest \(V(u)\). The scores along it are the first principal component of the points.

Along plus across stays the same

Figure: the same four points, a dashed line through their center, and the segment from each point straight across to the line. It needs JavaScript.

Line at

Along: 5 + Across: 1 = Total: 6

Each point's squared distance from the center splits into a part along the line and a part across it, by Pythagoras. Averaged over the points, along plus across is the same for every line through the center.

Exact definition

Distance across. The point of the line through \(\bar{p}\) along \(u\) closest to \(p_i\) is \(\bar{p} + z_iu\), so the squared distance from \(p_i\) to the line is \(\|d_i - z_iu\|^2\). Its average is

\[D(u) = \frac{1}{n}\sum_i \|d_i - z_iu\|^2.\]

Along plus across. By Pythagoras, \(\|d_i\|^2 = z_i^2 + \|d_i - z_iu\|^2\). Averaging over \(i\),

\[\begin{aligned} V(u) + D(u) &= \frac{1}{n}\sum_i \|d_i\|^2 \\ &= S_{xx} + S_{yy}, \end{aligned}\]

the total variance, which does not depend on \(u\). So a direction with the most variance along it is a direction with the least mean squared distance across it.

Fine print: names and scope

The method is principal component analysis, PCA. The directions are also called principal directions or loadings, and the numbers \(u^{\mathsf{T}}d_i\) are the scores. The data are centered first, so every line on this page passes through the center of the cloud. Centering is part of the method: the same computation on points that are not centered measures the mean square of the scores about the origin, not their variance, and its best direction can be pulled toward the line from the origin to the center of the cloud.

This page divides by \(n\). Software often divides by \(n - 1\) instead; that multiplies every variance by the same factor and changes no direction and no share of the total variance.

The points are data. Nothing here assumes how they arose, so nothing here says how far the directions would move with new data; the lesson on the bootstrap shows one way to study that.

1 See

Find the direction of most spread

2 Try

Where the first axis points

Two questions first. The explorer below them opens once both are answered.

The explorer and its goals open after you answer both questions.

3 Prove

Why the first eigenvector wins

Theorem

Assumptions. \(n \ge 3\) points in the plane, not all on one line, with deviations \(d_i\) from their center and covariance matrix \(S = \tfrac{1}{n}\sum_i d_id_i^{\mathsf{T}}\). For a unit vector \(u\), \(V(u) = \tfrac{1}{n}\sum_i (u^{\mathsf{T}}d_i)^2\) is the variance along \(u\) and \(D(u) = \tfrac{1}{n}\sum_i \|d_i - (u^{\mathsf{T}}d_i)u\|^2\) is the mean squared distance from the points to the line through the center along \(u\). Let \(\lambda_1 \ge \lambda_2\) be the eigenvalues of \(S\), with orthonormal eigenvectors \(v_1\) and \(v_2\).

Conclusion. For every unit vector \(u\),

\[V(u) \le \lambda_1 \quad\text{and}\quad D(u) \ge \lambda_2,\]

with equality at \(u = \pm v_1\). When \(\lambda_1 \gt \lambda_2\), equality holds only at \(u = \pm v_1\): the first principal axis is the line along \(v_1\).

Facts the proof uses.

  • \(V(u) = u^{\mathsf{T}}Su\), because \((u^{\mathsf{T}}d_i)^2 = u^{\mathsf{T}}d_id_i^{\mathsf{T}}u\);
  • \(S\) is symmetric, so by the spectral theorem from the lesson on symmetric matrices it has orthonormal eigenvectors \(v_1, v_2\) with real eigenvalues;
  • \(\lambda_2 \gt 0\): \(\lambda_2 = V(v_2)\) is an average of squares, and it would be 0 only if every \(d_i\) were perpendicular to \(v_2\), which would put all the points on one line;
  • \(D(u) = \lambda_1 + \lambda_2 - V(u)\): by Pythagoras \(V(u) + D(u)\) is the average of \(\|d_i\|^2\), which is \(S_{xx} + S_{yy}\), and this trace of \(S\) equals \(\lambda_1 + \lambda_2\);
  • ordinary algebra, and \(\cos^2\varphi + \sin^2\varphi = 1\).

Fix a unit vector \(u\). If \(u\) is an eigenvector of \(S\), then \(Su = \lambda u\) with \(\lambda = \lambda_1\) or \(\lambda = \lambda_2\), so \(V(u) = u^{\mathsf{T}}Su = \lambda \le \lambda_1\) and \(D(u) = \lambda_1 + \lambda_2 - \lambda \ge \lambda_2\), with equality exactly when \(\lambda = \lambda_1\); when \(\lambda_1 \gt \lambda_2\), the unit eigenvectors for \(\lambda_1\) are \(\pm v_1\). From here on \(u\) is not an eigenvector. Then \(\lambda_1 \gt \lambda_2\), because \(\lambda_1 = \lambda_2\) would make \(S = \lambda_1 I\). Also \(u\) lies along neither \(v_1\) nor \(v_2\), so after changing the sign of \(v_1\) or of \(v_2\) if needed, \(u = \cos\varphi\, v_1 + \sin\varphi\, v_2\) with \(0^\circ \lt \varphi \lt 90^\circ\).

Each option is one step about this \(u\). Pick the one that is true for every cloud and every unit vector \(u\) the setup allows: \(u\) is not an eigenvector, so \(\lambda_1 \gt \lambda_2 \gt 0\) and \(0^\circ \lt \varphi \lt 90^\circ\).

What else follows
  • The second axis. Line 2 also gives \(V(u) = \lambda_2 + (\lambda_1 - \lambda_2)\cos^2\varphi\), which is at least \(\lambda_2\): the direction \(v_2\), perpendicular to the first axis, has the least variance, \(\lambda_2\). The scores along \(v_1\) and \(v_2\) are uncorrelated: the average of their products is \(v_1^{\mathsf{T}}Sv_2\), and \(v_1^{\mathsf{T}}Sv_2 = \lambda_2v_1^{\mathsf{T}}v_2 = 0\).
  • More variables. For \(p\) variables, \(S\) is \(p \times p\) with eigenvalues \(\lambda_1 \ge \dots \ge \lambda_p\) and orthonormal eigenvectors \(v_1, \dots, v_p\). Writing \(u = \sum_j c_jv_j\) with \(\sum_j c_j^2 = 1\), the same computation gives \(V(u) = \sum_j \lambda_jc_j^2 \le \lambda_1\). Among directions perpendicular to \(v_1, \dots, v_{k-1}\) (so \(c_1 = \dots = c_{k-1} = 0\)), the largest variance is \(\lambda_k\), along \(v_k\): the \(k\)-th principal axis. The first \(k\) axes carry the share \(\frac{\lambda_1 + \dots + \lambda_k}{\lambda_1 + \dots + \lambda_p}\) of the total variance.
  • Equal eigenvalues. If \(\lambda_1 = \lambda_2\), then \(S = \lambda_1 I\) and every direction has variance \(\lambda_1\): a round cloud has no single first axis, as the explorer showed for equal standard deviations and \(\rho = 0\).
  • From the data matrix. Stack the \(d_i^{\mathsf{T}}\) as the rows of an \(n \times p\) matrix \(X\). Then \(S = X^{\mathsf{T}}X/n\), and a singular value decomposition \(X = U\Sigma W^{\mathsf{T}}\), as in the lesson on the SVD, gives \(S = W(\Sigma^{\mathsf{T}}\Sigma/n)W^{\mathsf{T}}\): the columns of \(W\) are principal axes, and \(\lambda_j = \sigma_j^2/n\). The rows must be the deviations: an SVD of the raw points, not centered, gives the directions of the largest mean square about the origin instead. Many software packages compute principal components this way, from the centered \(X\), without forming \(S\).
4 Apply

Problems

Each problem has hints and a full solution. Exact answers can be typed as a decimal or a fraction.

Key point To find principal components, center the data, form \(S\), and find its eigenvalues \(\lambda_1 \ge \lambda_2 \ge \dots\) and orthonormal eigenvectors. The \(k\)-th axis is \(v_k\), its scores \(v_k^{\mathsf{T}}d_i\) have variance \(\lambda_k\), and the first \(k\) axes carry the share \(\frac{\lambda_1 + \dots + \lambda_k}{\lambda_1 + \dots + \lambda_p}\) of the total variance.
Level Intermediate Prerequisites the covariance ellipse and symmetric matrices; projections, variance and eigenvectors (refreshed in Before you start) Time ~35 min

Related: the singular value decomposition computes principal components from the data matrix; least squares fits a line by vertical misses instead; the bootstrap can show how much the axes of a sample would vary.

Further reading