Linear Algebra for Machine Learning

Least Squares

Least squares as the projection that best fits noisy linear data.

Lesson
9

Goal

Understand least squares as the best approximation you can make when an exact solution does not exist.

The key move is to replace “solve the system exactly” with “find the closest point in the model space.”

Motivation

Real data is noisy.

That means the equations we wish were consistent usually are not.

If we insist on exact equality, many modeling problems have no answer at all.

Least squares gives us a sensible replacement: choose the parameters that make the total squared error as small as possible.

That is the backbone of linear regression and a large number of related fitting procedures.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
AAdesign matrixThe matrix that turns parameters into predictions.
xxparameter vectorThe coefficients we want to fit.
bbobservation vectorThe data we are trying to match.
x^\hat{x}least-squares solutionThe best-fit parameter vector.
eeresidualThe difference bAx^b - A\hat{x}.
ATAA^TAnormal-equation matrixThe square matrix that appears after projecting.
ATbA^Tbprojected right-hand sideThe data vector expressed in the normal equations.

Core Ideas

When exact solving fails

If you have more equations than unknowns, the system is overdetermined.

That does not mean the problem is meaningless. It means the equations are asking for too much exact agreement.

For noisy data, exact agreement is usually the wrong target.

Least squares as a projection problem

The vector AxAx always lives in the column space of AA.

If bb is not in that space, there is no exact solution to Ax=bAx = b.

So we ask for the point in col(A)\mathrm{col}(A) closest to bb.

That is a projection problem.

The residual

e=bAx^e = b - A\hat{x}

must be orthogonal to every column of AA.

The normal equations

Orthogonality to every column means

AT(bAx^)=0.A^T(b - A\hat{x}) = 0.

Expanding gives

ATAx^=ATb.A^TA\hat{x} = A^Tb.

Those are the normal equations.

If the columns of AA are linearly independent, then ATAA^TA is invertible and we can solve

x^=(ATA)1ATb.\hat{x} = (A^TA)^{-1}A^Tb.

Why this matters

This is the first place where the geometry and the algebra line up perfectly:

They are the same statement in different languages.

Worked Example

Suppose we want to fit a line

y=ax+by = ax + b

to the data

(1,2.1), (2,3.8), (3,6.2), (4,7.9).(1, 2.1),\ (2, 3.8),\ (3, 6.2),\ (4, 7.9).

The design matrix and target vector are

A=[11213141],b=[2.13.86.27.9].A = \begin{bmatrix} 1 & 1 \\ 2 & 1 \\ 3 & 1 \\ 4 & 1 \end{bmatrix}, \qquad b = \begin{bmatrix} 2.1 \\ 3.8 \\ 6.2 \\ 7.9 \end{bmatrix}.

The normal equations use

ATA=[3010104],ATb=[59.920].A^TA = \begin{bmatrix} 30 & 10 \\ 10 & 4 \end{bmatrix}, \qquad A^Tb = \begin{bmatrix} 59.9 \\ 20 \end{bmatrix}.

So we solve

[3010104][ab]=[59.920].\begin{bmatrix} 30 & 10 \\ 10 & 4 \end{bmatrix} \begin{bmatrix} a \\ b \end{bmatrix} = \begin{bmatrix} 59.9 \\ 20 \end{bmatrix}.

The solution is

a=1.98,b=0.05.a = 1.98, \qquad b = 0.05.

So the best-fit line is

y=1.98x+0.05.y = 1.98x + 0.05.

The residuals are

0.07, 0.21, 0.21, 0.07.0.07,\ -0.21,\ 0.21,\ -0.07.

They are small and balanced rather than forced to vanish.

That is what a good least-squares fit looks like.

Why This Shows Up In ML

Least squares is the ancestor of linear regression.

If each row of AA describes an example and each entry of bb is a target value, then x^\hat{x} is the parameter vector that best predicts the targets in the least-squares sense.

That matrix form shows up constantly in machine learning papers.

Whenever you see XTXX^TX, a projection problem is usually hiding underneath.

Common Confusions

Exercises

  1. Why does an overdetermined system usually not have an exact solution?

  2. In one sentence, what geometric operation is least squares performing?

  3. If x^\hat{x} is the best approximation, what must be true about the residual e=bAx^e = b - A\hat{x}?

  4. If AA is 10×310 \times 3, what are the dimensions of ATA^T, ATAA^TA, and ATbA^Tb?

  5. Starting from AT(bAx)=0A^T(b - Ax) = 0, derive the normal equations.

Implementation Exercise

Write a TypeScript helper that fits a line y=ax+by = ax + b to a set of 2D points using the normal equations.

A simple API could look like this:

type Point = { x: number; y: number };

type LineFit = {
  slope: number;
  intercept: number;
};

function fitLineLeastSquares(points: Point[]): LineFit;

The implementation should:

That makes the algebra concrete and keeps the matrix structure visible.

Reflection Questions

Next Lesson

Next: Singular Value Decomposition.

The bridge is direct: SVD explains the geometry of every matrix, including the ones that are rectangular or rank deficient, and it connects the projection and least-squares ideas to PCA.