Skip to main content

Welcome to a Grasp lesson

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

Lesson 41

Solving Least Squares: Normal Equations & QR Decomposition

Recommended resources

Hello! Welcome back to our module on Applications in Data Analysis.

In our last lesson, we formulated the least squares problem, turning the intuitive goal of finding a "best-fit" line into a precise mathematical objective. We started with an overdetermined, inconsistent system and derived the normal equations: . This gave us a new, solvable system for the optimal parameters .

Today, our goal is to solve the least squares problem using the normal equations or QR decomposition. We will explore two distinct methods for finding the vector that minimizes . This is a crucial skill for your future studies in deep learning, as these methods form the basis for training simple machine learning models like linear regression.

1. Method 1: Solving the Normal Equations

In the previous lesson, we used a geometric argument—the orthogonality of the error vector to the column space of A—to derive the normal equations. Given your background in multivariate calculus, you might appreciate an alternative derivation based on optimization.

The least squares problem is to find the vector that minimizes the scalar function:

Expanding this, we get:

Since is a scalar, it's equal to its own transpose, . So we can simplify this to:

To find the minimum, we take the gradient with respect to and set it to zero. The gradient of this quadratic form is:

Setting gives us , which simplifies to the familiar normal equations:

This confirms our geometric result using calculus.

A Practical Example

Now that we have the equation, let's see how it's used in practice.

Normal equation solution of the least-squares problem | Lecture 27 | Matrix Algebra for Engineers

The following video, 'Normal equation solution of the least-squares problem' by Jeffrey Chasnov, provides a clear, step-by-step calculation for finding a best-fit line using this method.

Please watch the segment from 6:18 to 12:59. Focus on the concrete steps: constructing A and b, calculating A^T A and A^T \mathbf{b}, and finally solving the resulting 2x2 system.

As the video demonstrates, the procedure is:

  1. Set up the matrix and vector from your data points and model.
  2. Compute the matrix and the vector .
  3. Solve the square linear system for the parameter vector . You can use any method for solving a linear system, such as Gaussian elimination.

The Caveat: Numerical Instability

The normal equations are elegant, but they have a significant drawback in computational practice. The process of forming can dramatically worsen the conditioning of the problem.

A key result states that the condition number of is the square of the condition number of :

If cond(A) is large (meaning the matrix is ill-conditioned), cond(A^T A) will be much larger. This means that small errors in the input data can be greatly amplified in the solution, leading to numerical instability and inaccurate results. This is a critical concern in computer science and numerical analysis.

This motivates the search for a more robust method.


2. Method 2: Solving via QR Decomposition

A better way to solve the least squares problem is to use the QR decomposition, which you'll remember from our module on orthogonality. This method avoids forming altogether.

The Core Idea

Let's start again with the goal of minimizing .

  1. Decompose the matrix (assuming it has linearly independent columns) into , where is an matrix with orthonormal columns and is an invertible upper triangular matrix.
  2. Substitute this into our expression:
  3. Recall a key property of orthogonal matrices: they preserve the Euclidean norm. That is, . We can multiply the vector inside the norm by without changing the value:
  4. Since has orthonormal columns, (the identity matrix). The expression simplifies beautifully:

    Our problem is now to find the that minimizes . Since is invertible, we can make this norm exactly zero by choosing such that:

    This leaves us with a much nicer system to solve. Because is upper triangular, we can find easily using back substitution, which is computationally efficient and stable.

The following reading provides a great summary of both the normal equations and this QR approach.

2. Least squares: geometric, algebraic, and numerical ...

The textbook 'Mathematics for Machine Learning and Data Science' gives a concise overview of using QR decomposition for least squares.

Read the first half of the section titled “QR decomposition and Householder transformations.” Focus on the part that shows how QR decomposition is used to solve linear least squares problems. Don't worry about the details of Householder reflections for now; the key is understanding why we use QR.

Summary of the QR Method

The procedure is as follows:

  1. Decompose . (This can be done using the Gram-Schmidt process, which we've studied, or more stable methods like Householder reflections).
  2. Compute the vector .
  3. Solve the upper triangular system for using back substitution.

This process is summarized nicely in the following image, which also includes a code snippet showing how this would be implemented in a language like MATLAB.

QR Factorization for Least Squares
This diagram shows the complete workflow for solving a least squares problem using QR factorization. It transforms the original problem \(A\mathbf{x} = \mathbf{b}\) into the simpler triangular system \(R\mathbf{x} = Q^T\mathbf{b}\), which is then solved for \(\mathbf{x}\).
Test your understanding!

Imagine you are a programmer tasked with building a library function to solve least squares problems. A colleague suggests implementing the normal equations method because "it looks simpler."

Based on what you've learned, what is the main argument you would make for implementing the QR decomposition method instead?

Show answer

The main argument is numerical stability. While the normal equations method seems simpler on paper, it requires computing the matrix product . This step can square the condition number of the matrix (), making the problem much more sensitive to small errors in the input data. In practice, for ill-conditioned matrices common in real-world data, this can lead to highly inaccurate solutions. The QR method avoids this step entirely and works directly with the better-conditioned matrix , providing more reliable and accurate results.


Conclusion

Today we explored two powerful techniques for solving the least squares problem. We saw that while the normal equations provide a direct algebraic solution, the QR decomposition offers a more numerically robust and computationally preferred alternative.

Key Takeaways:

  • The least squares solution can be found by solving the normal equations . This method is conceptually straightforward but can be numerically unstable.
  • A more stable approach is to use QR decomposition. By factoring , the problem transforms into solving the upper triangular system using back substitution.
  • The QR method is generally preferred in practice because it avoids forming the potentially ill-conditioned matrix .

Preview of the Next Lesson:

We're not done yet! There is a third, very elegant way to solve the least squares problem that ties together several concepts we've studied, including SVD and matrix inverses. In the next lesson, we will see how to find the least squares solution using the pseudoinverse, which is derived from the Singular Value Decomposition (SVD).