9 Least squares

9.1 Consistency in a system of equations

For an M×NM \times N matrix AA:

The equation Ax=bAx=b fundamentally asks if bb can be constructed as a linear combination of the columns of AA.
If bb lives in the column space of AA, then the system is consistent. Otherwise, it is inconsistent.
Testing if bb lies in the column space of AA:

In real-world data collection, systems are usually over-determined, so bb doesn’t lie in the column space. The goal is to find the point in the column space that is closest to bb. This best approximation is found using orthogonality from bb to the column space.

9.2 The Least Squares Problem

Given that bb does not lie in the column space, we can define an error vector between our prediction AxAx and bb: e=b−Axe = b - Ax We want to make the error vector as short as possible. We want to find a set of weight x̂\hat{x} such that ∥b−Ax̂∥≤∥b−Ax∥\|b - A\hat{x}\| \leq \|b - Ax\| Since ||e||=e12+e22+…+em2||e|| = \sqrt{e_1^2 + e_2^2 + \dots + e_m^2} we can minimise the squared distance, ||e||2||e||^2: ||e||2=e12+e22+…+em2||e||^2 = e_1^2 + e_2^2 + \dots + e_m^2 The method is called Least Squares because we are finding the solution that provides the least possible sum of the squared errors.

9.3 Best Approximation Theorem

9.3.0.1 Theorem

If WW is a finite-dimensional subspace of an inner product space VV, and if 𝒃\mathbf{b} is a vector in VV, then projW𝒃\text{proj}_{W} \mathbf{b} is the best approximation to 𝒃\mathbf{b} from W in the sense that ||𝒃−projW𝒃||<||𝒃−𝒘||||\mathbf{b}-proj_{W}\mathbf{b}||<||\mathbf{b}-\mathbf{w}|| for every vector 𝒘\mathbf{w} in W that is different from projW𝒃\text{proj}_W \mathbf{b}.

9.4 The Normal Equation

We want the error vector to be orthgonal to the entire column space of AA, so AT𝒆=𝟎AT(𝒃−A𝒙̂)=𝟎ATA𝒙̂=AT𝒃[The normal equation]\begin{align*} A^T \mathbf{e} &= \mathbf{0}\\ A^T (\mathbf{b} - A\mathbf{\hat{x}}) &= \mathbf{0}\\ A^TA\mathbf{\hat{x}} &= A^T\mathbf{b} \quad [\text{The normal equation}] \end{align*} Finding a unique solution:

9.4.0.1 Theorem

Let A be an m×nm\times n matrix. The following statements are logically equivalent:

  1. The equation A𝒙=𝒃A\mathbf{x}=\mathbf{b} has a unique least-squares solution for each 𝒃\mathbf{b} in ℝm\mathbb{R}^{m}.

  2. The columns of AA are linearly independent.

  3. The matrix ATAA^{T}A is invertible.

When these statements are true, the least-squares solution 𝒙̂\mathbf{\hat{x}} is given by 𝒙̂=(ATA)−1AT𝒃\mathbf{\hat{x}}=(A^{T}A)^{-1}A^{T}\mathbf{b}.

9.5 Projection Matrix

We want a projection matrix PP that takes any vector 𝒃\mathbf{b} and outputs the closest point in our column space, i.e. 𝒃̂\mathbf{\hat{b}} =P= P𝒃\mathbf{b}.
First, the projection of 𝒖\mathbf{u} onto a 1-D line defined by 𝒗\mathbf{v}: Proj𝒗𝒖=𝒗(𝒖T𝒗||𝒗||2)=𝒗(𝒖T𝒗𝒗T𝒗)=(𝒗𝒗T𝒗T𝒗)𝒖=P𝒖Proj_{\mathbf{v}}\mathbf{u} = \mathbf{v}\left(\frac{\mathbf{u}^T\mathbf{v}}{||\mathbf{v}||^2}\right) = \mathbf{v}\left(\frac{\mathbf{u}^T\mathbf{v}}{\mathbf{v}^T\mathbf{v}}\right) = \left(\frac{\mathbf{v}\mathbf{v}^T}{\mathbf{v}^T\mathbf{v}}\right)\mathbf{u} = P\mathbf{u} Therefore, the projection matrix P=𝒗𝒗T𝒗T𝒗P = \frac{\mathbf{v}\mathbf{v}^T}{\mathbf{v}^T\mathbf{v}}.
From the normal equation, 𝒙̂=(ATA)−1AT𝒃𝒑=A𝒙̂=A(ATA)−1AT𝒃\begin{align*} \mathbf{\hat{x}} &= (A^TA)^{-1}A^T\mathbf{b}\\ \mathbf{p} = A\mathbf{\hat{x}} = A(A^TA)^{-1}A^T\mathbf{b} \end{align*} Therefore, P=A(ATA)−1ATP = A(A^TA)^{-1}A^T.
Two properties of projection matrix:

  1. PT=PP^T = P. PP is symmetric, guaranteeing that the error vector is orthogonal to the column space.

  2. P2=PP^2 = P. PP represents the projection onto the column space. Applying it repeatedly has the same effect of applying it just once.

9.6 QR Factorisation

using the Normal Equation method, computing ATAA^T A and finding its inverse can be computationally expensive.
QR factorisation is faster. Factorising A=QRA=QR,

Substituting A=QRA=QR, A𝒙=𝒃QR𝒙=𝒃R𝒙=QT𝒃\begin{align*} A\mathbf{x} &= \mathbf{b} \\ QR\mathbf{x} &= \mathbf{b} \\ R\mathbf{x} &= Q^T \mathbf{b} \end{align*} Since RR is a simple upper-triangular staircase, we can just solve using back-substitution.

9.7 Application to linear models

When plotting experimental data on a graph, the points are scattered.
We want to draw a straight line through them defined as y=β0+β1xy = \beta_0 + \beta_1x, where β0\beta_0 is the y-intercept and β1\beta_1 is the gradient.
For any given data point (xj,yjx_j, y_j), the line will predict a specific height 𝒚̂j\mathbf{\hat{y}}_j.
The vertical gap is the residual, ϵj\epsilon_j: ϵj=yj−(β0+β1xj)\epsilon_j = y_j - (\beta_0 + \beta_1x_j) The goal is to find the specific parameters (β0\beta_0 and β1\beta_1) that make the total sum of all these squared residuals as small as mathematically possible.

First, translate the equation A𝒙=𝒃A\mathbf{x}=\mathbf{b} into X𝜷=𝒚X\mathbf{\beta} = \mathbf{y}.

Adding the errors ϵ\epsilon, the full equation is: 𝒚=X𝜷+𝝐\mathbf{y} = X\mathbf{\beta} + \mathbf{\epsilon} Example We have 4 data points (2,1),(5,2),(7,3),(8,3)(2, 1), (5, 2), (7, 3), (8, 3).
We set up 𝒚\mathbf{y} = [1233]\begin{bmatrix} 1 \\ 2 \\ 3 \\ 3 \end{bmatrix} and XX = [12151718]\begin{bmatrix} 1 & 2 \\ 1 & 5 \\ 1 & 7 \\ 1 & 8 \end{bmatrix}. Using the normal equation that minimises ϵ\epsilon: XTXX^TX𝜷\mathbf{\beta} = XTX^T𝒚\mathbf{y}.
We have XTX=[42222142]X^TX = \begin{bmatrix} 4 & 22 \\ 22 & 142 \end{bmatrix} and XTX^T𝒚\mathbf{y} =[957]= \begin{bmatrix} 9 \\ 57 \end{bmatrix}, so solving we get 𝜷\mathbf{\beta} = [2/75/14]\begin{bmatrix} 2/7 \\ 5/14 \end{bmatrix}.
The best-fit line is y=27+514xy = \frac{2}{7} + \frac{5}{14}x.

Least squares can fit a curve y=β0+β1x+β2x2y = \beta_0 + \beta_1x + \beta_2x^2. The linear model is linear wrt to the parameters β\beta not the data xx.
Simply add a third column to XX containing the xx-coordinates squared (for β2\beta_2), then solve the Normal equation XTXX^TX𝜷\mathbf{\beta} = XTX^T𝒚\mathbf{y}.