Four observations sit at , , and , and each one must be replaced by a point on a single line. Which line loses the least total squared distance, and is it the line along which the data spread out the most? Principal component analysis answers both questions with one singular value decomposition, taken after the data are centered. This lesson builds the centered data matrix and the sample covariance matrix, proves that maximum projected variance and minimum reconstruction error choose the same subspaces, and shows how the answer changes when the center or the units change. Everything here is linear algebra on real data, with no probability model needed.
Key terms
Centered data matrix
Definition Let be observations, each a column of real measurements. Their mean is the vector
computed coordinate by coordinate. The centered data matrix is the real matrix whose th row is . So rows are observations and columns are measured coordinates. If has rows and is the vector of ones, then . Every column of sums to zero, , so the rows are dependent and . The mean is the best single center. For every ,
because the cross term vanishes. So is the unique minimizer of the total squared distance.
In plain words Subtract the average observation from every observation. The data then sit around the origin, and every later question is about variation around the mean rather than about distance from an arbitrary origin.
Example For the four observations above the mean is , and
Both columns sum to zero. In the first coordinate alone, the values have total squared distance from a number , which is smallest at the mean .
Sample covariance matrix
Definition For a centered data matrix with , the sample covariance matrix is the matrix
It is real symmetric and positive semidefinite, since , and . The diagonal entry is the sample variance of coordinate , and for the entry is the sample covariance of coordinates and . The divisor is a convention. Dividing by instead multiplies by the positive constant , which scales every variance by the same factor and leaves every eigenvector and every comparison unchanged.
In plain words The covariance matrix collects the variance of every coordinate and the covariance of every pair of coordinates in one symmetric table. Each entry is an inner product of two centered columns, divided by .
Example For the matrix above, the squared horizontal deviations add to , the squared vertical ones to , and every product of a horizontal and a vertical deviation is zero. So , and with
The two coordinates are uncorrelated, and the horizontal one varies more.
Projected variance
Definition Let be a unit vector. The scores of the centered observations along are the scalar projections , the entries of . They sum to zero, because , so their sample variance is
the projected variance along . This is the Rayleigh quotient of the symmetric matrix at a unit vector. In this lesson the eigenvalues of are listed in decreasing order, , to match the order of the singular values. By the bounds on the Rayleigh quotient, the maximum of the projected variance is , attained exactly at the unit vectors of the eigenspace of . For a -dimensional subspace spanned by the orthonormal columns of , the retained variance is
the sum of the projected variances along the columns of . It depends only on the subspace, and its largest possible value is , as the reconstruction error below shows.
In plain words Project every centered point onto a line and measure how spread out the projections are. The direction with the widest spread is a top eigenvector of the covariance matrix.
Example Along the four centered points have scores , , and , so the projected variance is
It is largest, , for the horizontal direction , and smallest, , for the vertical direction.
Principal components
Definition Let be a centered data matrix with and real full singular value decomposition , with orthogonal of size , orthogonal of size and singular values , where . Since , the right singular vectors are orthonormal eigenvectors of , with eigenvalues for and for any remaining ones. The principal directions, or principal axes, are in this order, and the th principal component score vector is , whose entries are the coordinates of the centered observations along . For the variance along is and . The first direction maximizes projected variance, and each later maximizes it among unit vectors orthogonal to . The directions live in the measurement space and the scores in the observation space . Books differ on whether “principal component” names the direction or the score vector, so say which one you mean. The directions are not unique. Replacing by flips the sign of its scores but leaves the reconstruction unchanged, and when singular values tie, any orthonormal basis of the tied eigenspace of serves equally well.
In plain words The principal directions are perpendicular axes for the data cloud, ordered by how much spread each one carries. One singular value decomposition of the centered matrix gives the axes, the spreads and the coordinates of every observation along the axes.
Example For the four points, , so , , and . The first score vector is
and the variances along the principal directions are and .
Reconstruction error
Definition Let have orthonormal columns spanning a subspace of dimension , and let be the orthogonal projector onto . The reconstruction of the centered data in is , whose rows are the orthogonal projections of the centered observations onto . Its reconstruction error is
Each row splits into perpendicular parts, so
The left side does not depend on , and is times the retained variance. So maximizing retained variance and minimizing reconstruction error are the same problem. When and is spanned by the top principal directions, is the truncated SVD, with error . Every has rank at most , so the Eckart-Young theorem in the Frobenius norm shows that no -dimensional subspace does better. The largest retained variance is , and when and the best subspace is unique. For the raw observations and an affine subspace , the same splitting around the mean gives
so a best affine fit of dimension can pass through the mean, and is one.
In plain words Replace every centered point by its projection on a -dimensional subspace and add up the squared distances lost. A point’s squared length is the squared length of its projection plus the squared loss, so keeping the most variance and losing the least are one and the same goal.
Example For the four points and the line through the mean in the direction , the total squared length is and the retained part is , so the reconstruction error is
It is smallest at , with value . For the raw observations the best line is , through the mean . It keeps and exactly and moves both and to , a squared loss of .
Effect of centering and units
Definition The answer of principal component analysis depends on two choices made before any computation, the center and the inner product on the measurement space, which the units of the coordinates fix. Without centering, the SVD of finds best subspaces through the origin, and since ,
A nonzero mean adds a rank-one term that can dominate the leading direction. Centered principal component analysis, by contrast, is unchanged when every observation is translated by the same vector. Changing units multiplies each coordinate by a positive constant , which replaces by and by , where , and this generally changes the principal directions. When every coordinate has nonzero variance, standardizing divides each coordinate by its sample standard deviation. That is the choice , and it turns into the correlation matrix. Standardizing is a modeling decision with mathematical consequences, not a step the mathematics makes for you. The optimality theorem guarantees a best subspace for the chosen center, units and dimension, and nothing more. A direction of large variance is not automatically important, causal or relevant to a prediction.
In plain words The method measures spread around a chosen center in chosen units. Move the center or rescale an axis and you are asking a different question, which can have a different answer.
Example Translate the four centered points up by , to , , and . Centering returns the same , so centered analysis still picks the horizontal direction. The raw Gram matrix is , since , so the leading uncentered direction is vertical and mostly describes the offset. Units matter too. Multiplying the vertical coordinate of the centered points by gives , , and , with , and the leading direction becomes vertical.
Check yourself
- Center the observations , , and . Find , the sample covariance matrix, the first principal direction, the variances along both principal directions, the best line and its reconstruction error.
Show the answer
The mean is , so the centered rows are , , and . The squared deviations add to horizontally and vertically, and every cross product is zero, so
The first principal direction is vertical, , with variance , and the second is horizontal with variance . The best line is the vertical line through the mean. It loses the horizontal deviations, a reconstruction error of , which is .
- The centered rows are , , and . Find the sample covariance matrix, the principal directions and their variances, the scores along the first direction, and the reconstruction error of the best line.
Show the answer
The columns sum to zero, so the data are centered. Adding squares and products gives
The vector is an eigenvector of with eigenvalue and one with eigenvalue . So with variance , and with variance . The scores along are . The best line is . It reconstructs the rows as , , and , with error , and the total splits as .
- The observations are , , and . Someone skips centering and takes the leading right singular vector of the raw data matrix. Which line through the origin do they get, and what is its squared error? What does centered principal component analysis give?
Show the answer
The raw Gram matrix is , because the squared first coordinates add to , the squared second coordinates to , and the products add to . The leading raw direction is vertical, so the line is the -axis, and it loses all the horizontal coordinates, a squared error of . Centering subtracts the mean and leaves the rows , , and , so . The first principal direction is horizontal with variance , and the best line is through the mean, with reconstruction error , because every point lies on it. The uncentered answer describes the offset of the data from the origin, not their variation.