Simple Linear Regression (One Variable)

 

Simple Linear Regression (One Variable)


🔹 Definition

Simple Linear Regression (SLR) is a supervised learning method used to model the relationship between:

  • One independent variable (input)
  • One dependent variable (output)

👉 It assumes a linear relationship between them


🔹 Problem Formulation

Given dataset:

D={(x1,y1),(x2,y2),...,(xn,yn)}D = \{(x_1,y_1), (x_2,y_2), ..., (x_n,y_n)\}

👉 Goal:
Learn a function:

y=f(x)y = f(x)

🔹Model Representation (Hypothesis)

The model is a straight line:

y=β0+β1xy = \beta_0 + \beta_1 x

Where:

  • β0\beta_0 → intercept
  • β1\beta_1 → slope
  • xx → input
  • yy → predicted output

👉 This represents the best-fit line through the data


🔹 Interpretation of Parameters

📌 Intercept (β0\beta_0)

  • Value of yy when x=0x = 0

📌 Slope (β1\beta_1)

  • Change in yy for a unit change in xx

👉 Example:
If slope = 2 → increasing xx by 1 increases yy by 2


🔹Key Idea: Best-Fit Line

👉 The regression line is chosen such that:

  • It minimizes the error between actual and predicted values
  • Error = vertical distance from data point to line

👉 This is called residual


🔹  Loss Function (Important)

Most commonly used:

L=i=1n(yiy^i)2L = \sum_{i=1}^{n}(y_i - \hat{y}_i)^2

👉 This is Mean Squared Error (MSE)

👉 Goal:

minβ0,β1(yi(β0+β1xi))2\min_{\beta_0,\beta_1} \sum (y_i - (\beta_0 + \beta_1 x_i))^2

👉 The best-fit line minimizes squared errors



🔹 Solution (Closed Form)

🔹 Problem Setup

Given data:

(x1,y1),(x2,y2),...,(xn,yn)(x_1,y_1), (x_2,y_2), ..., (x_n,y_n)

Model:

y^i=β0+β1xi\hat{y}_i = \beta_0 + \beta_1 x_i

👉 Goal: Find β0,β1\beta_0, \beta_1 that best fit the data.

🔹  Define Loss Function (Least Squares)

J(β0,β1)=i=1n(yiβ0β1xi)2J(\beta_0, \beta_1) = \sum_{i=1}^{n} (y_i - \beta_0 - \beta_1 x_i)^2

👉 We minimize this error.


🔹 Take Partial Derivatives

We compute:

Jβ0=0,Jβ1=0\frac{\partial J}{\partial \beta_0} = 0, \quad \frac{\partial J}{\partial \beta_1} = 0


🔹 Derivative w.r.t.

Jβ0=2(yiβ0β1xi)\frac{\partial J}{\partial \beta_0} = -2 \sum (y_i - \beta_0 - \beta_1 x_i)

Set to zero:

(yiβ0β1xi)=0\sum (y_i - \beta_0 - \beta_1 x_i) = 0


Simplify:

yi=nβ0+β1xi\sum y_i = n\beta_0 + \beta_1 \sum x_i


🔹 Derivative w.r.t.β1

Jβ1=2xi(yiβ0β1xi)\frac{\partial J}{\partial \beta_1} = -2 \sum x_i (y_i - \beta_0 - \beta_1 x_i)

Set to zero:

xiyi=β0xi+β1xi2\sum x_i y_i = \beta_0 \sum x_i + \beta_1 \sum x_i^2


🔹 Solve the Two Equations

We now have:

Equation (1):

yi=nβ0+β1xi\sum y_i = n\beta_0 + \beta_1 \sum x_i

Equation (2):

xiyi=β0xi+β1xi2\sum x_i y_i = \beta_0 \sum x_i + \beta_1 \sum x_i^2


🔹 Use Mean Values

Let:

xˉ=1nxi,yˉ=1nyi\bar{x} = \frac{1}{n}\sum x_i,\quad \bar{y} = \frac{1}{n}\sum y_i


From Equation (1):

β0=yˉβ1xˉ\beta_0 = \bar{y} - \beta_1 \bar{x}


🔹Substitute into Equation (2)

xiyi=(yˉβ1xˉ)xi+β1xi2\sum x_i y_i = (\bar{y} - \beta_1 \bar{x}) \sum x_i + \beta_1 \sum x_i^2


Expand:

xiyi=yˉxiβ1xˉxi+β1xi2\sum x_i y_i = \bar{y} \sum x_i - \beta_1 \bar{x} \sum x_i + \beta_1 \sum x_i^2


Rearrange:

xiyiyˉxi=β1(xi2xˉxi)\sum x_i y_i - \bar{y} \sum x_i = \beta_1 \left( \sum x_i^2 - \bar{x} \sum x_i \right)


🔹 Final Simplification

Using:

xˉ=xin\bar{x} = \frac{\sum x_i}{n}

Identity 1

xiyi1n(xi)(yi)=(xixˉ)(yiyˉ)\sum x_i y_i - \frac{1}{n}(\sum x_i)(\sum y_i) = \sum (x_i - \bar{x})(y_i - \bar{y})

 Identity 2

xi21n(xi)2=(xixˉ)2\sum x_i^2 - \frac{1}{n}(\sum x_i)^2 = \sum (x_i - \bar{x})^2

We get:

β1=(xixˉ)(yiyˉ)(xixˉ)2\beta_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})} {\sum (x_i - \bar{x})^2}


From optimization, we get:

β1=(xixˉ)(yiyˉ)(xixˉ)2\beta_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})} {\sum (x_i - \bar{x})^2} β0=yˉβ1xˉ\beta_0 = \bar{y} - \beta_1 \bar{x}



Key Insight

β1=Cov(x,y)Var(x)\beta_1 = \frac{\text{Cov}(x,y)}{\text{Var}(x)}
Alternate Formula (Using Raw Sums)
β1=nxy(x)(y)nx2(x)2\beta_1 = \frac{n\sum xy - (\sum x)(\sum y)} {n\sum x^2 - (\sum x)^2} β0=yβ1xn\beta_0 = \frac{\sum y - \beta_1 \sum x}{n}

Alternate Formulas for Simple Linear Regression - Summary


🔹 1. Using Mean Values (Most Important)

Slope β1\beta_1

β1=(xixˉ)(yiyˉ)(xixˉ)2\beta_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})} {\sum (x_i - \bar{x})^2}

Intercept β0\beta_0

β0=yˉβ1xˉ\beta_0 = \bar{y} - \beta_1 \bar{x}

🔹 2. Equivalent Statistical Form

This is a very elegant form:

β1=Cov(x,y)Var(x)\beta_1 = \frac{\text{Cov}(x,y)}{\text{Var}(x)} β0=yˉβ1xˉ\beta_0 = \bar{y} - \beta_1 \bar{x}

🔹 3. Alternate Formula (Using Raw Sums)


β1=nxy(x)(y)nx2(x)2\beta_1 = \frac{n\sum xy - (\sum x)(\sum y)} {n\sum x^2 - (\sum x)^2} β0=yβ1xn\beta_0 = \frac{\sum y - \beta_1 \sum x}{n}

🔹 When to Use Which?

MethodWhen to Use
Mean form    Easier for understanding
Raw sum form    Faster in exams
Covariance form    Conceptual questions


🔹 Intuition 

👉 Simple linear regression tries to:

  • Fit a line through data points
  • Capture trend between variables

Assumptions of Simple Linear Regression

  1. Linearity
    • Relationship between xx and yy is linear
  2. Independence
    • Observations are independent
  3. Constant variance (Homoscedasticity)
    • Error variance is constant
  4. Normality of errors
    • Errors are normally distributed

🔹Example

📌 Salary Prediction

  • Input: Years of experience (xx)
  • Output: Salary (yy)

Model:

Salary=β0+β1(Experience)\text{Salary} = \beta_0 + \beta_1 (\text{Experience})

👉 Used widely in real-world ML tasks


🔹 Steps in Applying SLR

  1. Collect data
  2. Plot data (scatter plot)
  3. Choose linear model
  4. Define loss function
  5. Estimate parameters
  6. Evaluate model

🔹 Advantages

  • Simple and interpretable
  • Fast computation
  • Works well for linear relationships

🔹Limitations

  • Cannot model non-linear patterns
  • Sensitive to outliers
  • Assumes linearity


📝 Summary 

  • Model:
y=β0+β1xy = \beta_0 + \beta_1 x
  • Goal:
min(yiy^i)2\min \sum (y_i - \hat{y}_i)^2
  • Output: Continuous value

Comments

Popular posts from this blog

Machine Learning PCCST503 Semester5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Machine Learning (ML)

Distinguishing Machine Learning from Traditional Programming