All courses

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

▶ Watch this lesson Start the whole course

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.

More concepts than dimensions?

Inside a model, concepts often turn out to be **directions** in a space of $d$ numbers. If every concept needed its own exactly perpendicular direction, a $d = 4096$ space could hold only 4096 concepts. Models seem to track far more than that. How can $d$ dimensions hold many more than $d$ distinct directions without those directions constantly getting mixed up with one another?

Random directions barely overlap

Build two random vectors by drawing each of their $d$ coordinates independently, with mean 0 and variance 1. Their dot product is a sum of $d$ independent, signed terms, each with variance 1. You already know such a sum spreads like $\sqrt d$. Each vector's length is $\sqrt{\text{sum of } d \text{ squares}} \approx \sqrt d$. The cosine is the dot product divided by both lengths, so it scales like $\sqrt d / d = 1/\sqrt d$. In high dimension, two random directions are almost always **nearly** perpendicular. Near-perpendicularity is the default, not something rare.

Size of the cosine, and how many fit

For random unit vectors $u, v \in \mathbb R^d$: $\cos = u\cdot v$, with mean $0$ and variance exactly $1/d$. The standard deviation is $1/\sqrt d$. The tail is tiny: $P(|u\cdot v|>\varepsilon) \le 2e^{-d\varepsilon^2/2}$. With $n$ vectors there are about $n^2/2$ pairs. By the union bound, the expected number of pairs with $|\cos|>\varepsilon$ is at most $n^2e^{-d\varepsilon^2/2}$. This stays below 1 while $n < e^{d\varepsilon^2/4}$. Below that count a valid set exists, and random choice almost always works when $n$ is well below it. That count is **exponential** in $d$, whereas exactly orthogonal directions max out at $d$.

10,000 directions in 1,000 dimensions

Take $d=1000$ and tolerance $\varepsilon=0.2$. - Std of the cosine is $1/\sqrt{1000}\approx0.032$, so $0.2$ is about 6 std. - Per pair: $P \le 2e^{-1000\cdot0.04/2}=2e^{-20}\approx4\times10^{-9}$. - With $n=10{,}000$ there are $5\times10^{7}$ pairs. - Expected bad pairs $\le 5\times10^7 \cdot 4\times10^{-9}=0.2$. So 10× more directions than dimensions fit, every pair within 0.2 of perpendicular.

Check it: 2,000 random directions

This puts 2,000 random unit vectors in $d$ dimensions and measures every pairwise cosine. The spread tracks $1/\sqrt d$. At $d=1000$ the worst pair among about 2 million pairs is only around $0.16$–$0.17$ (about 5 std), even though there are twice as many vectors as dimensions. At $d=10$ some pairs are nearly parallel.
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

Random unit vectors in $d$ dimensions have a cosine of about $\pm 1/\sqrt d$, so high-dimensional space is full of nearly perpendicular directions. If you accept a small overlap $\varepsilon$, roughly $e^{d\varepsilon^2/4}$ directions fit, exponentially more than $d$. That gives room to store far more concept directions than dimensions, at the cost of small interference between them.

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.