Skip to content
nonconvexcard

Embedding Geometry and Similarity

Cosine, dot product and Euclidean distance and the exact relations between them; why high-dimensional near-orthogonality is what makes retrieval possible; and the maximum-of-N problem that limits it.

RAGGraduate⏱ 3 hours11 min readembeddingscosine similaritydot productnormalisationretrieval
5 problems →

An embedding model is a map f:X→Rdf:\Xcal\to\R^d from text (or images, or anything) to vectors. Retrieval ranks documents by a similarity s(f(q),f(z))s(f(q), f(z)) between a query embedding and a document embedding. This page is about the geometry that ranking lives in.

Three similarities and how they relate

PropositionCosine, dot product and Euclidean distance agree on normalised vectors

For u,vu,v with ∥u∥2=∥v∥2=1\norm u_2 = \norm v_2 = 1,

∥u−v∥22=2−2⟨u,v⟩,cos⁡(u,v)=⟨u,v⟩.\norm{u-v}_2^2 = 2 - 2\ip uv, \qquad \cos(u,v) = \ip uv .

Hence ranking by largest dot product, largest cosine, or smallest Euclidean distance gives the identical ordering on the unit sphere.

PropositionWithout normalisation the norm dominates the ranking

For general u,vu,v,

⟨u,v⟩=∥u∥2∥v∥2cos⁡(u,v),∥u−v∥22=∥u∥22+∥v∥22−2∥u∥2∥v∥2cos⁡(u,v).\ip uv = \norm u_2\norm v_2\cos(u,v), \qquad \norm{u-v}_2^2 = \norm u_2^2 + \norm v_2^2 - 2\norm u_2\norm v_2\cos(u,v).

So dot-product ranking of documents vv against a fixed query uu orders by ∥v∥2cos⁡(u,v)\norm v_2\cos(u,v), which a document can increase by growing its norm at fixed direction. Euclidean ranking penalises large ∥v∥2\norm v_2 instead. The three criteria therefore give three different orderings.

Near-orthogonality

TheoremDistribution of the cosine between random directions

Let u,vu,v be independent and uniform on Sd−1S^{d-1}, d≥3d\ge3. Then t=⟨u,v⟩t = \ip uv has density

ϱd(t)=Γ ⁣(d2)π Γ ⁣(d−12)(1−t2)d−32,t∈[−1,1],\varrho_d(t) = \frac{\Gamma\!\left(\frac d2\right)}{\sqrt\pi\,\Gamma\!\left(\frac{d-1}{2}\right)}\big(1-t^2\big)^{\frac{d-3}{2}}, \qquad t\in[-1,1],

with Et=0\E t = 0, Var⁡(t)=1/d\Var(t) = 1/d, and

P(∣t∣≥ε)≤2 e−dε2/2.\P\big(|t|\ge\eps\big) \le 2\,e^{-d\eps^2/2} .
Run it: Embedding geometry and retrievalCosine similarity between random unit vectors in R^d, and the separation available to a retriever.

Key idea

Two independent directions in R768\R^{768} have cosine within about 0.036=1/7680.036 = 1/\sqrt{768} of zero. So a "background" similarity of 0.040.04 is what noise looks like, and a genuine match at 0.30.3 stands out by a factor of eight. This is what makes dense retrieval possible at all: high dimension supplies an enormous number of nearly-orthogonal directions, so distinct meanings can be assigned distinct directions without crowding.

Quantitatively, by the tail bound plus a union bound, one can place NN directions with all pairwise cosines below ε\eps as long as N2e−dε2/2<1N^2e^{-d\eps^2/2}<1, i.e. N<edε2/4N < e^{d\eps^2/4} — exponentially many in the dimension. At d=768d = 768 and ε=0.2\eps = 0.2 that is e7.7≈2200e^{7.7}\approx 2200 strictly ε\eps-separated directions by this crude bound, and far more with a sharper packing argument; the exact statement is the packing number of the sphere.

The maximum-of-N problem

Retrieval is not a pairwise question. The relevant document must beat all NN distractors, so the quantity that matters is the maximum of NN similarities.

TheoremThe maximum of N distractor similarities

Let z1,…,zNz_1,\dots,z_N be independent uniform directions and qq a fixed unit vector. Then

E[max⁡j≤N⟨q,zj⟩]≤2log⁡Nd,\E\left[\max_{j\le N}\ip{q}{z_j}\right] \le \sqrt{\frac{2\log N}{d}} ,

and with probability at least 1−δ1-\delta,

max⁡j≤N⟨q,zj⟩≤2log⁡(N/δ)d.\max_{j\le N}\ip q{z_j} \le \sqrt{\frac{2\log(N/\delta)}{d}} .
CorollaryA sufficient condition for correct top-1 retrieval

Suppose the relevant document z⋆z^\star satisfies ⟨q,z⋆⟩=α\ip{q}{z^\star} = \alpha and the NN distractors are independent uniform directions. Then top-1 retrieval succeeds with probability at least 1−δ1-\delta provided

α>2log⁡(N/δ)d.\alpha > \sqrt{\frac{2\log(N/\delta)}{d}} .

Key idea

Put numbers in. At d=768d = 768 and δ=0.05\delta = 0.05:

Corpus size NNRequired alignment α\alpha
10310^30.160.16
10610^60.200.20
10910^90.240.24
101210^{12}0.270.27

The threshold grows like log⁡N\sqrt{\log N} — slowly, but it grows, and there is no corpus size at which it stops. Tripling the required alignment is not possible by scaling the index; it requires a better embedding model or a re-ranking stage. Conversely, raising dd helps as 1/d1/\sqrt d, which is the mathematical case for larger embedding dimensions.

Empirical observationReal embeddings are anisotropic

Empirical observation — a regularity seen in experiments. No theorem on this page establishes it in general.

Learned embeddings do not occupy the sphere uniformly. Ethayarajh measured average cosine similarities between random unrelated sentences in contextual models and found values of 0.30.3–0.60.6 in some layers — far above the 1/d≈0.041/\sqrt d \approx 0.04 the isotropic model predicts. The embeddings lie in a narrow cone.

What this does not say

Every theorem on this page assumes the distractors are independent uniform directions. Anisotropy breaks that assumption in the direction that makes retrieval harder:

  • the baseline similarity is inflated, so the contrast between a match and a non-match shrinks;
  • the distractors are correlated, so the maximum-of-NN bound — which is driven by the effective number of independent directions — is not the right calculation, and the effective NN can behave like a much larger number of truly independent draws in the cone's few informative directions;
  • near-duplicate documents, which real corpora are full of, are the worst case: they are highly correlated with the true match and the maximum-of-NN analysis says nothing about them.

The standard remedies are all attempts to restore approximate isotropy: mean-centring, whitening (BERT-flow and successors), and contrastive training with in-batch negatives, which explicitly penalises high similarity between unrelated pairs and is why models like Sentence-BERT and DPR behave far better than raw pretrained encoders.

Read this page's thresholds as a best case: the alignment actually required on real corpora is higher, and how much higher is an empirical question about the specific embedding model, not something the geometry determines.

Problems

Three similarities agree on the unit sphereProof · Foundation · 15 min The density of the cosine between random directionsProof · Advanced · 1 hour The maximum of N distractor similaritiesProof · Advanced · 30 min A sufficient condition for correct retrievalProof · Intermediate · 30 min Measure anisotropy in an embedding spaceCoding · Intermediate · 1 hour

References

These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.

Results stated on this page

Experiments

Problems (5)

All problems

Papers that use this mathematics