Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

PCA: Eigendecomposition of the Covariance Matrix

Hello! Welcome to the fifth lesson in our module on eigenanalysis.

Introduction

In our last lesson, we established the theory of matrix diagonalization. We proved that an matrix is diagonalizable if and only if it has linearly independent eigenvectors, and we saw that this allows for the powerful eigendecomposition . We noted that symmetric matrices are a special case, as they are always diagonalizable and have an orthonormal basis of eigenvectors.

Today, we will apply this powerful decomposition to derive one of the most fundamental techniques in data science: Principal Component Analysis (PCA). The learning outcome for this lesson is to use the eigendecomposition of a covariance matrix to derive the formulation of Principal Component Analysis (PCA).

We will start by framing the intuitive goal of PCA—finding the directions of maximum variance in a dataset. We will then formalize this goal mathematically and, through two distinct but complementary derivations, prove that the solution to this problem is found by computing the eigenvectors of the data's covariance matrix. This lesson will provide the complete theoretical "why" behind PCA.

1. The Goal of PCA and the Covariance Matrix

Imagine you have a dataset with many features (i.e., it's high-dimensional). Many of these features might be correlated, meaning they contain redundant information. The goal of PCA is to find a new, smaller set of uncorrelated features, called principal components, that capture the maximum possible variance from the original data. In essence, it's a method for dimensionality reduction that aims to lose the least amount of information.

The central mathematical object in PCA is the covariance matrix.

Covariance Matrix Explained
This image defines the formula for the covariance between two variables and lists key properties of the covariance matrix. For our purposes, we will assume the data is 'centered' (mean of each variable is subtracted), which simplifies the calculation.

Let's consider a dataset represented by a matrix of size , where is the number of features and is the number of data points. After centering the data (subtracting the mean from each feature), the covariance matrix is a square matrix calculated as:

The key properties of the covariance matrix are:

  • Symmetry: . The covariance between feature and feature is the same as between and .
  • Diagonal entries: The diagonal elements are the variances of each feature.
  • Off-diagonal entries: The off-diagonal elements are the covariances between features and .

From our last lesson, we know that symmetric matrices are special. A symmetric matrix is always diagonalizable, and its eigenvectors form an orthonormal basis. This property is the linchpin of the PCA derivation.

2. Derivation 1: PCA as Variance Maximization

The most common way to derive PCA is to frame it as a constrained optimization problem. We want to find the direction in our feature space (represented by a unit vector ) along which the projected data has the greatest possible variance.

Let's formalize this.

  1. Projection: A data point (a column vector) is projected onto the direction . The coordinate of the projection is given by the dot product .
  2. Variance of Projections: We want to find the variance of these projected coordinates over the whole dataset.
  3. Objective: Maximize this variance.
  4. Constraint: The direction must be a unit vector, i.e., . Without this constraint, we could make the variance arbitrarily large just by scaling .

The following video walks through this derivation step-by-step.

10.1 Principal Component Analysis: Maximum Variance (UvA - Machine Learning 1 - 2020)

This video from the University of Amsterdam derives PCA from the maximum variance principle. It will show how the variance of the projected data can be expressed using the covariance matrix, and then use the method of Lagrange multipliers to solve the constrained optimization problem.

Watch the video from 07:28 to 15:19. The first part (07:28 - 11:24) derives the expression for the variance of the projected data: Var(projection) = u₁ᵀSu₁. The second part (11:24 - 15:19) sets up the Lagrangian and solves for the optimal u₁, showing it must be an eigenvector of S.

Let's summarize the key steps of the derivation you just saw.

  1. Expressing the Variance: The variance of the data projected onto a direction is neatly expressed as:

    where is the covariance matrix.

  2. Constrained Optimization: The problem becomes:

  3. Lagrangian Formulation: We introduce a Lagrange multiplier and form the Lagrangian:

  4. Solving for the Optimum: To find the maximum, we take the gradient with respect to and set it to zero:

    This simplifies to:

This is precisely the eigenvalue equation for the covariance matrix . This proves that the directions of maximum variance must be the eigenvectors of the covariance matrix.

But which eigenvector? Let's substitute this result back into our expression for the variance:

The variance captured by an eigenvector is its corresponding eigenvalue . Therefore, to maximize the variance, we must choose the eigenvector with the largest eigenvalue. This eigenvector is the first principal component (PC1). The second principal component is the eigenvector with the second-largest eigenvalue, and so on.

3. Derivation 2: PCA as Diagonalization

There is another, equally powerful way to derive PCA that connects directly to our work on diagonalization from the previous lesson. This approach frames the goal not as maximizing variance, but as finding a new basis where the features are uncorrelated.

If the new features are uncorrelated, their covariance matrix must be a diagonal matrix (all off-diagonal covariance terms are zero).

A Tutorial on Principal Component Analysis

This tutorial provides an elegant derivation of PCA from this 'diagonalization' perspective. It demonstrates that the transformation that diagonalizes the covariance matrix is precisely the one formed by its eigenvectors.

Read Section V, 'SOLVING PCA: EIGENVECTORS OF COVARIANCE'. This section is short but dense. Follow the algebra that starts with the covariance of the transformed data, C_Y, and shows that to make C_Y diagonal, the transformation matrix P must be composed of the eigenvectors of the original covariance matrix.

Let's walk through the logic from that reading.

  1. Define the Transformation: Let the original data be with covariance matrix . Let be an orthonormal transformation matrix where each row is a new basis vector. The transformed data is .

  2. Covariance of Transformed Data: The covariance matrix of the new data, , is:

  3. The Goal: We want to be a diagonal matrix, which we'll call . So our goal is to find an orthonormal matrix such that:

  4. Connecting to Eigendecomposition: Since is orthonormal, . We can right-multiply by (which is ) to get:

    And by pre-multiplying by , we get:

This final equation, , is exactly the eigendecomposition of the symmetric matrix that we studied in the last lesson (where the columns of are the eigenvectors of , and is the diagonal matrix of eigenvalues).

Therefore, the rows of the transformation matrix —our principal components—must be the orthonormal eigenvectors of the covariance matrix . This second derivation beautifully confirms our first result: finding a basis that uncorrelates the data is equivalent to finding the eigenvectors of the covariance matrix.

4. Synthesis and Properties

Both derivations lead to the same fundamental conclusion:

The principal components of a dataset are the orthonormal eigenvectors of its covariance matrix. The first principal component is the eigenvector corresponding to the largest eigenvalue, the second principal component corresponds to the second-largest eigenvalue, and so on.

This is the core theoretical formulation of PCA.

PCA in a Nutshell: A Step-by-Step Visual Guide
This image provides a clear, step-by-step visualization of the entire PCA process, from the original data to the final low-dimensional representation. Steps 3 and 4 (computing the covariance matrix and its eigenvectors/eigenvalues) are what we have mathematically derived today.

A few important properties emerge from this derivation:

  • Variance Explained: The eigenvalue corresponding to the -th principal component represents the variance of the data projected onto that component.
  • Total Variance: The sum of all the eigenvalues is equal to the trace (sum of the diagonal elements) of the original covariance matrix . This means the sum of the eigenvalues is the total variance in the original data.
  • Uncorrelated Components: As shown in the second derivation, the new features (projections onto the principal components) are mutually uncorrelated. Their covariance matrix is diagonal.
Test your understanding!

Suppose you perform PCA on a 2D dataset and find the covariance matrix . You compute its eigenvalues and , with corresponding orthonormal eigenvectors and .

  1. Which vector represents the first principal component (PC1)? Why?
  2. What is the variance of the data when projected onto PC1?
  3. What percentage of the total variance is captured by PC1?
  4. In the new coordinate system defined by the basis , what is the covariance matrix of the data?
Show answer
  1. is the first principal component (PC1) because it is the eigenvector corresponding to the largest eigenvalue ().
  2. The variance of the data projected onto PC1 is equal to its corresponding eigenvalue, so the variance is 10.
  3. The total variance is the sum of the eigenvalues: . The percentage of variance captured by PC1 is .
  4. In the new basis formed by the eigenvectors, the data is uncorrelated. The new covariance matrix is a diagonal matrix with the eigenvalues on the diagonal:

Conclusion

In this lesson, we have rigorously derived the mathematical formulation of Principal Component Analysis. By connecting the intuitive goal of finding directions of maximum data variance to the formal machinery of linear algebra, we have proven a cornerstone result of machine learning.

Key Takeaways:

  • PCA seeks to find a set of orthogonal axes (principal components) that capture the maximum variance in a dataset.
  • We proved through two different derivations (variance maximization and covariance diagonalization) that these principal components are the eigenvectors of the data's covariance matrix.
  • The eigenvalues of the covariance matrix quantify the variance captured by each corresponding principal component, allowing us to rank them by importance.
  • The eigendecomposition of the covariance matrix, , provides the complete solution: contains the principal components, and contains their associated variances.

Preview of the Next Lesson:
Now that we have established the "why" and "what" of PCA, the next lesson will focus on the "how." We will move from derivation to application, learning how to take a data matrix, compute its principal components, and use them to perform dimensionality reduction. We will discuss the practical steps of choosing how many components to retain and visualizing the transformed data.

Can't find a good explanation? Sign up and we'll make it for you

Sign up