Showing posts with label RSS. Show all posts
Showing posts with label RSS. Show all posts

Tuesday, August 27, 2024

Breaking Down RSS, TSS, and ESS for Better Regression Understanding

Understanding TSS, RSS, ESS and R²

Understanding RSS, TSS, ESS & R² in Regression

In regression analysis, we measure how well a model explains variation in data using three core quantities: Total Sum of Squares (TSS), Residual Sum of Squares (RSS), and Explained Sum of Squares (ESS).


🎯 Learning Goal

Understand how total variation in data is decomposed into explained and unexplained parts.

💡 Core Identity: TSS = ESS + RSS

📘 Key Definitions

1️⃣ Total Sum of Squares (TSS)

Definition: Measures total variation in y around its mean.

TSS = Σ (y_i - y_mean)^2
  • y_i → actual values
  • y_mean → mean of y
💡 TSS represents total variability before modeling.

2️⃣ Residual Sum of Squares (RSS)

Definition: Measures unexplained variation (model error).

RSS = Σ (y_i - y_hat_i)^2
  • y_hat_i → predicted values
💡 RSS measures how wrong the model is.

3️⃣ Explained Sum of Squares (ESS)

Definition: Measures variation explained by the model.

ESS = Σ (y_hat_i - y_mean)^2
💡 ESS measures how much the model explains.

🔗 The Fundamental Relationship

TSS = ESS + RSS

The total variability in y is split into:

  • Explained part (ESS)
  • Unexplained part (RSS)
💡 Regression decomposes total variation into explained and residual components.

📊 Visual Interpretation (Conceptual)

Think of It Geometrically

TSS → Distance from actual points to the mean ESS → Distance from predictions to the mean RSS → Distance from actual points to predictions

Graphically:

  • Mean line → baseline model
  • Regression line → improved model
  • Vertical gaps → residuals


📈 Coefficient of Determination (R²)

R^2 = ESS / TSS
R^2 = 1 - (RSS / TSS)

Interpretation

  • R² = 1 → Perfect fit
  • R² = 0 → No improvement over mean
💡 R² measures the proportion of total variance explained by the model.

🧪 Step-by-Step Example Logic

How You Compute in Practice
  1. Compute y_mean
  2. Calculate TSS using actual values
  3. Fit regression → obtain y_hat
  4. Calculate RSS
  5. Compute ESS = TSS − RSS
  6. Compute R²

📌 Final Summary

  • TSS → Total variability
  • ESS → Explained variability
  • RSS → Unexplained variability
💡 A good regression model minimizes RSS and maximizes ESS.

End of Interactive Learning Guide

What Happens If a Linear Regression Model Doesn't Converge to Zero?

If the derivatives (or gradients) of the cost function do not converge to zero during the optimization process, several issues might arise, leading to suboptimal or incorrect solutions in a linear regression model. Here's what could happen if we don't achieve convergence to zero:

### **1. Suboptimal Solution**
- **Incomplete Minimization**: If the gradient (the vector of partial derivatives) does not converge to zero, it means that the algorithm has not found the true minimum of the cost function (e.g., Residual Sum of Squares, RSS). The coefficients \( \beta_0 \) and \( \beta_1 \) may not be at their optimal values, resulting in a model that does not fit the data as well as it could.
  
- **Higher RSS**: Since the model parameters have not been optimized, the Residual Sum of Squares (RSS) will likely be higher than necessary. This means the predictions will be less accurate, leading to larger errors.

### **2. Gradient Descent Issues**
- **Learning Rate Too High**: If you're using an iterative optimization method like gradient descent, and the learning rate is too high, the algorithm might "overshoot" the minimum. This can cause the gradient to oscillate or even diverge rather than converge to zero.

- **Learning Rate Too Low**: Conversely, if the learning rate is too low, the algorithm might converge very slowly or get stuck in a region where the gradient is small but not zero, leading to premature stopping before reaching the true minimum.

- **Stuck in a Plateau or Local Minimum**: In some cases, the algorithm might get stuck in a plateau where the gradient is close to zero, but it's not the global minimum. This can happen in more complex models or when the cost function has a complicated shape.

### **3. Non-Linearity in Data**
- **Model Misspecification**: If the underlying relationship between the independent and dependent variables is not linear, the linear regression model may never truly minimize the cost function, because the model is inherently incapable of capturing the true relationship. In such cases, the residuals might not decrease sufficiently, and the gradients might not converge to zero.

### **4. Numerical Issues**
- **Precision Errors**: In some cases, especially when dealing with very large or very small numbers, numerical precision errors might prevent the gradient from reaching exactly zero. Instead, it might fluctuate around a small value close to zero but not exactly zero.

### **5. Regularization Terms**
- **Regularization**: If you're using regularization (e.g., Ridge or Lasso regression), the cost function includes additional penalty terms (like \( \lambda \beta_1^2 \) for Ridge). The presence of these terms means the minimum might not correspond to a gradient of exactly zero because the cost function is more complex.

### **Consequences**
- **Poor Model Performance**: Ultimately, if the optimization does not converge properly, the model may have poor predictive performance on both training and unseen data.
  
- **Unstable Solutions**: In cases where the gradient doesn't converge due to issues like a high learning rate, the solution might be unstable, with the algorithm potentially oscillating around the minimum rather than settling down.

### **Conclusion**
Achieving convergence (where the gradient is zero or close enough to zero) is crucial in ensuring that the model parameters are optimized. This ensures that the model provides the best possible fit to the data, minimizing prediction errors. If convergence is not achieved, steps should be taken to diagnose the issue—whether it's adjusting the learning rate, re-evaluating the model's assumptions, or checking for numerical stability. 

How Derivatives Help Optimize Linear Regression Models

Linear Regression — Complete Deep Learning Guide

📘 Linear Regression — Full Concept + Math + Intuition

📑 Table of Contents

📌 What is Linear Regression?

Linear Regression is a statistical and machine learning technique used to model the relationship between variables.

It tries to answer a simple question: "Can we predict output (y) using input (x)?"

The model assumes a linear relationship:

ŷ = β0 + β1x
  • β0 → Intercept (value when x = 0)
  • β1 → Slope (how much y changes when x changes)

❓ Why Do We Need Linear Regression?

In real life, relationships exist everywhere:

  • Hours studied → Marks scored
  • Ad spend → Sales
  • Experience → Salary

Linear regression helps us quantify and predict these relationships.

🧠 Deep Intuition

Click to expand

Imagine plotting points on a graph. There are infinite lines you could draw.

But we want the "best" line.

Best means:

  • Closest to all points
  • Minimum total error

Instead of guessing, we use math to find this optimal line.

📊 Dataset

xy
12
23

📉 Residual Sum of Squares (RSS)

Residual = Actual - Predicted

RSS measures total squared error.

RSS = (2 - (β0 + β1*1))^2 + (3 - (β0 + β1*2))^2

Why square?

  • Avoid negative cancellation
  • Penalize large errors more

📐 Full Step-by-Step Derivation (Deep Explanation)

Expand Full Math with Explanation

Step 1: Start with RSS

RSS = (2 - β0 - β1)^2 + (3 - β0 - 2β1)^2

Step 2: Expand each term

(2 - β0 - β1)^2 = (2 - β0 - β1)(2 - β0 - β1)
= 4 - 4β0 - 4β1 + β0^2 + 2β0β1 + β1^2

(3 - β0 - 2β1)^2 = (3 - β0 - 2β1)(3 - β0 - 2β1)
= 9 - 6β0 - 12β1 + β0^2 + 4β0β1 + 4β1^2

Step 3: Add both expressions

RSS = (4 + 9)
      + (β0^2 + β0^2)
      + (β1^2 + 4β1^2)
      + (2β0β1 + 4β0β1)
      + (-4β0 - 6β0)
      + (-4β1 - 12β1)

RSS = 13 + 2β0^2 + 5β1^2 + 6β0β1 -10β0 -16β1

Step 4: Take derivative w.r.t β0

d(RSS)/dβ0 = d/dβ0 (2β0^2 + 6β0β1 -10β0)
= 4β0 + 6β1 -10

Step 5: Take derivative w.r.t β1

d(RSS)/dβ1 = d/dβ1 (5β1^2 + 6β0β1 -16β1)
= 10β1 + 6β0 -16

Step 6: Set derivatives to zero

4β0 + 6β1 = 10
6β0 + 10β1 = 16

Step 7: Solve using elimination

Multiply first equation by 3:
12β0 + 18β1 = 30

Multiply second equation by 2:
12β0 + 20β1 = 32

Subtract:
(12β0 + 20β1) - (12β0 + 18β1) = 32 - 30
2β1 = 2
β1 = 1

Substitute into first equation:
4β0 + 6(1) = 10
4β0 + 6 = 10
4β0 = 4
β0 = 1

Final Result:

β0 = 1
β1 = 1

ŷ = x + 1

🧮 Solving Equations

Set derivatives = 0 to find minimum:

4β0 + 6β1 = 10
6β0 + 10β1 = 16

Solving gives:

β0 = 1
β1 = 1

Final Model:

ŷ = x + 1

💻 Code Example

import numpy as np
from sklearn.linear_model import LinearRegression

X = np.array([1,2]).reshape(-1,1)
y = np.array([2,3])

model = LinearRegression()
model.fit(X,y)

print(model.intercept_)
print(model.coef_)

🖥 CLI Output

1.0
[1.0]

💡 Key Takeaways

  • Linear regression models relationships
  • RSS measures error
  • Derivatives minimize error
  • Gives best-fit line mathematically

🔗 Related Articles

Residuals and RSS in Linear Regression

Understanding Residuals and RSS in Linear Regression

📊 Understanding Residuals and RSS in Linear Regression

📖 Introduction

Linear regression helps us understand relationships between variables. But how do we measure how good our predictions are?

That’s where residuals and RSS (Residual Sum of Squares) come in.

💡 Residual = Actual Value − Predicted Value

📊 Dataset

Hours Studied (x)Actual Score (y)
250
460
665
880

We want to predict how study hours affect scores.

📈 Linear Regression Model

Our model:

ŷ = 5x + 40

This means: - For every extra hour studied, score increases by 5 - Base score starts at 40

🔽 Expand: Why linear model?

Linear regression assumes a straight-line relationship between variables. It is simple, interpretable, and often effective for small datasets.

✅ Step 1: Calculate Predictions

ŷ₁ = 5(2) + 40 = 50
ŷ₂ = 5(4) + 40 = 60
ŷ₃ = 5(6) + 40 = 70
ŷ₄ = 5(8) + 40 = 80

We now have predicted values for each data point.

📉 Step 2: Calculate Residuals

Residual₁ = 50 - 50 = 0
Residual₂ = 60 - 60 = 0
Residual₃ = 65 - 70 = -5
Residual₄ = 80 - 80 = 0

Residuals tell us how far off each prediction is.

🔽 Expand: Why negative residual?

A negative residual means the model overestimated the value.

🔢 Step 3: Square the Residuals

0² = 0
0² = 0
(-5)² = 25
0² = 0

Squaring removes negative signs and penalizes larger errors.

📌 Step 4: Calculate RSS

RSS = 0 + 0 + 25 + 0 = 25
🎯 RSS measures total prediction error. Lower = better fit.

📊 Mathematical Insight

The RSS formula is:

RSS = Σ (y - ŷ)²

This sums all squared differences between actual and predicted values.

📐 Mathematical Explanation of Residuals and RSS

In linear regression, we quantify error using residuals and RSS.

Residual Definition

The residual for each data point is:

\[ e_i = y_i - \hat{y}_i \]

Where:

  • \( y_i \): actual value
  • \( \hat{y}_i \): predicted value
  • \( e_i \): residual (error)

Residual Sum of Squares (RSS)

The total error across all observations is:

\[ RSS = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 \]

Applying to Our Example

\[ RSS = (50 - 50)^2 + (60 - 60)^2 + (65 - 70)^2 + (80 - 80)^2 \]

\[ RSS = 0 + 0 + 25 + 0 = 25 \]

Why Squaring?

  • Prevents positive and negative errors from canceling out
  • Penalizes larger errors more strongly
  • Makes optimization mathematically convenient
💡 The goal of regression is to minimize RSS, leading to the best-fitting line.

💻 CLI Implementation Example

Code Example

x = [2,4,6,8]
y = [50,60,65,80]

def predict(x):
    return 5*x + 40

rss = 0

for i in range(len(x)):
    y_hat = predict(x[i])
    residual = y[i] - y_hat
    rss += residual**2

print("RSS:", rss)

CLI Output

$ python regression.py
RSS: 25
🔽 Expand CLI Explanation

The script loops through each data point, computes residuals, squares them, and sums them.

🎯 Key Takeaways

  • Residuals measure prediction error
  • Negative residual = overestimation
  • Squaring ensures all errors are positive
  • RSS summarizes total model error
  • Lower RSS = better model performance

📘 Final Thoughts

Residuals and RSS form the foundation of machine learning evaluation. Understanding them deeply will help you build better predictive models.

Featured Post

How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing

The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...

Popular Posts