Skip to main content

Welcome to a Grasp lesson

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

Lesson 4

Orthogonal Projections, Least Squares, and Data Whitening

Recommended resources

Welcome back. In our last lesson, we established a complete geometric blueprint for any linear transformation by examining its four fundamental subspaces. We saw how any input vector can be decomposed into two orthogonal parts: a component in the row space that gets mapped to the output, and a component in the null space that is annihilated.

Today, we'll put this concept of orthogonality to work. We will explore how to project vectors onto subspaces, which is the geometric tool for finding the "best" approximation of a vector within a given subspace. This single idea has profound consequences. It will allow us to derive the method of least squares, the cornerstone of linear regression, directly from geometric principles. We will then extend this to see how projections and related transformations are used for data preprocessing in machine learning, specifically for decorrelating and whitening data.

Orthogonal Projection: Finding the Closest Point

At its heart, projection is about simplification. When we project a vector onto a subspace, we are finding the vector within that subspace that is closest to . The "error" of this approximation, the vector , is orthogonal to the subspace itself.

The Geometry Underneath the Algebra - by Agus Sudjianto

This article by Agus Sudjianto provides a wonderful high-level perspective on the geometric ideas we'll be using. It will frame our discussion perfectly.

Please read the introduction and the first section, "Projection Is Controlled Simplification". The key takeaway is the idea of projection as a "controlled simplification" that preserves relevant information and discards the rest.

The geometry of projection provides a powerful way to solve problems where an exact solution doesn't exist. To build the machinery, we will derive the formula for a projection matrix step-by-step, starting from the simplest case and building to the general one.

The following video provides an exceptionally clear, bottom-up derivation of the projection matrix formulas. It moves from projecting onto a single vector to projecting onto a general subspace, which is exactly the tool we need.

Orthogonal Projection Formulas (Least Squares) - Projection, Part 2

This video by Sam Levey builds the concept of projection from the ground up, visually and algebraically. It's a great way to see how the general formula arises.

Please watch the segments from projecting onto a unit vector to the general case. The key steps are: Projection onto a vector: The derivation begins by projecting one vector onto another. Pay attention to how the dot product is used to enforce orthogonality between the error vector and the target direction. Projection onto a subspace: The logic is then extended to projecting onto a plane (a 2D subspace). Notice how the problem becomes a system of linear equations. The General Formula: The final result is the formula for projecting onto the subspace spanned by the columns of an arbitrary matrix A. This is the main result we'll be using.

Let's summarize the crucial result from the video. To project a vector onto the column space of a matrix , we must find the point in such that the error vector is orthogonal to . As we learned last lesson, being orthogonal to the column space means being in the left null space, .

This gives us the core condition:

Rearranging this gives the celebrated normal equations:

Solving for gives the coordinates of the projection in the basis defined by . The projection vector itself is .

If is invertible, we can write:

And the projection is:

This defines the projection matrix that maps any vector to its projection in :


Application: The Geometry of Least Squares

The framework of orthogonal projection provides the most elegant derivation of the solution to the least squares problem. Consider an overdetermined system , common in data science, where you have more equations (observations) than unknowns. In this case, is typically not in the column space of , so no exact solution exists.

The goal is to find the "best" approximate solution, , which minimizes the length of the error vector . Geometrically, this minimum is achieved precisely when is the orthogonal projection of onto .

Therefore, solving the least squares problem is equivalent to solving the normal equations.

Professor Strang's lecture provides an excellent walkthrough of this connection. He masterfully switches between the two pictures of least squares: the data-fitting view with points and lines, and the vector space view with projections.

16. Projection Matrices and Least Squares

Professor Strang connects the abstract projection matrix to the concrete problem of fitting a line to data points. This is the heart of the lesson's learning outcome.

Please watch these key segments: Problem Setup: He sets up the problem of finding the best line to fit three non-collinear points, resulting in an inconsistent system of equations A\mathbf{x} = \mathbf{b}. Two Pictures: Here he explains the two views—the data plot with error bars, and the vector space view where \mathbf{b} is being projected onto C(A). This conceptual link is crucial. The Normal Equations: He derives and solves the normal equations for his example.

An essential technical point is the invertibility of the matrix . If this matrix isn't invertible, a unique least squares solution doesn't exist. This happens if the columns of are linearly dependent.

Professor Strang provides a concise proof that if the columns of are linearly independent (which is the case when they form a basis for the column space), then is guaranteed to be invertible. This is a key theoretical underpinning for least squares.

16. Projection Matrices and Least Squares

This short segment provides the proof for the invertibility of AᵀA, which guarantees a unique least squares solution when the columns of A are independent.

Watch the proof starting at the proof. The argument hinges on showing that the null space of A^TA is the same as the null space of A. If A has independent columns, its null space contains only the zero vector, and therefore so does A^TA, making it invertible.


Data Decorrelation and Whitening

We've seen how projections help solve systems. Now, let's see how related geometric transformations are used to preprocess data. In many machine learning algorithms, particularly older ones but also as a component in modern architectures, performance is improved if the input features are uncorrelated and have similar variance.

Let's represent our dataset as a matrix , where is the number of samples and is the number of features. We assume the data is centered, meaning the mean of each feature (column) has been subtracted.

The sample covariance matrix is then given by . The diagonal entries of are the variances of the features, and the off-diagonal entries are the covariances.

  1. Decorrelation: Our goal is to transform the data such that the new features are uncorrelated. This is equivalent to making the new covariance matrix diagonal. This is precisely what Principal Component Analysis (PCA) achieves. The principal components are the eigenvectors of the covariance matrix . Projecting the data onto these eigenvectors rotates the data to align with its axes of principal variance, which by construction are orthogonal.

  2. Whitening: Whitening takes this one step further. It not only decorrelates the data but also scales it so that each feature has a variance of 1. The resulting covariance matrix is the identity matrix, . Geometrically, if the original data forms an elliptical cloud, whitened data forms a spherical cloud. This process is also known as "sphering" the data.

The sequence of transformations to achieve whitening is shown beautifully in the following diagram.

This diagram illustrates the process of whitening. Starting with the original correlated data (top left), we first center it by subtracting the mean. Then, we rotate it to align with its principal axes (decorrelation). Finally, we scale each axis to have unit variance, resulting in a spherical, whitened data cloud.

There are two main types of whitening:

  • PCA Whitening: After rotating the data onto its principal axes (given by the right singular vectors of ), we get scores . We then scale each new feature by the inverse of its standard deviation (the singular values ). The full transformation is , where is the diagonal matrix of singular values of . The new covariance matrix is indeed .

  • ZCA Whitening: PCA whitening can drastically change the data by rotating it. ZCA (Zero-phase Component Analysis) whitening aims to whiten the data while staying as close as possible to the original. It achieves this by applying the inverse of the PCA rotation after whitening.

    You may see this written as , where is the inverse square root of the covariance matrix. This form is common in deep learning literature, for example, as a theoretical basis for some optimizer or normalization techniques.

The image below compares the result of these two transformations.

This image shows the effect of whitening on a 2D dataset. The "RAW data" is correlated. "PCA-whitened" data is spherical and rotated to align with the standard axes. "ZCA-whitened" data is also spherical but retains the original data's primary orientation.

Conclusion

In this lesson, we explored the practical power of orthogonality through the lens of projections.

Key Takeaways:

  • Orthogonal projection finds the closest point in a subspace to a given vector. The projection matrix projects vectors onto the column space of .
  • The least-squares solution to an inconsistent system is found by solving the normal equations , which is a direct consequence of orthogonal projection.
  • Decorrelation and whitening are crucial data preprocessing steps that simplify a dataset's statistical properties. They transform a correlated, anisotropic data cloud into an uncorrelated (diagonal covariance) or spherical (identity covariance) one.
  • These transformations are fundamentally geometric operations—rotations and scalings—revealed by the eigendecomposition of the covariance matrix or the SVD of the data matrix.

We are building a powerful geometric vocabulary. So far, we have seen how linear maps can be decomposed into rotations and scalings, and how we can use this to understand information flow, solve systems, and transform data distributions.

In our next lesson, we will add another layer to our understanding by exploring determinants and traces. We will see how determinants measure the change in volume induced by a linear transformation, a concept that is not just a theoretical curiosity but a critical component in one of your target implementations: Normalizing Flows.