Last updated 13 November 2026
What is least squares?
Least squares finds the best approximate solution to a system that has no exact solution, by choosing the that makes the sum of squared errors as small as possible. Geometrically, it projects onto the column space of , and the solution satisfies the normal equations , which is exactly how linear regression fits its weights. On TensorTonic you implement least squares, vector and orthogonal projections and linear regression from scratch, with tests that check your output.
The geometry of fitting
Most real world systems are overdetermined: we have more equations than unknowns. The system has no exact solution. The data is too noisy, the model is too simple, or both.
Least Squares asks: "If we cannot hit exactly, what is the closest we can get?" The answer is the projection of onto the column space of .
The Core Insight
Linear regression, polynomial fitting, and even deep learning (in parts) all reduce to projections. The error is minimized when it is perpendicular to the subspace of possible outputs.
Vector Projection
Before matrices, let us project a single vector onto another vector .
Vector Projection
Visualizing proj_b(a) and the orthogonal error vector.
Target Vector (a)
Base Vector (b)
The Projection
The component of that lies along . This is what we keep.
The Error
. The residual, always perpendicular to .
Interactive: Fitting Lines
In coordinate space, "perpendicular error" translates to minimizing the sum of vertical distance squared. Drag the points to see how the optimal line shifts to keep the residuals orthogonal to the feature space.
Data Space
Optimal Line
Orthogonality Check
The error vector must be orthogonal to the feature space (Column Space of X).
Total Squared Error
Visualize this as the total area of all the squares in the plot. Least Squares finds the minimum possible area.
Column Space View
For a matrix , the column space is the set of all possible outputs . If is not in , we project it.
- The target lives in (m data points).
- The column space is a subspace of (spanned by n features).
- We find the point closest to .
- The error is perpendicular to .
Key Geometric Fact
The shortest distance from a point to a subspace is measured along the perpendicular. This is why (the error is orthogonal to every column of ).
The Normal Equations
From the perpendicularity condition , we simplify to find the best weights :
When Does This Work?
The inverse exists only when has full column rank, i.e., all columns are linearly independent. If features are collinear (one is a linear combination of others), is singular and cannot be inverted. This is exactly why ridge regression adds to the diagonal: the matrix is always invertible for , regardless of rank.
The Pseudoinverse
The matrix is called the Moore Penrose Pseudoinverse. It gives the least squares solution even when is not square.
Case Study: Bulb Lifespan Prediction
The Problem
You have 100 bulbs with features (voltage, temperature) and lifespan measurements. You want to fit a linear model: Lifespan = w₁×Voltage + w₂×Temperature + w₀.
The Setup
Design matrix is 100×3 (100 samples, 3 features including bias). Target is 100×1. We solve for (3×1).
The Geometric View
The column space of is a 3D subspace in 100D space. The vector (lifespans) is projected onto this subspace. The residuals are the prediction errors, perpendicular to all features.
QR Decomposition Solution
Directly computing is numerically unstable. In practice, we use QR decomposition.
- Decompose where is orthogonal, is upper triangular.
- Substitute:
- Simplify:
- Since :
- Result: (solve by back substitution)
Numerical Advantage: QR avoids squaring the condition number of . This is how numpy.linalg.lstsq works internally.
ML Applications
Linear Regression
The closed form solution is exactly the normal equations. Gradient descent converges to the same point.
Ridge Regression
Add regularization: . When is singular (features are collinear), OLS breaks. Adding shifts all eigenvalues away from zero, guaranteeing invertibility while also shrinking coefficients toward zero.
PCA via SVD
PCA finds the subspace that best approximates the data (least reconstruction error). This is a projection problem solved via SVD.
Kernel Methods
Kernel Ridge Regression projects data into a high dimensional feature space and applies least squares there (via the kernel trick).
In practice, least-squares problems are solved with a QR decomposition rather than the normal equations, because it avoids squaring the condition number.
Frequently asked questions about projections and least squares
What are the normal equations?
The normal equations AᵀAx̂ = Aᵀb come from requiring the error b - Ax̂ to be perpendicular to every column of A. When A has full column rank, their solution is x̂ = (AᵀA)⁻¹Aᵀb, which is the closed-form solution of linear regression.
Why solve least squares with QR instead of the normal equations?
Computing (AᵀA)⁻¹ directly is numerically unstable because forming AᵀA squares the condition number of A. QR decomposition avoids this by reducing the problem to Rx̂ = Qᵀb, which is solved by back substitution.
Practice this
On TensorTonic you implement this from scratch and test it in the browser.
- Least Squares Solution: solve an overdetermined system with the normal equations.
- Vector Projection: project one vector onto another.
- Orthogonal Projection Matrix: build the projection onto a whole column space.
- Linear Regression from Scratch: fit a regression model, which is least squares in practice.
- Ridge Regression: Closed-Form Solution: add L2 regularization to the normal equations.