Recommended resources
The freely available resources this lesson will direct you to.
- Linear Regression from a Probabilistic Perspective | Deriving the Least Squares Losswww.youtube.com
- Machine learning - Maximum likelihood and linear regressionwww.youtube.com
- Akaike Information Criterion 1 Maximum Likelihood ...harvard-iacs.github.io
- Linear regression - Maximum likelihood estimationwww.statlect.com
Hello! Welcome to your next lesson in our journey through the foundations of machine learning.
In our last two lessons, we derived the Ordinary Least Squares (OLS) solution for linear regression. First, we used multivariate calculus to find the parameters that minimize the sum of squared residuals. Then, we used linear algebra to interpret this as an orthogonal projection, yielding the same closed-form Normal Equations. Both approaches are powerful, but they answer how to find the solution, not why the "sum of squared errors" is the right criterion to minimize in the first place.
Today, we will address that "why". Our learning outcome is to derive the probabilistic interpretation of linear regression using Maximum Likelihood Estimation (MLE) with Gaussian errors. By framing the problem statistically, you'll see that the OLS solution is not just a convenient computational trick but a direct consequence of a plausible assumption about the nature of noise in our data. This provides a deep and satisfying justification for the methods we've learned so far.
This lesson builds on your knowledge of MLE from the probability module and connects it directly to the regression problem.
1. The Probabilistic Model: Assuming Gaussian Errors
So far, we have treated the error term in our model as just a "residual". To build a probabilistic model, we must make an explicit assumption about where these errors come from.
The most common and foundational assumption in linear regression is that the errors are Independent and Identically Distributed (i.i.d.) according to a Gaussian (Normal) distribution with a mean of 0 and a constant variance . We write this as:
This assumption implies three things:
- Zero Mean: The errors are not systematic. On average, the model's predictions are correct.
- Constant Variance (Homoscedasticity): The spread of the errors is the same for all values of the features .
- Normality: The errors follow a bell-shaped curve. Small errors are common, and very large errors are rare.
- Independence: The error for one observation provides no information about the error for another observation.
Given this assumption about the errors, what can we say about the distribution of our target variable, ? Since is the sum of a deterministic term () and a Gaussian random variable (), itself is a Gaussian random variable. Its distribution is:
This is the heart of the probabilistic interpretation: for any given input , the model doesn't predict a single value for , but a full probability distribution centered at the regression line's prediction, .
This video provides a very clear explanation of this idea, starting with a simple univariate case.
Linear Regression from a Probabilistic Perspective | Deriving the Least Squares Loss
The video 'Linear Regression from a Probabilistic Perspective' by the Machine Learning & Simulation channel visualizes how the Gaussian error assumption leads to a probabilistic model for the output variable.
Watch from 01:02 to 05:32. Focus on how the assumption of a normal distribution for the 'spreading' of y-values around the regression line is formulated mathematically. The graphical model representation is particularly insightful.
The image below, which you may find familiar, perfectly illustrates this. The regression line gives the mean () of a Gaussian distribution at each point, and the actual observations (Y) are samples from these distributions.

2. Constructing the Likelihood Function
Now that we have a probabilistic model, we can use the principle of Maximum Likelihood Estimation (MLE) to find the best parameters. Recall from the statistics module that MLE seeks to find the parameter values that maximize the probability (or probability density) of observing the data we have actually collected.
Our parameters are . Our data is the set of pairs . The likelihood function, , is the joint probability density of observing all the values, given the matrix and the parameters.
The probability density for a single observation is given by the PDF of the normal distribution we defined above:
Because we assumed our observations are independent, the joint probability density is the product of the individual densities:
This is our likelihood function. Our goal is to find the and that maximize this expression.
3. Maximizing the Log-Likelihood
Directly maximizing the likelihood function, which is a product of many terms, is often difficult. It's much easier to maximize its natural logarithm, the log-likelihood function . Since the logarithm is a monotonically increasing function, the parameters that maximize the log-likelihood are the same as those that maximize the likelihood.
Let's derive the log-likelihood:
The log of a product is the sum of the logs:
Using the properties and :
Simplifying the terms:
This is our final expression for the log-likelihood function.
The Connection to Least Squares
Now, let's find the parameter vector that maximizes this function. We want to solve:
Notice that the first two terms do not depend on . Therefore, maximizing the entire expression is equivalent to maximizing just the third term.
Since is positive, the term is a negative constant. Maximizing a function that is a negative constant multiplied by some value is equivalent to minimizing that value. Therefore:
This is precisely the cost function for Ordinary Least Squares!
This proves that the OLS estimator is identical to the Maximum Likelihood Estimator under the assumption of i.i.d. Gaussian errors.
For a rigorous, step-by-step walkthrough of this derivation in a formal style, the following resource is excellent.
Linear regression - Maximum Likelihood Estimation
The article 'Linear regression - Maximum Likelihood Estimation' from StatLect provides a complete and formal derivation, perfectly matching the mathematical rigor you appreciate.
Read the sections 'Assumptions', 'Implications of the assumptions', 'The likelihood function', 'The log-likelihood function', and 'The maximum likelihood estimators'. This will walk you through the entire derivation we just discussed, from the initial assumption to the final estimators for both oldsymbol{eta} and \sigma^2.
Test your understanding!
The log-likelihood function also depends on the variance, . Using the final expression for the log-likelihood and your knowledge of calculus, can you derive the Maximum Likelihood Estimate for the variance, ?
Hint: Take the partial derivative of with respect to , set it to zero, and solve for . Remember to substitute for in the final expression.
Show answer
Our log-likelihood function is:
Take the partial derivative with respect to :
Set the derivative to zero:
Multiply by to clear the denominators:
Solving for and substituting gives the MLE for the variance:
This is the average of the squared residuals, which is an intuitive estimate for the error variance.
Conclusion
In this lesson, we have uncovered the deep statistical foundation of Ordinary Least Squares. By moving from a purely procedural approach to a probabilistic one, we have shown that the familiar method of minimizing squared errors is not arbitrary, but is in fact the optimal approach under the widely-used assumption of normally distributed errors.
Key Takeaways:
- Linear regression can be interpreted as a probabilistic model by assuming the errors are independent and identically distributed (i.i.d.) from a Gaussian distribution, .
- This assumption implies that each observation is drawn from a normal distribution centered on the regression line: .
- The principle of Maximum Likelihood Estimation (MLE) finds the parameters that make our observed data most probable.
- Maximizing the log-likelihood function for this model with respect to the regression coefficients is mathematically equivalent to minimizing the sum of squared residuals.
- Thus, the OLS estimator is the Maximum Likelihood Estimator for a linear model with Gaussian errors, providing a powerful statistical justification for its use. MLE also provides a principled way to estimate the error variance .
Preview of the Next Lesson:
We have now established the OLS solution from three different perspectives: optimization, geometry, and probability. With this solid foundation, we are ready to analyze the statistical properties of the OLS estimator. In the next lesson, we will begin our exploration of these properties by proving the unbiasedness of the OLS estimators, a crucial concept for understanding the reliability of our model's parameters. We will also introduce the set of "classical linear model assumptions" that are necessary for these properties to hold.