The Math Behind Linear Regression Explained Simply

CalcAiHub TeamJanuary 20, 20258 min read

Linear Regression is one of the most fundamental algorithms in statistics and machine learning. It looks simple on the surface — just fitting a straight line to data — but understanding the math behind it makes everything else (from AI models to Excel trendlines) much easier to grasp.

What is Linear Regression?

Linear Regression is a method used to find the relationship between two variables by fitting a straight line through data points. This line lets us predict an unknown value (Y) based on a known value (X).

The equation of this line is:

Y = mX + c

    Where:

  • Y = Dependent variable (the value we want to predict)
  • X = Independent variable (the value we already know)
  • m = Slope of the line (rate of change)
  • c = Y-intercept (value of Y when X = 0)

The Core Formulas

To find the best-fitting line, we need to calculate m (slope) and c (intercept) using the following formulas:

### 1. Slope (m)

$$ m = \frac{n(\sum XY) - (\sum X)(\sum Y)}{n(\sum X^2) - (\sum X)^2} $$

### 2. Intercept (c)

$$ c = \frac{\sum Y - m(\sum X)}{n} $$

Where n is the total number of data points.

Step-by-Step Numeric Example

Let's calculate this manually using a small dataset:

| X | Y |

|---|---|

| 1 | 2 |

| 2 | 4 |

| 3 | 5 |

| 4 | 4 |

| 5 | 5 |

Step 1: Find the required sums

  • n = 5
  • ΣX = 1+2+3+4+5 = 15
  • ΣY = 2+4+5+4+5 = 20
  • ΣXY = (1×2)+(2×4)+(3×5)+(4×4)+(5×5) = 2+8+15+16+25 = 66
  • ΣX² = 1²+2²+3²+4²+5² = 1+4+9+16+25 = 55

Step 2: Calculate slope (m)

m = (5×66 - 15×20) / (5×55 - 15²)

m = (330 - 300) / (275 - 225)

m = 30 / 50

m = 0.6

Step 3: Calculate intercept (c)

c = (20 - 0.6×15) / 5

c = (20 - 9) / 5

c = 11 / 5

c = 2.2

Step 4: Final Regression Equation

Y = 0.6X + 2.2

So, if X = 6, predicted Y = 0.6(6) + 2.2 = 3.6 + 2.2 = 5.8

Understanding Error (Residuals)

No line fits data perfectly. The difference between the actual value and the predicted value is called the residual (error):

Error = Actual Y − Predicted Y

To measure how good the overall fit is, we use Mean Squared Error (MSE):

$$ MSE = \frac{1}{n} \sum (Y_{actual} - Y_{predicted})^2 $$

A lower MSE means the line fits the data more accurately.

Why is This Math Important?

  • It's the foundation of predictive analytics and forecasting.
  • Machine learning models like linear regression in Python (scikit-learn) use the exact same formulas internally.
  • Understanding the manual calculation helps you interpret results instead of blindly trusting a tool.

Key Takeaways

  • Linear Regression fits a straight line: Y = mX + c.
  • Slope (m) and Intercept (c) are calculated using sum-based formulas.
  • Manual calculation involves finding ΣX, ΣY, ΣXY, and ΣX².
  • Error is measured using Mean Squared Error (MSE) to judge accuracy.
  • The same math powers everything from Excel trendlines to real-world ML models.

In summary, once you understand these formulas and calculate them by hand even once, Linear Regression stops feeling like a black box — it becomes simple algebra applied to real data.

Formula

Y = mX + c, where m = (nΣXY - ΣXΣY) / (nΣX² - (ΣX)²) and c = (ΣY - mΣX) / n