Simple Linear Regression using Gradient Descent

 

Simple Linear Regression using Gradient Descent


🔹 1. Model (Hypothesis)

In simple linear regression:

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

Where:

  • β0\beta_0 → intercept
  • β1\beta_1 → slope

🔹 2. Cost Function (MSE)

We minimize the Mean Squared Error (MSE):

J(β0,β1)=12ni=1n(yiy^i)2J(\beta_0, \beta_1) = \frac{1}{2n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2

👉 The factor 12\frac{1}{2} simplifies derivatives.


🔹 3. Goal

👉 Find β0\beta_0 and β1\beta_1 that minimize JJ


🔹 4. Compute Gradients (Very Important)

We take partial derivatives:


🔸 Derivative w.r.t β0\beta_0

Jβ0=1n(yiy^i)\frac{\partial J}{\partial \beta_0} = -\frac{1}{n} \sum (y_i - \hat{y}_i)

🔸 Derivative w.r.t β1\beta_1

Jβ1=1nxi(yiy^i)\frac{\partial J}{\partial \beta_1} = -\frac{1}{n} \sum x_i (y_i - \hat{y}_i)

🔹 5. Update Rules

Using Gradient Descent:


🔸 Update β0\beta_0

β0=β0α(1n(yiy^i))\beta_0 = \beta_0 - \alpha \left(-\frac{1}{n} \sum (y_i - \hat{y}_i)\right)
=β0+αn(yiy^i)= \beta_0 + \frac{\alpha}{n} \sum (y_i - \hat{y}_i)

🔸 Update β1\beta_1

β1=β1α(1nxi(yiy^i))\beta_1 = \beta_1 - \alpha \left(-\frac{1}{n} \sum x_i (y_i - \hat{y}_i)\right)
=β1+αnxi(yiy^i)= \beta_1 + \frac{\alpha}{n} \sum x_i (y_i - \hat{y}_i)

🔹 6. Algorithm Steps

📌 Gradient Descent Procedure

  1. Initialize β0,β1\beta_0, \beta_1 (0 or small random values)
  2. Compute predictions:

    y^i=β0+β1xi\hat{y}_i = \beta_0 + \beta_1 x_i
  3. Compute error:

    ei=yiy^ie_i = y_i - \hat{y}_i
  4. Compute gradients
  5. Update parameters
  6. Repeat until convergence

🔹 7. Intuition

  • If prediction is too small → increase parameters
  • If prediction is too large → decrease parameters

👉 Gradually moves toward best-fit line


🔹 8. Why Use Gradient Descent?

  • Works for large datasets
  • Avoids matrix inversion
  • Scales well

🔹 9. Convergence

Stops when:

  • Cost function stops decreasing
  • Maximum iterations reached

🔹 10. Important Notes

  • Learning rate α\alpha is critical
  • Feature scaling improves performance
  • Works even when closed-form solution is expensive

🔹 11. Summary

StepDescription
Model                    y^=β0+β1x\hat{y} = \beta_0 + \beta_1 x
CostMSE
MethodIterative optimization
UpdateMove opposite gradient

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