LINEAR-ALGEBRA ยท Unit 15 ยท Video 2 ยท Interactive Practice

No Perfect Answer? Why Squaring Your Errors Finds the Best-Fit Line

IKey Formulas

Formula Name Description
y=C+Dty = C + Dt Best-fit line Intercept CC, slope DD
E=โˆฅAxโˆ’bโˆฅ2=โˆ‘ei2E = \|Ax - b\|^2 = \sum e_i^2 Total squared error Sum of squared residuals
ATAx^=ATbA^T A \hat{x} = A^T b Normal equations Solve for best-fit parameters x^\hat{x}
e=bโˆ’Ax^โŠฅCol(A)e = b - A\hat{x} \perp \text{Col}(A) Orthogonality Error is perpendicular to column space

IIVisualization 1 โ€” The Least-Squares Minimum

The best fit for (1,1)(1,1), (2,2)(2,2), (3,2)(3,2) is the line whose squared residuals โ€” drawn as literal squares โ€” have the least total area.

๐Ÿ’ก The line where that total area bottoms out is exactly the solution of the normal equations ATAโ€‰x^=ATbA^T A\,\hat{x} = A^T b.

IIIVisualization 2 โ€” Why Square the Errors?

Three ways to total the residuals as the slope DD varies โ€” but only squaring gives a smooth curve with a single, calculus-findable minimum.

๐Ÿ’ก Setting the squared curve's slope to zero, dEdD=0\frac{dE}{dD} = 0, is a linear equation โ€” exactly what ATAโ€‰x^=ATbA^T A\,\hat{x} = A^T b encodes.

IVVisualization 3 โ€” Outlier Sensitivity

One rogue point can tilt the whole fit, because squaring magnifies its single large error.

๐Ÿ’ก Robust fits (least absolute deviations, Huber loss) blunt this by not squaring large errors โ€” so always plot your data before trusting a formula.

VQuiz Questions

Question 1

For data points (1,1)(1, 1), (2,2)(2, 2), (3,2)(3, 2), the system Ax=bAx = b uses:

A=[111213],b=[122]A = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix}, \quad b = \begin{bmatrix} 1 \\ 2 \\ 2 \end{bmatrix}

What is ATAA^T A?

โœ… Correct! Top-left = sum of ones = 3, off-diagonal = sum of t-values = 6, bottom-right = sum of tยฒ = 1 + 4 + 9 = 14.

โŒ Not quite. Each entry of A^T A is a dot product of columns of A. Try computing the bottom-right entry: 1ยฒ + 2ยฒ + 3ยฒ = ?

Show solution

Solution:

AT=[111123]A^T = \begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 3 \end{bmatrix}

Multiply row by column:

  • Top-left: 1โ€‰โฃโ‹…โ€‰โฃ1+1โ€‰โฃโ‹…โ€‰โฃ1+1โ€‰โฃโ‹…โ€‰โฃ1=31\!\cdot\!1 + 1\!\cdot\!1 + 1\!\cdot\!1 = 3
  • Top-right: 1โ€‰โฃโ‹…โ€‰โฃ1+1โ€‰โฃโ‹…โ€‰โฃ2+1โ€‰โฃโ‹…โ€‰โฃ3=61\!\cdot\!1 + 1\!\cdot\!2 + 1\!\cdot\!3 = 6
  • Bottom-left: 1โ€‰โฃโ‹…โ€‰โฃ1+2โ€‰โฃโ‹…โ€‰โฃ1+3โ€‰โฃโ‹…โ€‰โฃ1=61\!\cdot\!1 + 2\!\cdot\!1 + 3\!\cdot\!1 = 6 (symmetric)
  • Bottom-right: 1โ€‰โฃโ‹…โ€‰โฃ1+2โ€‰โฃโ‹…โ€‰โฃ2+3โ€‰โฃโ‹…โ€‰โฃ3=1+4+9=141\!\cdot\!1 + 2\!\cdot\!2 + 3\!\cdot\!3 = 1 + 4 + 9 = 14

ATA=[36614]A^T A = \begin{bmatrix} 3 & 6 \\ 6 & 14 \end{bmatrix}

Question 2

The normal equations for the same data reduce to:

3C+6D=5and6C+14D=113C + 6D = 5 \qquad \text{and} \qquad 6C + 14D = 11

Solving by elimination, what is the least-squares best-fit line y=C+Dty = C + Dt?

โœ… Correct! The best-fit line is y=23+12ty = \frac{2}{3} + \frac{1}{2}t, with intercept C=23C = \frac{2}{3} and slope D=12D = \frac{1}{2}.

โŒ Not quite. Try elimination: multiply the first equation by 2, then subtract from the second to isolate D.

Show solution

Solution:

From the normal equations: 3C+6D=5(1)6C+14D=11(2)3C + 6D = 5 \quad (1) \qquad 6C + 14D = 11 \quad (2)

Step 1: Multiply (1)(1) by 2: โ€…โ€Š6C+12D=10\;6C + 12D = 10

Step 2: Subtract from (2)(2): (6C+14D)โˆ’(6C+12D)=11โˆ’10(6C + 14D) - (6C + 12D) = 11 - 10, so 2D=12D = 1, giving D=12D = \tfrac{1}{2}.

Step 3: Back-substitute into (1)(1): 3C+3=53C + 3 = 5, so C=23C = \tfrac{2}{3}.

y=23+12โ€‰t\boxed{y = \tfrac{2}{3} + \tfrac{1}{2}\,t}

Question 3

True or False: The calculus approach (setting โˆ‚Eโˆ‚C=0\frac{\partial E}{\partial C} = 0 and โˆ‚Eโˆ‚D=0\frac{\partial E}{\partial D} = 0) yields different equations than the linear algebra normal equations ATAx^=ATbA^T A \hat{x} = A^T b.

โœ… Correct! Both methods produce the same normal equations. Two roads, one destination โ€” because both minimize the same quadratic objective.

โŒ Not quite. Both methods are minimizing the same thing โ€” the total squared error. Think about whether the same objective function can lead to different optimality conditions.

Show solution

Answer: False โ€” both approaches give exactly the same equations.

Calculus route: Expand E=(C+Dโˆ’1)2+(C+2Dโˆ’2)2+(C+3Dโˆ’2)2E = (C + D - 1)^2 + (C + 2D - 2)^2 + (C + 3D - 2)^2, then set โˆ‚Eโˆ‚C=0\frac{\partial E}{\partial C} = 0 and โˆ‚Eโˆ‚D=0\frac{\partial E}{\partial D} = 0. After simplification:

3C+6D=56C+14D=113C + 6D = 5 \qquad 6C + 14D = 11

Linear algebra route: Compute ATAx^=ATbA^T A \hat{x} = A^T b directly:

[36614][CD]=[511]\begin{bmatrix} 3 & 6 \\ 6 & 14 \end{bmatrix} \begin{bmatrix} C \\ D \end{bmatrix} = \begin{bmatrix} 5 \\ 11 \end{bmatrix}

These are identical. The deep reason: differentiating a squared linear expression always produces a linear equation โ€” exactly what ATAx^=ATbA^T A \hat{x} = A^T b encodes.

Question 4

A least-squares fit includes three well-behaved points with errors of about 1 unit each, plus one outlier with an error of 8 units. Approximately what fraction of the total squared error comes from the outlier alone?

โœ… Correct! Since 8ยฒ = 64 dwarfs 3 ร— 1ยฒ = 3, the outlier alone accounts for roughly 96% of the total squared error.

โŒ Not quite. Remember to square each error before comparing. The outlier contributes 8ยฒ = 64, while the three good points together contribute only 3.

Show solution

Solution: About 96%.

Compute squared errors:

  • Three good points: 12+12+12=31^2 + 1^2 + 1^2 = 3
  • Outlier: 82=648^2 = 64
  • Total: 3+64=673 + 64 = 67

Outlier's share: 6467โ‰ˆ95.5%โ‰ˆ96%\dfrac{64}{67} \approx 95.5\% \approx 96\%

Squaring amplifies large errors disproportionately. The outlier contributes about 96% of the total despite being only one of four data points. This is why one extreme value can dominate the entire least-squares fit.

Solved: 0 / 4