Skip to main content

Welcome to a Grasp lesson

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

Lesson 30

Understanding Linear Regression: Minimizing the Sum of Squared Errors

Recommended resources

Hello! Welcome to the first lesson in our new module, "Linear Regression and Regularization."

In the last module, we built a powerful toolkit for optimization. We learned how algorithms like gradient descent and Adam can iteratively find the parameters that minimize a given loss function. Now, we're going to put that toolkit to work on our very first supervised machine learning model: Linear Regression.

Today, we will focus on the fundamental principle behind this model. By the end of this lesson, you will be able to formulate simple and multiple linear regression in terms of minimizing the sum of squared errors. This is the crucial first step in understanding not just how linear regression works, but also why it's structured the way it is.

What is Linear Regression?

At its heart, linear regression is about finding a linear relationship between variables. Imagine you have a dataset of houses, and you want to predict a house's price based on its size (in square feet). Linear regression attempts to do this by drawing a straight line through the data points that best represents the underlying trend.

This is called Simple Linear Regression because it involves only one predictor variable (size) and one target variable (price). The equation for this line is likely familiar to you:

Where:

  • (y-hat) is the predicted price.
  • is the size of the house.
  • is the slope of the line. It tells us how much the price is predicted to change for a one-square-foot increase in size.
  • is the y-intercept. It's the predicted price of a house with zero square feet (a theoretical baseline).

and are the model parameters—the values we need to "learn" from the data.

But what if we have more information, like the number of bedrooms or the age of the house? We can extend the model to include multiple predictor variables. This is called Multiple Linear Regression. The equation simply expands:

Here, represent the different features (size, bedrooms, age, etc.), and are their corresponding coefficients.

For a clearer distinction between these two types, you can quickly review the following article.

Understanding Ordinary Least Squares (OLS) Regression

The article 'Understanding Ordinary Least Squares (OLS) Regression' from Built In clearly explains the difference between simple and multiple regression.

Read the section titled 'How OLS Applies to Linear Regression'. Focus on the paragraphs that define and provide examples for simple and multiple regression. You can stop right before the formula yi = α + βxi + εi.

Now, the main question is: how do we find the best values for and ? Out of all the infinite lines we could draw, which one is the "best fit"?


Finding the Best Line: The Concept of Error

To find the best line, we first need a way to measure how "bad" any given line is. We do this by calculating the error, or residual, for each data point. A residual is simply the vertical distance between an actual data point and the prediction made by our line.

  • Actual value: (the true price of the i-th house)
  • Predicted value: (the price our line predicts for the i-th house)
  • Residual for point i:

Let's watch a video that builds this intuition from the ground up.

The Main Ideas of Fitting a Line to Data (The Main Ideas of Least Squares and Linear Regression.)

The StatQuest video 'The Main Ideas of Fitting a Line to Data' provides an excellent visual explanation of residuals and how we can combine them to measure a line's overall fit.

Watch from 0:23 to 4:04. Pay close attention to how the video moves from the general idea of a 'best' line to measuring the distance for each point (the residual) and why squaring these distances is a necessary step.


The Sum of Squared Errors (SSE)

As the video explained, simply summing the residuals isn't useful because positive and negative errors cancel each other out. The standard solution is to square each residual before adding them up. This gives us a single, powerful metric for a model's total error: the Sum of Squared Errors (SSE), also known as the Residual Sum of Squares (RSS).

The formula for SSE is:

This formula is the cornerstone of linear regression. It takes the error for each of the data points, squares it, and sums them all up.

Visualizing Residuals and Squared Residuals in Linear Regression
This image visually breaks down the SSE. The left plot shows the residuals as vertical lines. The right plot shows the squared residuals as squares, where the area of each square is the value we are summing up.

The Goal: Minimizing the Sum of Squared Errors

Now we have a way to score any potential line. A line with a high SSE is a poor fit, and a line with a low SSE is a good fit. The "best" line, therefore, is the one that results in the lowest possible SSE.

This transforms our task into an optimization problem, which should sound very familiar from our last module! We are trying to find the values of the parameters () that minimize the SSE. This is why the method is called Ordinary Least Squares (OLS).

Let's continue the video to see this optimization concept in action.

The Main Ideas of Fitting a Line to Data (The Main Ideas of Least Squares and Linear Regression.)

This next segment of the StatQuest video visualizes the process of finding the line that minimizes the sum of squared residuals.

Watch from 4:04 to 9:01. The video shows how rotating the line changes the SSE and introduces the idea that calculus (finding where the derivative is zero) can find the 'sweet spot' with the minimum error. This connects directly to the optimization techniques we've studied.

Formulating the Optimization Problem

We can now formally state our goal. For simple linear regression, we substitute the line equation into the SSE formula. This gives us the loss function we need to minimize:

For multiple linear regression, the principle is identical, just with more parameters:

This is the central formulation you need to understand. We have defined a loss function (SSE) that measures our model's error, and our goal is to find the parameters ('s) that minimize this loss.

To see this formulation presented in a clear, textual format, please read the following brief section.

Sum of Squared Errors (SSE): Complete Guide to Measuring ...

The article 'Sum of Squared Errors (SSE): Complete Guide' clearly presents the optimization objective for least squares regression.

Read only the section 'SSE in Least Squares Regression', up to (but not including) 'Why Squared Errors?'. Focus on how it frames the search for the best-fitting line as a minimization problem.


Why Use Squared Errors?

You might be wondering why we use the sum of squared errors and not, for instance, the sum of absolute errors. This is an excellent question, and it has deep connections to the optimization concepts you've learned.

Sum of Squared Errors (SSE): Complete Guide to Measuring ...

Let's read the next section of the same article, which explains the practical and mathematical advantages of using squared errors.

Read the section 'Why Squared Errors?'. Pay special attention to the first point about 'Differentiability'.

Let's recap the two most important reasons:

  1. Differentiability: The SSE function is a smooth, continuous curve (a parabola, in fact). This means we can calculate its gradient at any point. As you know from our work with gradient descent, being able to compute the gradient is essential for finding the minimum of a function. The absolute error function has a sharp corner at zero, where its derivative is undefined, making optimization much harder.
  2. Penalizes Large Errors: By squaring the error, we penalize large mistakes much more heavily than small ones. An error of 10 contributes 100 to the SSE, while an error of 2 contributes only 4. This forces the model to pay more attention to avoiding significant outliers.
Test your understanding!

Suppose you have a very simple dataset with three points:

  • (x=1, y=2)
  • (x=2, y=5)
  • (x=3, y=4)

You are testing two potential regression lines:

  • Line A:
  • Line B:

Calculate the Sum of Squared Errors (SSE) for both lines. Which line is the better fit for this data according to the OLS criterion?

Show answer

For Line A ():

  1. Point (1, 2): Predicted . Error = . Squared Error = .
  2. Point (2, 5): Predicted . Error = . Squared Error = .
  3. Point (3, 4): Predicted . Error = . Squared Error = .
    SSE for Line A = 0 + 1 + 4 = 5

For Line B ():

  1. Point (1, 2): Predicted . Error = . Squared Error = .
  2. Point (2, 5): Predicted . Error = . Squared Error = .
  3. Point (3, 4): Predicted . Error = . Squared Error = .
    SSE for Line B = 0 + 4 + 0 = 4

Conclusion: Line B has a lower SSE (4 vs. 5), so it is the better fit for the data according to the Ordinary Least Squares criterion.


Conclusion

In this lesson, we established the fundamental principle of linear regression. We've moved from the abstract idea of a loss function to a concrete, powerful, and interpretable one.

Key Takeaways:

  • Linear Regression models the relationship between variables by fitting a line (for simple regression) or a hyperplane (for multiple regression).
  • The quality of the fit is measured by the residuals, which are the differences between the actual and predicted values.
  • The overall model error is quantified by the Sum of Squared Errors (SSE), which is our loss function.
  • The goal of linear regression is to find the model parameters ('s) that minimize the SSE. This is known as the principle of Ordinary Least Squares (OLS).
  • This formulation turns model fitting into an optimization problem, directly connecting to the methods we learned in the previous module.

Preview of the Next Lesson:

We now understand the objective of linear regression: to find the line that minimizes the sum of squared errors. In our next lesson, we will focus on the result. We'll learn how to interpret the coefficients ('s), intercept, and the R-squared value of a fitted regression model to understand the relationships it has discovered in the data.