Introduction
Real data is noisy. Ten measurements never lie exactly on a line, and a system of ten equations in two unknowns usually has no exact solution. What should we do? Find the answer that is closest. That closest-fit idea rests on one picture from this course: dropping a perpendicular. The projection of one vector onto another is the nearest point on its line, and least squares is the same idea in higher dimensions. It is the engine of regression, the most used tool in data science.
How it works
Projection. To project b onto the direction a, find the multiple k·a that is closest to b. The closest point is the foot of the perpendicular, so the leftover b − k·a must be perpendicular to a: a·(b − k·a) = 0. Solving for k gives
k = (a·b) ÷ (a·a), and the projection is k·a.
For a = (3, 1) and b = (2, 4): a·b = 10 and a·a = 10, so k = 1 and the projection is (3, 1). The residual is (−1, 3), and its dot product with a is −3 + 3 = 0, which the example confirms.
Least squares for a line. Given points (xᵢ, yᵢ), we want y = m·x + c that makes the total of the squared errors, Σ(yᵢ − m·xᵢ − c)², smallest. Think of the list of y values as one vector. The set of all vectors m·x + c·1 is a plane, and the best fit is the projection of the y vector onto that plane. Setting the error perpendicular to both x and 1 gives formulas:
m = (n·Σxy − Σx·Σy) ÷ (n·Σx² − (Σx)²), c = (Σy − m·Σx) ÷ n
The example fits the points (1, 2), (2, 4), (3, 5), (4, 8) and finds y = 1.9x with errors 0.1, 0.2, −0.7, 0.4. They add up to 0, and the total squared error is 0.7.
Normal equations. For any model that is a sum of known columns, such as y = c₀ + c₁x + c₂x², stack the columns in a matrix A and the data in b. Perpendicular error means Aᵀ(b − A·x) = 0, so we solve AᵀA·x = Aᵀb, an ordinary square system solved by the elimination of the earlier lesson. The same recipe fits parabolas, planes and models with dozens of inputs.
Squaring the errors makes big misses count more and gives smooth formulas, but one wild outlier can pull the line far away.
Example
def dot(u, v):
return sum(a * b for a, b in zip(u, v))
a = [3, 1]
b = [2, 4]
k = dot(a, b) / dot(a, a)
p = [round(k * x, 4) for x in a]
residual = [y - k * x for x, y in zip(a, b)]
print("projection of b onto a:", p)
print("residual:", [round(r, 4) for r in residual])
print("residual · a =", round(dot(residual, a), 6) + 0.0)
xs = [1, 2, 3, 4]
ys = [2, 4, 5, 8]
n = len(xs)
sx, sy = sum(xs), sum(ys)
sxx = dot(xs, xs)
sxy = dot(xs, ys)
slope = (n * sxy - sx * sy) / (n * sxx - sx * sx)
intercept = (sy - slope * sx) / n
print("best line: y =", round(slope, 4), "x +", round(intercept, 4))
errors = [y - (slope * x + intercept) for x, y in zip(xs, ys)]
print("errors:", [round(e, 4) for e in errors])
print("sum of squared errors:", round(dot(errors, errors), 4))
print("errors add up to", round(sum(errors), 6) + 0.0)Line by line
dotis the dot product. We project b = (2, 4) onto a = (3, 1) with k = (a·b) ÷ (a·a).- The residual b − k·a has dot product 0 with a, which shows it is perpendicular.
- The second part fits a line to four points with the slope and intercept formulas.
- The errors add up to 0 and their squares sum to 0.7, the smallest possible for any straight line.
Expected result
projection of b onto a: [3.0, 1.0]
residual: [-1.0, 3.0]
residual · a = 0.0
best line: y = 1.9 x + 0.0
errors: [0.1, 0.2, -0.7, 0.4]
sum of squared errors: 0.7
errors add up to 0.0
In the real world
Spreadsheets draw a trend line with this very formula. Economists estimate the effect of one variable on another, doctors fit dose-response curves, and machine-learning models start from linear regression. Audio and graphics software project signals onto simple building blocks to compress them, and GPS receivers solve a least-squares problem to turn many noisy distances into one position.
Common mistakes
- Dividing by a·b instead of a·a when projecting.
- Using the formula with all x values equal. The denominator is then 0 and no slope exists.
- Reading a fit as a cause. A line can describe data without explaining it.
- Letting an outlier go unnoticed. One wild point can tilt the whole fit.
Best practices
- Plot or print the errors; they should add up to about 0 and show no pattern.
- Check that the residual is perpendicular to the model's columns.
- Use exact fractions for small hand-checkable examples and a library for large problems.
- Look for outliers before you trust a fit.
Summary
The projection of b onto a is (a·b ÷ a·a)·a, the closest point on the line, and the residual is perpendicular to a. Least squares uses the same idea: the best fit makes the error perpendicular to the model, which gives formulas for a line and the normal equations AᵀA·x = Aᵀb for any model of this kind.
Quick check
Key takeaways
- The projection of b onto a is (a·b ÷ a·a) × a, the closest point to b on the line through a.
- The leftover vector is perpendicular to a.
- Least squares makes the error vector perpendicular to the model's columns, giving the normal equations.
- For a line: slope = (n·Σxy − Σx·Σy) ÷ (n·Σx² − (Σx)²).
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.