Welcome back. In our last lesson, we explored the Spectral Theorem, establishing that a symmetric matrix like the covariance matrix can be diagonalized by its orthogonal eigenvectors: . We concluded with the powerful intuition that the eigenvectors of the covariance matrix represent the principal axes of variation in a dataset, and the eigenvalues quantify the variance along these axes.
Today, we will make this intuition mathematically rigorous. You will learn to derive Principal Component Analysis (PCA) from first principles, framing it as an optimization problem. By solving this problem, we will formally prove that the directions of maximum variance in the data are precisely the eigenvectors of the covariance matrix. This lesson will solidify the connection between the geometry of data and the algebraic properties of its covariance matrix, a cornerstone for understanding dimensionality reduction and data representation.
1. The Duality of PCA: Maximizing Variance vs. Minimizing Error
At its core, PCA seeks a lower-dimensional projection of a dataset that preserves as much "information" as possible. But what does it mean to preserve information? There are two equivalent ways to frame this goal, which turn out to be two sides of the same coin.
-
Maximizing Variance: We can project the data onto a new, lower-dimensional subspace such that the variance of the projected data is maximized. The intuition here is that directions with high variance are the most informative, while directions with low or zero variance are redundant or noisy.
-
Minimizing Reconstruction Error: We can project the data and then reconstruct it back into the original space. The goal is to find a projection that minimizes the squared distance between the original data points and their reconstructions.
The following image illustrates this duality. The gray line represents a potential principal component. Projecting the blue data points onto this line maximizes their spread (variance) along the line, while simultaneously minimizing the length of the red lines (the projection or reconstruction error).

We can see why these two objectives are equivalent through a simple geometric argument based on the Pythagorean theorem. Consider a single mean-centered data point and its projection onto the direction defined by a unit vector .
A single data point is projected onto the line defined by the unit vector . The projection is , the reconstruction error is , and the squared length of the projection is .
The original vector forms the hypotenuse of a right-angled triangle. The other two sides are the projection vector and the error vector . The squared length of the projection is . By the Pythagorean theorem:
Since the original data points are fixed, is a constant for each point. Therefore, maximizing the projected variance (which we'll see is a sum of terms) is equivalent to minimizing the sum of squared reconstruction errors ().
For a more formal walkthrough of this equivalence, the following reading is excellent.
This blog post by Peter Bloem provides a clear, step-by-step proof of the equivalence between minimizing reconstruction error and maximizing projected variance.
Focus on the section Minimal reconstruction error is maximal variance. Follow the derivation that starts from the variance maximization objective and shows how it algebraically transforms into the reconstruction error minimization objective.
While both perspectives are valid, the variance-maximization framework leads to a more direct and elegant derivation, which we will now pursue.
2. Deriving PCA: An Eigenvalue Problem
Let's formalize the problem of finding the direction of maximum variance. Assume we have a dataset represented by a matrix of size , where is the number of data points and is the number of features. We'll assume the data has been mean-centered, meaning the mean of each feature column has been subtracted, so the dataset's centroid is at the origin.
Our goal is to find the direction, represented by a unit vector of size , that maximizes the variance of the data when projected onto it.
The projection of a single data point (a row vector) onto the direction is a scalar value . The variance of these projected points is:
Since we are maximizing this quantity, the constant factor can be ignored. Our objective function becomes:
This expression can be rewritten in a more convenient matrix form. The summation is the squared norm of the projected data vector :
The matrix is closely related to the sample covariance matrix, . So, maximizing is equivalent to maximizing . This quantity is a quadratic form, which, as we saw in the previous lesson, describes how the transformation stretches space. We are looking for the direction where the stretch is greatest.
Our optimization problem is now:
This is a constrained optimization problem that can be solved using Lagrange multipliers. The Lagrangian is:
Taking the gradient with respect to and setting it to zero gives:
This is the fundamental eigenvector equation for the covariance matrix . It shows that the optimal direction must be an eigenvector of the covariance matrix.
But which one? To find out, let's substitute this result back into our objective function:
This is a crucial result. The variance of the data projected onto an eigenvector is simply the corresponding eigenvalue . To maximize the variance, we must choose the eigenvector corresponding to the largest eigenvalue.
This eigenvector is the first principal component (). The second principal component () is the direction that maximizes variance while being orthogonal to , and so on. The Spectral Theorem, which we covered last lesson, guarantees that for a symmetric matrix like , all its eigenvectors are mutually orthogonal. Thus, the principal components form an ordered, orthogonal basis, with each component corresponding to an eigenvector of the covariance matrix, ordered from the largest eigenvalue to the smallest.
The following resource provides an elegant and detailed walkthrough of this derivation.
Principal Component Analysis - Gregory Gundersen
This article provides a superb derivation of PCA as a variance-maximization problem. The author's approach is clear and builds beautifully from the optimization problem to the eigenvector solution.
Please read the subsection titled Computing the principal components. Pay close attention to how the author transforms the problem into the eigenbasis (using the variable \mathbf{w}) to show why the optimal solution must correspond to the largest eigenvalue. This is a very insightful way to solve the constrained optimization problem without explicitly using Lagrange multipliers.
3. Interpreting the Components
We have now formally derived the main result of PCA:
- The principal components are the orthonormal eigenvectors of the data's covariance matrix .
- The variance of the data projected onto each principal component is the corresponding eigenvalue .
This provides a clear recipe for finding and interpreting the components. The eigenvectors tell us the directions of greatest variance, and the eigenvalues tell us the magnitude of that variance.
By sorting the eigenvalues in descending order, , we get a natural ranking of the principal components in terms of their importance. The total variance in the data is given by the trace of the covariance matrix, which is also equal to the sum of its eigenvalues:
The fraction of total variance "explained" by the -th principal component is simply . This allows us to perform dimensionality reduction in a principled way: we can keep the first principal components that capture a desired percentage (e.g., 95%) of the total variance, and discard the rest. The data transformed into this new basis, , where the columns of are the top eigenvectors, will have uncorrelated features whose variances are the eigenvalues.
The following visual summary captures the entire process, from a correlated dataset to the final, uncorrelated principal components.

To solidify your understanding of these final steps, please continue with the same article.
Principal Component Analysis - Gregory Gundersen
This reading covers the practical interpretation and application of the PCA results we just derived.
Please read the following three short sections starting from the "A Complete Example" heading: Principal components and eigenvectors: This section explicitly states the connection between the covariance matrix eigenvectors and the principal components. Latent variables with uncorrelated features: This proves that the new features (data projected onto the principal components) are uncorrelated and their variances are the eigenvalues. Dimension reduction and variance explained: This explains how to use the eigenvalues to decide how many components to keep.
Conclusion
In this lesson, we have formally derived Principal Component Analysis from the ground up, connecting the intuitive goal of preserving information to a concrete mathematical procedure.
Key Takeaways:
- PCA aims to find a lower-dimensional representation of data by either maximizing the variance of the projected data or, equivalently, minimizing the reconstruction error.
- Formulating PCA as a variance-maximization problem leads to the optimization problem subject to , where is the covariance matrix.
- The solution to this problem reveals that the principal components are the eigenvectors of the covariance matrix.
- The variance of the data along the direction of each principal component is given by its corresponding eigenvalue.
- This provides a principled method for dimensionality reduction: by ranking components by their eigenvalues, we can retain the most significant dimensions of variation and discard the least significant ones.
This lesson concludes our module on matrix decompositions. We have seen how SVD and eigendecomposition are not just abstract mathematical tools, but powerful lenses for analyzing the structure of data, models, and loss landscapes. Having established this geometric foundation, we will now pivot to the calculus of optimization. Our next module begins by laying the groundwork for automatic differentiation, starting with the fundamentals of vector and matrix calculus.