MATH-125 · Lesson 10 of 12 · 30 min
Orthogonality and least squares
Measured data never fit a model exactly. A spring tested at ten loads gives ten points that lie close to, but not on, a straight line; a survey network with redundant measurements gives slightly conflicting coordinates; a set of temperature readings scatters around a trend. The corresponding linear systems have more equations than unknowns and no exact solution. The method of least squares, developed independently by Legendre and Gauss around 1800 to fit planetary orbits and survey the Earth, finds the solution that comes closest, in the sense of minimizing the sum of squared errors. Its theory rests on orthogonality: the best approximation is the one whose error is perpendicular to everything the model can produce. This lesson develops orthogonal vectors and projections, the Gram-Schmidt process and QR factorization, and the normal equations of least squares, and applies them to fitting lines, curves and survey networks.
Orthogonal vectors and subspaces
Two vectors are orthogonal if their dot product is zero (Lesson 1). A set of vectors is orthogonal if every pair is orthogonal, and orthonormal if, in addition, every vector has length 1. Orthogonal sets of non-zero vectors are automatically linearly independent, and coordinates with respect to an orthonormal basis are simply dot products (Lesson 2), which makes such bases exceptionally convenient.
A vector is orthogonal to a subspace if it is orthogonal to every vector in it, which is equivalent to being orthogonal to each vector in a basis of the subspace. The set of all such vectors is the orthogonal complement. For example, in \(\mathbb{R}^3\), the orthogonal complement of a plane through the origin is the line along its normal vector. A key fact connects this to the subspaces of Lesson 7: the null space of a matrix is the orthogonal complement of its row space, since \(A\mathbf{x} = \mathbf{0}\) says exactly that \(\mathbf{x}\) is orthogonal to every row of \(A\).
Projection onto a subspace
Lesson 8 projected a vector onto a line. More generally, the orthogonal projection of a vector \(\mathbf{b}\) onto a subspace \(W\) is the vector \(\hat{\mathbf{b}}\) in \(W\) closest to \(\mathbf{b}\). It is characterized by a simple geometric condition: the error \(\mathbf{b} - \hat{\mathbf{b}}\) is orthogonal to \(W\). The distance from \(\mathbf{b}\) to the subspace is the length of this error, by the Pythagorean theorem, since the error is perpendicular to every other displacement within \(W\).
If \(W\) has an orthogonal basis \(\mathbf{u}_1, \ldots, \mathbf{u}_k\), the projection is the sum of the projections onto each basis vector:
Worked problem
Projecting onto a plane
Project \(\mathbf{b} = (3, 1, 4)\) onto the plane spanned by the orthogonal vectors \(\mathbf{u}_1 = (1, 1, 0)\) and \(\mathbf{u}_2 = (1, -1, 1)\). Find the distance from \(\mathbf{b}\) to the plane.
Show solution
- Check orthogonality: \(\mathbf{u}_1\cdot\mathbf{u}_2 = 1 - 1 + 0 = 0\).
- Coefficients: \(\dfrac{\mathbf{b}\cdot\mathbf{u}_1}{2} = \dfrac{4}{2} = 2\); \(\dfrac{\mathbf{b}\cdot\mathbf{u}_2}{3} = \dfrac{6}{3} = 2\).
- \(\hat{\mathbf{b}} = 2(1, 1, 0) + 2(1, -1, 1) = (4, 0, 2)\). Error: \(\mathbf{b} - \hat{\mathbf{b}} = (-1, 1, 2)\), orthogonal to both \(\mathbf{u}_1\) and \(\mathbf{u}_2\).
Answer: The projection is (4, 0, 2) and the distance is |(−1, 1, 2)| = √6 ≈ 2.45.
The Gram-Schmidt process and QR factorization
The projection formula needs an orthogonal basis. The Gram-Schmidt process builds one from any basis: take the first vector as it is; from the second, subtract its projection onto the first; from the third, subtract its projections onto the first two new vectors; and so on. Each step removes the components along earlier directions, leaving a vector orthogonal to them. Dividing each result by its length gives an orthonormal basis.
Worked problem
Gram-Schmidt
Orthogonalize \(\mathbf{a}_1 = (1, 1, 1)\) and \(\mathbf{a}_2 = (1, 2, 4)\).
Show solution
- \(\mathbf{u}_1 = \mathbf{a}_1 = (1, 1, 1)\).
- \(\mathbf{u}_2 = \mathbf{a}_2 - \dfrac{\mathbf{a}_2\cdot\mathbf{u}_1}{\mathbf{u}_1\cdot\mathbf{u}_1}\mathbf{u}_1 = (1, 2, 4) - \dfrac{7}{3}(1, 1, 1) = \left(-\tfrac{4}{3}, -\tfrac{1}{3}, \tfrac{5}{3}\right)\).
- Check: \(\mathbf{u}_1\cdot\mathbf{u}_2 = (-4 - 1 + 5)/3 = 0\).
Answer: u₁ = (1, 1, 1) and u₂ = (−4, −1, 5)/3 span the same plane as a₁ and a₂ and are orthogonal. Normalizing gives an orthonormal basis.
Recording the Gram-Schmidt process in matrix form gives the QR factorization \(A = QR\), where \(Q\) has orthonormal columns and \(R\) is upper triangular. It plays the role for least squares that LU plays for square systems (Lesson 5), and numerical libraries compute it with more stable variants, such as Householder reflections, rather than classical Gram-Schmidt, which can lose orthogonality to rounding errors.
The least squares problem
Suppose \(A\mathbf{x} = \mathbf{b}\) has more equations than unknowns and no exact solution: \(\mathbf{b}\) is not in the column space of \(A\) (Lesson 7). The least squares solution is the vector \(\hat{\mathbf{x}}\) that makes \(A\hat{\mathbf{x}}\) as close as possible to \(\mathbf{b}\), minimizing the length of the residual \(\mathbf{r} = \mathbf{b} - A\mathbf{x}\), equivalently the sum of the squared residuals \(\sum r_i^2\).
Geometrically, \(A\hat{\mathbf{x}}\) is the projection of \(\mathbf{b}\) onto the column space, so the residual must be orthogonal to every column of \(A\): \(A^T(\mathbf{b} - A\hat{\mathbf{x}}) = \mathbf{0}\). Rearranging gives the normal equations:
If the columns of \(A\) are independent, \(A^TA\) is a square, symmetric, invertible (indeed positive definite) matrix, and the normal equations have a unique solution. For small problems they are solved directly; for large or ill-conditioned problems, the QR factorization is preferred, because forming \(A^TA\) squares the condition number (Lesson 5): with \(A = QR\), the least squares solution satisfies \(R\hat{\mathbf{x}} = Q^T\mathbf{b}\).
Fitting a straight line
To fit a line \(y = c_0 + c_1 t\) to data points \((t_i, y_i)\), each point gives one equation \(c_0 + c_1 t_i = y_i\). With \(m\) points this is the system \(A\mathbf{c} = \mathbf{y}\), where \(A\) has a column of ones and a column of the \(t_i\). The normal equations become:
Worked problem
Calibrating a load cell
A load cell gives outputs of 0.1, 2.1, 3.9, 6.2 and 7.9 mV under loads of 0, 1, 2, 3 and 4 kN. Fit a line \(V = c_0 + c_1 F\) by least squares.
Show solution
- Sums: \(m = 5\), \(\sum F = 10\), \(\sum F^2 = 30\), \(\sum V = 20.2\), \(\sum FV = 0 + 2.1 + 7.8 + 18.6 + 31.6 = 60.1\).
- Normal equations: \(5c_0 + 10c_1 = 20.2\) and \(10c_0 + 30c_1 = 60.1\).
- Subtract twice the first from the second: \(10c_1 = 19.7\), so \(c_1 = 1.97\); then \(c_0 = (20.2 - 19.7)/5 = 0.10\).
- Residuals: \(0.1 - 0.10 = 0.00\), \(2.1 - 2.07 = 0.03\), \(3.9 - 4.04 = -0.14\), \(6.2 - 6.01 = 0.19\), \(7.9 - 7.98 = -0.08\).
Answer: V = 0.10 + 1.97F mV, a sensitivity of 1.97 mV/kN with an offset of 0.10 mV. The residuals sum to zero (a property of least squares with an intercept) and are small and patternless, suggesting the linear model is adequate. In use, the calibration is inverted (MATH-100) to convert readings to loads.
The quality of a fit is judged from its residuals. A pattern in the residuals, such as all positive at the ends and negative in the middle, means the model is missing something, perhaps curvature. The root-mean-square residual estimates the measurement noise, and the coefficient of determination \(R^2\) measures the fraction of the variation explained by the model. STAT-235 develops the statistics of fitted models, including confidence intervals for the coefficients.
Fitting curves and models
Least squares applies to any model that is linear in its unknown coefficients, even if it is not linear in the variable. A quadratic \(y = c_0 + c_1t + c_2t^2\) gives a matrix with columns \(1\), \(t_i\) and \(t_i^2\). A periodic model \(y = c_0 + c_1\cos\omega t + c_2\sin\omega t\), used to fit daily temperature cycles or tidal records, gives columns \(1\), \(\cos\omega t_i\), \(\sin\omega t_i\). Models that are not linear in their coefficients can sometimes be transformed: the exponential \(y = Ae^{kt}\) becomes linear after taking logarithms, \(\ln y = \ln A + kt\) (MATH-100, Lesson 2), though the transformation changes how errors are weighted.
Adding more terms always reduces the residuals on the data used for fitting, but beyond a point it fits the noise rather than the underlying behavior, a problem called overfitting. A polynomial of degree \(m - 1\) passes exactly through \(m\) points but usually oscillates wildly between them. The cure is to choose models with physical justification, use as few parameters as the data require, and check predictions against data not used in the fit, ideas that are central to machine learning (AI-210).
Worked problem
A survey network
Three distances are measured along a straight line between points A, B and C, with A fixed at 0: AB = 101.03 m, BC = 98.95 m and AC = 200.02 m. Find the least squares positions of B and C.
Show solution
- Unknowns \(x_B\), \(x_C\). Equations: \(x_B = 101.03\); \(x_C - x_B = 98.95\); \(x_C = 200.02\). The three are inconsistent by 0.04 m.
- Normal equations: \(A^TA = \begin{pmatrix} 2 & -1 \\ -1 & 2 \end{pmatrix}\), \(A^T\mathbf{b} = (101.03 - 98.95, 98.95 + 200.02) = (2.08, 298.97)\).
- Solve: \(x_B = (2 \times 2.08 + 298.97)/3 = 101.043\); \(x_C = (2.08 + 2 \times 298.97)/3 = 200.007\).
Answer: B at 101.043 m and C at 200.007 m. The 0.04 m misclosure is spread among the three measurements (residuals −0.013, −0.014 and +0.013 m), rather than dumped on one, which is what least squares adjustment of survey networks does on a large scale.
Weighted least squares
Ordinary least squares treats every measurement as equally reliable. When some measurements are more precise than others, for example a distance measured by laser alongside one measured by tape, it is better to give each squared residual a weight inversely proportional to the variance of that measurement. Minimizing \(\sum w_i r_i^2\) leads to the weighted normal equations \(A^TWA\,\hat{\mathbf{x}} = A^TW\mathbf{b}\), where \(W\) is the diagonal matrix of weights. Survey adjustments, GPS positioning and sensor fusion all use weighted least squares, and the Kalman filter used in navigation is a recursive form of it that updates the estimate as each new measurement arrives. The same theory also gives the uncertainty of the fitted values: the matrix \((A^TWA)^{-1}\) is their covariance matrix, which Lesson 11 shows how to interpret through its principal axes.
Common mistakes
MistakeUsing the projection formula with a non-orthogonal basis.
InsteadOrthogonalize first (Gram-Schmidt) or solve the normal equations.
MistakeSolving AᵀA x = Aᵀb for ill-conditioned problems without care.
InsteadUse QR, since forming AᵀA squares the condition number.
MistakeAssuming least squares requires a model linear in the variable.
InsteadIt requires linearity in the coefficients; columns can be any functions of t.
MistakeJudging a fit only by small residuals.
InsteadLook for patterns in the residuals and test on independent data.
MistakeAdding terms until the fit is perfect.
InsteadAvoid overfitting; use physically justified models with few parameters.
Summary and key terms
- Orthogonal vectors have zero dot product; orthonormal bases make coordinates simple dot products.
- The null space is the orthogonal complement of the row space.
- The projection of b onto a subspace is the closest point; the error is orthogonal to the subspace.
- Gram-Schmidt builds orthogonal bases; recorded as A = QR.
- Least squares minimizes the sum of squared residuals; the normal equations are AᵀA x̂ = Aᵀb.
- Line, polynomial and periodic fits use columns of functions of the variable; residuals reveal model adequacy.
- Least squares spreads measurement misclosures in surveys; overfitting is avoided with parsimonious models.
- Orthogonal
- Having a zero dot product; perpendicular.
- Orthonormal set
- Mutually orthogonal unit vectors.
- Orthogonal complement
- All vectors orthogonal to a given subspace.
- Orthogonal projection
- The closest vector in a subspace to a given vector.
- Gram-Schmidt process
- A procedure for building an orthogonal basis from any basis.
- QR factorization
- A = QR with orthonormal columns in Q and upper triangular R.
- Least squares solution
- The x minimizing |b − Ax|.
- Normal equations
- AᵀA x̂ = Aᵀb.
- Residual
- The difference between observed and fitted values.
- Overfitting
- Fitting noise by using too many parameters.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Are (1, 2, −1) and (3, −1, 1) orthogonal?
Show answer
Yes: 3 − 2 − 1 = 0.
Project (2, 3) onto the line through (1, 1).
Show answer
(2.5, 2.5).
Distance from (2, 3) to that line?
Show answer
|(−0.5, 0.5)| = 0.707.
Apply Gram-Schmidt to (1, 0, 1) and (1, 1, 0).
Show answer
u₂ = (1, 1, 0) − ½(1, 0, 1) = (½, 1, −½).
Why are orthogonal non-zero vectors independent?
Show answer
Dotting a zero combination with each vector shows each weight is zero.
Fit y = c₀ + c₁t to (0, 1), (1, 3), (2, 4).
Show answer
c₁ = 1.5, c₀ = 7/6 ≈ 1.17.
What columns does A have for fitting y = c₀ + c₁t + c₂t²?
Show answer
1, tᵢ and tᵢ².
How can y = Ae^(kt) be fitted by linear least squares?
Show answer
Fit ln y = ln A + kt.
What does a curved pattern in residuals suggest?
Show answer
The model is missing curvature (e.g., a quadratic term).
Why is QR preferred to the normal equations for ill-conditioned problems?
Show answer
Forming AᵀA squares the condition number.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.