Linear Algebra I · Least squares and fitting data

Prerequisites: matrix products and transpose, column spaces, rank and nullspace, solving linear systems, and the preceding orthogonal-projection lesson. All vectors and coefficients here are real. Goals: build a design matrix, minimize squared residuals, derive and check the normal equations, identify when coefficients are unique, and interpret a fit without overclaiming what the data establish. Work through the examples, complete the lab, then attempt practice and the open-book assessment before opening their solutions.

From inconsistent equations to a fitting objective

A straight-line model predicts ŷᵢ=β₀+β₁xᵢ. Given fixed input coordinates xᵢ and measured responses yᵢ, put a row [1,xᵢ] in the design matrix A, the coefficients in β=(β₀,β₁), and observations in y. The predicted observation vector is Aβ. There may be no β with Aβ=y, because more measurements can impose incompatible conditions. Ordinary least squares chooses β to minimize J(β)=||y−Aβ||²=∑ᵢ(yᵢ−β₀−β₁xᵢ)². We define residual r=y−Aβ throughout; some references use its negative, which gives the same squared objective.

This objective treats response errors equally, with x fixed. Residuals on a data plot are vertical differences, not shortest geometric distances from points to the plotted line. In observation space ℝᵐ, however, Aβ is a point in col(A) and the optimal residual is perpendicular to that whole subspace. Distinguish these two pictures. A line fit is affine in the plotted input x but linear in the unknown coefficient vector β.

Normal equations and the uniqueness condition

At the optimum, each column of A is perpendicular to r=y−Aβ̂, so Aᵀr=0 and AᵀAβ̂=Aᵀy. These are the normal equations. If A is m×n with independent columns, then for any nonzero z, zᵀAᵀAz=||Az||²>0. Thus AᵀA has no nonzero kernel vector and is invertible; the coefficient vector β̂ is unique. Having m>n alone does not ensure independence. If columns are dependent, the fitted vector p=Aβ̂ remains the unique projection of y onto col(A), but several coefficient vectors can describe that same fit. Do not apply (AᵀA)⁻¹ when A lacks full column rank.

Why the normal equations minimize the error

Suppose β̂ satisfies the normal equations. For any coefficient change δ, write y−A(β̂+δ)=r−Aδ. Since Aᵀr=0, the cross term vanishes and J(β̂+δ)=||r||²+||Aδ||²≥J(β̂). This proves global minimality without relying on a picture or a stationary-point guess. With independent columns, equality forces δ=0, proving unique coefficients. With dependent columns, every δ in null(A) keeps the fitted vector and objective unchanged. In the full-column-rank case, P=A(AᵀA)⁻¹Aᵀ maps y to p and satisfies Pᵀ=P and P²=P, extending the previous line-projection formula.

012xy(0,1)(1,2)(2,4)ŷ = 5/6 + (3/2)x
Original diagram: ordinary line fitting minimizes the sum of squared vertical residuals. The residual vector is (1/6,−1/3,1/6), and its squared length is 1/6. Perpendicularity refers to the design-matrix columns in observation space, not to these drawn vertical segments and the fitted line.

Worked examples

  1. Constant fit: approximate responses (1,2,6) by c(1,1,1). The normal equation is 3c=9, so c=3. Residual (−2,−1,3) sums to zero and SSE=4+1+9=14. The fitted constant is the mean; the residual need not vanish.
  2. Fit the diagram’s data: A=[1 0; 1 1; 1 2], y=(1,2,4). Then AᵀA=[3 3; 3 5] and Aᵀy=(7,10). Equations 3β₀+3β₁=7 and 3β₀+5β₁=10 give 2β₁=3, hence β₁=3/2 and β₀=5/6. Fitted values are (5/6,7/3,23/6), residuals (1/6,−1/3,1/6), and SSE=1/6. Both residual sum and x-weighted residual sum are zero.
  3. Check a competing line: coefficients (1,1) predict (1,2,3), giving residual (0,0,1) and SSE=1. Since Aᵀr=(1,2)≠0, it is not optimal. The coefficient change from the optimum is δ=(1/6,−1/2), so Aδ=(1/6,−1/3,−5/6) has squared norm 5/6. The error identity gives 1=1/6+5/6.
  4. Dependent columns: suppose all three rows of A are [1,1] and y=(1,2,3). Only β₀+β₁ affects predictions. Every pair with β₀+β₁=2 produces the unique fitted vector (2,2,2), residual (−1,0,1), and minimum SSE=2. Coefficients are not unique and AᵀA is singular, even though there are more rows than columns.

Common mistakes and model limits

Aᵀr=0 does not imply r=0; the system can remain inconsistent. With an intercept column, residuals sum to zero, but this one condition alone is insufficient when other columns are present. Fit quality is not proof of causation or safe extrapolation. Squaring errors makes large residuals influential; an outlier can strongly move a fit. Record units and use the same response scale across observations. The small exact-arithmetic lab illustrates normal equations; it is not a recommendation to explicitly invert AᵀA for large floating-point problems. Orthogonal methods such as QR are a subsequent numerical topic.

Practice

  1. Warm-up: fit one constant to y=(2,4,6). Find the residual and SSE.
  2. At x=0,1,2, fit y=(2,4,6) using an intercept and slope. Is the fit exact?
  3. For x=0,1,2 and y=(0,1,0), find the best line and compare its SSE with the zero line. Check both normal-equation residual sums.
  4. Challenge: for y=(1,2,4), a constant prediction (7/3,7/3,7/3) has residuals summing to zero. Why is it not the optimal intercept-and-slope fit?
Fully worked solutions
  1. The mean is 4, so residual (−2,0,2) and SSE=4+0+4=8. Its sum is zero, the normal equation for a constant-only model.
  2. β₀=2, β₁=2 predicts all three responses, so r=0 and SSE=0. A zero squared norm is the smallest possible objective.
  3. Aᵀy=(1,1). Subtracting normal equations gives 2β₁=0; then 3β₀=1. Fit (1/3,1/3,1/3), residual (−1/3,2/3,−1/3), SSE=2/3. Both residual sum and r₁+2r₂ vanish, with indices 0,1,2. The zero line has SSE=1.
  4. Residual (−4/3,−1/3,5/3) sums to zero but its x-weighted sum is −1/3+2·5/3=3. Thus the slope column is not perpendicular to the residual and the full normal equations fail.

Assessment · Open book · 18 points

Open book: use books, lesson notes, cited readings, calculators, and relevant free software. Explain your own reasoning, assumptions, units, and checks; identify references or software you use. Attempt the questions before opening the answer key. Suggested study time: 40–60 minutes; this is a self-study guide, not a timed restriction.

Solve independently before opening the rubric. Use x=0,1,2 for the data-fitting question.

  1. Fit y=(0,2,3) with an intercept and slope. Give A, normal equations, coefficients, fitted values, residual, and SSE. (6)
  2. Starting from Aᵀr=0, prove that no coefficient change δ can reduce SSE. State when equality forces δ=0. (3)
  3. If a 3×2 design matrix has two identical nonzero columns, are fitted values unique? Are coefficients unique? Can (AᵀA)⁻¹ be used? Explain. (3)

Application and reasoning · 6 points

Fit an intercept and slope to y=(1,0,1) at x=0,1,2 (2). A reviewer proposes coefficients (1,0); use residual orthogonality to accept or reject them (2). Explain and compute the SSE penalty using the coefficient-change identity (2).

Complete answer key and scoring rubric
  1. A=[1 0;1 1;1 2] (1). Normal equations 3β₀+3β₁=5 and 3β₀+5β₁=8 (2). Thus β₁=3/2, β₀=1/6 (1). Fit (1/6,5/3,19/6) and residual (−1/6,1/3,−1/6) (1). SSE=1/6 (1).
  2. Write the new residual as r−Aδ (1). The cross term rᵀAδ=(Aᵀr)ᵀδ=0, so the new SSE is ||r||²+||Aδ||² (1). The added term is nonnegative; full column rank makes equality require δ=0 (1).
  3. Fitted values are the unique projection onto the column space (1). Coefficients are not unique because (1,−1) is in null(A) (1). AᵀA is singular, so its inverse formula cannot be used (1).

Application and reasoning · 6 points

Normal equations give coefficients (2/3,0), residual (1/3,−2/3,1/3), SSE=2/3 (2). Trial residual (0,−1,0) gives Aᵀr=(−1,−1), so it is not optimal (1 each). δ=(1/3,0), Aδ=(1/3,1/3,1/3), so excess ||Aδ||²=1/3 and trial SSE=1 (1 identity, 1 result).

Scoring: existing questions 12 + application problem 6 = 18 points. Award the stated method and reasoning marks, not only final answers.

Download the data-fitting lab

Download the exact-arithmetic Python lab

Objective: fit three synthetic responses at x=0,1,2 and compare the optimum against a trial intercept and slope. Install free Python 3 for Windows, macOS, or Linux from the official link below. Save the file, open a terminal in its folder, and run the commands; Windows users can use py -3 instead of python3. The file needs no account or extra packages and runs offline after installation. Five arguments are y₀,y₁,y₂,trial intercept,trial slope, each from −4 to 6; negative fractions are accepted.

python3 ee-least-squares.py 1 2 4 1 1
python3 ee-least-squares.py 2 4 6 2 2
python3 ee-least-squares.py 0 1 0 0 0
python3 ee-least-squares.py 2 2 2 2 0

Procedure: build A and AᵀA on paper, predict coefficients and SSE, then run each case and repeat it in the browser lab. Expected (intercept,slope), best SSE, trial SSE are: (5/6,3/2),1/6,1; (2,2),0,0; (1/3,0),2/3,1; and (2,0),0,0. The script checks both residual orthogonality sums and trial SSE−best SSE=||Aδ||² exactly. Why can the trial never improve on the optimum? The excess is a squared norm. Does zero SSE prove the physical law? No: these are synthetic observations and an exact fit alone neither establishes causation nor validates extrapolation. Physical calibration, uncertainty estimation, and collecting reliable measurements remain separate skills.

Free primary readings and software

Read MIT’s lecture summary for projection and normal equations. Stanford’s least-squares slides pp. 2–9 cover the objective, orthogonality, and error identity; its data-fitting slides pp. 2–4 show how basis functions form a design matrix. The materials are public and free without institutional login. No paid textbook or numerical package is required for this lab.

Fitting lab · Compare the optimum with a trial line

Fit three responses at x=0,1,2, check Aᵀr=0, and compare SSE.

Free tool built into this page. Use it in your browser without an account or download.

Procedure

Try (y0,y1,y2,trial intercept,trial slope)=(1,2,4,1,1), (2,4,6,2,2), (0,1,0,0,0), and (2,2,2,2,0). Predict both coefficients and squared errors before running. Compare with the downloaded Python lab.

Expected results and answer check

Expected (intercept,slope), best SSE, trial SSE: (5/6,3/2),1/6,1; (2,2),0,0; (1/3,0),2/3,1; (2,0),0,0. Both Aᵀr components are zero. The excess equals ||Aδ||²≥0. Zero SSE is an exact fit to these synthetic points, not proof of a physical law.

The browser rounds results; Python uses exact fractions. Input coordinates are fixed at 0,1,2 and response errors have equal weight. This model does not estimate sensor uncertainty, account for uncertain x, or validate extrapolation.