Linear Regression

Abstract

Linear regression is the most basic type of regression and commonly used in predictive analysis. Unlike the previous algorithms, linear regression can only be used for regression as it returns a real predicted value, i.e. 567 dollars per share, or predicting your son grows to be 6ft4. It models the relationship between dependent variable and one or more independent variables by fitting a linear equation to observed data. Linear regression finds the linear trend within the data with *weights *(parameters), and uses that to predict real values. We use *gradient descent* or *normal equations* to find the proper weights.

Once Upon A Time

You’re going to Europe to study abroad and the only option for your living situation is this house in the middle of nowhere, 45 mins away from university. The weather in the area is mysterious, as it’s raining 24/7. It’s an old, wooden house and has a melancholic vibe to it. It seems abandoned, maybe even haunted.

You get settled in and decide to take a more detailed tour of the house. You walk into the basement and you see a bloody knife stabbed into the wall. You think nothing of it.

You go about your day. The next morning, you go to the basement to wash some clothes. You’re putting your clothes in the dryer and you notice a second knife stabbed into the wall. You think maybe you just didn’t catch it yesterday.

By day four, you see four knives, and you begin to realize that the house just might be haunted. You pack up your bags and attempt to leave but all the doors are locked shut and there is no service to make a phone call. You go back into your room and hope it was all just a dream. Days go by and the new knifes continue stacking up.

Finally, after 10 days you hear a voice. The voice tells you that you if you can predict where the next knife will go, it will open the doors and let you free. Else, you are stuck in the house for eternity.

You go to the basement and you see 10 knives stabbed into the wall. Your jobs is to predict where the 11th knife will go.

You notice that there is a linear pattern in how the knives are being placed. Being the logical person you are, you realize your best bet is to approximate the pattern with a *straight* metal rod. How do you know where to place the rod to approximate the linear pattern the best? You use rubber bands.

You have 10 rubber bands, all of the same (small) size, and unbreakable. You tie each rubber band onto a knife and tie the rubber band to the metal rod directly above/below the respective knife, fully stretching each one.

Your goal is to find the best line (metal road) to approximate the data (knives). We can achieve that by minimizing the total vertical distance from the knives to the metal road. This is done naturally with the rubber bands, as the elastic energy of each rubber band will try to minimize the stretching as best as it can. Once it has reached equilibrium (convergence), you will see that the metal rod has done a pretty good job of approximating the pattern.

You then use the metal rod to predict where you thing the next knife will be placed. This is your best bet. You draw a red mark at that spot and go to sleep.

The next day, you see the sky has cleared up, the sun is out, and all the doors are unlocked. You have predicted the 11th knife correctly and are now set free.

Linear Regression

Linear regression attempts to fit a line to the data where the target value is expected to be a linear combination of the input variables. It fits a linear model with coefficients/weights to minimize the residual sum of squares between the observed responses in the dataset and the responses predicted by our linear approximation.

So, in 3 dimensions, this means we are trying to minimize the total distance between the plane and all of those points (residual sum of squares). This results in a linear plane that approximates our data.

Given training data with features, where and we want to learn a regression function so that it returns a real value instead of a class.

Regression model is said to be linear if it is represented by a linear function. Our linear regression model is given as:

with for all

’s are called parameters or coefficients or weights. So, learning the linear model means learning the ’s of the model. But how do we learn the ’s and how do we check how well your model () is performing? We use least square loss:

*Notice that this is the difference between our predicted answer and the actual answer. We should want to reduce this difference for an accurate model.*

We want to minimize the loss over all examples, that is minimize the residual sum of squares we talked about. We call this our cost function :

Our goal is to find the weights that minimize the above cost function.

For example, imagine we just had one feature :

Notice that this is the same as the equation for a line .

We want to minimize:

We want to find and that minimizes the above function. So, we’re left with:

With more than one feature, we could write it as:

to find the that minimizes

When you have the ’s, that is your model. You formed those from your training model. When you have new data, you can then multiply it with those and get a (hopefully) accurately predicted label.

*Remember, finding the weights that minimize the cost function means that you found the weights that form the best line/plane to approximate your data.*

You can look for those by using 1 of 2 methods: *gradient descent* or *normal equations*. Both have their pros and cons. We will go over gradient descent first.

Gradient Descent

Before discussing gradient descent, make sure you are familiar with partial derivatives/gradients.

Remember that our goal is to find the ’s that make close to , which is the same as minimizing the cost function . If we plot our cost function as a function of , we get:

As you can see, our error curve is *convex*, meaning we can simply find the minimum cost by finding the ’s that result in the bottom of the graph, known as the *global minima*.

To do so, we use gradient descent to start with some *initial guess* for , and we repeatedly change it to make smaller. Think of it as, you are weight and you take a step of size in the opposite direction of . You repeatedly move in this direction until you find this minimum.

Choosing the right step size is extremely important. You want to get to the center, so naturally you’ll go the opposite direction of (partial derivative/slope), but the actual step size is determined by . If the step size is too small, it will take an extremely long time to converge. If it’s too large, you’ll overshoot the global minimum. You must find just the right .

Recall that you can find the minimum by finding where the derivative is zero. So minimize , that is:

and for :

We can then update *simultaneously all * for and to slowly converge to that minimum point:

We repeat this step until our gradient is near zero, which is where we find our minimum. We call this convergence. This is known as an iterative method because we slowly update the until we reach a minimum point.

The second way is to solve it analytically in a closed-form with normal equations

Normal Equations

Let’s first try to write everything elegantly with matrices.

Let be a matrix where each row starts with a 1 followed by a feature vector.

Let be the label vector of the training set.

Let be the vector of weights (that we want to estimate).

Recall that we appended 1’s to the matrix first column because when we multiply it with the weight vector, for any we get which is what we needed.

We want to find ’s that minimize J. We have:

Remember, in order to find the minimum, we must set to zero, so:

Moving a few pieces around, we see that the unique solution is then:

Notice that if we take the second partial derivative, we have that:

is positive definite which ensures that that is a minimum.

*I can't stress this enough. The purpose of these algorithms is to find the optimal weights that form a line/plane that best approximates your data. This line/plane can then be used to predict new information*

Algorithm

Given a training set , with all training examples in matrix , and all training labels in vector , the algorithm (Normal Equations) to find the weights for Linear Regression works like this:

- Calculate with matrix multiplication.
- Calculate the inverse .
- Calculate the matrix-vector product of
- The weights are found by
- Given a new example , you can predict it’s value by simply calculating .

Example

You’re a real estate agent trying to predict your career trajectory. So far, you’ve been in the game for 6 years and have sold 4 homes. At year 1, you sold 2 homes. By year 3, you have sold 3 homes. This year (year 6), you have sold 4 homes. This is your training data:

You want to predict how many homes you would have sold 12 years from now, at year 18. We could solve it by gradient descent, but instead of iterating, we could just use normal equations to solve it analytically.

We first append a 1 to each training example, which would act as the bias , giving us:

We use matrix multiplication to calculate:

We then take it’s inverse:

Use matrix-vector multiplication:

Solving for our weights , we get:

So, to predict for , which is how many homes we will sell by year 18, we first append 1 for the bias:

And we simply calculate:

So, based on our algorithm and model, by year 18 we would have sold about 9 homes! Our line is given by

Summary/Discussion

Linear regressions’s relationships are modeled using linear predictor functions whose unknown parameters are estimated from the data. Such models are called linear models. One variable is the explanatory variable , and the other is the dependent variable . Like all forms of regression analysis, linear regression focuses of the conditional probability distribution , rather than on the joint probability distribution , which is the domain of multivariate analysis.

In order to find the right weights to fit a line to the data, we had to use gradient descent or normal equations. Once the optimal weights are found, we can then use that model to predict the value of any new data .

Other observations:

- When searching for weights, you can use two methods: Normal equation or Gradient Descent:
- Normal equation is an analytical approach. There is no need to specify a convergence rate or iterate. However, it only works if is invertible and very slow if large, as it will take to computer
- Gradient Descent is an iterative approach. It is effective and efficient even in high dimensions. However, since it’s iterative, it may need many iterations to converge and you also need to choose an efficient learning rate .

- Multicollinearity (or inter correlation) exists when at least some of the predictor variables are correlated among themselves. It is a
*linear*relation between the predictors. Predictors are usually related to some extent, it is a matter of degree. - When correlation among the ’s are low, linear regression has a lot of information to estimate . When correlation is high, it makes us relatively uncertain about our estimate of , because that means we have ’s that are a bit redundant.
- Multicollinearity could increase the standard error, which in turn means that the coefficients for some independent variables may be found statistically insignificant when they should be significant and the corresponding variable should be highly correlated with . You can detect this if you add or delete a variable and the regression coefficients change dramatically.