MLS.1.a Concepts for Linear Regression
Tags: algebra, linearregression, machinelearning
Published: 2019-11-12 06:13:16


The idea behind simple linear regression is to "fit" the observations of two variables into a linear relationship between them. Graphically, the task is to draw the line that is "best-fitting" or "closest" to the points.

The equation of a straight line is written using the **`y = mx + b`**, where **`m`** is the slope (Gradient) and **`b`** is y-intercept (where the line crosses the Y axis).Where, **`m`** is the slope (Gradient) and **`b`** is y-intercept (Bias).In calculation of mean and y-intercept, we use some of the mathematical concepts explained below:

### **Mean**

This term is used to describe properties of statistical distributions. It is determined by adding all the data points in a population and then dividing the total by the number of points. The resulting number is known as the mean or the average.
 <center>**`x̄ = Sum of observations / number of observations`**</center>

### **Variance**

Variance (**`σ2`**) is a measurement of the spread between numbers in a data set. That is, it measures how far each number in the set is from the mean and therefore from every other number in the set.
<strong><center><code>Variance = n * sum of all(x<sub>i</sub> − x̄)<sup>2</sup></code></center></strong>
Where:
&nbsp;&nbsp;&nbsp;&nbsp; <code><strong>x<sub>i</sub></strong></code> = i<sup>th</sup> data point
&nbsp;&nbsp;&nbsp;&nbsp; <code><strong>x̄</strong></code> = the mean of all data points
&nbsp;&nbsp;&nbsp;&nbsp; <code><strong>n</strong></code> = the number of data points

### **Co-variance**

Covariance is a measure of how much two random variables vary together. It’s similar to variance, but where variance tells you how a single variable varies, co-variance tells you how two variables vary together.Square root of variance is called `Standard Deviation`
<center> **`Cov(X,Y) = Σ (E(X) - μ) * (E(Y) - ν) / (n - 1)`** </center>
Where
&nbsp;&nbsp;&nbsp;&nbsp; **`X`** is a random variable
&nbsp;&nbsp;&nbsp;&nbsp; **`E(X)`** = **`μ`** is the expected value (the mean) of the random variable **`X`**
&nbsp;&nbsp;&nbsp;&nbsp; **`E(Y)`** = **`ν`** is the expected value (the mean) of the random variable **`Y`**
&nbsp;&nbsp;&nbsp;&nbsp; **`n`** = the number of items in the data set

### **Correlation**

**Correlation(r)** is a statistical technique that can show whether and how strongly pairs of variables are related.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/70yu9ljtxsx0o45qs90j.png)
<center><small><strong><code>S<sub>x</sub></code></strong>, <strong><code>S<sub>y</sub></code></strong> = Standard deviation of `x`, `y`</small></center>

### **Root Mean Square Error (RMSE)**

Root Mean Square Error (RMSE) is the standard deviation of the residuals(prediction errors). Residuals are a measure of how far from the regression line data points are.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/kgllofd5bfihljduw61m.png)

### **Calculation of Slope and Bias**


The slope of the line is calculated as the change in y divided by change in x.

<center><strong><code>slope m = change in y / change in x</code></strong></center>

The y-intercept over bias shall be calculated using the formula

<center><strong><code>y = m(x - x<sub>1</sub>) + y<sub>1</sub></code></strong></center>

These values are different from what was actually there in the training set and if we plot this (x, y) graph against the original graph, the straight line will be way off the original points in the graph. This may lead to error which is the difference of values between actual points and the points on the straight line. Ideally, we’d like to have a straight line where the error is minimized across all points.Error can be reduced using many mathematical ways. One of such method is "Least Square Regression"

### **Least Square Regression**

Least Square Regression is a method which minimizes the error in such a way that the sum of all square error is minimized.
<center><strong>
<code>m = (Σ ((x - x̄) * (y - ȳ)) / Σ (x - x̄))<sup>2</sup></code></strong>
*(or)*
<strong><code>m = r(S<sub>y</sub> / S<sub>x</sub>)</code></strong>
*(and we get the y-interept)*
<strong><code>b = ȳ - m * x̄</code>
</strong></center>

Where
&nbsp;&nbsp;&nbsp;&nbsp; <code><strong>S<sub>x</sub></strong></code> is standard deviation of `x`
&nbsp;&nbsp;&nbsp;&nbsp; <code><strong>S<sub>y</sub></strong></code> is standard deviation of `y`
&nbsp;&nbsp;&nbsp;&nbsp; **`r`** is correlation between `x` and `y`
&nbsp;&nbsp;&nbsp;&nbsp; **`m`** is slope
&nbsp;&nbsp;&nbsp;&nbsp; **`b`** is the y-intercept

This method is intended to reduce the sum square of all error values. The lower the error, lesser the overall deviation from the original point.

### **Cost Function**

The cost function calculates the square of the error for each example in the dataset, sums it up and divides this value by the number of examples in the dataset (denoted by `m`).This cost function helps in determining the best fit line.The cost function for two variables <strong><code>θ<sub>0</sub></code></strong> and <strong><code>θ<sub>1</sub></code></strong> denoted by **`J`** and is given as follows.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9s039516h7vc4ty8i2me.png)
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ymej6u66w2gueseg9x7a.png)

Now, we have to make use of cost function to adjust our parameters  <strong><code>θ<sub>0</sub></code></strong> and <strong><code>θ<sub>1</sub></code></strong> such that they result in the least cost function value. We make use of a technique called Gradient Descent to minimize the cost function. 

###__Read On :memo:__

1. [MLS.1.b Gradient Descent in Linear regression](https://dev.to/ml_scratch/mls-1-b-gradient-descent-in-linear-regression-53dl)

## Contributors 

This series is made possible by help from:
* Pranav (@devarakondapranav)
* Ram (@r0mflip)
* Devika (@devikamadupu1)
* Pratyusha(@prathyushakallepu)
* Pranay (@pranay9866)
* Subhasri (@subhasrir)
* Laxman (@lmn)
* Vaishnavi(@vaishnavipulluri)
* Suraj (@suraj47)