SECTION 1
Historical Context & Motivation
Imagine you're trying to draw a straight line through a scatter of data points, but no single line passes through every point. This is the problem that least squares solutions were invented to solve. In the real world, systems of equations often have no exact solution because the data is noisy, measurements are imperfect, or there are more equations than unknowns. The least squares method finds the answer that comes as close as possible to satisfying all the equations at once.
1795 • Gauss's Early Work
1805 • Legendre Publishes First
1809 • Gauss Publishes His Version
1900s • Modern Linear Algebra Connection
The central question is this: when a system of equations Ax = b has no exact solution, how do we find the vector x̂ that makes Ax̂ as close to b as possible? The answer lies in the geometry of orthogonal projection.
SECTION 2
Core Principles & Definitions
Before diving into the math, let's build an understanding of the key ideas. A system Ax = b may have no solution because the vector b doesn't live inside the column space of A (the set of all possible outputs Ax). The least squares approach finds the point in the column space that is nearest to b.
Inconsistent Systems
Column Space
Orthogonal Projection
Residual Vector
Normal Equation
KEY TAKEAWAY
SECTION 3
Visual Explanation
The diagram below shows the geometry behind least squares. The column space of A is represented as a flat plane. The vector b floats above the plane because it is not in Col(A). The projection b̂ = Ax̂ is the closest point on the plane to b, and the residual r = b − Ax̂ is perpendicular to the plane.
Notice the key geometric fact: the residual r is perpendicular (orthogonal) to the column space. This perpendicularity is what makes the projection the closest point. If r were tilted at any other angle, the distance from b to the plane would be longer. This is exactly the same reason the shortest distance from a point to a line is the perpendicular distance — a fact you may remember from geometry class.
SECTION 4
Mathematical Framework
Now let's see how the geometry translates into equations. We want to minimize the distance between b and Ax, which means we want to minimize the length ‖b − Ax‖. Since the residual r = b − Ax̂ must be perpendicular to every column of A, we can write this perpendicularity condition using the transpose of A.
ORTHOGONALITY CONDITION
The residual (b − Ax̂) is orthogonal to every column of A, so Aᵀ times the residual equals the zero vector.
If we distribute the Aᵀ and rearrange, we get the famous normal equation:
NORMAL EQUATION
A = the m × n coefficient matrix, Aᵀ = transpose of A, x̂ = the least squares solution vector, b = the target vector. The product AᵀA is always an n × n matrix.
LEAST SQUARES ERROR
The least squares error is the length (norm) of the residual vector. This is the smallest possible distance between b and any vector in Col(A).
Why "Least Squares"?
SECTION 5
Least Squares and Line Fitting
One of the most common uses of least squares is fitting a line of best fit to data points. Suppose you have several data points (x₁, y₁), (x₂, y₂), …, (xₘ, yₘ) and you want to find the line y = c₀ + c₁x that passes as close to all of them as possible. You can set this up as the system Ax = b where each row of A comes from one data point.
To fit a line y = c₀ + c₁x to data points, we set up the matrix equation with A having a column of 1's and a column of x-values, x̂ = [c₀, c₁]ᵀ is the vector of unknowns (intercept and slope), and b is the vector of y-values. Since the data points usually don't all lie exactly on any line, we solve the normal equation AᵀA x̂ = Aᵀb to find the best-fit coefficients.
| Matrix / Vector | Definition | Size |
|---|---|---|
| A | Each row is [1, xᵢ] for each data point | m × 2 |
| x̂ | The unknowns [c₀, c₁]ᵀ (intercept and slope) | 2 × 1 |
| b | The y-values [y₁, y₂, …, yₘ]ᵀ | m × 1 |
| AᵀA | A symmetric 2×2 matrix that we can solve | 2 × 2 |
Setting up the line-fitting problem as Ax = b
SECTION 6
Worked Example
Let's work through a complete example. We want to find the least squares solution to the system Ax = b where:
GIVEN SYSTEM
This represents the three data points (1, 2), (2, 3), (3, 5). We want the best-fit line y = c₀ + c₁x.
Finding the Least Squares Line Through Three Points
- 1
Step 1 — Compute Aᵀ
The transpose of A swaps rows and columns. Since A is 3 × 2, its transpose Aᵀ is 2 × 3: Aᵀ = [1, 1, 1; 1, 2, 3] - 2
Step 2 — Compute AᵀA
Multiply Aᵀ (2×3) by A (3×2) to get a 2×2 matrix: AᵀA = [1+1+1, 1+2+3; 1+2+3, 1+4+9] = [3, 6; 6, 14]AᵀA = [3, 6; 6, 14] - 3
Step 3 — Compute Aᵀb
Multiply Aᵀ (2×3) by b (3×1) to get a 2×1 vector: Aᵀb = [1(2)+1(3)+1(5); 1(2)+2(3)+3(5)] = [10; 23]Aᵀb = [10; 23] - 4
Step 4 — Solve AᵀA x̂ = Aᵀb
We solve the 2×2 system: 3c₀ + 6c₁ = 10 6c₀ + 14c₁ = 23 From the first equation: c₀ = (10 − 6c₁)/3. Substitute into the second: 6(10 − 6c₁)/3 + 14c₁ = 23 20 − 12c₁ + 14c₁ = 23 2c₁ = 3, so c₁ = 3/2 = 1.5 Then c₀ = (10 − 9)/3 = 1/3 ≈ 0.333x̂ = [1/3; 3/2], so the best-fit line is y = 1/3 + (3/2)x - 5
Step 5 — Check the Residual
Compute Ax̂ and the residual r = b − Ax̂: Ax̂ = [1/3 + 3/2; 1/3 + 3; 1/3 + 9/2] = [11/6; 10/3; 29/6] ≈ [1.833; 3.333; 4.833] r = b − Ax̂ = [2 − 1.833; 3 − 3.333; 5 − 4.833] ≈ [0.167; −0.333; 0.167] ‖r‖ = √(0.028 + 0.111 + 0.028) = √0.167 ≈ 0.408Least squares error ≈ 0.408
SECTION 7
Strengths & Limitations
The least squares method is incredibly powerful and widely used, but like any mathematical tool, it has both strengths and limitations. Understanding these helps you know when to use it and when to be cautious.
| Strengths | Limitations |
|---|---|
| Always gives an answer — even when the system has no exact solution, the normal equation AᵀAx̂ = Aᵀb is always consistent. | Sensitive to outliers — a single extreme data point can drag the best-fit line far from the majority of the data. |
| Has a clear geometric meaning — the solution is the orthogonal projection, which makes it easy to visualize and understand. | Only finds linear relationships (in standard form). If the true pattern is curved, a straight line won't capture it well. |
| Computationally efficient — solving the normal equation involves multiplying by Aᵀ and then solving a small system. | If AᵀA is nearly singular (close to having no inverse), the solution may be unstable and sensitive to small data changes. |
| Foundation for many techniques — used in data science, physics, engineering, statistics, and machine learning. | Assumes errors are equally important — it treats every residual the same, which may not suit all applications. |
Comparing the strengths and limitations of the least squares method
KEY TAKEAWAY
SECTION 8
Connections to Advanced Theory
Least squares is not just a stand-alone technique — it connects to several powerful ideas in mathematics. As you advance in linear algebra, you'll see how it links to orthogonal bases, matrix factorizations, and even machine learning. Here's a preview of those connections.
| Concept | Connection to Least Squares | Where You'll See It |
|---|---|---|
| QR Factorization | Decomposes A into an orthogonal matrix Q and upper triangular matrix R. This gives a more numerically stable way to solve least squares: Rx̂ = Qᵀb. | Numerical linear algebra, computer algorithms |
| Gram-Schmidt Process | Converts any set of vectors into an orthogonal set. This is the foundation for computing the QR factorization used in least squares. | Inner product spaces, orthogonal projections |
| Pseudoinverse (A⁺) | A generalized inverse that works even when A doesn't have a regular inverse. The least squares solution can be written x̂ = A⁺b. | Advanced linear algebra, data science |
| Linear Regression | In statistics, fitting a line to data using least squares is called linear regression. It's the same math with a statistical interpretation. | Statistics, machine learning, data analysis |
How least squares connects to advanced topics
If you continue studying linear algebra, you'll discover that the Singular Value Decomposition (SVD) provides the most general and robust framework for least squares problems. The SVD works even when AᵀA is not invertible, making it the go-to tool in modern computational applications. Many algorithms in machine learning and artificial intelligence rely on least squares through SVD every day.
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
When a system Ax = b has no exact solution, the least squares solution x̂ minimizes the distance ‖b − Ax‖ by finding the vector in the column space of A that is closest to b. This closest point is the orthogonal projection of b onto Col(A), and the residual r = b − Ax̂ is perpendicular to every column of A.
The solution is found by solving the normal equation AᵀAx̂ = Aᵀb. This technique powers line fitting (linear regression), data analysis, and many algorithms in science and engineering. Key advanced extensions include QR factorization and the pseudoinverse, which provide more robust computational methods.