Skip to the lesson
FixTheRobot Explore Robotics / Linear Algebra / M5 Least Squares and Numerical Methods / M5-T2

Linear Algebra · Theory · M5

M5-T2 — Least-Squares Formulation

Least squares turns the vague wish “fit the data as well as possible” into a precise optimisation problem with a unique, computable answer.

  • Lesson 2 of 8
  • Module 5: Least Squares and Numerical Methods
  • Least squares
  • About 9 minutes to read

To fit a model to data you need to say what “as well as possible” means. Least squares says: make the sum of squared prediction errors as small as possible. This lesson states the problem in matrix form, shows why squaring is a sound choice, and why the resulting cost has one lowest point.

Learning objectives

After this lesson, you should be able to:

  • Write a linear fitting problem as minimise ‖A x − b‖² and identify A, x and b from data.
  • Define residuals and explain why their squares are summed.
  • Show that the cost is a convex quadratic in x whose minimum is unique when the columns of A are independent.
  • Compare squared, absolute and robust costs, including the effect of an outlier.

Introduction and motivation

Every calibration, line fit and estimator in robotics begins by choosing a cost: a single number that says how badly a candidate answer explains the data. The squared-error cost is the default because it makes the problem solvable in closed form with matrix algebra, it corresponds to the most likely answer when the noise is Gaussian, and it is smooth enough to optimise. Its main weakness, sensitivity to outliers, is the reason robust variants exist.

The intuition

Suppose you want a line through scattered points. For any candidate line, each point has a residual: the vertical gap between what the line predicts and what was measured. Some are positive, some negative; adding them would let errors cancel. Squaring makes every gap count as a positive number and punishes large gaps more than small ones. The cost is the sum of the squared gaps, and the best line is the one with the lowest cost.

Interactive figure. Six data points at x equal to 1 to 6 and the least-squares line through them, with dashed segments showing the vertical residuals. The readout gives the fitted line, the sum of squared residuals and the normal equations. This figure needs JavaScript to be interactive.

The orange dashes are the residuals; the readout shows their weighted sum of squares. Drag a point and look at how the best line and the cost respond: the green line is exactly the one with the lowest possible cost.
The cost as a bowlThe sum of squared residuals plotted against the unknown. It is a parabola in one unknown and a bowl in two or more. Its single lowest point is the least-squares solution, found where the slope is zero. x̂ (minimum)cost f(x) = ‖A x − b‖² cost(x) = Σ rᵢ² = ‖A x − b‖² residual rᵢ = (A x − b)ᵢ = prediction − data Convex bowl when the columns of Aare independent: one lowest point. x̂ = argmin ‖A x − b‖²
The cost is a bowl in the unknowns. The least-squares answer is its lowest point, where the slope is zero.

Geometric and physical interpretation

Geometric view. In the space of unknowns, the cost ‖Ax − b‖² is a bowl-shaped surface (a paraboloid) whose level curves are ellipses. The least-squares solution is the bottom of the bowl. Physical view. Each residual is the discrepancy between what the model says a sensor should read and what it actually read. The cost adds up the energy of those discrepancies, in the way that the elastic energy of springs attached to the data points adds up, so the best fit is the equilibrium of the springs.

The mathematics

A
the m × n design matrix: row i describes how the unknowns predict measurement i.
x
the n unknowns (slope and intercept, plane parameters, calibration constants).
b
the m measurements.
r
the residual vector r = A x − b (prediction minus data).

The problem

minimise over x:     f(x) = ‖A x − b‖² = Σ ( ai · x − bi )²        x̂ = argmin f(x)

What it means. Choose the unknowns to minimise the sum of squared residuals. Row i of A multiplied by x is the prediction of measurement i, so f counts the squared mismatch over all measurements.

Setting up A, x and b

line y = m x + c:     A = [ x1 1 ; x2 1 ; ... ; xm 1 ],   x = (m, c),   b = (y1, ..., ym)

What it means. Each measurement is one row: [xi, 1] times (m, c) predicts yi. Any model that is linear in its unknown parameters gets a design matrix this way, even if it is nonlinear in the input (a polynomial, for instance).

Expanding the cost

f(x) = xᵀ (AᵀA) x − 2 bᵀA x + bᵀb

What it means. AᵀA is symmetric and positive semidefinite; it is positive definite (a bowl) exactly when the columns of A are independent. So f has a single lowest point, and the lesson on quadratic forms tells you what its level curves look like.

Least squares: x̂ minimises the sum of squared residuals ‖Ax − b‖². The cost is a bowl with a single minimum when the columns of A are independent.

Step-by-step derivation: the cost is a bowl, and the average as the one-unknown case

Expanding the cost. Using ‖v‖² = vᵀv:

  1. f(x) = (Ax − b)ᵀ(Ax − b).
  2. Expand with (Ax − b)ᵀ = xᵀAᵀ − bᵀ: f = xᵀAᵀAx − xᵀAᵀb − bᵀAx + bᵀb.
  3. The two middle terms are scalars and are transposes of each other, so they are equal: f = xᵀ(AᵀA)x − 2bᵀAx + bᵀb.

The bowl. For any non-zero x, xᵀ(AᵀA)x = (Ax)ᵀ(Ax) = ‖Ax‖² ≥ 0, so AᵀA is positive semidefinite. It is positive definite exactly when Ax = 0 forces x = 0, i.e. when the columns of A are independent. Then f is a strictly convex quadratic, and a strictly convex quadratic has exactly one stationary point, which is its global minimum.

Why squares. Three reasons: (1) calculus: squares are smooth, so setting the derivative to zero gives linear equations (the normal equations), solvable in one step; (2) geometry: sums of squares are squared lengths, so the best fit is an orthogonal projection (next lesson); (3) statistics: if the measurement errors are independent Gaussian with equal variance, the x that maximises the likelihood of the data is exactly the minimiser of the sum of squares.

The price. Squaring magnifies large errors. For readings 9.8, 10.1, 10.3 and an outlier 15.0 the mean (the least-squares answer) is 11.3 m, dragged 1.1 m away from the three consistent readings, while the median, which minimises the sum of absolute errors, stays at 10.2 m. This is why robust costs (Huber, absolute value) exist, and why outlier rejection precedes a least-squares fit in practice.

A worked example: setting up and evaluating the cost

One unknown. For the readings 9.8, 10.1, 10.3, A = [1, 1, 1]ᵀ. At the candidate x = 10 the residuals are (−0.2, 0.1, 0.3) and f = 0.04 + 0.01 + 0.09 = 0.14. At the mean x = 10.0667, the residuals are (−0.2667, 0.0333, 0.2333) and f = 0.1267, smaller, as it must be.

A line. The points (1, 1), (2, 2), (3, 2) give A = [[1, 1], [2, 1], [3, 1]] and b = (1, 2, 2). For the line y = x (m = 1, c = 0) the residuals are (0, 0, 1) and f = 1. For y = 0.5x + 0.667 the residuals are (0.167, −0.333, 0.167) (prediction minus data) and f = 0.0278 + 0.1111 + 0.0278 = 0.1667: the better line has the lower cost.

The quadratic form. AᵀA = [[14, 6], [6, 3]] has determinant 14·3 − 36 = 6 > 0 and trace 17: positive definite, so the cost is a bowl in (m, c).

An outlier. Adding the reading 15.0 moves the mean to (9.8 + 10.1 + 10.3 + 15.0)/4 = 11.3, a shift of 1.23 m from the earlier answer of 10.07 m.

Robotics application

Calibration as a fit. A range sensor with scale error and offset follows z = s d + o; measuring true distances 1 to 5 m gives z = 1.12, 2.05, 3.10, 4.18, 5.10 m. The least-squares fit gives s = 1.009 and o = 0.083 m, with residuals of a few centimetres: the sensor reads about 0.9% long with an 8 cm offset.

Plane and line fitting. Fitting the ground plane to a LiDAR scan minimises the sum of squared point-to-plane distances (Module 6 uses the SVD for the best version of this). The SLAM track's ICP lesson minimises the sum of squared point-to-point distances in the same spirit.

Odometry and filtering. A Kalman filter update minimises a weighted sum of squared prediction and measurement errors; Module 9 shows this equivalence.

Dealing with outliers. Perception pipelines remove outliers (RANSAC) before the least-squares step, or replace the square by a robust loss; see robust optimisation.

Engineering implications and limitations

Practical rules

  • Look at the residuals, not only the answer. A least-squares fit always returns something. Plot or summarise the residuals: they should look like noise (no trend), and be about as large as the sensor noise.
  • Remove gross outliers first. One bad measurement can dominate the squared cost. Use a consensus method or a robust loss when outliers are possible.
  • Choose units and scaling deliberately. The cost adds squares of residuals; mixing metres and radians in one residual vector makes the fit favour whichever has larger numbers. Scale each residual by its expected noise (weighted least squares, later).
  • Check the rank. If the columns of A are dependent, the bowl has a flat valley and the answer is not unique. Check the smallest singular value, not only the rank.
  • Fit the model your data supports. More parameters always lower the cost on the training data; a lower cost with more parameters can still be a worse model. Compare on separate data.

Limits

Squared error is optimal under independent Gaussian noise of equal size. Heavy-tailed noise, outliers, noise that grows with distance, and errors in the inputs as well as the outputs all violate that assumption, and each has its own remedy (robust loss, weights, total least squares).

Common misconceptions

“Minimising the sum of the residuals is the same as least squares.”

The plain sum lets positive and negative errors cancel and is minimised by a useless line. Squares (or absolute values) are needed.

“Least squares finds a line that passes through the most points.”

It need not pass through any of them; it minimises the total squared gap.

“A lower cost always means a better model.”

Adding parameters can always reduce the cost on the data at hand. Judge a model on how well it predicts new data and whether the residuals look like noise.

“Least squares is only for lines.”

It applies to any model linear in its parameters (polynomials, sums of known functions) and, via linearisation, to nonlinear ones.

Engineering takeaways

  1. Least squares chooses x to minimise the sum of squared residuals f(x) = ‖Ax − b‖².
  2. The design matrix A has one row per measurement; for a line each row is [xi, 1].
  3. f(x) = xᵀAᵀAx − 2bᵀAx + bᵀb is a convex bowl; it has a unique minimum when the columns of A are independent.
  4. Squares are used because they are smooth, geometric (projection) and optimal for Gaussian noise; they are sensitive to outliers.
  5. Always inspect the residuals: they measure the quality of the fit and expose outliers and model errors.

References and further reading

Primary papers and authoritative documentation.

Knowledge check

Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

Set up A, x and b to fit y = m x + c to (0, 1), (1, 3), (2, 4), and compute the cost of the line y = 1.5x + 1.

Explanation

A = [[0, 1], [1, 1], [2, 1]], x = (m, c), b = (1, 3, 4). For m = 1.5, c = 1 the predictions are (1, 2.5, 4) and the residuals (prediction − data) are (0, −0.5, 0), so the cost is f = 0.25. A line with a lower cost exists (the least-squares line).

Question 2

The readings 9.8, 10.1, 10.3 are joined by a faulty reading of 15.0. By how much does the least-squares estimate move, and what would you do?

Explanation

The mean moves from 10.0667 to (9.8 + 10.1 + 10.3 + 15.0)/4 = 11.3, a shift of about 1.23 m caused by one bad reading, because squares weight large errors heavily. Detect and reject the outlier first (for example a consensus test or a gate based on the residual size) or use a robust loss (median, Huber) instead of plain squares.

Question 3

Why is the cost ‖Ax − b‖² a bowl with exactly one lowest point when the columns of A are independent, and what changes if they are dependent?

Explanation

The quadratic part is xᵀ(AᵀA)x = ‖Ax‖², which is positive for every non-zero x exactly when Ax = 0 only for x = 0, i.e. independent columns. A positive definite quadratic is a strictly convex bowl with one minimum. If the columns are dependent there is a null-space direction along which the cost does not change: the bowl becomes a flat-bottomed valley and infinitely many x give the same minimum cost.

What’s next

Next you will see that the least-squares answer has a beautiful geometric meaning: it is the projection of the data onto the set of predictions the model can make, and the residual is perpendicular to that set.

Practice this lesson

LiDAR Plane Fitting

Fit the ground plane to a noisy LiDAR scan, find out why a naive fit fails on a wall, and fix it with a stable method.

Next lesson

M5-T3 — Geometric Interpretation of Residuals

See the least-squares answer as a projection onto the column space and the residual as orthogonal to it.

Nothing below is required to finish this lesson.