14 min read
Why Does PCA Work? A Geometric and Statistical Derivation
Md Anisujjaman
A step-by-step derivation of principal component analysis, connecting variance maximization, covariance eigenvectors, and optimal linear reconstruction.
Why Does PCA Work? A Geometric and Statistical Derivation
Principal Component Analysis (PCA) is one of the most widely used methods for dimensionality reduction. It appears in exploratory data analysis, visualization, noise reduction, and machine learning pipelines.
But why does it work?
PCA does not simply select a few original features. Instead, it constructs new, mutually orthogonal directions that capture as much of the data’s variation as possible. These directions turn out to be the eigenvectors of the covariance matrix, ordered by their corresponding eigenvalues.
This article derives that result from first principles. We will connect three perspectives:
- Statistical: Find directions that preserve the greatest variance.
- Geometric: Project data onto a lower-dimensional subspace while losing as little as possible.
- Computational: Use eigendecomposition or Singular Value Decomposition (SVD) to find the optimal subspace.
Finally, we will test these connections on a reproducible synthetic dataset with 50 features.
1. The problem: representing high-dimensional data
Suppose our dataset contains observations and numerical features. We represent it as a matrix:
Each row represents an observation, and each column represents a feature.
When is large, analyzing the dataset directly can be difficult. We may want a lower-dimensional representation with only dimensions, where .
A simple approach would be to select of the original features. However, this can discard important relationships between features. A better alternative is to construct new features as linear combinations of the original ones.
Before doing so, we center the data by subtracting the mean of each feature:
where contains the feature means and is a vector of ones.
After centering, each feature has mean zero, up to numerical precision. The sample covariance matrix is then
The matrix summarizes the variances of individual features and the covariances between pairs of features.
Our goal is to discover a small number of directions that retain as much useful variation in the centered data as possible.
2. Why maximize projected variance?
Consider a single direction . Projecting each observation onto this direction produces a one-dimensional representation:
Here, contains the projected values for all observations.
The sample variance of these projected values is
Substituting , we obtain
Therefore, the variance captured by a direction is determined by the quadratic form .
There is one complication: multiplying by a large constant also multiplies the projected variance by the square of that constant. Without a constraint, we could increase the objective indefinitely without finding a more meaningful direction.
We solve this by requiring the direction to have unit length:
Our optimization problem becomes
This is the central idea behind PCA: find a unit direction along which the projected data has maximum variance.
The statistical formulation of PCA is associated with the work of Hotelling, while the broader geometric perspective of fitting lower-dimensional linear structures has earlier roots in Pearson’s work.(Hotelling (1933), Pearson (1901), and Jolliffe and Cadima (2016))
3. Deriving the first principal component
We now solve the optimization problem.
3.1 Use a Lagrange multiplier
We want to maximize subject to . Introduce a Lagrange multiplier and define
Taking the gradient with respect to gives
At a stationary point, the gradient must equal zero:
Rearranging yields
This is precisely the eigenvector equation.
The direction must be an eigenvector of the covariance matrix, and is its associated eigenvalue.
3.2 Why choose the largest eigenvalue?
The eigenvector equation tells us which directions are candidates, but we still need to determine which candidate maximizes the variance.
Multiplying on the left by gives
Since ,
The variance captured by a unit eigenvector is therefore equal to its eigenvalue.
Because the covariance matrix is symmetric and positive semidefinite, its eigenvalues are real and nonnegative. Let them be ordered as
The maximum attainable variance is . Consequently, the first principal component is an eigenvector associated with the largest eigenvalue:
Its projected data is , and its variance is .
Two details are worth remembering:
- The sign of an eigenvector is arbitrary. The directions and describe the same one-dimensional subspace and preserve identical variance.
- If the largest eigenvalue is repeated, there may be multiple equally optimal directions. The individual first principal component is then not unique, although its optimal subspace is well defined up to the corresponding eigenspace.
The key result: The first principal component is a leading covariance eigenvector because maximizing projected variance under a unit-length constraint leads directly to the eigenvector equation.
4. From one component to components
Keeping one dimension may discard too much information. We therefore extend the problem to a -dimensional subspace.
Let
where the columns are orthonormal:
The lower-dimensional representation is
Its total sample variance, summed across the orthogonal component directions, is
Here, denotes the Frobenius norm, and denotes the matrix trace.
We now want to maximize
The solution is a subspace spanned by eigenvectors corresponding to the largest eigenvalues of . Its maximum retained variance is
This result follows from the maximum principle for sums of eigenvalues, commonly associated with Ky Fan’s work. It extends the one-component argument: rather than retaining just the direction with the greatest variance, we retain the -dimensional subspace with the greatest total variance. (Fan (1951))
4.1 Explained variance
The total variance of the centered dataset is
The proportion of variance retained by the first components is therefore
This is called the cumulative explained variance ratio.
For example, if the first five components explain 90% of the total variance, then retaining those components preserves 90% of the dataset’s variance under this measure.
It does not mean that 90% of every feature, every observation, or every task-relevant signal has been preserved. Explained variance is a specific mathematical criterion, not a universal measure of information.
5. The geometric connection: minimizing reconstruction error
Variance maximization gives us one reason to use PCA. There is another, geometric interpretation: PCA finds the best lower-dimensional linear subspace for reconstructing centered data under squared Euclidean error.
Suppose we project the observations into the subspace spanned by :
We can reconstruct an approximation in the original feature space by projecting back:
The reconstruction residual is
A natural measure of reconstruction quality is the sum of squared errors:
Because is an orthogonal projection matrix, the squared norm separates into the energy retained by the projection and the energy discarded by it. Thus,
Using , we get
Notice that is constant for a fixed dataset. Minimizing the reconstruction error is therefore equivalent to maximizing the variance retained by the projection.
This establishes the connection:
Both objectives select the subspace spanned by the leading covariance eigenvectors.
This is a precise statement about centered data, orthogonal linear projections, and squared reconstruction error. It does not claim that PCA minimizes every possible loss function or preserves all information relevant to a downstream task.
The geometric interpretation is closely connected to Pearson’s early work on lines and planes of closest fit. The matrix approximation perspective is formalized by the Eckart–Young theorem.(Pearson (1901) and Eckart and Young (1936))
5.1 The SVD connection
There is a computationally useful way to express the same result.
Take the Singular Value Decomposition of the centered data matrix:
The columns of are the right singular vectors, and the diagonal entries of are the singular values:
Since
substituting the SVD gives
Therefore, the covariance eigenvectors are the right singular vectors of , and their eigenvalues satisfy
The optimal rank- reconstruction error is consequently
This is the low-rank approximation connection: keeping the leading singular directions minimizes the squared Frobenius reconstruction error among rank- approximations.
In practice, PCA can be computed through covariance eigendecomposition or directly through SVD. Direct SVD is often numerically preferable when forming the covariance matrix could amplify conditioning problems. Which method is faster depends on the relative sizes of and , the implementation, and the computational environment. (Eckart and Young (1936); see also the scikit-learn PCA documentation)
6. Numerical verification: a 50-dimensional experiment
The derivation tells us what PCA should do. We can now test its consequences on a controlled dataset.
Rather than using a two-dimensional example that can be inspected visually, we construct a dataset with 2,000 observations and 50 features.
6.1 Constructing the dataset
We generate five latent factors and let each factor influence a block of ten observed features. Independent Gaussian noise is then added to the observations.
A simplified representation of the data-generating process is
where:
- contains the latent factor values.
- contains the feature loadings.
- contains independent Gaussian noise.
We use a fixed random seed for reproducibility, set the noise standard deviation to 0.35, and center the generated data before performing PCA.
This setup provides two things to investigate: whether PCA recovers the planted five-dimensional factor subspace, and whether its reconstruction error is lower than that of randomly chosen subspaces.
6.2 Inspecting the correlation structure
Figure 1. Feature correlation structure of the synthetic dataset. The block structure reflects the way the latent factors influence groups of features.
The visible block structure is a consequence of the data-generation process. Features influenced by the same latent factor tend to share variation.
This does not mean that real datasets necessarily have clean blocks or five underlying factors. Here, the controlled structure gives us a known reference against which to evaluate PCA.
6.3 Measuring explained variance
Figure 2. Cumulative explained variance as the number of retained principal components increases.
Our experiment produced the following results:
| Components retained | Cumulative variance retained |
|---|---|
| 1 | 21.07% |
| 2 | 40.90% |
| 3 | 58.51% |
| 5 | 90.69% |
| 10 | 91.98% |
| 20 | 94.33% |
| 30 | 96.45% |
| 40 | 98.35% |
| 50 | 100.00% |
The first five principal components retain approximately 90.69% of the total variance. Retaining all 50 components preserves 100% of the variance, apart from negligible floating-point effects.
Why do five components not explain exactly 100% of the variance, given that we generated five latent factors?
Because the observed dataset also contains noise. The noise contributes additional variation outside the planted latent subspace. The five-factor construction does not imply that the observed matrix has rank five.
The result illustrates an important distinction: the number of latent factors used to generate data is not automatically the number of principal components required to explain all observed variance.
6.4 Comparing PCA with random subspaces
To test the geometric interpretation, we compare PCA against 50 randomly generated orthogonal subspaces at each selected dimension.
For each , we calculate the squared reconstruction error and express it as a percentage of the total squared norm of the centered dataset.
Figure 3. Normalized reconstruction error for PCA and sampled random subspaces. PCA gives the minimum error theoretically; the random-subspace results illustrate this behavior empirically.
For five components, PCA retains 90.69% of the variance and discards 9.31%. Its normalized reconstruction error is therefore approximately 9.31%.
The mean normalized error across the 50 sampled random five-dimensional subspaces is approximately 90.12%.
| Components | PCA error | Mean random-subspace error |
|---|---|---|
| 1 | 78.93% | 97.99% |
| 2 | 59.10% | 96.25% |
| 3 | 41.49% | 94.12% |
| 5 | 9.31% | 90.12% |
| 10 | 8.02% | 80.08% |
| 20 | 5.67% | 59.95% |
In this experiment, PCA’s reconstruction error was no greater than that of any sampled random subspace for the tested dimensions.
That is consistent with the theoretical result, but the random comparison is illustrative rather than a proof. Sampling 50 alternatives cannot establish optimality over every possible subspace. The proof comes from the variance-maximization and low-rank approximation results derived earlier.
6.5 Checking the numerical identities
We also verify the mathematical identities directly.
Check 1: covariance eigenvalues versus singular values.
We compare the eigenvalues of the sample covariance matrix with the squared singular values divided by :
The maximum absolute difference in our run was approximately , consistent with floating-point numerical precision.
Check 2: reconstruction error versus the discarded singular-value tail.
For each tested , we compare the directly computed reconstruction error with
The values agreed to numerical precision for all tested dimensions.
Check 3: recovery of the planted factor subspace.
We compare the five-dimensional subspace spanned by the leading principal components with the planted factor subspace. The largest principal angle between the two subspaces was approximately , and the Frobenius norm of the difference between their projection matrices was approximately 0.0544.
These results indicate close recovery of the planted subspace in this controlled experiment. The principal angles compare the subspaces as wholes; they do not require individual PCA directions to correspond one-to-one with the original latent factors.
Together, the checks provide numerical evidence that the implementation is consistent with the theoretical relationships.
7. What PCA does not guarantee
PCA has a strong mathematical foundation, but its assumptions and objective matter.
7.1 Centering is not standardization
Centering subtracts each feature’s mean. Standardization additionally scales features, often to unit variance.
If one feature is measured in thousands and another in fractions, the high-magnitude feature may dominate the covariance matrix. Whether to standardize depends on the meaning of the features and the analytical objective; it is not automatically necessary in every application.
7.2 High variance is not always useful information
PCA preserves variation, not necessarily predictive value. A low-variance feature can be essential for predicting a target, while a high-variance feature can mostly represent irrelevant fluctuations.
Consequently, retaining a large explained-variance percentage does not guarantee strong predictive performance.
7.3 Outliers can change the components
Covariance and variance are sensitive to extreme observations. A small number of outliers can substantially alter the leading principal directions.
Data inspection and, when appropriate, robust alternatives should be considered before interpreting the components.
7.4 PCA is a linear method
PCA finds an optimal linear subspace under its objective. It does not necessarily discover nonlinear structure.
A dataset may lie near a curved manifold that cannot be represented well by a low-dimensional linear projection. In such cases, other dimensionality-reduction approaches may be more appropriate, depending on the task.
7.5 Explained variance is not a universal stopping rule
Choosing by a threshold such as 90% or 95% is a useful heuristic, not a mathematical guarantee of downstream usefulness. The choice should also reflect interpretability, computational requirements, reconstruction quality, and—when applicable—performance on held-out data.
For practical implementation details and solver considerations, consult the scikit-learn PCA documentation.
8. Conclusion
PCA works because several mathematical ideas converge on the same solution.
First, maximizing the variance of a unit projection leads to the eigenvector equation for the covariance matrix. The largest eigenvalue identifies the direction with the greatest projected variance.
Second, extending the problem to orthogonal directions shows that the leading eigenvectors maximize total retained variance.
Third, projecting onto this subspace minimizes squared reconstruction error among -dimensional orthogonal linear projections of the centered data. The SVD expresses the same result through the singular values and gives a direct formula for the discarded reconstruction error.
The numerical experiment supports these connections: the covariance eigenvalues agree with the SVD relationship, the reconstruction error agrees with the discarded singular-value tail, and PCA closely recovers the planted factor subspace.
The broader lesson is that PCA is not merely a convenient transformation. Its usefulness follows from a precise optimization objective. Understanding that objective also tells us when PCA is appropriate—and when preserving variance may not be enough.
References
-
Pearson, K. (1901). LIII. On lines and planes of closest fit to systems of points in space. The London, Edinburgh, and Dublin Philosophical Magazine and Journal of Science, 2(11), 559–572.
-
Hotelling, H. (1933). Analysis of a complex of statistical variables into principal components. Journal of Educational Psychology, 24(6), 417–441.
-
Jolliffe, I. T., & Cadima, J. (2016). Principal component analysis: a review and recent developments. Philosophical Transactions of the Royal Society A: Mathematical, Physical and Engineering Sciences, 374(2065), 20150202.
-
Eckart, C., & Young, G. (1936). The approximation of one matrix by another of lower rank. Psychometrika, 1, 211–218.
-
Fan, K. (1951). Maximum properties and inequalities for the eigenvalues of completely continuous operators. Proceedings of the National Academy of Sciences, 37(11), 760–766.
-
scikit-learn developers. PCA: Principal component analysis. scikit-learn documentation.