Least Squares

Summary

Linear least squares chooses parameters of a model that is linear in those parameters by minimizing the sum of squared residuals. For a straight line, the normal equations give a unique solution when the xi are not all equal.

Prerequisites

Problem Type

Given data (xi,yi)i=1n , fit f(x)=a+bx (or a polynomial linear in coefficients) by minimizing

E(a,b)=i=1n(yi(a+bxi))2.

Method Definition

Set E/a=0 and E/b=0 to obtain the normal equations:[1]

{na+bxi=yiaxi+bxi2=xiyi

Matrix form: with design matrix

A=(1x11xn),a^=(ab),y=(y1yn), AAa^=Ay.

Prefer solving minAa^y2 via QR rather than forming AA when n is large or columns are poorly scaled.

Assumptions / Requirements

Algorithm

  1. Compute sums n , x , y , x2 , xy .
  2. Solve the 2×2 normal system for (a,b) .
  3. Report residuals ri=yi(a+bxi) and E=ri2 .

Worked Example 1 — three points

Data: (1,2) , (2,3) , (3,5) .

n=3, x=6, y=10, x2=14, xy=23. {3a+6b=106a+14b=23

Multiply the first equation by 2: 6a+12b=20 . Subtract from the second: 2b=3b=3/2 . Then 3a+6(3/2)=103a+9=10a=1/3 .

Fitted line:

f(x)=13+32x.

(Intercept 1/3 , slope 1.5 not intercept 0.5 .)

Worked Example 2 — four points

Data: (1,2) , (2,3) , (3,5) , (4,4) .

n=4, x=10, y=14, x2=30, xy=39. {4a+10b=1410a+30b=39

From the first equation, 2a+5b=7 , so a=(75b)/2 . Substitute into the second:

1075b2+30b=395(75b)+30b=3935+5b=39b=0.8.

Then 4a+8=14a=1.5 . Fitted line: f(x)=1.5+0.8x .

Error / Accuracy

Minimizing E does not imply every residual is small. Always inspect residuals and, for inference, model assumptions.

Common Failure Modes

Connections

References


  1. Burden & Faires, Numerical Analysis, least squares; Golub & Van Loan, Matrix Computations; NIST/SEMATECH e-Handbook, https://www.itl.nist.gov/div898/handbook/ ↩︎