Real data rarely fit a model exactly. In Algebra 1 you drew a line of fit and looked at residuals; here we find the best line exactly. In matrix language: $A\mathbf{x} = \mathbf{b}$ has no solution, so we look for the $\hat{\mathbf{x}}$ that makes $A\hat{\mathbf{x}}$ as close as possible to $\mathbf{b}$. Projection gives the answer.
Key idea
Why it works
Worked example: An inconsistent system
- $A^{T}A = \begin{bmatrix} 3 & 4 \\ 4 & 10 \end{bmatrix}$ and $A^{T}\mathbf{b} = \begin{bmatrix} 11 \\ 24 \end{bmatrix}$.
- Solve $\begin{bmatrix} 3 & 4 \\ 4 & 10 \end{bmatrix}\hat{\mathbf{x}} = \begin{bmatrix} 11 \\ 24 \end{bmatrix}$: the determinant is $30 - 16 = 14$, and $\hat{\mathbf{x}}$ $= \frac{1}{14}\begin{bmatrix} 10 & -4 \\ -4 & 3 \end{bmatrix}\begin{bmatrix} 11 \\ 24 \end{bmatrix}$ $= \begin{bmatrix} 1 \\ 2 \end{bmatrix}$.
- $A\hat{\mathbf{x}} = \begin{bmatrix} 1 \\ 3 \\ 7 \end{bmatrix}$, residual $\mathbf{b} - A\hat{\mathbf{x}} = \begin{bmatrix} 2 \\ -3 \\ 1 \end{bmatrix}$. Check: it is orthogonal to both columns ($2 - 3 + 1 = 0$ and $0 - 3 + 3 = 0$).
- Least-squares error: $\sqrt{4 + 9 + 1} = \sqrt{14}$.
Answer: $\hat{\mathbf{x}} = (1, 2)$
Worked example: Fitting a line
- Each point gives an equation $\beta_0 + \beta_1x_i = y_i$. The design matrix has a column of $1$s and a column of $x$'s: $X = \begin{bmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix}$, $\mathbf{y} = \begin{bmatrix} 1 \\ 1 \\ 3 \\ 3 \end{bmatrix}$.
- $X^{T}X = \begin{bmatrix} 4 & 6 \\ 6 & 14 \end{bmatrix}$ and $X^{T}\mathbf{y} = \begin{bmatrix} 8 \\ 16 \end{bmatrix}$.
- Solve: determinant $56 - 36 = 20$; $\boldsymbol{\beta}$ $= \frac{1}{20}\begin{bmatrix} 14 & -6 \\ -6 & 4 \end{bmatrix}\begin{bmatrix} 8 \\ 16 \end{bmatrix}$ $= \begin{bmatrix} \frac{4}{5} \\ \frac{4}{5} \end{bmatrix}$.
- The line is $y = 0.8 + 0.8x$. Its residuals $(0.2, -0.6, 0.6, -0.2)$ add up to $0$, as they always do when the model has an intercept.
Answer: $y = 0.8 + 0.8x$
Common mistake
This is exactly ordinary least squares (OLS) in statistics and economics. With many explanatory variables, the design matrix $X$ gets one column per variable, and the estimate is $\hat{\boldsymbol{\beta}} = (X^{T}X)^{-1}X^{T}\mathbf{y}$. The Teddy Rook Book Econometrics course builds on this formula, starting with fitting a line and multiple regression.