Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 51

PCA: Maximizing Variance with Lagrange Multipliers

Recommended resources

Welcome to Module 11! In the previous modules, we explored unsupervised learning through the lens of clustering, where our goal was to discover hidden groups within our data. We now shift our focus to another fundamental task in unsupervised learning: dimensionality reduction.

High-dimensional data is everywhere, from images with millions of pixels to financial models with thousands of features. Working with such data presents challenges in computation, visualization, and interpretation—often called the "curse of dimensionality." Principal Component Analysis (PCA) is one of the most widely used and powerful techniques for addressing this problem.

Today's lesson addresses the first learning outcome for this module: to derive Principal Component Analysis (PCA) from first principles by maximizing variance using Lagrange Multipliers. We will build the entire method from the ground up, starting with a simple intuitive goal and using calculus and linear algebra to arrive at a beautiful and profound result. This approach, centered on a formal derivation, is tailored to your interest in the mathematical underpinnings of data science algorithms.

1. The Core Idea: Maximizing Variance

Imagine you have a cloud of data points in a high-dimensional space. If you were forced to represent this data using only one dimension (i.e., projecting it onto a single line), which line would you choose? Intuitively, you'd want to pick the line that spreads the projected data out as much as possible, thereby preserving the most information about the original data's structure. In statistical terms, "spreading out" means maximizing the variance of the projected data.

This is the central idea of PCA. The first principal component is the direction in space along which the data varies the most. The second principal component is the direction that captures the most remaining variance, with the added condition that it must be orthogonal (perpendicular) to the first. This process continues, with each subsequent component being orthogonal to all previous ones.

The following video provides an excellent high-level introduction to this concept.

Principal Component Analysis (PCA)

The video 'Principal Component Analysis (PCA)' by Visually Explained offers a great conceptual start. It uses a real-world example to explain why we need PCA and what it aims to achieve.

Watch the first minute of the video (0:00 - 1:05) to get a feel for the problem PCA solves and the goal of creating new, ranked factors (principal components) that preserve information.


2. Formulating the Problem Mathematically

Let's translate this idea into mathematics. We have a dataset of data points , where each . For simplicity, we'll assume the data has been centered, meaning the mean of the data points is the zero vector.

Our goal is to find a direction, represented by a unit vector (so that ), onto which we can project our data.

The projection of a single data point onto the line defined by is a scalar value given by the dot product . The variance of these projected points is:

Since the original data is centered, the mean of the projections is also zero (). So, the variance simplifies to:

This expression can be rewritten in a more compact matrix form, which is crucial for the derivation. The following video walks through this process.

Principal Component Analysis (The Math) : Data Science Concepts

The video 'Principal Component Analysis (The Math)' by ritvikmath provides a rigorous step-by-step derivation. This first segment sets up the problem and rewrites the variance in terms of the covariance matrix, a key step for our optimization.

Watch the segment from 2:02 to 8:09. Follow the derivation closely as it moves from the geometric idea of maximizing spread to the algebraic formulation for the variance of the projections. The key result you should understand is that the variance can be written as \mathbf{u}^T \mathbf{S} \mathbf{u}, where \mathbf{S} is the sample covariance matrix of the data.

As derived in the video, the variance of the projected data is neatly expressed as:

where is the sample covariance matrix of our centered data.

Our problem is now clearly defined:

  • Maximize:
  • Subject to the constraint: (i.e., is a unit vector)

3. The Derivation: Lagrange Multipliers to the Rescue

This is a classic constrained optimization problem, perfectly suited for the method of Lagrange multipliers. As you may recall from your math background, this method converts a constrained problem into an unconstrained one by introducing a new variable, the Lagrange multiplier .

We form the Lagrangian function :

To find the maximum, we take the gradient of with respect to and set it to zero. Using matrix calculus identities ( for symmetric and ), we get:

This simplifies to a familiar and powerful equation:

This is the eigenvalue equation! This remarkable result tells us that the optimal direction must be an eigenvector of the covariance matrix .

But which eigenvector? Let's go back to our objective function, the variance. If we pre-multiply our result by :

Since our constraint is , this simplifies to:

The variance we wanted to maximize is simply the eigenvalue . Therefore, to maximize the variance, we must choose the eigenvector that corresponds to the largest eigenvalue .

The following segment of the video walks through exactly this derivation.

Principal Component Analysis (The Math) : Data Science Concepts

Let's continue with the same video to see the Lagrange multiplier method in action.

Watch the segment from 8:09 to 12:15. This part is the core of today's lesson. It formulates the Lagrangian, takes the derivative, and arrives at the conclusion that the principal components are the eigenvectors of the covariance matrix, ranked by their eigenvalues.

To solidify this crucial derivation, please study the following formal proof. It mirrors the steps we've just discussed and serves as an excellent reference.

PCA: A formal description with proofs

This resource from a Multivariate Statistics course provides a concise, formal proof of the PCA derivation using Lagrange multipliers. It's a great way to review and consolidate the material from the video.

Read the text up to and including the 'Proof' section for Proposition 4.1. Focus on the derivation for the first component (j=1), which directly corresponds to what we've just covered.


4. Subsequent Principal Components

We have now found the first principal component, , which is the eigenvector of with the largest eigenvalue .

How do we find the second principal component, ? We follow the same logic: we want to find the direction that maximizes the variance, but with a new constraint that it must be orthogonal to .

  • Maximize:
  • Subject to: and

The inductive proof in the resource LINK shows that solving this new problem leads to the eigenvector of corresponding to the second-largest eigenvalue. This pattern continues for all subsequent components. The -th principal component is the eigenvector of the covariance matrix corresponding to the -th largest eigenvalue.


Conclusion

In this lesson, we derived Principal Component Analysis from first principles. We started with the intuitive goal of finding the direction of maximum variance in a dataset and used the method of Lagrange multipliers to solve this constrained optimization problem.

Key Takeaways:

  • PCA is a dimensionality reduction technique that finds a new set of orthogonal axes, called principal components, that align with the directions of maximum variance in the data.
  • The problem of finding the first principal component can be formulated as maximizing the variance of the projected data () subject to the constraint that the projection axis is a unit vector ().
  • Solving this using Lagrange multipliers leads directly to the eigenvalue equation .
  • This proves that the principal components are the eigenvectors of the data's covariance matrix . The amount of variance captured by each component is given by its corresponding eigenvalue .
  • To capture the most variance, the principal components are chosen by ranking the eigenvectors according to their corresponding eigenvalues in descending order.

Preview of the Next Lesson

We have just completed the core mathematical derivation of PCA. In the next lesson, we will build on this foundation to explain the geometric interpretation of PCA as a rotation of the coordinate system. We will also see how the solution we found today is deeply connected to the eigendecomposition of the covariance matrix, solidifying the link between the abstract goal of maximizing variance and the concrete machinery of linear algebra.