Projection and orthogonality

Finding the closest point on a line or subspace — the single idea behind least squares.

What you'll learn
  • Project a vector onto a line with one formula
  • Know why the error of the best approximation is perpendicular
  • Split any vector into parallel and perpendicular parts

Given a point \(\mathbf{b}\) and a line through the origin in direction \(\mathbf{a}\), which point on the line is closest to \(\mathbf{b}\)?

Every point on the line is \(\beta\mathbf{a}\) for some number \(\beta\). The closest one is where the error \(\mathbf{b} - \beta\mathbf{a}\) meets the line at a right angle. Writing “at a right angle” as a dot product equal to zero solves the problem in one line:

\[\mathbf{a}^{\mathsf{T}}(\mathbf{b} - \beta\mathbf{a}) = 0 \quad\Longrightarrow\quad \beta = \frac{\mathbf{a}^{\mathsf{T}}\mathbf{b}}{\mathbf{a}^{\mathsf{T}}\mathbf{a}}\]
b wanders around the plane; βa follows it along the line. The read-out shows a·(b − βa) staying at zero: the error is always perpendicular to the line.

The notebook draws exactly this:

b = np.array([4, 1])     # the point
a = np.array([2, 5])     # the line's direction

beta = (a.T @ b) / (a.T @ a)
# closest point on the line: beta * a

Why perpendicular?

Picture the distance from \(\mathbf{b}\) to a point sliding along the line. It shrinks, bottoms out, and grows again. At the bottom, moving along the line neither helps nor hurts — so the error has no component along the line. No component along the line means perpendicular to it. This is the geometric heart of least squares, and of every “best approximation” in linear algebra.

Projecting onto a subspace

Replace the single direction \(\mathbf{a}\) with the columns of a matrix \(A\). The same perpendicularity condition, now against every column at once, gives the normal equations:

\[A^{\mathsf{T}}(\mathbf{b} - A\mathbf{x}) = \mathbf{0} \quad\Longrightarrow\quad A^{\mathsf{T}}A\,\mathbf{x} = A^{\mathsf{T}}\mathbf{b}\]
m, n = 3, 2
b = np.random.randn(m, 1)
A = np.random.randn(m, n)

x  = np.linalg.solve(A.T @ A, A.T @ b)
Ax = A @ x                     # the projection of b onto the column space of A

In 3D, the column space of a 3×2 matrix is a plane, and \(A\mathbf{x}\) is the foot of the perpendicular dropped from \(\mathbf{b}\) onto it.

Parallel and perpendicular parts

Any vector \(\mathbf{w}\) splits into a part along a reference direction \(\mathbf{v}\) and a part perpendicular to it — the notebook’s decomposition challenge:

w = np.array([2, 3])     # vector to decompose
v = np.array([4, 0])     # reference direction

beta = np.dot(v, w) / np.dot(v, v)
wp  = beta * v           # parallel part
wpp = w - wp             # perpendicular part

np.dot(wp, wpp)          # 0: the two parts are orthogonal

QR: making the columns perpendicular

When a matrix’s columns are perpendicular unit vectors — an orthonormal matrix \(Q\) — projection becomes trivial, because \(Q^{\mathsf{T}}Q = I\). The QR decomposition \(A = QR\) rewrites any matrix that way: \(Q\) holds orthonormal columns spanning the same space as \(A\), and \(R\) is upper-triangular.

Q, R = np.linalg.qr(A, 'complete')

Try it yourself. Set the animation’s line to 90° and predict \(\beta\) for a point \((x, y)\) before checking the read-out. In the notebook, project a 3D vector onto the plane spanned by two columns and verify that the error is perpendicular to both columns.

View source on GitHub →