Linear Regression and Maximum Likelihood
A Probabilistic View
In the previous lesson, we chose squared error as the objective for linear regression because errors can be positive or negative.
In this lesson, we look at linear regression from a probabilistic point of view. We'll see that the same squared error objective comes out of a statistical method called maximum likelihood estimation.
Maximum Likelihood Estimation
Maximum likelihood estimation (MLE) is a statistical method used in machine learning. It works by maximizing the likelihood function, which represents the probability of observing the data given specific parameter values.
Our dataset gives us both the inputs and the labeled outputs. So we try to maximize the likelihood of the actual output given the input.
Here is a small example. Suppose you want to estimate a coin's bias, so you flip it 100 times and get 60 heads.
MLE says the most likely probability of heads is , because that is the value that best explains your data.
The formula for the likelihood depends on the distribution the data comes from. Next, we'll apply this idea to linear regression.
Recap: The Linear Regression Model
We'll use the house price problem from the previous lesson. The inputs are the house area, number of bedrooms, number of toilets, and number of balconies. The output is the house price.
We have data samples:
Each is a -dimensional input, and each is a real value. The predicted output is:
As before, we collect the weights into a vector of values, and add a 1 to each so it has values too. The prediction then becomes:
The error for the -th sample is the difference between the actual output and the predicted output:
On a plot, these errors are the distances from the data points to the line. Our objective so far has been to minimize the sum of the squared errors.
Assuming Gaussian Errors
Many lines are possible, but only the best one matches our data. If we have the right line, two things should be true about the errors:
- They are symmetric around the line, so points are equally likely to fall above or below it.
- Small errors are more likely than large ones.
This lets us assume the errors come from a Gaussian (normal) distribution with mean zero and variance :
The Distribution of the Output
Each actual output is the predicted output plus the error:
For all samples together, is a matrix because each is a vector, while and are vectors of the outputs and errors:
The output depends on the error , which is Gaussian. So, given the input, is also Gaussian. Adding shifts the mean, and the variance stays the same:
The Likelihood
Since the actual output follows a Gaussian distribution, we can use the Gaussian formula to write the likelihood of the actual output given the input:
This is the likelihood for one sample. To cover the whole dataset, we also assume the errors are independent of each other, so the likelihood of all outputs is the product of the individual likelihoods:
We want to find the that maximizes this likelihood.
The Log-Likelihood
For ease of computation, we take the logarithm. Maximizing the log of a function gives the same as maximizing the function itself, and the log turns the product into a sum:
From Likelihood to Squared Error
Now we simplify. Remember that our variable is .
- The first term doesn't contain , so it's a constant and we can ignore it. We're left with maximizing:
- Maximizing a negative quantity is the same as minimizing the positive quantity:
- The is also a constant, so we can remove it too:
This is the squared error objective from the previous lesson.
Summary
The key ideas are:
- Maximum likelihood estimation finds the parameter values that make the observed data most likely.
- If the errors are Gaussian with mean zero, each output is Gaussian with mean and variance .
- Maximizing the log-likelihood of the outputs is the same as minimizing the squared error.
So squared error isn't an arbitrary choice. It is what maximum likelihood gives us when we assume Gaussian errors.