Skip to main content

Welcome to a Grasp lesson

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

Lesson 15

Understanding Linear Regression: Intuition, Cost Function, and Assumptions

Recommended resources

Welcome to the third module of our course. Having mastered the art of data preprocessing, you are now fully prepared to move from preparing data to building predictive models. We will begin this journey with one of the most fundamental and widely-used algorithms in machine learning: Linear Regression.

This lesson will establish the conceptual foundation for many models to come. We will explore the core intuition behind linear regression, understanding how it models relationships in data. Specifically, we will cover how it defines a "best fit" line using a cost function called Mean Squared Error (MSE), the method of Ordinary Least Squares (OLS) used to find this line, and the critical assumptions that must hold for the model's results to be trustworthy.

Modeling Relationships with a Line

At its heart, linear regression is a statistical method for modeling the relationship between a dependent variable (the target you want to predict) and one or more independent variables (the features). The "linear" part of the name signifies that the model assumes this relationship can be represented by a straight line.

For the simple case of one feature (X) and one target (Y), this relationship is described by the familiar equation of a line:

Let's break this down:

  • is the dependent variable (the target).
  • is the independent variable (the feature).
  • is the intercept, representing the predicted value of when is zero.
  • is the slope or coefficient for feature . It quantifies the change in for a one-unit change in .
  • is the error term, which captures the variability in that cannot be explained by the linear relationship with .

The goal of a linear regression model is to learn the optimal values for the coefficients ( and ) from the training data. But how does it determine what is "optimal"?


Finding the Best Line: Ordinary Least Squares (OLS)

Imagine scattering your data points on a plot. You could draw an infinite number of straight lines through them. The "best" line is the one that comes closest to all the data points simultaneously. To quantify this, we first need to define the error.

For each data point, the error, or residual, is the vertical distance between the actual value () and the value predicted by the line ().

We can't simply sum these residuals, because positive errors (when the line is below the point) and negative errors (when the line is above) would cancel each other out. To solve this, we square each residual. This makes all errors positive and has the added benefit of penalizing larger errors much more heavily than smaller ones.

On the left, the residuals are shown as the vertical distance from each data point (blue) to the regression line (red). On the right, these distances are squared to form the basis of the cost function.

The sum of all these squared residuals is called the Sum of Squared Errors (SSE) or Residual Sum of Squares (RSS). Dividing this by the number of data points gives us the Mean Squared Error (MSE), which is the most common cost function for regression problems.

The method used to find the line that minimizes this cost function is called Ordinary Least Squares (OLS). The following video provides a wonderfully clear, intuitive explanation of this process.

Linear Regression, Clearly Explained!!!

This StatQuest video, "Linear Regression, Clearly Explained!!!", is excellent for building intuition. We will focus on the first part, which explains how OLS works.

Watch from the beginning of the explanation. Pay close attention to how Josh Starmer visualizes the concept of "residuals" and the process of rotating the line to find the one with the "least sum of squares".

As you saw, OLS systematically finds the unique line that minimizes the sum of the squared vertical distances from the data points to that line. While there is a direct analytical formula to solve for the coefficients in OLS (known as the normal equation), understanding it as an optimization problem—finding the parameters that minimize a cost function—is a crucial concept that applies to nearly all machine learning models.

For a more detailed textual summary of these concepts, the following resource provides a concise walkthrough.

3 Ordinary Least-squares Regression - GitHub Pages

This chapter from the "Regression Cookbook" formalizes the concepts we've just discussed.

Please read the following sections: What is OLS? The "Best Line" Understanding Residuals Why Squared Errors? The Mathematical Formulation


The Rules of the Game: Key Assumptions

For the coefficients estimated by OLS to be reliable, unbiased, and efficient, several key assumptions about the data and the residuals must be met. Violating these assumptions doesn't necessarily mean the model is useless, but it can make your interpretations and conclusions misleading.

These assumptions can be easily remembered with the acronym LINE:

  • Linearity
  • Independence
  • Normality
  • Equal variance (Homoscedasticity)
This image provides a quick visual guide to the core assumptions of linearity, homoscedasticity (equal variance), and normality of error distribution.

The following video provides a clear, practical explanation of each assumption, why it matters, and how to spot violations. This is a very common topic in data science interviews, so a solid grasp here is valuable.

What are Assumptions of Linear Regression? Easy Explanation for Data Science Interviews

This video by Emma Ding is titled "What are Assumptions of Linear Regression? Easy Explanation for Data Science Interviews". It breaks down the LINE acronym perfectly for our purposes.

First, watch the introduction from the beginning to get an overview and learn the "LINE" acronym. Watch the section on Linearity from this timestamp. This is the most critical assumption. Next, watch the explanation for Independence of residuals from this section. Then, learn about the Normality of residuals from this part. Note her point about the Central Limit Theorem making this assumption less critical for large datasets. Finally, understand Equal Variance (Homoscedasticity) from this last section.

To complement the video, you can refer to the "Regression Cookbook" resource again for a more technical description of how to check these assumptions, often involving specific statistical tests that you may encounter in your coursework.

3 Ordinary Least-squares Regression - GitHub Pages

This section of the "Regression Cookbook" details the diagnostic checks for the LINE assumptions.

Skim through the section "Checking Model Assumptions". Focus on understanding the purpose of the diagnostic plots mentioned for each assumption: Linearity: Assessed with a "Residuals vs. Fitted" plot. Independence of Errors: Often relevant for time-series data and checked with the Durbin-Watson test. Homoscedasticity: Also checked with the "Residuals vs. Fitted" plot, looking for constant variance. The Breusch-Pagan test is mentioned as a formal statistical test. Normality of Residuals: Assessed visually with a Q-Q (Quantile-Quantile) plot.


Conclusion

In this lesson, we have laid the groundwork for our study of supervised learning by understanding linear regression. You've learned how this model conceptualizes the relationship between variables as a line, how it defines the "best" line by minimizing the Mean Squared Error, and the OLS method used to achieve this. Crucially, you now also understand the four key assumptions (LINE) that must be checked to ensure the model's results are valid and interpretable.

Key Takeaways:

  • Linear Regression models a linear relationship between features and a target variable.
  • The cost function, typically Mean Squared Error (MSE), quantifies how poorly the model fits the data.
  • Ordinary Least Squares (OLS) is the optimization method that finds the model parameters (coefficients) that minimize the MSE.
  • The reliability of a linear regression model depends on four key assumptions, remembered by the acronym LINE: Linearity, Independence, Normality, and Equal variance.

In this lesson, we discussed that OLS finds the optimal parameters by minimizing the cost function. For linear regression, there is a direct analytical solution to this problem. However, for most other machine learning algorithms, no such direct solution exists. Instead, we must use an iterative optimization algorithm to gradually "descend" towards the minimum of the cost function. In our next lesson, we will explore the most fundamental and important of these algorithms: Gradient Descent.