Linear Regression

PUBLISHED: MAY 2, 20262 MIN READ

Regressionlinear regressionpolynomial regressionlogistic regressionLinear regression is a method in machine learning where we fit a straight line to a set of da

Divya Sachan
Divya SachanAuthor
budew
#406
budew

Regression

  • linear regression
  • polynomial regression
  • logistic regression

Linear regression is a method in machine learning where we fit a straight line to a set of data points that appear to be concentrated around the line. This model helps to predict the outcome for future values.

Equation of line:

  • where is scaler used to transform the line.
  • and (coefficient of x) is slope of the line
Linear regression graph showing best fit line with actual data points, predicted values, slope (beta), and y-intercept
(Input)..
(Outcome)..

In the figure, n data points are plotted. We are trying to fit the line to this set of discrete data points.

  1. Given Data:
    • (Actual Data): The value of the output variable where , as provided by the data set.
  2. Line Equation:
    • (Predicted Data): The value of the outcome as predicted by the line , corresponding to .
  3. Error Calculation:
    • Error: The difference between (actual data point) and (predicted value from the line).
    • Square of Errors:
    • Sum of Square of Errors:

Where:



Goal

Now, we are trying to find out those value of and for which is minimum.

Partial Derivative with respect to :

using the power rule:

  1. Differentiate with respect to


  2. Apply the Chain Rule:

  3. Differentiate the Inner Term:








  4. Put the inner term:










  5. Simplify:






    Divide both sides by :






Equation1:yˉ=α+βxˉEquation^1:\bar{y}=\alpha+\beta{\bar{x}}

Partial Derivative with respect to :

  1. Differentiate the Error Function with respect to


  2. Apply the Chain Rule:
  3. Differentiate the Inner Term:








  4. Put the inner term:







  5. Simplify:







    Divide both sides by :



Equation2:(xy)=αx+β(x2)Equation^2:\overline{(x y)}=\alpha\overline{x}+\beta\overline{(x^2)}

Equation 1:

Equation 2:

Multiply Equation 1 by :

Subtracting from Equation 2:

Equation3:β=(xy)x.y((x2)x2)Equation^3:\beta=\frac{\overline{(xy)} - \overline{x} . \overline{y}}{(\overline{(x^2)} - \overline{x}^2)}

Variance

Equation4:Var(x)=(x2)xˉ2Equation^4:Var(x)=\overline{(x^2)}-\bar{x}^2

Co-variance

cov(x,y)=1ni=1n(xix)(yiy)cov(x,y)=\frac{1}{n}\sum_{i=1}^{n}\left(x_{i}-\overline{x})(y_{i}-\overline{y}\right)
cov(x,y)=1ni=1n(xiyixiyxyi+x.y)cov(x,y)=\frac{1}{n}\sum_{i=1}^{n}(x_{i}y_{i}-x_{i}\overline{y}-\overline{x}y_{i}+\overline{x}.\overline{y})
cov(x,y)=1n(i=1nxiyii=1nxiyi=1nxyi+i=1nx.y))cov(x,y)=\frac{1}{n}\left(\sum_{i=1}^{n}x_{i}y_{i}-\sum_{i=1}^{n}x_{i}\overline{y}-\sum_{i=1}^{n}\overline{x}y_{i}+\sum_{i=1}^{n}\overline{x}.\overline{y})\right)
cov(x,y)=1n(i=1nxiyiyi=1nxixi=1nyi+x.yi=1n.1)cov(x,y)=\frac{1}{n}\left(\sum_{i=1}^{n}x_{i}y_{i}-\overline{y}\sum_{i=1}^{n}x_{i}-\overline{x}\sum_{i=1}^{n}y_{i}+\overline{x}.\overline{y}\sum_{i=1}^{n}.1\right)
cov(x,y)=(xy)y.xx.y+x.ycov(x,y)=\overline{(xy)}-\overline{y}.\overline{x}-\overline{x}.\overline{y}+\overline{x}.\overline{y}
cov(x,y)=(xy)x.ycov(x,y)=\overline{(xy)}-\overline{x}.\overline{y}
Equation5:cov(x,y)=(xy)x.yEquation^5:cov(x,y)=\overline{(xy)}-\overline{x}.\overline{y}
Equation1:yˉ=α+βxˉEquation^1:\bar{y}=\alpha+\beta{\bar{x}}
Equation2:(xy)=αx+β(x2)Equation^2:\overline{(x y)}=\alpha\overline{x}+\beta\overline{(x^2)}
Equation3:β=(xy)x.y((x2)x2)Equation^3:\beta=\frac{\overline{(xy)}-\overline{x}.\overline{y}}{\left(\overline{(x^2)}-\overline{x}^2\right)}
Equation4:Var(x)=(x2)xˉ2Equation^4:Var(x)=\overline{(x^2)}-\bar{x}^2
Equation5:cov(x,y)=(xy)x.yEquation^5:cov(x,y)=\overline{(xy)}-\overline{x}.\overline{y}
β=(xy)y.y((x2)x2)\beta=\frac{\overline{(xy)}-\overline{y}.\overline{y}}{\left(\overline{(x^2)}-\overline{x}^2\right)}
β=cov(x,y)var(x)\beta=\frac{cov(x, y)}{var(x)}

Put the value of in :

α=ycov(x,y)var(x)x\alpha=\overline{y}-\frac{cov(x, y)}{var(x)}\overline{x}
α=ycov(x,y)(x2)x2×x\alpha = \overline{y} - \frac{cov(x, y)}{\overline{(x^2)} - \overline{x}^2} \times \overline{x}
α=ycov(x,y)(x2)x\alpha=\overline{y}-\frac{cov(x, y)}{\overline{(x^2)} - \overline{x}}

Final Value

β=cov(x,y)var(x)\beta=\frac{cov(x,y)}{var\left(x\right)}
α=yβx\alpha=\overline{y}-\beta\overline{x}