Linear Regression
The Problem
Linear regression is one of the simplest and most powerful techniques in machine learning.
A classic linear regression problem is predicting the price of a house from its area. The bigger the house, the higher we expect the price to be.
The price is a continuous value, and we need past data to make any assumptions about it. So suppose we have the area and price of a few houses.
If we plot this data, with house area on the x-axis and house price on the y-axis, the data points mostly fall along a straight line. Given this, can we estimate the price of a new house?
This is a supervised regression problem:
- It is supervised because the data contains both the inputs and the output labels.
- It is regression because the output is a continuous value.
We call this the one-dimensional case because there is only one input, the house area.
Simple One-Dimensional Linear Regression
We start with a dataset of labeled data points:
For all , both and are real values:
- is the feature, which here is the house area.
- is the label, which here is the house price.
We want to build a model that takes any input and predicts .
Since the data mostly falls on a straight line, we can use the equation of a line as our model. Instead of writing it as , we write:
Many straight lines are possible, but most of them don't match the data points. Our job is to find the values of and that give us the line that fits best.
Measuring the Error
For the samples, the model's predicted outputs are:
The actual outputs from the training data are .
We want the predicted outputs to be as close as possible to the actual outputs. For the -th sample, the error is the gap between the two:
On a plot, these errors are the vertical distances between the data points and the line.
We can't just add up the errors and minimize the sum. Some errors are positive and some are negative, so they would cancel each other out. Instead, we square each error.
This gives us the objective function:
The doesn't change the answer, because the values of and that minimize a function also minimize half of it. We add it because it cancels a factor of 2 when we take derivatives.
Substituting , the function we want to minimize is:
Minimizing a Function
Let's first recall how to minimize a function of one variable. Take this function:
- Take the first derivative: .
- Set it to zero: , so . This is a critical point, where the function can have a local minimum or maximum.
- Take the second derivative: .
- Since , the critical point is a local minimum.
The minimum value is .
Our objective function is different because it has two variables, and . In this multivariate case, we replace the first and second derivatives with the gradient and the Hessian, which are built from partial derivatives.
The Gradient
First, we take the partial derivative of the objective with respect to each variable. The 2 from the power cancels with the we added earlier:
The gradient is a vector containing all the partial derivatives of a function. It plays the role of the first derivative:
The Hessian
The Hessian plays the role of the second derivative. It is a matrix of second-order partial derivatives:
Taking the partial derivatives of the gradient terms gives:
If the Hessian is positive definite, the critical point is a local minimum. A matrix is positive definite if, for every non-zero vector :
This is the multivariate version of checking that the second derivative is greater than zero.
Multi-Dimensional Linear Regression
So far we have used a single input. In practice, a house price depends on more than its area.
Suppose the inputs are the house area, the number of bedrooms, the number of toilets, and the number of balconies. The output is still the house price.
The dataset still has data points . The difference is that each is now a -dimensional vector, where is the number of input features. In this example, . Each is still a real value.
The model now combines all features:
The superscripts are the dimensions, so is the -th feature of the -th sample. We can write this more compactly as:
Vector Form
We can simplify this further using vectors.
Let be the vector , which has values. Each has only values, so we add a 1 at the start to give it values too:
The prediction then becomes a single vector multiplication:
Matrix Form
The error for each sample is the same as before, . To handle all samples at once, we build a matrix where each row is one data sample:
The dimensions work out as follows:
| Term | Dimensions |
|---|---|
The total squared error on the training data is then a function of :
The Closed-Form Solution
To minimize the error, we first expand it:
Taking the derivative with respect to gives:
Setting the derivative to zero gives:
Solving for gives the closed-form solution:
This only works if is invertible. If it isn't, we use the pseudo-inverse instead:
Why We Need Another Approach
The formula gives us the exact answer, but it becomes highly computationally expensive when the dataset is large. For large datasets, we can't find directly this way, so we need a different approach.
Summary
| Concept | Simple linear regression | Multiple linear regression |
|---|---|---|
| Inputs | One feature | features |
| Model | ||
| Objective |
The key ideas are:
- Linear regression fits a line to the data to predict a continuous value.
- We find the best line by minimizing the sum of squared errors.
- Setting the gradient to zero gives us a critical point, and a positive definite Hessian confirms it is a minimum.
- The closed-form solution is , but it is too expensive for large datasets.