Regression
Linear Regression by Matrix Inversion Method/LSM
2023-12-13
Linear Regression - Least Square Method
We would like to create a model that best represents a variable "y" defined in terms of variable "X". In this case, the framing of the model, such as the degree, the algorithm, etc., should be chosen by the user. The detailed content of the model will be filled using machine learning techniques with the given data.
X: Source Parameters, Variable (n number of data , d number of features) [nxd] Matrix
y: Response Parameters, Measurements (n number of data) [nx1] Matrix
Linear regression is a methodology for determining the coefficient vector, w, when expressing the predicted value as (y' = Xw), where w is a matrix of size [d x 1].
The cost function for finding the value of w in linear regression is defined as minimizing the difference between the actual value, y, and the predicted value, y'. Mathematically, it can be expressed as:
\(L=\sum_{\ }^{\ } \epsilon^2 = \sum_{\ }^{\ }(y-y')^2=(y-Wx)^T(y-Wx)\)
Here, y and X are data, and only w is a variable. In other words, L = f(w).
The minimum value of L occurs at w where the partial derivative (i.e. the Least Square Method)
\(\partial L/ \partial w = 0\)
To find w that satisfies
\(-X^T(y-Xw) = 0\)
we are going to use Matrix Inversion Method.
When solving the system of equations,
\(X^Ty = X^TXw\)
Note that the size of the matrix is [dxn][nx1] = [dxn][nxd][dx1]. Therefore, if XTX is standardized in advance with the average value and has a size of [dxd], it becomes a Covariance Matrix. Additionally, regardless of the number of data (n), it forms a square matrix of size equal to the number of variables (d), which ensures the existence of an inverse matrix.
By multiplying the inverse of XTX (also known as (XTX)-1) with the matrix product, we can obtain the coefficient w.
\(w = (X^TX)^{-1}X^Ty\)
In the case of a single variable, (i.e. X=x, y=xw)
\(w=\dfrac {x^Ty}{x^Tx}\) =SUMPRODUCT(x,y)/SUMSQ(x)
When dealing with multiple variables, let's explain using the following example. Given the data X, Y, Z:
(step 1) Normalize the values by their mean:
\(X=\dfrac{x:y-\overline{x:y}}{n}\)
\(Y=\dfrac {z-\overline{z}}{n}\)
(step 2) XTX = {MMULT(TRANSPOSE(X),X)}, here {} means to use "Array function" which required to select the cell ahead of the function call.
(step 3) XTY = {MMULT(TRANSPOSE(X),Y)}
(step 4) (XTX)-1 = MINVERSE(XTX)
(step 5) w = {MMULT((XTX)-1,XTY)}
(step 6) offset
\(c=\overline {z}-\overline {x}a-\overline {y}b\)

<NOTE> We can also use the "Solver" to find the coefficients using the following steps.
(step 1) Initialize variables a, b, and c with arbitrary values as follows:
(step 2) The Solver will find the optimal values for a, b, and c that minimize the sum of squared errors.
