(BackBack)
ML//3 min read

Linear Regression

Linear regression is a linear approach for modeling the relationship between a scalar response and one or more explanatory variables.

Linear Regression

Linear Regression is a powerful tool used for prediction and forecasting in many fields. It's the starting point for regression analysis from an optimization viewpoint.

Suppose we have data with two features like in the figure below. This data represents the number of people in a specific state (x-axis) and the number of traffic accidents in each state (y-axis). We want to develop a model for this data. It looks like we can use a linear model (a straight line) to describe the relationship.

f(x)=ax+bf(x) = ax + b

Optimization: Finding the Best Line

How do we find the best line (i.e., determine the parameters 'a' and 'b')? This is the crux of linear regression.

Cost Function: Measurement of Errors

Figure 2. The difference between the predicted value of the linear model and the actual data.
Figure 2. The difference between the predicted value of the linear model and the actual data.

To determine which line best describes our data, we can define an error (ee) for each data point (ii) as follows:

ei=yi^yie_i = \hat{y_i} - y_i

Here, yi^\hat{y_i} is the predicted y-value from our model, and yiy_i is the actual y-value from our data.

To get a positive error irrespective of the sign, we square it:

ei=(yi^yi)2e_i = (\hat{y_i} - y_i)^2

And to simplify future differentiation, we divide it by 2:

ei=12(yi^yi)2e_i = \frac{1}{2}(\hat{y_i} - y_i)^2

Now, the average error for all data (total number of data = N) is:

E=12Ni=1N(yi^yi)2E = \frac{1}{2N}\sum_{i=1}^N(\hat{y_i} - y_i)^2

We already know that yi^=axi+b\hat{y_i} = ax_i + b, so we substitute:

E=12Ni=1N(axi+byi)2E = \frac{1}{2N}\sum_{i=1}^N(ax_i + b - y_i)^2

Cost Function: Visualization

The previously calculated EE is called a "cost function". The lower its value, the better the model's ability to explain the data.

We can view this cost function EE as a function of aa and bb, because xix_i and yiy_i are given in our data.

E=f(a,b)=12Ni=1N(axi+byi)2E = f(a,b) = \frac{1}{2N}\sum_{i=1}^N(ax_i + b - y_i)^2

The challenge now is to find the values of aa and bb that minimize EE.

Figure 3. Cost function and its minimum value in a space where slope and intercept are domain.
Figure 3. Cost function and its minimum value in a space where slope and intercept are domain.

Cost Function: Calculation

There are many ways to find the minimum value of a function, one common method being gradient descent.

Gradient descent is a method that allows us to iteratively move in the direction of steepest descent until we reach the minimum of the function.

Therefore, we can update the parameter a,ba,b after setting it to an arbitrary value in the function f(a,b)f(a,b) we want to obtain.

The vector [a,b]T\begin{bmatrix} a, b \end{bmatrix} ^ T may be updated as follows.

[ab]:=[ab]αf(a,b)\begin{bmatrix} a \\ b \end{bmatrix} := \begin{bmatrix} a \\ b \end{bmatrix} -α∇f(a,b)

α=learingrateα = learing rate

We start with initial guesses for aa and bb and update these guesses iteratively using the following update rules:

a:=aαfaa:=a−α\frac{∂f}{∂a}

b:=bαfbb:=b−α\frac{∂f}{∂b}

Figure 5. The process of finding the minimum value of the cost function using gradient descent.
Figure 5. The process of finding the minimum value of the cost function using gradient descent.

where αα is the learning rate, and f/a∂f/∂a and f/b∂f/∂b are the gradients of the function with respect to aa and bb.

By repeating these steps, we will eventually find the values of aa and bb that minimize the cost function E=f(a,b)E = f(a,b), thereby giving us the best line that fits our data.