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

Linear Algebra · Theory · M5

M5-T3 — Geometric Interpretation of Residuals

The best prediction is the closest point the model can reach, and what it cannot explain is exactly perpendicular to everything it can.

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

The previous lesson defined least squares by a cost. This one reveals what the answer looks like in space: a shadow. The data vector casts a shadow onto the set of predictions the model can make, and the residual is the light ray that meets that set at a right angle. The picture explains the normal equations, residual checks and the quality measure called R².

Learning objectives

After this lesson, you should be able to:

  • Explain the least-squares prediction A x̂ as the projection of b onto the column space of A.
  • State and justify that the residual is orthogonal to every column of A, and what that implies (for example residuals summing to zero when A contains a column of ones).
  • Use Pythagoras to split ‖b‖² into explained and unexplained parts.
  • Use the orthogonality as a unit test for a fit and the residual fraction as a quality measure.

Introduction and motivation

It is easy to run a fit; it is harder to know whether to trust it. The geometry gives you tests that do not depend on the particular data. The residual must be orthogonal to every column of the design matrix, which means (for a line fit with an intercept) that the residuals sum to zero and are uncorrelated with the inputs. When a solver returns an answer violating this, there is a bug or an inaccuracy. And the Pythagorean split of the data into fitted and leftover parts gives the standard quality measure for a fit.

The intuition

Collect all the data you could possibly predict: every vector of the form A x for some choice of unknowns. They form a flat set, the column space of A (a plane, for two unknowns). The measured data b is a point generally off that plane, because of noise. The best prediction is the point on the plane nearest to b, and the nearest point is found by dropping a perpendicular.

The residual is perpendicular to everything the model can predictThe data vector b lies outside the plane of all possible predictions, the column space of A. The least-squares prediction is the point of the plane closest to b: the orthogonal projection. The residual from that point to b is perpendicular to the plane, so it is orthogonal to every column of A.column space C(A): all predictions A x b (the data) A x̂ (best prediction) r = b − A x̂ Aᵀ r = 0the residual is orthogonal toevery column of A ‖b‖² = ‖A x̂‖² + ‖r‖²Pythagoras on the right triangle r lives in the left null space N(Aᵀ)
Least squares as projection: the prediction is the closest point of the column space, and the residual meets the column space at a right angle.

That perpendicularity is the entire content of the normal equations: “the residual is orthogonal to every column of A” is the condition for the minimum.

Interactive figure. Six data points and the least-squares line with dashed vertical residuals. The residuals of the best line sum to zero, reflecting the orthogonality of the residual to the column of ones. This figure needs JavaScript to be interactive.

Look at the residuals (orange). For the least-squares line, the positive and negative ones balance exactly (they sum to zero) and are uncorrelated with x. Move any point: the line re-balances so this stays true.

Geometric and physical interpretation

Geometric view. The m-dimensional data space contains the n-dimensional column space of A. The least-squares prediction is the orthogonal projection of b onto it, and the residual lies in the orthogonal complement, the left null space N(Aᵀ) of the earlier module. b = (fit part) + (error part), with the two parts perpendicular. Physical view. The fit part is the portion of the measurements the model can explain; the error part is everything it cannot. If the model is right and the noise is random, the error part is only noise. If the error part is large or structured, the model is missing something (a bias, a nonlinearity, another sensor effect).

The mathematics

p = A x̂
the least-squares prediction (projection of b).
r = b − A x̂
the residual (here defined as data minus prediction).
C(A)
the column space: all possible predictions.
N(Aᵀ)
the left null space: the orthogonal complement of C(A).

Orthogonality of the residual

Aᵀ r = Aᵀ (b − A x̂) = 0          i.e.   ai · r = 0 for every column ai of A

What it means. For each column (each feature of the model) the residual has no component along it. If a column is all ones, this says the residuals sum to zero; if a column holds the x-values, the residuals are uncorrelated with x.

Pythagoras

‖b‖² = ‖A x̂‖² + ‖r‖²          R² = ‖A x̂‖² / ‖b‖² = 1 − ‖r‖²/‖b‖²

What it means. The squared length of the data equals the squared length of the fitted part plus that of the residual. The fraction explained is a standard quality number (after subtracting the mean when appropriate).

Closest point

‖b − A x‖² ≥ ‖b − A x̂‖²   for every x          (equality only at x̂ when A has independent columns)

What it means. Any other prediction is farther from the data, by Pythagoras: the extra distance is the length of the segment between that prediction and the projection.

Least squares = projection of b onto C(A). The residual is perpendicular to every column of A, and ‖b‖² = ‖Ax̂‖² + ‖r‖².

Step-by-step derivation: orthogonality from the minimum, and Pythagoras

The minimum forces orthogonality. Suppose x̂ minimises ‖b − Ax‖². Perturb it by a direction d and a small number t: the cost of x̂ + t d is

‖b − A x̂ − t A d‖² = ‖r − t A d‖² = ‖r‖² − 2 t (r · A d) + t² ‖A d‖²

What it means. Expanding the square gives a quadratic in t. At t = 0 (the claimed minimum) it must not decrease, so its slope there must be zero.

The slope at t = 0 is −2 r·(Ad). For a minimum this must vanish for every direction d, so r · (Ad) = 0 for all d. Taking d = ej gives r · aj = 0 for each column: Aᵀ r = 0. Conversely, if Aᵀr = 0 then the quadratic's slope vanishes and, since its t² coefficient is non-negative, t = 0 is a minimum.

Pythagoras. Since b = Ax̂ + r with r ⟂ C(A) and Ax̂ ∈ C(A), we get ‖b‖² = ‖Ax̂‖² + 2(Ax̂)·r + ‖r‖² = ‖Ax̂‖² + ‖r‖², because the cross term is zero.

Residuals sum to zero. If the first column of A is all ones, a₁ = (1, …, 1), then a₁·r = Σ ri = 0: the residuals have zero mean.

The residual lies in the left null space. Aᵀr = 0 says exactly r ∈ N(Aᵀ), so the data split as b = (a vector of C(A)) + (a vector of N(Aᵀ)), the splitting of Rm into two perpendicular subspaces from the module on subspaces.

A worked example: checking orthogonality and Pythagoras on a line fit

Take the fit of the earlier lesson: A = [[1, 1], [2, 1], [3, 1]], b = (1, 2, 2), x̂ = (0.5, 0.6667).

  1. Prediction. p = A x̂ = (1.1667, 1.6667, 2.1667).
  2. Residual (data minus prediction). r = b − p = (−0.1667, 0.3333, −0.1667).
  3. Orthogonal to the column of ones: −0.1667 + 0.3333 − 0.1667 = 0 ✓ (the residuals sum to zero).
  4. Orthogonal to the column of x-values (1, 2, 3): −0.1667 + 0.6667 − 0.5 = 0 ✓.
  5. Pythagoras. ‖b‖² = 1 + 4 + 4 = 9, ‖p‖² = 1.3611 + 2.7778 + 4.6944 = 8.8333, ‖r‖² = 0.0278 + 0.1111 + 0.0278 = 0.1667, and 8.8333 + 0.1667 = 9 ✓. The line explains 98.1% of the squared data length.
  6. Another point is worse. The prediction from the line y = x is (1, 2, 3), and its residual (0, 0, −1) has squared length 1, larger than 0.1667, and is not orthogonal to the ones column (sum −1).

Robotics application

A unit test for any fit. After solving, compute Aᵀ r and check it is zero to machine precision (relative to ‖A‖‖r‖). It catches a wrong sign convention, a bad weighting, a stale matrix and an inaccurate solver (see conditioning, later). Most fitting bugs make this test fail.

Diagnosing sensor models. If the residuals are small and look like noise, the model is adequate. If they are structured (a drift with time, larger at long range), the model misses an effect, for instance a scale error that grows with distance. Calibration workflows plot residuals against each input for this reason.

LiDAR plane fitting. The residual of each point is its distance to the fitted plane; the residual vector is perpendicular to the columns of the design matrix. The standard deviation of the residuals estimates the sensor noise and is used to set the ground-segmentation threshold. See the SLAM lesson point-cloud preprocessing.

Explained fraction. R² and similar numbers summarise how much of the measured variation a model explains; in robotics they are used to compare, for example, an odometry model with and without a slip term.

Engineering implications and limitations

Practical rules

  • Always compute and look at the residual vector. It is free after the solve and tells you more than the parameters do.
  • Use Aᵀr = 0 as an assertion. Normalise by ‖A‖‖r‖ and expect about 10⁻¹⁵ for a well-conditioned problem. A value near 10⁻⁶ indicates an ill-conditioned problem or an inaccurate method.
  • Zero-mean residuals only if there is an intercept. Without a column of ones in A (a fit forced through the origin) the residuals need not sum to zero.
  • A small residual does not prove a correct model. A model with many parameters fits noise and leaves a small residual. Check the residuals on data not used in the fit.
  • Mind the scale of R². Whether R² uses the raw data or the data with its mean removed changes its meaning; state which.

Limits

The perpendicularity statements are about the ordinary dot product and plain least squares. With weights (Module 5, later) the residual is orthogonal in the weighted sense (AᵀW r = 0), and in total least squares the errors are measured perpendicular to the model instead.

Common misconceptions

“The residual of a good fit is zero.”

Only if the data lie exactly in the column space. With noise the residual is non-zero, and its size reflects the noise level, which is good: a zero residual with noisy data suggests overfitting.

“The residuals of least squares could have any sign pattern.”

They are constrained: they are orthogonal to every column of A. With an intercept they sum to zero, so they cannot all be positive.

“The fitted part and the residual can overlap.”

They are perpendicular by construction, which is why their squared lengths add (Pythagoras).

“Orthogonality of the residual proves the model is right.”

It holds for any least-squares fit, correct model or not. It is a check on the solver, not on the model.

Engineering takeaways

  1. The least-squares prediction A x̂ is the orthogonal projection of b onto the column space of A.
  2. The residual r = b − A x̂ is orthogonal to every column of A (Aᵀr = 0) and so lies in the left null space.
  3. Pythagoras: ‖b‖² = ‖A x̂‖² + ‖r‖²; the fraction explained is a quality measure.
  4. With a column of ones in A the residuals sum to zero.
  5. Test any solver output with Aᵀr ≈ 0; inspect the residuals for structure, 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

A line fit with an intercept returns residuals (0.2, −0.1, 0.3, −0.5). Is this plausible for a least-squares fit? What does your answer say about the solver?

Explanation

The residuals must sum to zero (orthogonal to the column of ones); here 0.2 − 0.1 + 0.3 − 0.5 = −0.1 ≠ 0. So this is not the least-squares solution of a problem with an intercept column: perhaps the intercept was left out, a different cost was minimised, or the solver is inaccurate. The check Aᵀr = 0 catches it immediately.

Question 2

For the data b = (1, 2, 2) and the fit A x̂ = (1.1667, 1.6667, 2.1667), compute ‖b‖², ‖Ax̂‖² and ‖r‖² and confirm Pythagoras.

Explanation

‖b‖² = 9; ‖Ax̂‖² = 1.3611 + 2.7778 + 4.6944 = 8.8333; the residual (−0.1667, 0.3333, −0.1667) has ‖r‖² = 0.1667. Indeed 8.8333 + 0.1667 = 9. The cross term 2(Ax̂)·r is zero because the residual is orthogonal to the column space.

Question 3

A calibration fit has tiny residuals but they steadily grow with range. What does that tell you about the model, even though the cost is small?

Explanation

Residuals that follow a pattern are not noise: the model is missing an effect that depends on range (for example a scale error or a range-dependent bias). Least squares guarantees the residual is orthogonal to the model columns, so the model captures the part linear in them; the remaining systematic part is something the columns cannot represent, such as a quadratic or a missing term. Add the missing term and refit.

What’s next

The perpendicularity condition Aᵀr = 0 can be written out and solved. That gives the normal equations, the classical way to compute a least-squares fit, together with an honest account of when it is accurate.

Next lesson

M5-T4 — Normal Equations

Derive and solve the normal equations, and state when they are valid and when they lose accuracy.

Nothing below is required to finish this lesson.