Skip to content

Projections: the closest point in a smaller space

Drop a point onto a line or plane and keep what's left over at right angles. Least-squares fits, PCA scores and random projections are all this one move, with different choices of where to project.

Applied ~12 min6 steps

What runs in CodeSplash

You will be able to

  • Project a vector onto a unit direction and onto a subspace with orthonormal columns
  • Recognize least-squares fitted values and PCA reconstructions as orthogonal projections
  • Say what a random projection keeps and why its dimension depends on the number of rows, not columns

Read first

01

Drop a point onto a line

Read

Take a unit vector uu (length 1). The orthogonal projectionOrthogonal projectionDrop a point straight onto a line or subspace: the closest point there, with a leftover at right angles to it. of xx onto the line through uu is

x^=(x⋅u) u.\hat{x} = (x \cdot u)\, u .

The dot productDot productMultiply two vectors entry by entry and add: large when they point the same way, zero when they are at right angles. x⋅ux\cdot u is the coordinate along the line, the score. The leftover x−x^x - \hat{x} is at right angles to uu, and x^\hat{x} is the closest point on the line to xx. Pythagoras splits the squared length: ∥x∥2=∥x^∥2+∥x−x^∥2\lVert x\rVert^2 = \lVert\hat{x}\rVert^2 + \lVert x-\hat{x}\rVert^2.

02

Onto a plane, or any subspace

Read

Stack qq orthonormal directions as the columns of WW. Then

x^=WW⊤x.\hat{x} = W W^\top x .

W⊤xW^\top x gives the qq scores; multiplying by WW rebuilds the point in the original space. With WW = the top qq principal directions, this is exactly the PCA reconstruction from the previous lesson. Projecting twice changes nothing: P2=PP^2 = P for every projection P=WW⊤P = WW^\top.

03

Least squares is a projection

Explore

Regression asks for the fitted values closest to yy among all mixes of the feature columns. The closest point in a subspace is the orthogonal projection, so

y^=X(X⊤X)−1X⊤y=Hy.\hat{y} = X (X^\top X)^{-1} X^\top y = H y .

HH is the hat matrix, and the residuals are at right angles to every feature column.

DExplorable · Fit the line yourself

What makes the least-squares line 'best'?

Add one point far below the trend at high x. What happens to the least-squares slope?

How sure?
4.00
0.20

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

Move the line until your sum of squared residuals is as small as it gets. That minimum is the projection.

04

Random directions work surprisingly well

Read

PCA learns its directions from the data. A random projectionRandom projectionMaps many numeric columns to fewer by fixed random weights; distances between rows are kept up to a small distortion. draws them at random:

z=Rx,Rij∼N(0,1/q).z = R x, \qquad R_{ij} \sim N(0, 1/q).

The Johnson–Lindenstrauss lemma says that for nn rows, a target dimension qq on the order of log⁡n/ϵ2\log n / \epsilon^2 keeps every pairwise distance within a factor 1±ϵ1 \pm \epsilon with high probability. The number of original columns doesn't appear. With ϵ\epsilon = 0.2, the usual bound asks for about 3,200 dimensions to keep every distance among a million rows, whether they started with ten thousand columns or ten million.

05

Check: projections

Checkpoint

Check yourself

x=(3,4)x = (3, 4) and u=(0.6,0.8)u = (0.6, 0.8), a unit vector. What is the score x⋅ux \cdot u?

Show the answer

Answer: 5

3⋅0.6+4⋅0.8=1.8+3.2=53 \cdot 0.6 + 4 \cdot 0.8 = 1.8 + 3.2 = 5. Here xx lies exactly on the line, so the leftover is zero.

Check yourself

In a least-squares fit with an intercept, what is true of the residuals?

Show the answer

Answer: a

Orthogonal projection leaves a residual perpendicular to the subspace. The intercept column is all ones, so perpendicular to it means the residuals sum to zero.

Check yourself

You project the PCA reconstruction x^\hat{x} onto the same principal directions again. What do you get?

Show the answer

Answer: a

Projections are idempotent: P2=PP^2 = P.

Check yourself

The Johnson–Lindenstrauss lemma sets the target dimension of a random projection. What does that dimension grow with?

Show the answer

Answer: a

q∼log⁡n/ϵ2q \sim \log n / \epsilon^2. The original dimension doesn't enter, which is why random projection scales to very wide data.

06

Recap

Recap
  • Projection keeps the closest point in a subspace and leaves a perpendicular remainder.
  • WW⊤xWW^\top x with orthonormal WW projects; least squares uses the hat matrix X(X⊤X)−1X⊤X(X^\top X)^{-1}X^\top.
  • Random projection keeps pairwise distances with a dimension set by log⁡n\log n, not by the number of columns.

Concepts in this lesson