Multivariable-Calculus ยท Unit 8 ยท Video 5 ยท Interactive Practice

Least Squares Is Just Solving Linear Equations: Lines, Exponentials and Parabolas

IKey Formulas

FormulaNameWhat you need
D(a,b)=โˆ‘i=1n(yiโˆ’axiโˆ’b)2D(a, b) = \sum_{i=1}^{n} (y_i - ax_i - b)^2Total square deviationThe data and a candidate line y=ax+by = ax + b
(โˆ‘xi2)a+(โˆ‘xi)b=โˆ‘xiyi(โˆ‘xi)a+nโ€‰b=โˆ‘yi\begin{aligned} \left(\sum x_i^2\right) a + \left(\sum x_i\right) b &= \sum x_i y_i \\ \left(\sum x_i\right) a + n\,b &= \sum y_i \end{aligned}Best-fit line: 2ร—22 \times 2 linear systemโˆ‚Dโˆ‚a=0\frac{\partial D}{\partial a} = 0 and โˆ‚Dโˆ‚b=0\frac{\partial D}{\partial b} = 0, divided by โˆ’2-2
y=cโ€‰eaxโ€…โ€ŠโŸบโ€…โ€Šlnโกy=ax+by = c\,e^{ax} \iff \ln y = ax + b, with c=ebc = e^{b}Exponential fitThe same system with lnโกyi\ln y_i in place of yiy_i
(โˆ‘xi4)a+(โˆ‘xi3)b+(โˆ‘xi2)c=โˆ‘xi2yi\left(\sum x_i^4\right) a + \left(\sum x_i^3\right) b + \left(\sum x_i^2\right) c = \sum x_i^2 y_iBest parabola y=ax2+bx+cy = ax^2 + bx + c: first row of a 3ร—33 \times 3 systemEach row down lowers every power of xx by one, ending in nโ€‰c=โˆ‘xi0โ€‰cn\,c = \sum x_i^0\, c

Key Insight: the unknown coefficients enter the model linearly, so setting every partial derivative of DD to zero gives a linear system โ€” the data only supply its numbers.

IIVisualization 1 โ€” Data, Then Sums, Then Solve

Every coefficient of the 2ร—22 \times 2 system is a sum computed from the data points.

๐Ÿ’ก Challenge: place the three points so that D=0D = 0, then so that the best-fit slope is a=0a = 0 although no two points share a height.

IIIVisualization 2 โ€” Moore's Law on Two Scales

A straight line fitted to lnโกy\ln y is an exponential curve y=cโ€‰eaty = c\,e^{at} on the ordinary scale.

IVVisualization 3 โ€” The Best Parabola Is a 3 ร— 3 System

Adding the point (3,2)(3, 2) to the video's data bends the trend; a parabola needs sums up to โˆ‘xi4\sum x_i^4.

Step 1 โ€” Sums from the data
n=4,โˆ‘xi=6,โˆ‘xi2=14n = 4,\quad \sum x_i = 6,\quad \sum x_i^2 = 14
โˆ‘xi3=36,โˆ‘xi4=98\sum x_i^3 = 36,\quad \sum x_i^4 = 98
โˆ‘yi=10,โˆ‘xiyi=17,โˆ‘xi2yi=37\sum y_i = 10,\quad \sum x_i y_i = 17,\quad \sum x_i^2 y_i = 37

VQuiz Questions

Problem 1 ยท Build the 2 ร— 2 System

Given: the data points (0,2)(0, 2), (1,2)(1, 2), (2,5)(2, 5) โ€” find the linear system whose solution is the best-fit line y=ax+by = ax + b.

โœ… Correct! Subtracting gives 2a=32a = 3, so a=32a = \tfrac{3}{2}, b=32b = \tfrac{3}{2}: the best-fit line is y=32x+32y = \tfrac{3}{2}x + \tfrac{3}{2}.
โŒ Count the b's. In โˆ‘(xia+bโˆ’yi)=0\sum (x_i a + b - y_i) = 0 one bb appears for each data point, so the coefficient of bb is n=3n = 3, not 11.
โŒ Sum of squares, not square of the sum. The coefficient of aa is โˆ‘xi2=0+1+4=5\sum x_i^2 = 0 + 1 + 4 = 5, while (โˆ‘xi)2=9\left(\sum x_i\right)^2 = 9.
โŒ The right-hand sides are swapped. The row with โˆ‘xi2\sum x_i^2 comes from โˆ‚Dโˆ‚a\frac{\partial D}{\partial a}, whose right side is โˆ‘xiyi=12\sum x_i y_i = 12; the row with nโ€‰bn\,b has right side โˆ‘yi=9\sum y_i = 9.
โŒ Not quite. Compute โˆ‘xi\sum x_i, โˆ‘xi2\sum x_i^2, โˆ‘yi\sum y_i, โˆ‘xiyi\sum x_i y_i and place them in the normal equations.
Show solution

Here n=3n = 3 and the four sums are

โˆ‘xi=0+1+2=3,โˆ‘xi2=0+1+4=5\sum x_i = 0 + 1 + 2 = 3,\qquad \sum x_i^2 = 0 + 1 + 4 = 5 โˆ‘yi=2+2+5=9,โˆ‘xiyi=0+2+10=12\sum y_i = 2 + 2 + 5 = 9,\qquad \sum x_i y_i = 0 + 2 + 10 = 12

Placing them in (โˆ‘xi2)a+(โˆ‘xi)b=โˆ‘xiyi\left(\sum x_i^2\right) a + \left(\sum x_i\right) b = \sum x_i y_i and (โˆ‘xi)a+nโ€‰b=โˆ‘yi\left(\sum x_i\right) a + n\,b = \sum y_i:

5a+3b=12,3a+3b=95a + 3b = 12,\qquad 3a + 3b = 9

Subtracting the second from the first gives 2a=32a = 3, so a=32a = \tfrac{3}{2}; then 92+3b=9\tfrac{9}{2} + 3b = 9 gives b=32b = \tfrac{3}{2}. The best-fit line is y=32x+32y = \tfrac{3}{2}x + \tfrac{3}{2}.

Problem 2 ยท Fitting an Exponential

Given: the data points (0,e)(0, e), (1,e3)(1, e^3), (2,e4)(2, e^4) and the model y=cโ€‰eaxy = c\,e^{ax}, fitted by least squares on lnโกy\ln y โ€” find aa and cc.

What is a?

What is c?

โœ… Correct! The fit is y=e7/6โ€‰e3x/2y = e^{7/6}\,e^{3x/2}: the best line lnโกy=32x+76\ln y = \tfrac{3}{2}x + \tfrac{7}{6} through the points (xi,lnโกyi)(x_i, \ln y_i), exponentiated.
โŒ Only the intercept gets exponentiated. In lnโกy=ax+b\ln y = ax + b the slope of the line is aa itself; it is b=lnโกcb = \ln c that needs c=ebc = e^{b}.
โŒ That is the intercept. Solving 5a+3b=115a + 3b = 11, 3a+3b=83a + 3b = 8 gives slope a=32a = \tfrac{3}{2} and intercept b=76b = \tfrac{7}{6}.
โŒ That line only uses two points. lnโกy\ln y rises from 11 to 33 between the first two points, but the best line must account for all three: a=32a = \tfrac{3}{2}.
โŒ Check aa. Take logs first: the points become (0,1)(0, 1), (1,3)(1, 3), (2,4)(2, 4).
โŒ That is b=lnโกcb = \ln c, not cc. The line fit returns b=76b = \tfrac{7}{6}; undo the logarithm: c=e7/6c = e^{7/6}.
โŒ Wrong direction. b=lnโกcb = \ln c, so c=ebc = e^{b}, not lnโกb\ln b.
โŒ The fit need not pass through (0,e)(0, e). At x=0x = 0 the best line gives lnโกy=76\ln y = \tfrac{7}{6}, not the data value lnโกe=1\ln e = 1.
โŒ Check cc. Find the intercept bb of the best line through (xi,lnโกyi)(x_i, \ln y_i), then c=ebc = e^{b}.
Show solution

Taking logs, lnโกy=lnโกc+ax\ln y = \ln c + ax. With b=lnโกcb = \ln c this is the line lnโกy=ax+b\ln y = ax + b, fitted to the points (xi,lnโกyi)=(0,1)(x_i, \ln y_i) = (0, 1), (1,3)(1, 3), (2,4)(2, 4) โ€” the video's three points.

The sums are n=3n = 3, โˆ‘xi=3\sum x_i = 3, โˆ‘xi2=5\sum x_i^2 = 5, โˆ‘lnโกyi=8\sum \ln y_i = 8, โˆ‘xilnโกyi=11\sum x_i \ln y_i = 11, so

5a+3b=11,3a+3b=85a + 3b = 11,\qquad 3a + 3b = 8

Subtracting gives 2a=32a = 3, so a=32a = \tfrac{3}{2}, and 92+3b=8\tfrac{9}{2} + 3b = 8 gives b=76b = \tfrac{7}{6}. Then

c=eb=e7/6โ‰ˆ3.21,y=3.21โ€‰e3x/2ย (approximately)c = e^{b} = e^{7/6} \approx 3.21,\qquad y = 3.21\,e^{3x/2}\ \text{(approximately)}

This fit minimizes the squared deviations of lnโกy\ln y, not of yy itself.

Problem 3 ยท Four Points, Full Fit

Given: the data points (0,1)(0, 1), (1,1)(1, 1), (2,2)(2, 2), (3,4)(3, 4) โ€” find the best-fit line and its total square deviation DD.

What is the best-fit line?

What is D at the best fit?

โœ… Correct! y=x+12y = x + \tfrac{1}{2} misses each point by exactly 12\tfrac{1}{2}, so D=4โ‹…14=1D = 4 \cdot \tfrac{1}{4} = 1.
โŒ Sum of squares, not square of the sum. The coefficient of aa in the first row is โˆ‘xi2=14\sum x_i^2 = 14; that line came from using (โˆ‘xi)2=36\left(\sum x_i\right)^2 = 36.
โŒ Count the b's. With four data points the second row is 6a+4b=86a + 4b = 8, not 6a+b=86a + b = 8.
โŒ The intercept is an unknown too. a=โˆ‘xiyi/โˆ‘xi2a = \sum x_i y_i / \sum x_i^2 is the best line through the origin; the model y=ax+by = ax + b needs both equations.
โŒ Check the line. The system is 14a+6b=1714a + 6b = 17, 6a+4b=86a + 4b = 8.
โŒ Add up every point. 14\tfrac{1}{4} is one squared deviation; there are four of them.
โŒ Square before adding. The deviations 12,โˆ’12,โˆ’12,12\tfrac12, -\tfrac12, -\tfrac12, \tfrac12 do sum to 00 โ€” that is the second normal equation โ€” but DD adds their squares.
โŒ Square, don't take absolute values. โˆ‘โˆฃriโˆฃ=2\sum |r_i| = 2, but D=โˆ‘ri2D = \sum r_i^2 and (12)2=14\left(\tfrac{1}{2}\right)^2 = \tfrac{1}{4}.
โŒ Check DD. Compute each deviation yiโˆ’(axi+b)y_i - (ax_i + b), square it, and add.
Show solution

Sums (n=4n = 4):

โˆ‘xi=6,โˆ‘xi2=14,โˆ‘yi=8,โˆ‘xiyi=0+1+4+12=17\sum x_i = 6,\quad \sum x_i^2 = 14,\quad \sum y_i = 8,\quad \sum x_i y_i = 0 + 1 + 4 + 12 = 17

System:

14a+6b=17,6a+4b=814a + 6b = 17,\qquad 6a + 4b = 8

Multiply the first by 22 and the second by 33: 28a+12b=3428a + 12b = 34 and 18a+12b=2418a + 12b = 24. Subtracting gives 10a=1010a = 10, so a=1a = 1, and 6+4b=86 + 4b = 8 gives b=12b = \tfrac{1}{2}.

Deviations yiโˆ’(xi+12)y_i - \left(x_i + \tfrac12\right): 12\tfrac{1}{2}, โˆ’12-\tfrac{1}{2}, โˆ’12-\tfrac{1}{2}, 12\tfrac{1}{2}, so

D=14+14+14+14=1D = \tfrac{1}{4} + \tfrac{1}{4} + \tfrac{1}{4} + \tfrac{1}{4} = 1

Problem 4 ยท A One-Coefficient Model

Given: the model y=ax2y = ax^2 (no other terms) and the data points (โˆ’1,2)(-1, 2), (1,4)(1, 4), (2,6)(2, 6) โ€” find the least-squares value of aa.

โœ… Correct! Dโ€ฒ(a)=0D'(a) = 0 gives (โˆ‘xi4)a=โˆ‘xi2yi\left(\sum x_i^4\right) a = \sum x_i^2 y_i, so a=3018=53a = \tfrac{30}{18} = \tfrac{5}{3}: a 1ร—11 \times 1 linear system.
โŒ That is โˆ‘yi/โˆ‘xi2\sum y_i / \sum x_i^2. Differentiating (yiโˆ’axi2)2(y_i - ax_i^2)^2 brings out a factor โˆ’xi2-x_i^2, so each term is weighted by xi2x_i^2: a=โˆ‘xi2yi/โˆ‘xi4a = \sum x_i^2 y_i / \sum x_i^4.
โŒ Averaging yi/xi2y_i / x_i^2 is not least squares. That treats every point as equally reliable for aa; least squares weights each point by xi2x_i^2.
โŒ Sum of fourth powers, not square of the sum. The denominator is โˆ‘xi4=1+1+16=18\sum x_i^4 = 1 + 1 + 16 = 18, not (โˆ‘xi2)2=36\left(\sum x_i^2\right)^2 = 36.
โŒ Not quite. Write D(a)=โˆ‘(yiโˆ’axi2)2D(a) = \sum (y_i - ax_i^2)^2 and set Dโ€ฒ(a)=0D'(a) = 0.
Show solution

The total square deviation is a function of the single unknown aa:

D(a)=โˆ‘i=13(yiโˆ’axi2)2D(a) = \sum_{i=1}^{3} \left(y_i - a x_i^2\right)^2

By the chain rule, Dโ€ฒ(a)=โˆ‘2(yiโˆ’axi2)(โˆ’xi2)D'(a) = \sum 2\left(y_i - a x_i^2\right)\left(-x_i^2\right). Setting it to zero and dropping the โˆ’2-2:

(โˆ‘xi4)a=โˆ‘xi2yi\left(\sum x_i^4\right) a = \sum x_i^2 y_i

With the data, โˆ‘xi4=1+1+16=18\sum x_i^4 = 1 + 1 + 16 = 18 and โˆ‘xi2yi=2+4+24=30\sum x_i^2 y_i = 2 + 4 + 24 = 30, so

a=3018=53a = \frac{30}{18} = \frac{5}{3}

This is the first row of the parabola system with the bb and cc columns removed, because those coefficients are no longer in the model. Since Dโ€ฒโ€ฒ(a)=2โˆ‘xi4>0D''(a) = 2\sum x_i^4 > 0, it is a minimum.

Solved: 0 / 4