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

Understanding the Linear Regression Model

Hello! Welcome to the first lesson of our new module, where we transition from the abstract mathematical foundations to our first concrete machine learning model: Linear Regression. In the previous modules, you've built a robust toolkit of concepts from linear algebra, calculus, optimization, and statistics. Now, we'll start putting them to work.

Your learning outcome for this lesson is to formulate the linear regression model, specifying its components and statistical assumptions. We will define what the model is, identify its parts using precise mathematical language, and, most importantly, understand the "rules of the game" — the key assumptions that must hold for the model to be reliable.

1. From Data to a Model: Defining the Linear Hypothesis

Linear regression is a cornerstone of supervised learning. The goal is to predict a continuous target value, , based on a set of input features, . Your previous project on house price prediction is a classic example of a regression problem.

Let's begin by formalizing the structure of the model. We'll watch a few short clips from a lecture by Andrew Ng, which clearly introduces the core components and notation.

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

This video from Stanford Online introduces linear regression using a housing price example. It will help us establish the basic terminology and the structure of our model, which we'll call the 'hypothesis'.

Please watch the following segments: 00:44 - 02:56: This section introduces the supervised learning context for regression. 02:56 - 08:20: Pay close attention to how the hypothesis h(x) is defined, what the parameters ( heta) are, and the notational trick of setting x_0 = 1. 08:20 - 12:36: This part defines the standard notation for the number of training examples (m), features (n), and how we refer to individual examples (x^{(i)}, y^{(i)}).

As you saw, we can define the model for a single data point with features as a linear equation. We'll adopt the notation common in statistics and econometrics, using for the parameters and for the error term.

The model for a single observation is:

Let's break down the components:

  • : The dependent variable (or target) for observation . This is the value we want to predict (e.g., the price of house ).
  • : The value of the -th independent variable (or feature) for observation (e.g., the size or number of bedrooms of house ).
  • : The -th parameter (or coefficient). represents the change in for a one-unit change in , holding all other features constant (a concept known as ceteris paribus). is the intercept, representing the value of when all features are zero.
  • : The error term (or disturbance) for observation . This is a crucial component. It's a random variable that captures all factors affecting that are not included in our model. If our model were perfect, would be zero.

2. The Model in Matrix Form

Writing the equation for every data point is cumbersome. As you know from your linear algebra module, matrix notation provides a more compact and powerful way to represent the system for all observations in our dataset.

This is a fundamental representation that we will use for all derivations in the upcoming lessons.

Notes on Econometrics

These 'Notes on Econometrics' by Mauricio Sarrias provide a rigorous, academic formulation of the linear model. We will focus on how to express the model using matrix algebra.

Please read the section titled 'Matrix Notation' (page 6). Focus on how the individual vectors \mathbf{y}, \mathbf{x}_i, oldsymbol{eta}, and the matrix \mathbf{X} are constructed. Make sure you understand how the equation y_i = \mathbf{x}_i^Toldsymbol{eta} + \epsilon_i for a single observation leads to \mathbf{y} = \mathbf{X}oldsymbol{eta} + oldsymbol{\epsilon} for the entire dataset.

To summarize the matrix formulation:

Let:

  • be an vector of dependent variables.
  • be an matrix of independent variables (also called the design matrix), where the first column is all ones to account for the intercept .
  • be a vector of parameters.
  • be an vector of error terms.

The entire linear regression model can be written in one simple equation:

This compact form is not just for neatness; it's essential for deriving the solution for , which we will do in a future lesson.

3. The Statistical Assumptions of the Model

The equation is just a mathematical statement. To make it a statistical model and to ensure that our estimates for have desirable properties (like being unbiased), we must make several crucial assumptions about the data generating process, particularly about the error term . These are known as the Classical Linear Regression Model (CLRM) assumptions.

The following document provides a concise list of these assumptions. We will then explore the most important ones in more detail.

Assumptions for OLS Regression and the Gauss-Markov ...

This document clearly outlines the core assumptions of Ordinary Least Squares (OLS) regression, which is the standard method for estimating the parameters of a linear model.

Read the first page of the document. Familiarize yourself with the list of assumptions from A.1 to A.7. We will be diving into the details of these right now.

Let's dissect the key assumptions with the mathematical rigor you're looking for.

Assumption 1: Linearity in Parameters

The model must be linear in the parameters . This means the terms are not squared, exponentiated, or otherwise transformed.

  • Why it matters: It ensures the model is a simple weighted sum of the features. Note that the features themselves can be non-linear (e.g., , ). For example, is still a linear regression model because it's linear in .

Assumption 2: No Perfect Collinearity

The data matrix must have full column rank, which is .

  • Mathematical Form: .
  • Why it matters: This means no feature can be written as an exact linear combination of the other features. If you had age_in_years and age_in_months as features, this assumption would be violated. If perfect collinearity exists, the matrix is singular and cannot be inverted, which prevents us from finding a unique solution for .

Assumption 3: Zero Conditional Mean (Strict Exogeneity)

This is arguably the most important and conceptually challenging assumption. It states that the expected value of the error term, conditional on the entire data matrix , is zero.

  • Mathematical Form: , which implies for each observation .
  • Why it matters: This assumption implies that the unobserved factors captured by the error term are uncorrelated with the features in our model. If this assumption is violated (a situation called endogeneity), our estimates of will be biased. A common cause is omitted variable bias, where a relevant variable is left out of the model and is correlated with both the included features and the dependent variable.

The following resource provides a rigorous exploration of this assumption and its profound implications.

Notes on Econometrics

We return to the 'Notes on Econometrics' to formally dissect the Strict Exogeneity assumption. This reading leverages concepts from probability theory to explain the deep implications of this assumption.

Please read Section 1.1.2, 'Strict Exogeneity' (pages 7-11). Focus on: The formal definition (Assumption 1.4). The implications derived using the Law of Iterated Expectations, particularly that the regressors are orthogonal to the error term (E(x_{jk}\epsilon_i) = 0) and therefore uncorrelated (Cov(x_{jk}, \epsilon_i) = 0). The example of ability and schooling (Example 1.5), which is a classic illustration of potential violation.

Assumption 4: Spherical Errors (Homoscedasticity and No Autocorrelation)

This assumption combines two distinct ideas about the variance of the errors.

  • Homoscedasticity (Constant Variance): The variance of the error term is the same for all observations.
    • Mathematical Form: for all .
  • No Autocorrelation: The error terms for any two different observations are uncorrelated.
    • Mathematical Form: for all .

These two conditions can be summarized beautifully in matrix form: the conditional variance-covariance matrix of the error vector is a scaled identity matrix.

  • Combined Mathematical Form: .
  • Why it matters: While OLS estimators remain unbiased even if this assumption is violated, it is crucial for ensuring that OLS is the most efficient (i.e., has the lowest variance) among all linear unbiased estimators. Violations (heteroscedasticity or autocorrelation) mean our standard errors will be wrong, leading to faulty hypothesis tests.

Assumption 5: Normality of Errors (Optional, for Inference)

For many purposes, we also assume the errors are normally distributed.

  • Mathematical Form: .
  • Why it matters: This assumption is not required for the unbiasedness or efficiency of the OLS estimator. However, it is essential for conducting statistical inference—that is, performing hypothesis tests (e.g., is significantly different from zero?) and constructing confidence intervals for our parameters.

This image provides a helpful visual summary of what these assumptions (and their violations) look like.

Assumptions of Linear Regression
A visual guide to the key assumptions of linear regression. For each assumption, the left panel shows a scenario where the assumption holds, while the right panel shows a violation. (Note: 'Multivariate Normality' refers to the normality of errors, and 'Endogeneity' refers to a violation of the zero conditional mean assumption).
Test your understanding!

Imagine you are building a model to predict the salary of a software developer. Your features are years_of_experience and lines_of_code_written_per_day.

  1. Write down the formal equation for this linear regression model for a single developer .
  2. The "Zero Conditional Mean" assumption () is critical. Can you describe a realistic factor that might be captured in the error term and could violate this assumption? Explain why it would be a violation.
Show answer
  1. Model Equation:

  2. Potential Violation: A very plausible omitted variable is the developer's innate problem-solving ability.

    • This "ability" is unobserved and is therefore part of the error term, .
    • It is likely correlated with salary (higher ability leads to higher salary).
    • It is also likely correlated with the included features. For example, a developer with higher ability might write more efficient (and perhaps fewer) lines of code to solve a problem. They might also be more likely to persist in the field, leading to more years of experience.
    • Because this unobserved factor ("ability") is part of and is also correlated with the features in , the error term is no longer uncorrelated with the features. This means , violating the Zero Conditional Mean assumption and leading to biased estimates of and .

Conclusion

In this lesson, you have formally defined the linear regression model and, more importantly, delved into the rigorous statistical assumptions that underpin it. Understanding these assumptions is not just an academic exercise; it's what separates a mechanical model-fitter from a thoughtful data scientist who can critically evaluate a model's validity.

Key Takeaways:

  • The linear regression model is expressed as , where we aim to estimate the parameter vector .
  • The model is linear in its parameters, but the features themselves can be non-linear.
  • The Classical Linear Regression Model (CLRM) relies on a key set of assumptions:
    • Linearity in parameters
    • No perfect collinearity (full rank of )
    • Zero conditional mean / Strict exogeneity (): The most critical assumption for unbiasedness.
    • Spherical errors (): Homoscedasticity and no autocorrelation, crucial for efficiency.
    • Normality of errors: Needed for valid statistical inference.

Preview of the Next Lesson:
Now that we have defined the model and its ideal conditions, the next logical question is: how do we find the "best" estimate for ? In the next lesson, we will begin to answer this by deriving the cost function for linear regression from a least-squares perspective. This will set up the optimization problem that your calculus and optimization skills are perfectly prepared to solve.

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

Sign up