SECTION 1
Historical Context & Motivation
Imagine you have a bunch of data points scattered across a graph. Maybe they show how many hours students studied and what scores they earned on a test. You can see a general trend — more studying tends to mean higher scores — but the points don't fall on a perfectly straight line. How do you draw the best possible line through those messy points? That's the question that least squares regression answers, and it's a question that mathematicians have worked on for over two hundred years.
1795 • Gauss's Early Work
1805 • Legendre Publishes First
1809 • Gauss Publishes His Derivation
1829 • Gauss and Normal Equations
1900s–Today • The Digital Age
The central question is simple but powerful: given a collection of data points that don't perfectly line up, how do we find the single straight line that comes closest to all of them at once? Least squares gives us a clear, mathematical answer — and it connects beautifully to ideas from linear algebra about projections and orthogonality.
SECTION 2
Core Principles & Definitions
Before we dive into formulas, let's nail down the key ideas. Linear regression means fitting a straight line to data. The word "regression" just means we're trying to describe the relationship between two quantities. The "least squares" part tells us how we choose the best line: we pick the one that makes the sum of the squared differences between each data point and the line as small as possible.
Residual (Error)
Sum of Squared Errors
Best-Fit Line
Projection & Orthogonality
KEY TAKEAWAY
SECTION 3
Visual Explanation
The diagram below shows five data points on a coordinate plane and the best-fit line running through them. The dashed vertical segments represent the residuals — the errors we want to shrink. Notice how some points sit above the line (positive residual) and some sit below (negative residual). The least squares method finds the specific line that makes the total of all those squared residuals as small as possible.
Why do we square the residuals instead of just adding them up? If we simply added the positive and negative residuals together, they would cancel each other out, and a terrible line could look like it has zero total error. Squaring makes every residual positive, and it also punishes large errors much more than small ones. A residual of 10 contributes 100 to the total, while a residual of 2 only contributes 4. This keeps the line honest — it can't just ignore faraway points.
SECTION 4
Mathematical Framework
Now let's see the actual formulas. Suppose we have n data points: (x₁, y₁), (x₂, y₂), …, (xₙ, yₙ). We want to find a line ŷ = mx + b that best fits the data. Here, m is the slope and b is the y-intercept.
SUM OF SQUARED ERRORS
yᵢ is the actual data value, m·xᵢ + b is the predicted value on the line, and Σ means we add this up for every data point i = 1, 2, …, n.
We can rewrite this problem using matrices. Think of it as a system of equations — one equation per data point — that we're trying to solve. In matrix form, the system looks like Ax = b, where A is the design matrix, x holds the unknowns (m and b), and b holds the y-values.
MATRIX FORM
Each row of A has a 1 (for the intercept) and the x-value for that data point. The vector x̂ contains the two unknowns we want to find: b and m.
Usually there are more data points than unknowns, so the system is "overdetermined" — there is no exact solution. Instead, we find the closest approximate solution by projecting the vector b onto the column space of A. The magic formula that does this is called the normal equation.
NORMAL EQUATION
Aᵀ means the transpose of A (swap rows and columns). Multiplying both sides by Aᵀ turns the unsolvable system into one that always has a solution. The solution x̂ gives us the slope m and intercept b of the best-fit line.
SLOPE FORMULA (DIRECT)
This formula comes from solving the normal equation. n is the number of data points. Once you know m, you can find b = ȳ − m·x̄, where x̄ and ȳ are the averages of the x and y values.
SECTION 5
The Geometry of Projection
Here's the beautiful geometric idea hiding inside the math. The vector of actual y-values (b) lives in a high-dimensional space. The set of all possible predictions from a line (Ax̂) forms a flat subspace called the column space of A. The least squares solution projects b onto this column space — it finds the point in the subspace that is closest to b. The leftover error (the residual vector) is perpendicular (orthogonal) to the column space.
This is like shining a flashlight straight down onto a tilted table. The shadow of a ball (vector b) on the table is the projection (Ax̂). The string connecting the ball to its shadow hangs straight down — perpendicular to the table. In our problem, "straight down" means the error vector e is orthogonal to every column of A. That perpendicularity condition is exactly what the normal equation AᵀA·x̂ = Aᵀb encodes.
Why Orthogonality Matters
SECTION 6
Worked Example
Let's work through a complete example. Suppose a teacher records how many hours five students studied and the scores they earned:
| Student | Hours (x) | Score (y) |
|---|---|---|
| A | 1 | 52 |
| B | 2 | 58 |
| C | 3 | 65 |
| D | 4 | 68 |
| E | 5 | 72 |
Study hours and test scores for five students
Finding the Best-Fit Line: ŷ = mx + b
- 1
Step 1 — Compute the Needed Sums
We need five sums. With n = 5 data points: Σx = 1 + 2 + 3 + 4 + 5 = 15, Σy = 52 + 58 + 65 + 68 + 72 = 315, Σx² = 1 + 4 + 9 + 16 + 25 = 55, Σxy = 52 + 116 + 195 + 272 + 360 = 995.n = 5, Σx = 15, Σy = 315, Σx² = 55, Σxy = 995 - 2
Step 2 — Calculate the Slope (m)
Use the slope formula: m = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²). Plug in: m = (5 × 995 − 15 × 315) / (5 × 55 − 15²) = (4975 − 4725) / (275 − 225) = 250 / 50.m = 5 - 3
Step 3 — Calculate the Y-Intercept (b)
First find the means: x̄ = 15/5 = 3 and ȳ = 315/5 = 63. Then b = ȳ − m·x̄ = 63 − 5 × 3 = 63 − 15.b = 48 - 4
Step 4 — Write the Best-Fit Line
Our least squares regression line is ŷ = 5x + 48. This means that for every additional hour studied, the model predicts an increase of 5 points on the test.ŷ = 5x + 48 - 5
Step 5 — Verify with a Prediction
If a student studies for 3 hours, the model predicts ŷ = 5(3) + 48 = 63. The actual score for Student C (who studied 3 hours) was 65, so the residual is 65 − 63 = 2. That's pretty close!Predicted: 63, Actual: 65, Residual: 2
SECTION 7
Strengths & Limitations
Least squares regression is incredibly useful, but like any tool, it works best in certain situations. Understanding when it shines and when it struggles will make you a smarter data analyst.
| Strengths | Limitations |
|---|---|
| Easy to compute — only basic arithmetic and algebra are needed, and computers can handle millions of data points instantly. | Assumes a linear (straight-line) relationship. If the true pattern is curved, the line will be a poor fit. |
| Always gives a unique answer (as long as the data isn't degenerate). There's exactly one best-fit line. | Very sensitive to outliers. A single extreme data point can tilt the entire line because squaring magnifies large errors. |
| Strong theoretical foundation in linear algebra and statistics. It's the best unbiased estimator under certain assumptions (Gauss–Markov theorem). | Only measures vertical distances (residuals). It doesn't account for uncertainty in the x-values. |
| Extends naturally to multiple variables (multiple regression) and to polynomial curves. | Can be misleading if you don't check whether a linear model is appropriate in the first place (always plot your data first!). |
Comparing the strengths and limitations of least squares regression
KEY TAKEAWAY
SECTION 8
Connection to Advanced Theory
The least squares idea you've learned here is just the beginning. As you study more math, you'll discover that it connects to many powerful ideas. Here's a quick preview of where these concepts lead.
| What You Learned Here | Where It Leads |
|---|---|
| Fitting a straight line (ŷ = mx + b) | Multiple regression: fitting planes or hyperplanes with many input variables (x₁, x₂, x₃, …) |
| The normal equation AᵀA·x̂ = Aᵀb | QR decomposition: a more numerically stable way to solve least squares using orthogonal matrices |
| Projecting b onto the column space of A | Orthogonal projections in inner product spaces: generalizing to infinite-dimensional function spaces (Fourier series, wavelets) |
| Minimizing the sum of squared errors | Regularization: adding penalty terms (Ridge regression, Lasso) to prevent overfitting in machine learning |
From basic least squares to advanced topics
The key geometric insight — that the best approximation comes from an orthogonal projection — is one of the most universal ideas in all of mathematics. It shows up in signal processing (filtering noise from audio), in quantum mechanics (measuring quantum states), and in statistics (computing correlations). Mastering it here gives you a head start on all of those fields.
SECTION 9
Practice Problems
Question 1: CONCEPTUAL
Show answer
Question 2: BASIC CALCULATION
Show answer
Question 3: INTERMEDIATE
Show answer
Question 4: APPLIED
Show answer
Question 5: CRITICAL THINKING
Show answer
SUMMARY
Lesson Summary
Summary
Linear regression via least squares finds the straight line that best fits a set of data points by minimizing the sum of squared residuals. Each residual is the vertical distance between a data point and the line, and squaring them ensures large errors are penalized heavily. The slope m and y-intercept b can be computed directly from the data using the slope formula or the normal equation AᵀA·x̂ = Aᵀb.
From a linear algebra perspective, the least squares solution is an orthogonal projection of the data vector onto the column space of the design matrix A. The error vector is perpendicular (orthogonal) to this column space, which guarantees that no other line can produce a smaller total error. This elegant geometric idea extends to multiple regression, Fourier analysis, and machine learning — making least squares one of the most foundational tools in all of applied mathematics.