Skip to main content

Welcome to a Grasp lesson

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

Lesson 22

Matrix Calculus for OLS Normal Equations

Recommended resources

Introduction

In our last lesson, we mastered the principle of Maximum Likelihood Estimation (MLE), a powerful technique for deriving estimators by reframing estimation as an optimization problem: we find the parameters that maximize the likelihood of our observed data.

Today, we shift our focus to modeling the relationship between variables, a central task in data science. We will explore another cornerstone of statistical modeling: Ordinary Least Squares (OLS). OLS is also an optimization problem, but with a different objective: to find the model parameters that minimize the sum of squared differences between the observed values and the model's predictions.

This lesson directly addresses your request for rigorous mathematical derivations. Our main goal is to derive the famous Normal Equations, which provide a closed-form solution for the OLS coefficients. We will achieve this using matrix calculus, a fundamental tool for data science and machine learning. As you'll see, the process of setting up an objective function and finding its minimum through differentiation is a recurring theme that connects many different methods.


1. Setting Up the OLS Problem

Before we can perform the calculus, we must first express the linear regression model and its associated error function in the language of linear algebra: matrix notation. This provides a compact and powerful way to handle models with any number of features.

A multiple linear regression model attempts to describe a dependent variable, , as a linear combination of several independent variables or features, . For observations, the model for the -th observation is:

Here, is the intercept, are the coefficients for each feature, and is the error term.

We can represent this entire system of equations compactly as:

where:

  • is the vector of observed outcomes.
  • is the design matrix, where the first column is all ones (for the intercept ) and subsequent columns contain the values of the features.
  • is the vector of unknown coefficients.
  • is the vector of error terms.

The goal of OLS is to find the coefficient vector that minimizes the Sum of Squared Residuals (SSR). The residual for the -th observation is , where is the predicted value. In matrix form, the vector of residuals is .

The SSR, our objective function to minimize, is the squared Euclidean norm of this residual vector:

This function is a scalar value representing the total squared error for a given choice of . Our task is to find the that makes this value as small as possible.


2. Deriving the Normal Equations with Matrix Calculus

Now we come to the core of the lesson: deriving the solution by minimizing . This involves expanding the objective function, differentiating it with respect to the vector , setting the result to zero, and solving.

The following two short videos provide an excellent step-by-step walkthrough of this derivation.

Ordinary Least Squares Estimators - derivation in matrix form - part 2

First, let's watch how the objective function is expanded and then differentiated term by term. This video clearly explains the matrix algebra and the initial calculus steps.

Watch the video from the beginning to 07:32. Pay close attention to how the expression for the sum of squared residuals is expanded and how each of the four resulting terms is differentiated with respect to the coefficient vector β-hat.

As shown in the video, the first step is to expand the objective function:

Noting that the middle two terms are scalars and thus equal to their own transpose, we can combine them:

Next, we take the gradient (the vector of partial derivatives) with respect to and set it to zero. The video walks through this, resulting in:

Now, let's see how to solve this for .

Ordinary Least Squares Estimators - derivation in matrix form - part 3

This second video picks up exactly where the last one left off and completes the derivation, showing the final algebraic steps to isolate β-hat.

Watch this video from the beginning to 02:38. It shows how to rearrange the result of the differentiation to arrive at the final closed-form solution for the OLS estimators.

Following the final steps, we rearrange the equation:

This elegant and fundamental result is known as the Normal Equation(s).

To get our final solution, we solve for by pre-multiplying by the inverse of , assuming it exists:

This is the celebrated closed-form solution for Ordinary Least Squares.


3. A More Formal Calculus Approach

To reinforce your understanding and satisfy your preference for formal mathematics, let's re-derive the result using the standard rules of matrix calculus more directly.

[PDF] Lecture 2: Least squares estimation

This document from a course by Laurent Lessard provides a concise and formal derivation using gradient rules. It's a slightly different perspective that arrives at the same conclusion.

Please read section 1.2 Calculus intuition on page 2. Focus on Lemma 1.1, which states the gradient rules for linear and quadratic forms, and see how they are applied directly to our objective function to obtain the normal equations.

As the reading shows, we can use two key gradient identities:

Our objective function was: . Let's apply the gradient to each term:

  • (no term)
  • (using rule 1, where )
  • (using rule 2, and since is symmetric)

Combining these gives the same gradient we found before:

Setting this to zero yields the Normal Equations, as expected.

Two Mathematical Proofs of Ordinary Least Squares
This diagram provides a fantastic summary, showing both the calculus approach we've just covered and a linear algebra approach (which we'll explore later) to arrive at the same OLS solution.

4. When Does the Solution Exist?

A crucial part of the final formula is the term . As you know from linear algebra, a matrix inverse exists only if the matrix is non-singular, or invertible.

For the matrix to be invertible, the columns of the design matrix must be linearly independent. In a regression context, this means there must be no perfect multicollinearity—no feature can be a perfect linear combination of other features. If this condition fails, the OLS solution is not unique, and we cannot find a single using this formula. This is both a mathematical necessity and a practical diagnostic to be aware of when building models.


Conclusion

In this lesson, we transitioned from the probabilistic framework of MLE to the geometric error-minimization framework of OLS. By applying matrix calculus, we derived one of the most fundamental results in statistical learning.

Key Takeaways:

  • The OLS objective is to minimize the Sum of Squared Residuals (SSR), given by the objective function .
  • By finding the gradient of with respect to and setting it to zero, we derive the Normal Equations:
  • Solving the Normal Equations gives the closed-form solution for the OLS coefficient estimates:
  • This solution is unique if and only if the matrix is invertible, which requires the features in to be linearly independent.

Preview of the Next Lesson:

While the calculus approach is powerful, it doesn't provide the full intuition. Why are the Normal Equations called "normal"? The name hints at orthogonality. In the next lesson, we will delve into the geometric interpretation of Least Squares. We will show that the OLS solution is equivalent to finding the orthogonal projection of the outcome vector onto the subspace spanned by the columns of the design matrix . This will provide a beautiful and deeply intuitive understanding of why OLS works the way it does, connecting directly to your background in physics and vector mechanics.