Multivariable-Calculus Β· Unit 8 Β· Video 4 Β· Interactive Practice
Least Squares: Finding the Best Line Means Minimizing a Function of a and b
IKey Formulas
Formula
Name
Meaning
yiββ(axiβ+b)
Vertical deviation
measured y minus the y the line predicts at xiβ
D(a,b)=βi=1nβ(yiββ(axiβ+b))2
Total square deviation
one number for each line; data xiβ,yiβ fixed
D(a,b)=(1βb)2+(3βaβb)2+(4β2aβb)2
Video example: (0,1),(1,3),(2,4)
D(1,1)=2, Β D(2,1)=1
βaβDβ=0,βbβDβ=0
Critical point of D
where the best a and b must lie
Key Insight: The unknowns are the coefficients a and b, not x and y: once the measured xiβ,yiβ are plugged in, D is a function of a and b alone, and the best-fit line is the point (a,b) where D is smallest.
IIVisualization 1 β Every Line Is a Point (a,b)
Each choice of (a,b) is one line through the fixed data, and D(a,b) scores it with one number.
π‘ Challenge: get D below 1, the better of the video's two lines. Finding the exact minimum, by solving βaβDβ=βbβDβ=0, is the next video.
IIIVisualization 2 β Vertical Deviations Survive a Change of Units
Measuring x in tenths of a second instead of seconds moves the perpendicular-distance best line but not the vertical one.
IVVisualization 3 β Largest, Average or Squared Deviation?
One bad measurement pulls hardest on the line that minimizes the largest deviation.
VQuiz Questions
Problem 1 Β· Total Square Deviation of One Line
Given: data points (1,4), (2,2), (3,3) and the line y=x+1 (so a=1, b=1) β findD(1,1)=β(yiββ(axiβ+b))2.
β Correct! The deviations are 2, β1, β1, and their squares add to 4+1+1=6.
β Not quite. The signed deviations 2β1β1 do add to 0; that cancellation is exactly why each deviation is squared before adding.
β Not quite.4=2+1+1 is the sum of the sizes of the deviations; least squares adds their squares.
β Not quite.16=(2+1+1)2 squares the total; square each deviation first, then add.
β Not quite. Compute each deviation yiββ(xiβ+1), square it, then add.
Show solution
The line predicts xiβ+1, that is 2, 3, 4 at x=1,2,3. The deviations (measured minus predicted) are
The signed deviations add to 0, which is why the raw sum is useless: points above and below the line cancel.
Problem 2 Β· What Are the Unknowns?
Given: to fit y=ax+b to measured data, a student minimizes D(a,b)=βi=1nβ(yiββ(axiβ+b))2 by setting βxβDβ=0 and βyβDβ=0. Which statement is right?
β Correct!D is a function of a and b alone, so its critical point solves βaβDβ=βbβDβ=0.
β Not quite.x and y are not solved for: their values xiβ,yiβ are given as data. The unknowns are the slope a and intercept b.
β Not quite. The xiβ and yiβ are measured numbers; we cannot move them. Only a and b are free.
β Not quite.D(a,b)=0 would need every point exactly on one line, which real data almost never allows; we minimize D instead.
Show solution
Once the measurements are plugged in, every xiβ and yiβ in D(a,b)=β(yiββ(axiβ+b))2 is a number. What remains free is the pair of coefficients describing the line, so D is a function of two variables, a and b.
A minimum of a function of two variables is a critical point, where both partial derivatives vanish:
βaβDβ=0,βbβDβ=0.
Setting D=0 asks for a line through every point, which generally does not exist; the least-squares line makes D as small as possible, not zero.
Problem 3 Β· Expand the Video's D(a, b)
Given: the video's data (0,1), (1,3), (2,4), for which D(a,b)=(1βb)2+(3βaβb)2+(4β2aβb)2 β expandD as a polynomial in a and b.
What is the coefficient of a2?
What is the constant term?
β Correct!D(a,b)=5a2+6ab+3b2β22aβ16b+26, and it reproduces D(1,1)=2 and D(2,1)=1.
β Check the a2 coefficient. The a2 terms come from (βa)2 and (β2a)2; the first bracket has no a.
β Check the a2 coefficient.9=(0+1+2)2 squares the sum of the xiβ; each bracket contributes xi2β, so add the squares 02+12+22.
β Check the a2 coefficient.3 is the number of points, which is the coefficient of b2, not a2.
β Check the constant term. Set a=b=0: each bracket reduces to yiβ, so the constant is a sum of squares of the yiβ.
β Check the constant term.64=(1+3+4)2 squares the total; the constant is 12+32+42.
In general the coefficient of a2 is βxi2β=0+1+4=5 and the constant is βyi2β=1+9+16=26.
Check:D(1,1)=5+6+3β22β16+26=2 and D(2,1)=20+12+3β44β16+26=1, matching the video.
Problem 4 Β· A Line Through the Origin
Given: data (1,3), (2,5), (3,5) and a model y=ax forced through the origin, so D(a)=β(yiββaxiβ)2 has a single unknown β find the a that minimizes D, and the minimum value.
Which a minimizes D(a)?
What is the minimum value of D?
β Correct!Dβ²(a)=0 gives a=βxi2ββxiβyiββ=1428β=2, and the deviations 1, 1, β1 give D(2)=3.
β Check a. Differentiate: Dβ²(a)=β2βxiβ(yiββaxiβ), and solve Dβ²(a)=0.
β Check a.613β=βxiββyiββ is a natural guess, but it is not where Dβ²(a)=0: it gives D=1861ββ3.39.
β Check a.35β is the slope to the last point alone; the least-squares a uses all three.
β Check the minimum. Evaluate D at the minimizing a: compute each deviation yiββaxiβ, square, add.
β Check the minimum.D=0 would need all three points on one line through the origin; 3/1ξ =5/2, so they are not.
Show solution
With one unknown, this is one-variable minimization: