LINEAR-ALGEBRA ยท Unit 14 ยท Video 3 ยท Interactive Practice

No Perfect Line? How Projection Finds the Best Fit

IKey Formulas

Formula Name Description
ATAx^=ATbA^T A \hat{x} = A^T b Normal equation Converts unsolvable Ax=bAx = b into a solvable square system
p=Ax^p = A\hat{x} Projection Closest point in the column space of AA to bb
e=bโˆ’Ax^e = b - A\hat{x} Error vector Perpendicular to the column space: ATe=0A^T e = 0
โˆฅeโˆฅ2=โˆ‘(biโˆ’pi)2\lVert e \rVert^2 = \sum (b_i - p_i)^2 Squared error The quantity minimized by least squares

IIVisualization 1 โ€” Minimizing squared error

No line hits all three points; least squares picks the line whose total squared error is smallest.

๐Ÿ’ก The line b=tb = t passes through two of the points exactly, yet its total squared error is 11; the best fit accepts a small error at every point for a far lower total of 16\tfrac{1}{6}.

IIIVisualization 2 โ€” Projection makes the error perpendicular

The projection of bb onto the column space is the one point where the error runs perpendicular to it.

๐Ÿ’ก The same test governs the video's 3D case: projecting b=(1,2,2)b = (1,2,2) onto the plane spanned by (1,1,1)(1,1,1) and (1,2,3)(1,2,3) leaves an error with ATe=0A^T e = 0.

IVVisualization 3 โ€” How data reshapes the best fit

Drag the data points and the normal equation recomputes the fit โ€” yet ATAA^TA never changes.

Aแต€A = [[3, 6], [6, 14]] (fixed)

๐Ÿ’ก The residuals always sum to zero because AA's column of ones forces the error to have no component along it โ€” the intercept absorbs the average.

VQuiz Questions

Question 1

For the data points (1,1)(1,1), (2,2)(2,2), (3,2)(3,2) with model b=C+Dtb = C + Dt, the design matrix is A=[111213].A = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix}. What is ATAA^T A?

โœ… Correct! The top-left entry sums the squares of the ones column (12+12+12=31^2+1^2+1^2=3), and the bottom-right sums the squares of the tt-column (12+22+32=141^2+2^2+3^2=14).

โŒ Not quite. Entry (i,j)(i,j) of ATAA^T A is the dot product of column ii and column jj of AA. Try computing each entry carefully.

Show solution

Solution:

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

  • Entry (1,1)(1,1): 1(1)+1(1)+1(1)=31(1) + 1(1) + 1(1) = 3
  • Entry (1,2)(1,2): 1(1)+1(2)+1(3)=61(1) + 1(2) + 1(3) = 6
  • Entry (2,1)(2,1): 1(1)+2(1)+3(1)=61(1) + 2(1) + 3(1) = 6
  • Entry (2,2)(2,2): 1(1)+2(2)+3(3)=1+4+9=141(1) + 2(2) + 3(3) = 1 + 4 + 9 = 14

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

Question 2

True or False: If the system Ax=bAx = b is overdetermined (more equations than unknowns), the least-squares error vector e=bโˆ’Ax^e = b - A\hat{x} is always the zero vector.

โœ… Correct! An overdetermined system generally has no exact solution, so eโ‰ 0e \neq 0. The error measures how far bb is from the column space of AA.

โŒ Not quite. If e=0e = 0 then Ax^=bA\hat{x} = b exactly โ€” but we said there's no exact solution! Think about what 'overdetermined' means geometrically.

Show solution

Solution: False.

The whole reason we need least squares is that bb typically does not lie in the column space of AA when the system is overdetermined. If ee were zero, that would mean Ax^=bA\hat{x} = b exactly, contradicting the fact that the system has no exact solution.

In our example, e=(โˆ’16,โ€…โ€Š13,โ€…โ€Šโˆ’16)Tโ‰ 0e = \left(-\frac{1}{6},\; \frac{1}{3},\; -\frac{1}{6}\right)^T \neq 0.

The error is zero only in the special case where the overdetermined system happens to be consistent (all equations are simultaneously satisfiable).

Question 3

For the best-fit line b=23+12tb = \frac{2}{3} + \frac{1}{2}t through the points (1,1)(1,1), (2,2)(2,2), (3,2)(3,2), what is the total squared error โˆฅeโˆฅ2\lVert e \rVert^2?

Hint: Compute the prediction at each tt, find each residual, square them, and sum.

โœ… Correct! The minimum total squared error is 1/6โ‰ˆ0.1671/6 \approx 0.167. No line can do better for these three points.

โŒ Not quite. Start by computing each prediction: at t=1t=1, b^=2/3+1/2=7/6โ‰ˆ1.167\hat{b} = 2/3 + 1/2 = 7/6 \approx 1.167. Then find each residual (actual minus predicted), square, and sum.

Show solution

Solution:

Compute the prediction and residual at each data point:

tt Actual bb Predicted b^=23+12t\hat{b} = \frac{2}{3} + \frac{1}{2}t Residual ee e2e^2
1 1 23+12=76\frac{2}{3} + \frac{1}{2} = \frac{7}{6} 1โˆ’76=โˆ’161 - \frac{7}{6} = -\frac{1}{6} 136\frac{1}{36}
2 2 23+1=53\frac{2}{3} + 1 = \frac{5}{3} 2โˆ’53=+132 - \frac{5}{3} = +\frac{1}{3} 19=436\frac{1}{9} = \frac{4}{36}
3 2 23+32=136\frac{2}{3} + \frac{3}{2} = \frac{13}{6} 2โˆ’136=โˆ’162 - \frac{13}{6} = -\frac{1}{6} 136\frac{1}{36}

โˆฅeโˆฅ2=136+436+136=636=16\lVert e \rVert^2 = \frac{1}{36} + \frac{4}{36} + \frac{1}{36} = \frac{6}{36} = \frac{1}{6}

Question 4

To fit a parabola b=C+Dt+Et2b = C + Dt + Et^2 to data at t=1,2,3t = 1, 2, 3, what would the design matrix AA be?

Each row corresponds to one data point, and each column to one unknown coefficient.

โœ… Correct! Each new term in the model adds a column to AA: ones for the intercept, tt for linear, t2t^2 for quadratic. The recipe generalizes to any number of terms.

โŒ Not quite. Each row is one data point. The columns hold the values of 11, tt, and t2t^2 evaluated at that point's tt-value.

Show solution

Solution:

For the model b=C+Dt+Et2b = C + Dt + Et^2, substitute each data point's tt-value:

  • t=1t = 1: C(1)+D(1)+E(12)=C+D+EC(1) + D(1) + E(1^2) = C + D + E โ†’ row [1,โ€…โ€Š1,โ€…โ€Š1][1,\; 1,\; 1]
  • t=2t = 2: C(1)+D(2)+E(22)=C+2D+4EC(1) + D(2) + E(2^2) = C + 2D + 4E โ†’ row [1,โ€…โ€Š2,โ€…โ€Š4][1,\; 2,\; 4]
  • t=3t = 3: C(1)+D(3)+E(32)=C+3D+9EC(1) + D(3) + E(3^2) = C + 3D + 9E โ†’ row [1,โ€…โ€Š3,โ€…โ€Š9][1,\; 3,\; 9]

A=[111124139]A = \begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 4 \\ 1 & 3 & 9 \end{bmatrix}

Column 1 = ones (intercept), Column 2 = tt-values (linear), Column 3 = t2t^2-values (quadratic).

With 3 equations and 3 unknowns, this system is square and (if AA is invertible) has an exact solution โ€” the parabola passes through all three points!

Solved: 0 / 4