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.
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.
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
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.
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.
If we distribute the Aᵀ and rearrange, we get the famous normal equation:
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 |
Worked Example
Let's work through a complete example. We want to find the least squares solution to the system Ax = b where:
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. |
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 |
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.
Practice Problems
Lesson 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.