Near-orthogonality in high dimensions
Explain why random unit vectors in high dimension have cosine similarity around 1/√d. Explain why a d-dimensional space can hold far more than d nearly-orthogonal directions, room for many more concept directions than dimensions.
Builds on: Variance of sums and dot products of random vectors, Behavior is learned, not programmed
What the animation says
More concepts than dimensions
Inside a language model, a concept often lives as a direction in a space of a few thousand numbers. If every concept needed its own exactly perpendicular direction, four thousand ninety-six dimensions would hold just four thousand ninety-six concepts. But models seem to track far more concepts than that. So how do they keep them from mixing up? Start with a simpler question. Pick two completely random directions in a thousand dimensions. What angle do you expect between them?
Why random means perpendicular
Close to ninety degrees, almost every time. Here's why. Fill two vectors with random coordinates, each centred on zero with spread one. Their dot product multiplies matching coordinates and adds them up: d terms, each randomly positive or negative. Those terms mostly cancel, so the sum only grows like the square root of d. Each vector's length is also about the square root of d. The cosine divides the dot product by both lengths: square root of d over d, which is one over square root of d.
Check it: 2,000 random directions
Let's test it. This code makes two thousand random unit vectors. This line computes every pairwise cosine at once, about two million pairs. The print shows the spread of those cosines next to one over square root of d. At ten dimensions the spread is about a third, and some pairs are nearly parallel. At a thousand, it's about three hundredths. The worst of two million pairs is only around zero point one seven. The middle row is blank. Drag in the spread you expect at one hundred dimensions.
How many directions fit
Now accept a small overlap: cosines up to zero point two, at a thousand dimensions. That's six standard deviations out: a bad pair has about four chances in a billion. Ten thousand directions make fifty million pairs. Fifty million times four in a billion is about zero point two expected bad pairs, so random picks almost always work. Ten times more directions than dimensions, every pair within zero point two of perpendicular. So as d grows, how does the number of directions that fit grow?
Remember this
Exponentially. The count that fits is about e to the power d times epsilon squared over four. At a thousand dimensions and zero point two, that bound reaches about twenty-two thousand, against just a thousand exactly perpendicular directions. Keep two facts. Random cosines in d dimensions are about plus or minus one over square root of d. And accepting a small overlap buys room for far more concept directions than dimensions. The price is a little interference between them.
Random directions barely overlap
Size of the cosine, and how many fit
10,000 directions in 1,000 dimensions
Check it: 2,000 random directions
import numpy as np
rng = np.random.default_rng(0)
n = 2000
for d in [10, 100, 1000]:
U = rng.standard_normal((n, d))
U /= np.linalg.norm(U, axis=1, keepdims=True) # unit rows
C = U @ U.T # all cosines
off = C[~np.eye(n, dtype=bool)] # drop self-pairs
print(d, off.std().round(3), round(1/np.sqrt(d), 3),
np.abs(off).max().round(2))
Remember this
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses