In the context of linear regression we are trying to solve a very simple problem. Given a sequence of pairs of points we are trying to find the polynomial that best fits these points with respect to a loss function (least squares is very commonly used in these cases). Essentially we are trying to come up with a function of the form:
We show mathematically how to find the set of coefficients which minimizes the square loss function.
Let . Then, in the context of linear regression, we need to find the coefficients that minimize our error function. In this case let the error function be with formula:
Our objective is to minimize the loss w.r.t. hence we need to find such that is minimum.
Before taking the derivative we expand the expression for the loss:
In order to compute the derivative of above, we need to show the following results:
Let . Then:
Now let the more interesting function , for k-dimensional square matrix. Then:
Differentiating this expression we get:
Hence we get that:
Finally, we are in place to differentiate the original equation using our two latest results. Let and also let . Then we get:
Equating the derivative with and solving the equation we get :
It has to be noted that this is a very simple linear regression model which suffers from many drawbacks such as overfitting. Other more complex regression models exist and use regularizers to control the size of the coefficients (Lasso etc.)