Data fitting and optimization for machine learning · Lecture 23

Least Squares and Linear Regression

When there are more data points than unknowns, no curve passes through all of them. Least squares picks the curve that makes the total squared error smallest: the normal equations, their matrix form, how to judge a fit, and where the method breaks.

Md Mahbub E Noor · Assistant Professor
CSE-3107 Numerical Methods · B.Sc. 3rd Year 1st Semester
Department of CSE, University of Barishal

Why this lecture

The problem: predicting a delivery time

A food-delivery company records six orders: distance xx (km) and delivery time yy (minutes). How long will a 15 km order take?

  • The points lie almost on a line, but no line passes through all six
  • Interpolation (Lectures 16–18) forces a curve through every point: degree 5 here
  • That curve predicts 40.4 min at 12 km and 32.9 min at 13 km: a longer trip that is faster. Nonsense
  • Data have noise; we want the trend, not every wiggle
distance xx (km)time yy (min)
214
421
524
730
937
1142
15?
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
2 / 24

Today's goals

  • Define the residual and the least-squares criterion
  • Derive and solve the normal equations for a straight line
  • Write least squares in matrix form ATAc=ATyA^{T}A\mathbf{c} = A^{T}\mathbf{y} and fit models with several inputs
  • Judge a fit with SS, R2R^2 and the residual plot
  • Recognise when least squares misleads: outliers, extrapolation, ill-conditioning
Last time

Interpolation: a polynomial through every data point (Lectures 16–18); solving linear systems (Lectures 12–13).


Today

A simple curve that passes close to all points, chosen by minimising the sum of squared errors.

Data fitting and optimization for machine learning · you are hereBuilds on: L5, L13
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
3 / 24
Start here

The idea in one picture

xy24681012this line?or this one?residual = missbest line: total area of the squares is smallest
Measured data never lie exactly on a line.Many lines look reasonable. Which one is "best"?We need a rule to score a line.Take a candidate line.For each dot, measure the vertical miss (the residual).Square each miss and add them up. Least squares picks the line with the smallest total.
In plain words
  • Real data are noisy: no line passes through every point
  • Score a line by its misses, squared and added
  • The line with the smallest score is the least-squares line
New words today
residual
measured y − y predicted by the line
least squares
make the sum of squared residuals smallest
normal equations
the small linear system that gives a and b
regression
fitting a model to data
QR factorisation
a safer way to solve them (A = QR)
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
4 / 24

Working principle: make the squared residuals as small as possible

Model, and the residual of point i (vertical distance from the line)y^=a+bx,ei=yi−(a+bxi)\hat y = a + bx, \qquad e_i = y_i - (a + bx_i)
Least-squares criterion: choose a and b to minimiseS(a,b)=∑i=1nei2=∑i=1n(yi−a−bxi)2S(a, b) = \sum_{i=1}^{n} e_i^2 = \sum_{i=1}^{n} \big(y_i - a - bx_i\big)^2
  • nn equations a+bxi=yia + bx_i = y_i, only 2 unknowns: overdetermined, no exact solution
  • Why squares? Positive and negative errors cannot cancel, big errors count more, and SS is smooth, so calculus finds its minimum
  • Sum of ∣ei∣|e_i| is more robust but has no simple formula
  • Gauss (1795) and Legendre (1805) invented it to fit planetary orbits
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
5 / 24

The normal equations for a straight line

Set both partial derivatives of S to zero∂S∂a=−2∑(yi−a−bxi)=0,∂S∂b=−2∑xi(yi−a−bxi)=0\frac{\partial S}{\partial a} = -2\sum (y_i - a - bx_i) = 0, \quad \frac{\partial S}{\partial b} = -2\sum x_i(y_i - a - bx_i) = 0
Normal equations: a 2 × 2 linear systemna+b∑xi=∑yia∑xi+b∑xi2=∑xiyi\begin{aligned} na + b\textstyle\sum x_i &= \textstyle\sum y_i \\ a\textstyle\sum x_i + b\textstyle\sum x_i^2 &= \textstyle\sum x_iy_i \end{aligned}
Solutionb=n∑xiyi−∑xi∑yin∑xi2−(∑xi)2,a=yˉ−b xˉb = \frac{n\sum x_iy_i - \sum x_i\sum y_i}{n\sum x_i^2 - (\sum x_i)^2}, \qquad a = \bar y - b\,\bar x
  • Only four sums are needed: ∑x\sum x, ∑y\sum y, ∑x2\sum x^2, ∑xy\sum xy
  • The fitted line always passes through the mean point (xˉ,yˉ)(\bar x, \bar y)
  • The denominator is zero only if all xix_i are equal
  • SS is a convex bowl in (a,b)(a, b): this stationary point is the minimum
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
6 / 24
The method, step by step

Fitting a straight line, step by step

  1. Tabulate xix_i, yiy_i, xi2x_i^2, xiyix_iy_i and add each column
  2. Write the two normal equations with these sums
  3. Solve for bb, then a=(∑y−b∑x)/na = (\sum y - b\sum x)/n
  4. Compute the residuals ei=yi−a−bxie_i = y_i - a - bx_i and S=∑ei2S = \sum e_i^2
  5. Check: ∑ei≈0\sum e_i \approx 0; look at the residual pattern; predict y^=a+bx\hat y = a + bx

One pass over the data: O(n)O(n) work.

Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
7 / 24

Problem 1: the table of sums for the delivery data

Fit y=a+bxy = a + bx to the six orders. First build the table of sums.
iixix_iyiy_ixi2x_i^2xiyix_iy_i
1214428
24211684
352425120
473049210
593781333
61142121462
∑\sum381682961237
Normal equations

6a+38b=1686a + 38b = 168
38a+296b=123738a + 296b = 1237

n=6n = 6. Every number on the right comes from the bottom row of the table.

Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
8 / 24

Problem 1 (continued): solve, check and predict

1
D=n∑x2−(∑x)2=6(296)−382=1776−1444=332D = n\sum x^2 - (\sum x)^2 = 6(296) - 38^2 = 1776 - 1444 = 332
2
b=6(1237)−38(168)332=7422−6384332=1038332=3.126506b = \dfrac{6(1237) - 38(168)}{332} = \dfrac{7422 - 6384}{332} = \dfrac{1038}{332} = 3.126506
3
a=168−38(3.126506)6=168−118.80726=8.198795a = \dfrac{168 - 38(3.126506)}{6} = \dfrac{168 - 118.8072}{6} = 8.198795
4
Residuals: −0.45, 0.30, 0.17, −0.08, 0.66, −0.59-0.45,\ 0.30,\ 0.17,\ -0.08,\ 0.66,\ -0.59 (sum 0);   S=1.1145\;S = 1.1145
5
Prediction: y^(15)=8.1988+3.1265(15)=55.10\hat y(15) = 8.1988 + 3.1265(15) = 55.10 min
y=8.20+3.13xy = 8.20 + 3.13x: about 8 minutes to collect the order plus 3.1 minutes per km. R2=0.998R^2 = 0.998.
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
9 / 24

Explore: edit the data, watch the best line and its residuals

  • Red dashed segments are the residuals; the line minimises the sum of their squares
  • Change one yy value and see the whole line move
  • Outlier preset: one wrong value tilts the line from slope 3.1 to 5.5
  • Curved data: a line gives a pattern in the residuals (+, −, −, −, −, +)
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
10 / 24

Matrix form: any model that is linear in its coefficients

n equations, k unknown coefficients (n > k): A c ≈ y[1x11x2⋮⋮1xn]⏟A  (n×2)[ab]≈[y1y2⋮yn]\underbrace{\begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ \vdots & \vdots \\ 1 & x_n \end{bmatrix}}_{A\;(n\times 2)} \begin{bmatrix} a \\ b \end{bmatrix} \approx \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{bmatrix}
Minimise ‖y − Ac‖²: the normal equationsATA c=ATyA^{T}A\,\mathbf{c} = A^{T}\mathbf{y}
  • ATAA^{T}A is k×kk \times k, symmetric and positive definite: solve by Cholesky (Lecture 13)
  • Geometry: AcA\mathbf{c} is the projection of y\mathbf{y} onto the columns of AA; the residual is perpendicular to every column
  • More inputs = more columns: y=β0+β1x1+β2x2y = \beta_0 + \beta_1x_1 + \beta_2x_2 (multiple regression)
  • Columns may be any functions of xx: x2x^2, cos⁡x\cos x, … (Lecture 24)
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
11 / 24

Problem 2: multiple regression for house prices

Five flats: size x1x_1 (m²) = 50, 70, 80, 100, 120; rooms x2x_2 = 1, 2, 2, 3, 3; price yy (lakh Tk) = 30, 42, 47, 60, 68. Fit y=β0+β1x1+β2x2y = \beta_0 + \beta_1x_1 + \beta_2x_2.
1
Rows of AA: (1,x1,x2)(1, x_1, x_2), e.g. (1,50,1)(1, 50, 1); five rows, three columns
2
ATA=[54201142038200101011101027],ATy=[24722360592]A^{T}A = \begin{bmatrix} 5 & 420 & 11 \\ 420 & 38200 & 1010 \\ 11 & 1010 & 27 \end{bmatrix}, \quad A^{T}\mathbf{y} = \begin{bmatrix} 247 \\ 22360 \\ 592 \end{bmatrix}
3
Solve the 3 × 3 system by Gaussian elimination or Cholesky
4
β0=4.179\beta_0 = 4.179,   β1=0.4282\;\beta_1 = 0.4282 per m²,   β2=4.205\;\beta_2 = 4.205 per room
Price ≈4.18+0.428x1+4.21x2\approx 4.18 + 0.428x_1 + 4.21x_2; a 90 m², 3-room flat: 55.3 lakh Tk. R2=0.9994R^2 = 0.9994.
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
12 / 24

How good is the fit?

Residual sum of squares, and its spread around the meanS=∑ei2,St=∑(yi−yˉ)2S = \sum e_i^2, \qquad S_t = \sum (y_i - \bar y)^2
Coefficient of determination and standard error (k coefficients)R2=1−SSt,s=Sn−kR^2 = 1 - \frac{S}{S_t}, \qquad s = \sqrt{\frac{S}{n - k}}
  • R2R^2: fraction of the variation in yy explained by the model (0≤R2≤10 \le R^2 \le 1)
  • Delivery data: R2=0.998R^2 = 0.998, s=0.53s = 0.53 min: typical prediction error about half a minute
  • High R2R^2 is not proof: curved data fitted with a line still gives R2=0.93R^2 = 0.93
  • Always plot the residuals: random scatter = good model; a pattern = wrong model
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
13 / 24
Classical vs numerical

Same question, two approaches

Algebra

Classical (hand formula)

  • Straight line: two sums formulas for aa and bb
  • Delivery data: y=8.20+3.13xy = 8.20 + 3.13x in five minutes
  • One input variable, small nn
vs
Algorithm

Numerical (linear algebra)

  • Build AA, solve ATAc=ATyA^{T}A\mathbf{c} = A^{T}\mathbf{y} or use QR
  • Any number of inputs and basis functions
  • Same code for 6 points or 6 million
When the classical approach cannot work

Predicting a house price from 80 features and 1.5 million sales

There is no hand formula for 81 coefficients. Numerically: an 81 × 81 system ATAc=ATyA^{T}A\mathbf{c} = A^{T}\mathbf{y}, or a QR factorisation of AA, solved in well under a second. This is linear regression as used in industry.

Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
14 / 24

Linear regression in code

  • Accumulates the four sums in one loop
  • Solves the normal equations; prints aa, bb
  • Residuals, SS, R2R^2 and a prediction
  • Lab: multiple regression with Gaussian elimination
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
15 / 24
When least squares misleads

Limitations shown failing

  • Outliers: squaring gives a wrong point huge weight; one typo changes the 15 km prediction from 55 to 80 min
  • Extrapolation: the line says nothing about traffic beyond 11 km
  • Wrong model: a line through curved data leaves a residual pattern
  • Ill-conditioning: forming ATAA^{T}A squares the condition number (next slide)
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
16 / 24
When least squares misleads

Limitation: the normal equations square the condition number

Fit sales against the year x=2015,2016,…,2020x = 2015, 2016, \dots, 2020. The columns of AA are (1,…,1)(1, \dots, 1) and (2015,…,2020)(2015, \dots, 2020): almost parallel.
matrixcondition numberdigits lost (of 16)
AA (years as given)2.4×1062.4\times10^{6}about 6
ATAA^{T}A (normal equations)5.7×10125.7\times10^{12}about 13
ATAA^{T}A with centred x−2017.5x - 2017.52.9none

Fixes: centre and scale the inputs, or solve by QR (A=QRA = QR, then Rc=QTyR\mathbf{c} = Q^{T}\mathbf{y}), which works with cond(AA) instead of its square. Library routines (numpy.linalg.lstsq) use QR or the SVD.

Why centring helps

Years as given:

ATA=[6121051210524421855]A^{T}A = \begin{bmatrix} 6 & 12105 \\ 12105 & 24421855 \end{bmatrix}

determinant 105105 out of numbers near 10810^{8}: nearly singular.

Centred, x−2017.5=−2.5,…,2.5x - 2017.5 = -2.5, \dots, 2.5:

ATA=[60017.5]A^{T}A = \begin{bmatrix} 6 & 0 \\ 0 & 17.5 \end{bmatrix}

Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
17 / 24
Practice

Practice problem

Problem 3"Fit a straight line y=a+bxy = a + bx to the data xx: 1, 2, 3, 4, 5 and yy: 3, 5, 6, 9, 11 by the method of least squares. Estimate yy at x=6x = 6."
How to write a complete answer
  • Make the table: ∑x=15\sum x = 15, ∑y=34\sum y = 34, ∑x2=55\sum x^2 = 55, ∑xy=122\sum xy = 122
  • Normal equations: 5a+15b=345a + 15b = 34,   15a+55b=122\;15a + 55b = 122
  • Solve: b=5(122)−15(34)5(55)−225=10050=2b = \dfrac{5(122) - 15(34)}{5(55) - 225} = \dfrac{100}{50} = 2,   a=34−305=0.8\;a = \dfrac{34 - 30}{5} = 0.8
  • Answer y=0.8+2xy = 0.8 + 2x;   y(6)=12.8\;y(6) = 12.8; residuals 0.2,0.2,−0.8,0.2,0.20.2, 0.2, -0.8, 0.2, 0.2 add to 0 (a quick check)
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
18 / 24
When to use it

Linear least squares: method profile

Best case
  • Many noisy measurements, a model linear in its coefficients
  • Errors roughly equal in size and independent (then least squares is statistically optimal)
  • Closed-form answer: one linear solve, no iteration
Worst case
  • Outliers or heavy-tailed errors
  • Nearly parallel columns (ill-conditioning)
  • Models nonlinear in the parameters, e.g. aebxae^{bx} fitted directly
Best suited for
  • Calibration of sensors and instruments
  • Trend lines in economics and science
  • Linear regression in machine learning (the first model to try)
Avoid when
  • Exact data with no noise (interpolate instead)
  • Predictions far outside the data range
  • Data with gross errors (use robust regression)

Rule of thumb: fit, then look at the residuals before trusting R2R^2; solve by QR, not by forming ATAA^{T}A, when columns are nearly dependent.

Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
19 / 24

Comparative analysis: interpolation vs least squares, and ways to solve it

ApproachPasses through points?Work (nn points, kk coefficients)AccuracyUse when
Interpolation (L16–18)yes, allO(n2)O(n^2)exact at datadata exact, few points
Least-squares line by sumsnoO(n)O(n)sums may cancelone input, by hand
Normal equations + Choleskynonk2+k3/3nk^2 + k^3/3loses log⁡10cond(A)2\log_{10}\text{cond}(A)^2 digitswell-conditioned, n≫kn \gg k
QR factorisationno2nk22nk^2loses log⁡10cond(A)\log_{10}\text{cond}(A) digitsdefault in libraries
Gradient descent (L25)noper pass O(nk)O(nk)iterativehuge nn and kk

Interpolation trusts every point; least squares trusts the trend. With noisy data, least squares predicts better.

Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
20 / 24
Roadmap

Limitations, and where we fix them

Least squares is the foundation of data fitting. Its weak points lead to the last two lectures.

What it does well (keep this)
  • Closed form: one linear solve
  • Works for any model linear in its coefficients
  • Statistical meaning: best fit under random errors
  • Matrix form scales to many inputs
LimitationHow it is overcomeWhere
Data follow a curve, not a line
→
Polynomial least squares (more columns)
Lecture 24 →
Growth and power laws: aebxae^{bx}, axbax^b
→
Linearise with logarithms
Lecture 24 →
Too many coefficients fit the noise
→
Overfitting; choose the degree by test error
Lecture 24 →
Parameters inside a nonlinear model
→
Gauss–Newton iteration
Lecture 25 →
Millions of rows or features; robust losses
→
Gradient descent on SS
Lecture 25 →
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
21 / 24
Applications

Where this method runs in the real world

Click a card to see least squares at work.

Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
22 / 24

Checkpoint

Checkpoint · think, then discuss with your neighbourFor the points (0,1)(0, 1), (1,3)(1, 3), (2,4)(2, 4), what is the slope bb of the least-squares line?
A1.51.5
B1.56‾1.5\overline{6}
C22
D1.251.25
A. n=3n = 3, ∑x=3\sum x = 3, ∑y=8\sum y = 8, ∑x2=5\sum x^2 = 5, ∑xy=11\sum xy = 11:   b=3(11)−3(8)3(5)−9=96=1.5\;b = \dfrac{3(11) - 3(8)}{3(5) - 9} = \dfrac{9}{6} = 1.5,   a=8−4.53=1.16‾\;a = \dfrac{8 - 4.5}{3} = 1.1\overline{6}.
Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
23 / 24

Summary

Key points
  • Residual ei=yi−y^ie_i = y_i - \hat y_i; least squares minimises S=∑ei2S = \sum e_i^2
  • Line: na+b∑x=∑yna + b\sum x = \sum y,   a∑x+b∑x2=∑xy\;a\sum x + b\sum x^2 = \sum xy
  • Matrix form ATAc=ATyA^{T}A\mathbf{c} = A^{T}\mathbf{y}; any number of inputs; QR when ill-conditioned
  • Judge with R2R^2, ss and the residual plot; beware outliers and extrapolation
Opening problem: solved

Least squares gives y=8.20+3.13xy = 8.20 + 3.13x (R2=0.998R^2 = 0.998), so a 15 km order takes about 55 minutes, an extrapolation to be checked with new data. The degree-5 interpolant's 32.9 min at 13 km shows why we do not interpolate noisy data.

Homework
  1. Fit y=a+bxy = a + bx to xx: 0, 1, 2, 3 and yy: 1.1, 2.9, 5.2, 6.8. Find SS and R2R^2.
  2. A spring: FF = 0, 1, 2, 3, 4 N, LL = 10.0, 10.9, 12.1, 13.0, 13.9 cm. Fit L=L0+cFL = L_0 + cF and find the length at 2.5 N.
  3. Show that the least-squares line passes through (xˉ,yˉ)(\bar x, \bar y) and that ∑ei=0\sum e_i = 0.
  4. Write AA, ATAA^{T}A and ATyA^{T}\mathbf{y} for fitting y=β0+β1x1+β2x2y = \beta_0 + \beta_1x_1 + \beta_2x_2 to four data points of your choice, and solve it in the lab.
Next lecture

Lecture 24: polynomial, exponential and trigonometric fitting. Curved trends, growth laws, periodic data, and the danger of too many coefficients.

Md Mahbub E Noor
Lecture 23: Least squares and linear regression
University of Barishal
24 / 24