Four observations of two features sit at the corners of a rectangle, four units wide and two units tall, with center . You may keep only one number per observation, its coordinate along a line of your choice. Which line loses the least when the points are rebuilt from those numbers? Principal component analysis answers this by measuring spread around the mean, taking the directions of largest variance from the eigenvectors of a covariance matrix, and accounting exactly for what is kept and what is lost. Tuples such as stand for column vectors, and each observation is stored as a row of a data matrix, the transpose of such a column.
Key terms
Feature mean and centered data matrix
Definition A data matrix is a real matrix whose rows are observations and whose columns are features, the quantities recorded for each observation. Row is with in . The mean of feature is the average of column , and the feature means form the mean vector
where every entry of in equals . The centered data matrix has rows , that is
Every column of sums to zero, which says . Adding the same vector to every observation changes the mean to and leaves unchanged. Centering subtracts the means and nothing else. It does not change units or divide by any measure of spread.
In plain words Centering moves the origin to the middle of the cloud. What remains describes how the observations differ from one another, not where the cloud sits.
Example The observations , , and have feature means and , so and
Translating the whole rectangle to any other position would produce the same .
Sample covariance matrix
Definition Let and let be the centered data matrix. The sample covariance matrix is the matrix
Multiplying by column by row shows that is the sum of the outer products of the centered observations. The diagonal entry of is the sample variance of feature , the sum of the squared centered values in column divided by . For , the entry is the sample covariance of features and , the sum of the products of their centered values divided by . The matrix is symmetric and positive semidefinite, since for every . Its trace, the sum of the feature variances, is the total variance. Dividing by instead of gives mean squared deviations, which equal the sample variances times , and this page uses throughout. A covariance of zero says only that the products sum to zero. It does not mean that the features are statistically independent.
In plain words The diagonal says how widely each feature spreads around its mean. An entry off the diagonal is positive when two features tend to lie above or below their means together, and negative when one tends to be high while the other is low.
Example For the centered rectangle, the squared values in the first column add to , those in the second column add to , and the products add to . With ,
and the total variance is .
Projected variance
Definition Let be a unit vector in . The score of observation along is , and the scores form the vector in . They have mean zero, because , and their sample variance, the projected variance along , is
The point is the orthogonal projection of onto the line through with direction . The condition matters, because replacing by multiplies the quadratic form by without choosing a better direction.
In plain words Drop every point perpendicularly onto a line through the mean and measure how spread out the landing positions are. The quadratic form gives that spread for every direction at once, from alone.
Example For the rectangle and ,
The horizontal direction gives , the vertical direction gives , and the diagonal direction gives . The last value can be checked directly. The centered rows have scores , , and along the diagonal, their squares add to , and dividing by gives .
Principal component
Definition Let be the sample covariance matrix. By the spectral theorem it has real eigenvalues and an orthonormal basis of eigenvectors with . The eigenvalues are nonnegative because is positive semidefinite, and they are listed here in decreasing order . Writing a unit vector as with gives
with equality at . So the first principal direction has the largest projected variance, namely . In the same way, has the largest projected variance among unit vectors perpendicular to , and that variance is . The th principal component is the new variable measured along , with scores and sample variance . Many texts also call the direction itself a principal component. A direction can be replaced by its negative, which flips the sign of every score and changes nothing else, and when an eigenvalue is repeated any orthonormal basis of its eigenspace may be used. In terms of a full SVD , the right singular vectors serve as principal directions, and the eigenvalues of are the numbers for , followed by zeros when . For , the scores of the first components are , not alone.
In plain words The first principal direction spans a line along which the centered cloud is most spread out. The second is a direction of largest spread perpendicular to the first, and so on.
Example For the rectangle, is diagonal with distinct diagonal entries, so with and with . The first component has scores . Since , the singular values of are and , and indeed and . Choosing instead gives the scores and describes the same line.
Retained variance and reconstruction error
Definition For an integer with , keep the first principal directions as the columns of the matrix , which has orthonormal columns. The reconstruction of observation from its scores is
the orthogonal projection of onto the affine subspace . The retained variance is , the total variance is , and when the total is positive the retained fraction is their quotient. The reconstruction error is the total squared distance
Each residual is perpendicular to the kept directions, so the total squared length splits into the retained part plus the reconstruction error. The matrix is a truncated SVD of of rank at most , so by the Eckart-Young theorem no affine subspace of dimension through gives a smaller reconstruction error. One that misses does no better, because sliding it parallel to itself until it contains never increases the total squared distance. Maximizing the retained variance and minimizing the reconstruction error select the same directions.
In plain words Retained variance is the spread kept along the chosen directions, and reconstruction error is the spread thrown away, measured as squared distances from the points to their projections. Up to the factor the two add up to the total, so making one as large as possible makes the other as small as possible.
Example For the rectangle with , the centered rows are rebuilt as , , and . The lost second coordinates give reconstruction error , which equals and also . The total squared length is , so the first component keeps of , or percent, the same fraction as out of . Adding the mean back gives the reconstructed observations , , and , all on the horizontal line through the mean. A large retained fraction does not mean that the useful information was kept. This reconstruction has erased whether each point lay above or below the mean, which is exactly what some task might need.
Effect of centering and units
Definition Principal directions depend on two choices made before any eigenvalue is computed. The first is centering. Without it, the matrix of second moments about the origin takes the place of , and
so a mean far from the origin adds a large rank-one term in the direction of , which can dominate the leading eigenvector. The second choice is the units. Measuring feature in different units multiplies its centered column by a positive factor . With , the centered data become and the covariance matrix becomes , which in general has different eigenvectors from . Scaling every feature by the same factor multiplies by and leaves the directions alone. Standardizing, which divides each centered feature by its sample standard deviation so that every variance becomes , is one particular choice of and requires every feature variance to be positive. It answers a different question about the data, and it is not an automatic correction.
In plain words Subtracting the mean and choosing units are separate decisions, and each can change which direction counts as most variable. State both before reading meaning into a component.
Example The observations and have mean and centered rows and . With , the covariance matrix is , so the first principal direction is and it keeps all the variance. Without centering, , whose leading direction only reflects the constant first coordinate . For units, triple the second feature of the rectangle. The centered rows become , , and , the covariance matrix becomes , and the leading direction turns from horizontal to vertical.
Check yourself
- Find the feature means, the centered data matrix and the sample covariance matrix of the observations , and .
Show the answer
The means are and , so . Subtracting it from each row gives
The squared values add to in each column, and the products add to . With ,
- For the same data, find the first principal direction, the fraction of variance it retains, the reconstructions of the three observations from one component, and the reconstruction error.
Show the answer
The covariance matrix sends to and to , so its eigenvalues are and , and the first principal direction is . It retains , or percent, of the total variance. The scores are , and , so the centered reconstructions are , and . Adding the mean back gives , and . The residuals are , and , with squared lengths adding to , which matches .
- The centered observations , , and have equal feature variances. A student concludes that every direction retains the same variance, so no principal direction is preferred. Check the claim.
Show the answer
The squared values add to in each column, but the products add to , so
The variances are both , yet sends to and to . The eigenvalues are and , so the direction retains variance , which is percent of the total , while the horizontal direction retains only . The claim is false. Every direction retains the same variance only when is a multiple of the identity, as for the square with corners , where . Equal diagonal entries are not enough, because the covariance off the diagonal also matters.