9 Least squares
9.1 Consistency in a system of equations
For an matrix :
Under-determined : There are free variables infinite solutions.
Perfectly-determines : Unique solution.
Over-determined : Prone to inconsistency. Real-world data collection tends to result in over-determined system.
The equation
fundamentally asks if
can be constructed as a linear combination of the columns of
.
If
lives in the column space of
,
then the system is consistent. Otherwise, it is inconsistent.
Testing if
lies in the column space of
:
If rank() < rank(), introduces a new dimension, so the system is inconsistent.
If rank() = rank(), the system is consistent.
In real-world data collection, systems are usually over-determined, so doesn’t lie in the column space. The goal is to find the point in the column space that is closest to . This best approximation is found using orthogonality from to the column space.
9.2 The Least Squares Problem
Given that does not lie in the column space, we can define an error vector between our prediction and : We want to make the error vector as short as possible. We want to find a set of weight such that Since we can minimise the squared distance, : 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 is a finite-dimensional subspace of an inner product space , and if is a vector in , then is the best approximation to from W in the sense that for every vector in W that is different from .
9.4 The Normal Equation
We want the error vector to be orthgonal to the entire column space of , so Finding a unique solution:
9.4.0.1 Theorem
Let A be an matrix. The following statements are logically equivalent:
The equation has a unique least-squares solution for each in .
The columns of are linearly independent.
The matrix is invertible.
When these statements are true, the least-squares solution is given by .
9.5 Projection Matrix
We want a projection matrix
that takes any vector
and outputs the closest point in our column space, i.e.
.
First, the projection of
onto a 1-D line defined by
:
Therefore, the projection matrix
.
From the normal equation,
Therefore,
.
Two properties of projection matrix:
. is symmetric, guaranteeing that the error vector is orthogonal to the column space.
. 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
and finding its inverse can be computationally expensive.
QR factorisation is faster. Factorising
,
has orthonormal columns. .
is upper-triangular matrix.
Substituting , Since 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
,
where
is the y-intercept and
is the gradient.
For any given data point
(),
the line will predict a specific height
.
The vertical gap is the residual,
:
The goal is to find the specific parameters
(
and
)
that make the total sum of all these squared residuals as small as
mathematically possible.
First, translate the equation
into
.
: Vector containing all the actual -coordinates from the data. (The unreachable target).
: Vector containing the unknown weights: .
Design matrix : First column contains 1s to multiply with which is a constant. The second column of is filled with your actual -coordinates. These act as the multipliers for your slope .
Adding the errors
,
the full equation is:
Example We have 4 data points
.
We set up
=
and
=
.
Using the normal equation that minimises
:
=
.
We have
and
,
so solving we get
=
.
The best-fit line is
.
Least squares can fit a curve
.
The linear model is linear wrt to the parameters
not the data
.
Simply add a third column to
containing the
-coordinates
squared (for
),
then solve the Normal equation
=
.