RAG and vector searchLoading progress…

High dimensions

Implement an exact baseline, compute recall at k and explain approximate search and high-dimensional difficulty.

Why high dimensions are hard

Space grows explosively with dimension. A sub-box covering 0.1 of each axis holds 0.01 of the unit box in 2 dimensions but only 10⁻¹⁰ in 10. In high dimension random points are nearly all about the same distance apart (distance concentration), so ideas like "skip the far regions" prune less. Real data is usually structured and lower-dimensional than its raw size, so the effect varies, but noise dimensions only make it worse.

dimension2
a box covering 0.1 of every axis holds 1.00e-2 of the unit box's volume 300 random points: nearest distance 0.017, farthest 0.972 relative contrast (far − near) / near = 57.56

As the dimension grows, regions shrink to almost nothing and distances to different points become nearly equal, which weakens simple pruning. How strongly this bites depends on the geometry of real data, which is often lower-dimensional than its raw size. Adding noisy dimensions makes search harder, not easier.

Increase the dimension and watch the contrast between nearest and farthest fall.

Predict first. Does merely adding empty or noisy dimensions guarantee easier search?

Chapter context

An exact search compares every candidate under a defined metric. Approximate indexes reduce work by choosing promising candidates, accepting that some true nearest neighbors may be missed.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

An exact dense dot-product scan over N vectors of dimension d needs Nd coordinate products plus reductions.

recall@k⁡=∣Rk∩Gk∣k.\operatorname{recall@k}=\frac{|R_k\cap G_k|}{k}.

This assumes k ground-truth eligible neighbors. In a d-dimensional unit box, a sub-box spanning fraction r of every axis occupies fraction rdr^d of the volume.

Worked example

Scanning 1000 vectors of dimension 128 uses 128,000 coordinate products, before selection and other overhead. If the true three neighbors are {A,B,C} and the returned set is {A,C,E}, recall@3 is 2/3. A sub-box spanning 0.1 of each of ten axes occupies only 10^-10 of the unit-box volume.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
assert 1000*128 == 128000
truth,returned={'A','B','C'},{'A','C','E'}
assert isclose(len(truth & returned)/3,2/3)
assert isclose(.1**10,1e-10)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Index recall is not the same as judged semantic relevance. High-dimensional behavior depends on the data manifold. Tie policies can affect reported recall for identical distances.