We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

The purpose of this article is to support novice data scientists. In the previous article simple terms, we analyzed three methods for solving the linear regression equation: analytical solution, gradient descent, and stochastic gradient descent. For the analytical solution, we used the formula We present the equation of linear regression in matrix form.. In this article, as the title suggests, we will justify the use of this formula or, in other words, derive it ourselves.

Why it makes sense to pay special attention to the formula We present the equation of linear regression in matrix form.?

It is with the matrix equation that introductions to linear regression typically begin. However, detailed derivations of how the formula was derived are rarely encountered.

For example, in Yandex's machine learning courses, when students are introduced to regularization, they are advised to use functions from the library sklearn, yet there is no mention of the matrix representation of the algorithm. This is the moment when some students may feel inclined to delve deeper into the issue—writing code without using the built-in functions. To achieve this, one must first represent the equation with the regularizer in matrix form. This article will indeed help those who wish to master such skills. Let's get started.

Initial Conditions

Target Indicators

We have a series of values for the target indicator. For example, the target indicator could be the price of an asset: oil, gold, wheat, or the dollar, etc. By a series of values of the target indicator, we mean the number of observations. Such observations could be, for instance, the monthly oil prices over a year, which means we will have 12 values for the target indicator. Let's start by introducing notations. We will denote each value of the target indicator as We present the equation of linear regression in matrix form.. Overall, we have We present the equation of linear regression in matrix form. observations, and thus we can represent our observations as We present the equation of linear regression in matrix form..

Regressors

Let's assume that there are factors that, to a certain extent, explain the values of the target variable. For example, the exchange rate of the dollar/ruble is strongly influenced by the price of oil, the Federal Reserve rate, and others. Such factors are called regressors. In this case, each value of the target variable should correspond to a value of the regressor, meaning that if we have 12 target values for each month in 2018, we should also have 12 regressor values for the same period. We denote the values of each regressor as We present the equation of linear regression in matrix form.. Suppose that in our case there are We present the equation of linear regression in matrix form. regressors (i.e., We present the equation of linear regression in matrix form. factors that influence the values of the target variable). This means our regressors can be represented as follows: for the 1st regressor (for example, the price of oil): We present the equation of linear regression in matrix form., for the 2nd regressor (for example, the Federal Reserve rate): We present the equation of linear regression in matrix form., for the "We present the equation of linear regression in matrix form.-th" regressor: We present the equation of linear regression in matrix form.

The dependence of the target variables on the regressors

Let’s assume that the dependence of the target variable We present the equation of linear regression in matrix form. on the regressors "We present the equation of linear regression in matrix form.-th" observation can be expressed through a linear regression equation of the form:

We present the equation of linear regression in matrix form.

, where We present the equation of linear regression in matrix form. — «We present the equation of linear regression in matrix form.-th" value of the regressor from 1 to We present the equation of linear regression in matrix form.,

We present the equation of linear regression in matrix form. - the number of regressors from 1 to We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form. - the coefficients that represent the magnitude by which the calculated target variable will change on average with a change in the regressor.

In other words, we determine a "unique" coefficient for each regressor (except for We present the equation of linear regression in matrix form.), then we multiply the coefficients by the values of the regressors for the " We present the equation of linear regression in matrix form.-th" observation, resulting in an approximation of the "We present the equation of linear regression in matrix form.-th" target variable.We present the equation of linear regression in matrix form.Therefore, we need to find coefficients

, for which the values of our approximating function We present the equation of linear regression in matrix form.will be positioned as close as possible to the values of the target variables. We present the equation of linear regression in matrix form. Assessment of the quality of the approximating function

We will define the quality of the approximating function using the method of least squares. The quality assessment function will take the following form:

We need to find values for the coefficients $w$, for which the value

We present the equation of linear regression in matrix form.

will be minimal. We present the equation of linear regression in matrix form. We convert the equation into matrix form

Vector representation

To start, to make life easier, we should pay attention to the linear regression equation and notice that the first coefficient

To start with, to make your life easier, you should pay attention to the linear regression equation and notice that the first coefficient We present the equation of linear regression in matrix form. is not multiplied by any regressors. Consequently, when we convert the data into matrix form, this circumstance will seriously complicate the calculations. In this regard, it is proposed to introduce another regressor for the first coefficient We present the equation of linear regression in matrix form. and set it to one. More precisely, each "We present the equation of linear regression in matrix form.-th" value of this regressor will be set to one — since multiplying by one will not change our computational results, and from the perspective of matrix multiplication rules, it will significantly reduce our struggles.

Now, for a while, to simplify the material, let’s assume we have only one "We present the equation of linear regression in matrix form.-th" observation. Then, let's represent the regressor values of the "We present the equation of linear regression in matrix form.-th" observation as a vector We present the equation of linear regression in matrix form.. The vector We present the equation of linear regression in matrix form. has a dimension of We present the equation of linear regression in matrix form., which means We present the equation of linear regression in matrix form. rows and 1 column:

We present the equation of linear regression in matrix form.

The sought coefficients can be represented as a vector We present the equation of linear regression in matrix form., which has a dimension of We present the equation of linear regression in matrix form.:

We present the equation of linear regression in matrix form.

The linear regression equation for the "We present the equation of linear regression in matrix form.-th" observation will take the form:

We present the equation of linear regression in matrix form.

The function for evaluating the quality of the linear model will take the form:

We present the equation of linear regression in matrix form.

Note that according to the rules of matrix multiplication, we needed to transpose the vector We present the equation of linear regression in matrix form..

Matrix representation

As a result of multiplying the vectors, we will get a number: We present the equation of linear regression in matrix form., which was to be expected. This number is the approximation of the "We present the equation of linear regression in matrix form.-th" target indicator. However, we need an approximation of not just one target indicator but all of them. To achieve this, let's write all the "We present the equation of linear regression in matrix form.-th" regressors in matrix format We present the equation of linear regression in matrix form.. The resulting matrix has a dimension of We present the equation of linear regression in matrix form.:

We present the equation of linear regression in matrix form.

Now the linear regression equation will take the form:

We present the equation of linear regression in matrix form.

Let's denote the values of the target indicators (all We present the equation of linear regression in matrix form.) as a vector We present the equation of linear regression in matrix form. with a dimension of We present the equation of linear regression in matrix form.:

We present the equation of linear regression in matrix form.

Now we can write the equation evaluating the quality of the linear model in matrix format:

We present the equation of linear regression in matrix form.

In fact, from this formula, we subsequently obtain the well-known formula We present the equation of linear regression in matrix form.

How is this done? Parentheses are expanded, differentiation is performed, the resulting expressions are transformed, etc., and this is exactly what we will now engage in.

Matrix transformations

Let’s expand the parentheses

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

Prepare the equation for differentiation

For this, we will perform some transformations. In the subsequent calculations, it will be more convenient if the vector We present the equation of linear regression in matrix form. is presented at the beginning of each product in the equation.

Transformation 1

We present the equation of linear regression in matrix form.

How did this happen? To answer this question, it's enough to look at the sizes of the multiplied matrices and see that we obtain a number or otherwise We present the equation of linear regression in matrix form..

Let's write down the dimensions of the matrix expressions.

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

Transformation 2

We present the equation of linear regression in matrix form.

Let's outline similarly to transformation 1

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

On the output, we obtain an equation that we need to differentiate:
We present the equation of linear regression in matrix form.

Let's differentiate the quality assessment function of the model

We will differentiate with respect to the vector We present the equation of linear regression in matrix form.:

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

There should be no questions as to why We present the equation of linear regression in matrix form. but we will consider the operations for determining the derivatives in the two other expressions in more detail.

Differentiation 1

Let's break down the differentiation: We present the equation of linear regression in matrix form.

To determine the derivative of a matrix or vector, we need to look at their contents. Let's take a look:

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form. We present the equation of linear regression in matrix form.

We will denote the product of matrices We present the equation of linear regression in matrix form. as a matrix We present the equation of linear regression in matrix form.. The matrix We present the equation of linear regression in matrix form. is square and moreover, it is symmetric. These properties will be useful later, let's remember them. The matrix We present the equation of linear regression in matrix form. has a dimension of We present the equation of linear regression in matrix form.:

We present the equation of linear regression in matrix form.

Now our task is to correctly multiply the vectors by the matrix and avoid getting 'two times two equals five,' so let's focus and be extremely careful.

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

However, we ended up with an intricate expression! In fact, we have obtained a number—a scalar. And now, we are truly transitioning to differentiation. We need to find the derivative of the obtained expression with respect to each coefficient We present the equation of linear regression in matrix form. and we will get an output vector of size We present the equation of linear regression in matrix form.. Just in case, I will describe the procedures step by step:

1) differentiate with respect to We present the equation of linear regression in matrix form., we will obtain: We present the equation of linear regression in matrix form.

2) differentiate with respect to We present the equation of linear regression in matrix form., we will obtain: We present the equation of linear regression in matrix form.

3) differentiate with respect to We present the equation of linear regression in matrix form., we will obtain: We present the equation of linear regression in matrix form.

On output—the promised vector of size We present the equation of linear regression in matrix form.:

We present the equation of linear regression in matrix form.

If we look closely at the vector, we can notice that the left and corresponding right elements of the vector can be grouped in such a way that ultimately, from the presented vector, we can extract a vector We present the equation of linear regression in matrix form. of size We present the equation of linear regression in matrix form.. For example, We present the equation of linear regression in matrix form. (the left element of the top row of the vector) We present the equation of linear regression in matrix form. (the right element of the top row of the vector) can be represented as We present the equation of linear regression in matrix form., and We present the equation of linear regression in matrix form. — as We present the equation of linear regression in matrix form. and so on for each row. Let's group them:

We present the equation of linear regression in matrix form.

We will factor out the vector We present the equation of linear regression in matrix form. and we will obtain:

We present the equation of linear regression in matrix form.

Now, let's take a closer look at the resulting matrix. The matrix represents the sum of two matrices We present the equation of linear regression in matrix form.:

We present the equation of linear regression in matrix form.

Let's remember that some time ago, we pointed out one important property of the matrix We present the equation of linear regression in matrix form. — it is symmetric. Based on this property, we can confidently state that the expression We present the equation of linear regression in matrix form. is equal to We present the equation of linear regression in matrix form.. This is easy to verify by expanding the matrix multiplication element-wise. We present the equation of linear regression in matrix form.We won't do this here, those interested can conduct the check themselves.

Let's return to our expression. After our transformations, it turned out just as we wanted to see it:

We present the equation of linear regression in matrix form.

So, we have managed the first differentiation. Now, let's move on to the second expression.

Differentiation 2

We present the equation of linear regression in matrix form.

Let's take the beaten path. It will be much shorter than the previous one, so don't stray far from the screen.

Let's break down the vectors and the matrix element by element:

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

For now, let's remove the two from the calculations — it doesn't play a significant role, and we'll put it back later. We'll multiply the vectors by the matrix. First, we will multiply the matrix We present the equation of linear regression in matrix form. by the vector We present the equation of linear regression in matrix form., here we have no restrictions. We'll get a vector of size We present the equation of linear regression in matrix form.:

We present the equation of linear regression in matrix form.

Next, we will perform the following operation — multiply the vector We present the equation of linear regression in matrix form. by the resulting vector. We will end up with a number:

We present the equation of linear regression in matrix form.

This is what we will differentiate. We will get a vector of dimension We present the equation of linear regression in matrix form.:

We present the equation of linear regression in matrix form.

Does this remind you of something? That's right! This is the product of the matrix We present the equation of linear regression in matrix form. by the vector We present the equation of linear regression in matrix form..

Thus, the second differentiation is successfully completed.

In conclusion

Now we know how the equality was derived We present the equation of linear regression in matrix form..

Finally, let's outline a quick way to transform the basic formulas.

We will assess the model's quality according to the least squares method:
We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

Let's differentiate the obtained expression:
We present the equation of linear regression in matrix form. We present the equation of linear regression in matrix form.

We present the equation of linear regression in matrix form.

Literature

Internet sources:

1) habr.com/ru/post/278513
2) habr.com/ru/company/ods/blog/322076
3) habr.com/ru/post/307004
4) nabatchikov.com/blog/view/matrix_der

Textbooks, problem sets:

1) Lecture notes on higher mathematics: complete course / D.T. Pismennyi – 4th ed. – Moscow: Iris-Press, 2006
2) Applied regression analysis / N. Draper, G. Smith – 2nd ed. – Moscow: Finance and Statistics, 1986 (translated from English)
3) Problems on solving matrix equations:
function-x.ru/matrix_equations.html
mathprofi.ru/deistviya_s_matricami.html


Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster