⓪ The Problem: A Straight Line Through Scattered Points
Suppose you ran 5 experiments, each time recording input x and output y, obtaining these 5 data points:
You want to find a straight line y = ax + b, to make it "best" pass through these points. But what does "best" mean? Pass through which points? Stay as close as possible to all of them?
The line we seek is ŷ = ax + b (read "y-hat"), a is theSlope, b is theIntercept.
For each xᵢ, the predicted value from the line is ŷᵢ = a·xᵢ + b.
① Exploration: The Two-Point World
Let's start with the simplest case:only two data points.
Passes exactly through both points
Two points on a plane determine a unique line — this is middle-school geometry. The solution to the system of equations isexact, with zero residual.
In mathematics this is called a "well-posed problem" (number of equations = number of unknowns). But real data rarely has only two points.
② The Third Point Arrives — Overdetermined!
Now add a third point (3, 3), along with the first two points (1, 2), (2, 3.5), all together:
Look:no single line can pass through all three points simultaneously. The number of equations (3) exceeds the number of unknowns (2) — this is called an "overdetermined system" — usually unsolvable.
③ Discovery 1: Error Is the "Vertical Distance"
For any candidate line ŷ = ax + b, define theresidual (residual):
The residual is the length of thevertical dashed lines — thevertical distance (not the perpendicular distance! Because x is a precise input, error is only in the y-direction).
④ Discovery 2: Why Square?
Now combine the residuals into a single number representing "total error." Here are 4 natural candidates:
| Method | Expression | Problem |
|---|---|---|
| ① Direct sum | Σ eᵢ | Signs cancel out: +3 and −3 sum to 0, appearing "perfect" |
| ② Sum of absolute values | Σ |eᵢ| | Not everywhere differentiable, mathematically awkward |
| ③ ③ Sum of squares | Σ eᵢ² | Solves all the above problems |
| ④ Sum of fourth powers | Σ eᵢ⁴ | Overly sensitive to outliers, plus heavier computation |
① Signs can't cancel: after squaring, all errors become positive, and large errors are amplified and penalized.
② Differentiable everywhere: quadratic functions are smooth — we can confidently use calculus to find extrema.
③ Mathematically optimal: if errors follow a normal distribution, minimizing the sum of squares is equivalent to maximum likelihood estimation — proved by Gauss in 1809.
⑤ Discovery 3: Translating Intuition into Math
SSE(a, b) is abivariate quadratic function. Our task is:
How do you find the minimum of a bivariate quadratic function? Calculus tells us:Partial derivatives = 0.
SSE(a,b) is an upward-opening paraboloid; the minimum is at the bottom of the valley.
⑥ Derivation: Partial Derivatives → Normal Equations
Expand SSE, then take partial derivatives with respect to a and b:
⇒ a·Σxᵢ + n·b = Σyᵢ
⇒ a·Σxᵢ² + b·Σxᵢ = Σxᵢyᵢ
a·Σxᵢ + b·n = Σyᵢ
Solve this system of two linear equations to get the classic formulas:
b = ȳ − a·x̄
⑦ Compute It by Hand: 5 Data Points
Return to the 5 points from the beginning: (1, 2), (2, 3), (3, 5), (4, 4), (5, 6). Plug them into the formulas step by step:
| i | xᵢ | yᵢ | xᵢ² | xᵢyᵢ |
|---|---|---|---|---|
| 1 | 1 | 2 | 1 | 2 |
| 2 | 2 | 3 | 4 | 6 |
| 3 | 3 | 5 | 9 | 15 |
| 4 | 4 | 4 | 16 | 16 |
| 5 | 5 | 6 | 25 | 30 |
| Σ | 15 | 20 | 55 | 69 |
= (345 − 300) / (275 − 225)
= 45 / 50 = 0.9
= 4 − 0.9×3 = 4 − 2.7 = 1.3
| i | yᵢ | ŷᵢ=0.9xᵢ+1.3 | eᵢ | eᵢ² |
|---|---|---|---|---|
| 1 | 2 | 2.2 | −0.2 | 0.04 |
| 2 | 3 | 3.1 | −0.1 | 0.01 |
| 3 | 5 | 4.0 | +1.0 | 1.00 |
| 4 | 4 | 4.9 | −0.9 | 0.81 |
| 5 | 6 | 5.8 | +0.2 | 0.04 |
| SSE = Σeᵢ² = | 1.90 | |||
No other line can produce a smaller sum of squared errors than 1.90.
(Don't believe it? Go to §⑩ Interactive Lab and drag the sliders — deviate from 0.9 or 1.3, and SSE will definitely increase.)
Part 2 Deep Dive & Generalization
From two-variable linear equations to matrices, from computation to geometry, from one line to all of machine learning.
⑧ Generalization: Matrix Form
Write the n equations yᵢ ≈ a·xᵢ + b in matrix form:
The objective becomes:Minimize ‖y − Xβ‖²(squared vector length = sum of squared components = SSE).
⑨ Geometric Insight: Projection onto Column Space
The matrix form reveals an elegant geometric interpretation that is deeper than the calculus derivation:
The residual vector e = y − ŷ isperpendicular (orthogonal) to the column space — this is the geometric essence of "minimum": the hypotenuse of a right triangle is the shortest path.
Orthogonality implies:Xᵀe = 0 ⇒ Xᵀ(y − Xβ̂) = 0 ⇒ XᵀXβ̂ = Xᵀy.
See — the Normal Equations didn't "happen" to come from calculus; they are a direct translation of the geometric fact that "the residual is orthogonal to the column space."
⑩ Interactive Lab: Feel Least Squares Yourself
Drag the sliders to change slope a and intercept b, and watch how SSE changes. Click "Optimal Solution" to return to the least-squares solution.
⑪ Cross-Domain Easter Eggs: Least Squares Is Everywhere
Least squares isn't just for statistics — it's the "universal language" of data science. With the same XᵀXβ = Xᵀy, you can:
ŷ = a₀ + a₁x + a₂x² + …
Just add columns to the design matrix
Spring: F = k·x
Measure multiple (x,F) pairs to fit k
Least squares = supervised learning with MSE loss
Every "optimal approximate solution to an overdetermined system" is fundamentally least squares
⑫ Complete Map: You Derived Least Squares From Scratch
Look back at the question mark from Section ⓪ — that "?" has now become precise numbers:ŷ = 0.9x + 1.3, SSE = 1.90.
- Start from a scatter plot and write SSE(a, b) = Σ(yᵢ − axᵢ − b)²
- Take partial derivatives w.r.t. a and b to derive the Normal Equations
- Compute the optimal slope and intercept from concrete data
- Understand the origin of the matrix form β̂ = (XᵀX)⁻¹Xᵀy
- Explain in geometric terms: ŷ is the orthogonal projection of y onto the column space
- Recognize that every "optimal approximation to an overdetermined system" = least squares