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

Deriving OLS with Multivariate Calculus

Welcome back! In our last lesson, you successfully derived the least squares cost function, , which provides a quantitative measure of our linear regression model's error. We now have a clear objective: find the parameter vector that minimizes this function.

Today, we will achieve this objective. Your learning outcome for this lesson is to derive the Ordinary Least Squares (OLS) solution using multivariate calculus by setting the gradient to zero. This is a direct application of the optimization theory you've studied. By finding the point where the cost function's slope is zero in all directions, we will identify the parameters that give the best possible fit to the data according to the least squares criterion.

A Quick Recap: The Optimization Problem

From our previous lesson, we established the cost function for a linear model with parameters :

where is the number of data points.

Our goal is to find the specific parameter vector, which we'll call , that minimizes this function:

As you know from your study of optimization and calculus, a necessary condition for a minimum of a differentiable, convex function is that its gradient is the zero vector.

Surface and Contour Plots of a Cost Function
As we saw previously, the cost function \(J(\boldsymbol{\beta})\) for linear regression is convex, shaped like a bowl. The minimum is the single point at the bottom of the bowl where the gradient is zero.

We will find this minimum by taking the partial derivative of with respect to each parameter , setting each derivative to zero, and solving the resulting system of equations.

1. The Starting Point: Simple Linear Regression

Before tackling the general multivariate case, let's derive the solution for a simple linear regression model with just one feature. Your practical experience has likely involved fitting such models, and deriving the solution from first principles will solidify your theoretical understanding.

The model is: .
The cost function is:

To find the optimal and , we must solve the following system of equations:

The following video provides a clear, step-by-step derivation of these partial derivatives.

Deriving the least squares estimators of the slope and intercept (simple linear regression)

This video from jbstatistics will walk you through computing the partial derivatives of the simple linear regression cost function. Focus on the application of the power rule and chain rule.

Please watch from 03:16 to 05:09. The first part derives the partial derivative with respect to the intercept (eta_0), and the second part derives it for the slope (eta_1). Note that the video's cost function omits the rac{1}{2} factor, which is fine—it just means their derivatives have a factor of 2 that we won't have.

Let's summarize the results, applying the rules to our version of .

Partial derivative with respect to :
Using the chain rule: .
Summing over all observations:

Partial derivative with respect to :
Similarly, using the chain rule, the derivative of the inner term with respect to is .

Setting these two equations to zero gives us a system of two linear equations with two unknowns, and . Solving this system (as shown in the rest of the jbstatistics video) yields the well-known formulas for the slope and intercept in simple regression.

2. The General Case: Multiple Linear Regression

Now, let's generalize this to the multiple linear regression case, where we have features and parameters ().

Our model is , where we define for all .
The cost function remains .

Our goal is to compute the partial derivative for an arbitrary parameter .

The following segment from Andrew Ng's lecture provides an excellent derivation.

Stanford CS229: Machine Learning - Linear Regression and Gradient Descent | Lecture 2 (Autumn 2018)

Watch this clip from the Stanford CS229 lecture on linear regression. Andrew Ng derives the partial derivative of the cost function with respect to a single, arbitrary parameter heta_j (his notation for eta_j). This is the core of today's lesson.

Watch the segment from 22:49 to 32:40. Pay close attention to how the chain rule is applied and how only the term involving eta_j (or heta_j) survives the differentiation inside the summation. He first derives it for a single training example and then re-introduces the summation over all examples, which leads to the final gradient update rule.

Let's formalize the result from the video. To find the partial derivative of with respect to a specific parameter , we again apply the chain rule:

  1. Derivative of the outer square function:

  2. Derivative of the inner term:
    The inner term is . When we differentiate this with respect to , only the term is non-zero. The derivative is simply .

  3. Combining them:

This single, powerful equation gives us the partial derivative for any parameter .

Test your understanding!

Look at the final expression for . The term is the residual for the -th data point. The term is the value of the -th feature for that same data point.

Can you provide an intuitive explanation for why the gradient involves the product of the residual and the feature value? What does it mean if this sum is large and positive?

Show answer

The term represents a weighted sum of the residuals, where the weights are the feature values . This term is essentially the covariance between the feature and the model's errors.

If this sum is large and positive, it means that when the feature is large and positive, the residual also tends to be large and positive (i.e., the model is under-predicting). To correct this, we need to increase the model's prediction. Since is positive, increasing the corresponding parameter will increase the prediction . The gradient descent update rule would become , which correctly increases .

Conversely, if the sum is large and negative, the model tends to over-predict when is large, and the update rule will correctly decrease . When the sum is zero, there is no systematic correlation between feature and the error, indicating we are at an optimum with respect to .

3. The Result: A System of Normal Equations

To find the minimum of , we must set the partial derivative with respect to every parameter to zero. This gives us a system of equations:

This system of simultaneous linear equations is known as the Normal Equations.

For a more formal, textual derivation of this system, you can refer to the following resource.

Proofs involving ordinary least squares

The Wikipedia page 'Proofs involving ordinary least squares' provides a concise, formal derivation of the Normal Equations by setting the partial derivatives to zero, just as we have done.

Read the section named "Derivation of the normal equations". This text formalizes the steps we've just discussed, showing how setting the partial derivatives of the sum of squared residuals to zero leads directly to the Normal Equations.

This system of equations represents the solution. Any vector that satisfies these equations is a vector that minimizes the sum of squared errors.

Conclusion

In this lesson, you have successfully used multivariate calculus to find the conditions that the optimal OLS parameters must satisfy. By leveraging your knowledge of optimization, you've transformed the abstract goal of "fitting the best line" into a concrete mathematical problem and solved it.

Key Takeaways:

  • The minimum of the convex cost function occurs where its gradient with respect to is the zero vector.
  • The partial derivative of the cost function with respect to a single parameter is .
  • Setting all partial derivatives to zero yields a system of linear equations in the unknown parameters .
  • This resulting system is known as the Normal Equations.

Preview of the Next Lesson:
We have derived the system of Normal Equations. While one could solve this system using techniques like substitution or elimination, doing so by hand becomes extremely tedious for more than a couple of features. A far more elegant and powerful approach exists using the language of linear algebra. In the next lesson, we will express the Normal Equations in matrix form and derive the famous closed-form OLS solution: .

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

Sign up