Multivariable-Calculus Β· Unit 8 Β· Video 4 Β· Interactive Practice

Least Squares: Finding the Best Line Means Minimizing a Function of a and b

IKey Formulas

FormulaNameMeaning
yiβˆ’(axi+b)y_i - (ax_i + b)Vertical deviationmeasured yy minus the yy the line predicts at xix_i
D(a,b)=βˆ‘i=1n(yiβˆ’(axi+b))2D(a, b) = \sum_{i=1}^{n} \big(y_i - (ax_i + b)\big)^2Total square deviationone number for each line; data xi,yix_i, y_i fixed
D(a,b)=(1βˆ’b)2+(3βˆ’aβˆ’b)2+(4βˆ’2aβˆ’b)2D(a, b) = (1 - b)^2 + (3 - a - b)^2 + (4 - 2a - b)^2Video example: (0,1),(1,3),(2,4)(0, 1), (1, 3), (2, 4)D(1,1)=2D(1, 1) = 2, Β D(2,1)=1\ D(2, 1) = 1
βˆ‚Dβˆ‚a=0,βˆ‚Dβˆ‚b=0\frac{\partial D}{\partial a} = 0, \quad \frac{\partial D}{\partial b} = 0Critical point of DDwhere the best aa and bb must lie

Key Insight: The unknowns are the coefficients aa and bb, not xx and yy: once the measured xi,yix_i, y_i are plugged in, DD is a function of aa and bb alone, and the best-fit line is the point (a,b)(a, b) where DD is smallest.

IIVisualization 1 β€” Every Line Is a Point (a,b)(a, b)

Each choice of (a,b)(a, b) is one line through the fixed data, and D(a,b)D(a, b) scores it with one number.

πŸ’‘ Challenge: get DD below 11, the better of the video's two lines. Finding the exact minimum, by solving βˆ‚Dβˆ‚a=βˆ‚Dβˆ‚b=0\frac{\partial D}{\partial a} = \frac{\partial D}{\partial b} = 0, is the next video.

IIIVisualization 2 β€” Vertical Deviations Survive a Change of Units

Measuring xx 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)(1, 4), (2,2)(2, 2), (3,3)(3, 3) and the line y=x+1y = x + 1 (so a=1a = 1, b=1b = 1) β€” find D(1,1)=βˆ‘(yiβˆ’(axi+b))2D(1, 1) = \sum \big(y_i - (ax_i + b)\big)^2.

βœ… Correct! The deviations are 22, βˆ’1-1, βˆ’1-1, and their squares add to 4+1+1=64 + 1 + 1 = 6.
❌ Not quite. The signed deviations 2βˆ’1βˆ’12 - 1 - 1 do add to 00; that cancellation is exactly why each deviation is squared before adding.
❌ Not quite. 4=2+1+14 = 2 + 1 + 1 is the sum of the sizes of the deviations; least squares adds their squares.
❌ Not quite. 16=(2+1+1)216 = (2 + 1 + 1)^2 squares the total; square each deviation first, then add.
❌ Not quite. Compute each deviation yiβˆ’(xi+1)y_i - (x_i + 1), square it, then add.
Show solution

The line predicts xi+1x_i + 1, that is 22, 33, 44 at x=1,2,3x = 1, 2, 3. The deviations (measured minus predicted) are

4βˆ’2=2,2βˆ’3=βˆ’1,3βˆ’4=βˆ’1.4 - 2 = 2, \qquad 2 - 3 = -1, \qquad 3 - 4 = -1. D(1,1)=22+(βˆ’1)2+(βˆ’1)2=4+1+1=6.D(1, 1) = 2^2 + (-1)^2 + (-1)^2 = 4 + 1 + 1 = 6.

The signed deviations add to 00, 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+by = ax + b to measured data, a student minimizes D(a,b)=βˆ‘i=1n(yiβˆ’(axi+b))2D(a, b) = \sum_{i=1}^{n} \big(y_i - (ax_i + b)\big)^2 by setting βˆ‚Dβˆ‚x=0\frac{\partial D}{\partial x} = 0 and βˆ‚Dβˆ‚y=0\frac{\partial D}{\partial y} = 0. Which statement is right?

βœ… Correct! DD is a function of aa and bb alone, so its critical point solves βˆ‚Dβˆ‚a=βˆ‚Dβˆ‚b=0\frac{\partial D}{\partial a} = \frac{\partial D}{\partial b} = 0.
❌ Not quite. xx and yy are not solved for: their values xi,yix_i, y_i are given as data. The unknowns are the slope aa and intercept bb.
❌ Not quite. The xix_i and yiy_i are measured numbers; we cannot move them. Only aa and bb are free.
❌ Not quite. D(a,b)=0D(a, b) = 0 would need every point exactly on one line, which real data almost never allows; we minimize DD instead.
Show solution

Once the measurements are plugged in, every xix_i and yiy_i in D(a,b)=βˆ‘(yiβˆ’(axi+b))2D(a, b) = \sum \big(y_i - (ax_i + b)\big)^2 is a number. What remains free is the pair of coefficients describing the line, so DD is a function of two variables, aa and bb.

A minimum of a function of two variables is a critical point, where both partial derivatives vanish:

βˆ‚Dβˆ‚a=0,βˆ‚Dβˆ‚b=0.\frac{\partial D}{\partial a} = 0, \qquad \frac{\partial D}{\partial b} = 0.

Setting D=0D = 0 asks for a line through every point, which generally does not exist; the least-squares line makes DD as small as possible, not zero.

Problem 3 Β· Expand the Video's D(a, b)

Given: the video's data (0,1)(0, 1), (1,3)(1, 3), (2,4)(2, 4), for which D(a,b)=(1βˆ’b)2+(3βˆ’aβˆ’b)2+(4βˆ’2aβˆ’b)2D(a, b) = (1 - b)^2 + (3 - a - b)^2 + (4 - 2a - b)^2 β€” expand DD as a polynomial in aa and bb.

What is the coefficient of a2a^2?

What is the constant term?

βœ… Correct! D(a,b)=5a2+6ab+3b2βˆ’22aβˆ’16b+26D(a, b) = 5a^2 + 6ab + 3b^2 - 22a - 16b + 26, and it reproduces D(1,1)=2D(1, 1) = 2 and D(2,1)=1D(2, 1) = 1.
❌ Check the a2a^2 coefficient. The a2a^2 terms come from (βˆ’a)2(-a)^2 and (βˆ’2a)2(-2a)^2; the first bracket has no aa.
❌ Check the a2a^2 coefficient. 9=(0+1+2)29 = (0 + 1 + 2)^2 squares the sum of the xix_i; each bracket contributes xi2x_i^2, so add the squares 02+12+220^2 + 1^2 + 2^2.
❌ Check the a2a^2 coefficient. 33 is the number of points, which is the coefficient of b2b^2, not a2a^2.
❌ Check the constant term. Set a=b=0a = b = 0: each bracket reduces to yiy_i, so the constant is a sum of squares of the yiy_i.
❌ Check the constant term. 64=(1+3+4)264 = (1 + 3 + 4)^2 squares the total; the constant is 12+32+421^2 + 3^2 + 4^2.
Show solution

Expand each bracket:

(1βˆ’b)2=1βˆ’2b+b2(1 - b)^2 = 1 - 2b + b^2 (3βˆ’aβˆ’b)2=9+a2+b2βˆ’6aβˆ’6b+2ab(3 - a - b)^2 = 9 + a^2 + b^2 - 6a - 6b + 2ab (4βˆ’2aβˆ’b)2=16+4a2+b2βˆ’16aβˆ’8b+4ab(4 - 2a - b)^2 = 16 + 4a^2 + b^2 - 16a - 8b + 4ab

Add them:

D(a,b)=5a2+6ab+3b2βˆ’22aβˆ’16b+26.D(a, b) = 5a^2 + 6ab + 3b^2 - 22a - 16b + 26.

In general the coefficient of a2a^2 is βˆ‘xi2=0+1+4=5\sum x_i^2 = 0 + 1 + 4 = 5 and the constant is βˆ‘yi2=1+9+16=26\sum y_i^2 = 1 + 9 + 16 = 26.

Check: D(1,1)=5+6+3βˆ’22βˆ’16+26=2D(1, 1) = 5 + 6 + 3 - 22 - 16 + 26 = 2 and D(2,1)=20+12+3βˆ’44βˆ’16+26=1D(2, 1) = 20 + 12 + 3 - 44 - 16 + 26 = 1, matching the video.

Problem 4 Β· A Line Through the Origin

Given: data (1,3)(1, 3), (2,5)(2, 5), (3,5)(3, 5) and a model y=axy = ax forced through the origin, so D(a)=βˆ‘(yiβˆ’axi)2D(a) = \sum \big(y_i - ax_i\big)^2 has a single unknown β€” find the aa that minimizes DD, and the minimum value.

Which aa minimizes D(a)D(a)?

What is the minimum value of DD?

βœ… Correct! Dβ€²(a)=0D'(a) = 0 gives a=βˆ‘xiyiβˆ‘xi2=2814=2a = \frac{\sum x_i y_i}{\sum x_i^2} = \frac{28}{14} = 2, and the deviations 11, 11, βˆ’1-1 give D(2)=3D(2) = 3.
❌ Check aa. Differentiate: Dβ€²(a)=βˆ’2βˆ‘xi(yiβˆ’axi)D'(a) = -2\sum x_i(y_i - ax_i), and solve Dβ€²(a)=0D'(a) = 0.
❌ Check aa. 136=βˆ‘yiβˆ‘xi\tfrac{13}{6} = \tfrac{\sum y_i}{\sum x_i} is a natural guess, but it is not where Dβ€²(a)=0D'(a) = 0: it gives D=6118β‰ˆ3.39D = \tfrac{61}{18} \approx 3.39.
❌ Check aa. 53\tfrac{5}{3} is the slope to the last point alone; the least-squares aa uses all three.
❌ Check the minimum. Evaluate DD at the minimizing aa: compute each deviation yiβˆ’axiy_i - ax_i, square, add.
❌ Check the minimum. D=0D = 0 would need all three points on one line through the origin; 3/1β‰ 5/23/1 \ne 5/2, so they are not.
Show solution

With one unknown, this is one-variable minimization:

D(a)=(3βˆ’a)2+(5βˆ’2a)2+(5βˆ’3a)2.D(a) = (3 - a)^2 + (5 - 2a)^2 + (5 - 3a)^2. Dβ€²(a)=βˆ’2(3βˆ’a)βˆ’4(5βˆ’2a)βˆ’6(5βˆ’3a)=βˆ’2(βˆ‘xiyiβˆ’aβˆ‘xi2)=βˆ’2(28βˆ’14a).D'(a) = -2(3 - a) - 4(5 - 2a) - 6(5 - 3a) = -2\big(\textstyle\sum x_i y_i - a\sum x_i^2\big) = -2(28 - 14a).

Setting Dβ€²(a)=0D'(a) = 0 gives a=2814=2a = \frac{28}{14} = 2. Since Dβ€²β€²(a)=28>0D''(a) = 28 > 0, this is a minimum.

At a=2a = 2 the predictions are 22, 44, 66, so the deviations are 11, 11, βˆ’1-1 and

D(2)=12+12+(βˆ’1)2=3.D(2) = 1^2 + 1^2 + (-1)^2 = 3.

The full line y=ax+by = ax + b has two unknowns, so the same idea needs two partial derivatives set to zero.

Solved: 0 / 4