Skip to content

Locality-sensitive hashing

Use randomized hash collisions to retrieve nearby vectors, distinguish angular from Euclidean geometry, and understand the tradeoff between selective buckets and multiple tables.

Locality-sensitive hashing (LSH) is a randomized method for grouping objects so that nearby objects, under a specified distance or similarity measure, collide more often than distant ones. It can narrow a large archive of financial-document vectors to a candidate set before expensive comparisons. A collision means “worth checking”; it does not establish that two documents are identical or that one is the nearest match.
LSH addresses retrieval cost. A method such as k-nearest neighbors instead specifies how to predict from selected neighbors. Approximate retrieval can supply those neighbors, but missed records can change the prediction.

The promise belongs to a family

Choose a distance dd, near and far thresholds r1<r2r_1<r_2, and probabilities p1>p2p_1>p_2. A hash family is locality-sensitive when, for a function hh sampled from that family,

d(x,y)≤r1  ⟹  P(h(x)=h(y))≥p1,d(x,y)>r2  ⟹  P(h(x)=h(y))≤p2.\begin{aligned} d(x,y)\le r_1 &\implies \mathbb{P}(h(x)=h(y))\ge p_1,\\ d(x,y)>r_2 &\implies \mathbb{P}(h(x)=h(y))\le p_2. \end{aligned}
The probability is over the choice of hash function, with the two objects held fixed. The definition leaves a transition region between the thresholds; it does not require every near pair to collide or every far pair to separate. This probabilistic separation is the central idea in Indyk and Motwani’s original construction, §4.2.
An ordinary hash table uses equality checks to find an exact key despite collisions. LSH deliberately makes collisions informative about proximity. A cryptographic hash has security objectives and is unsuitable as a substitute for this geometric property. Feature hashing, which maps feature identifiers into a fixed number of coordinates, is another distinct use of hashing.

A random hyperplane gives one angular bit

For a nonzero vector xx, draw a normal vector uu with independent standard-normal coordinates and use the dot product to define
hu(x)={1,u⊤x≥0,0,u⊤x<0.h_u(x)=\begin{cases}1,&u^\top x\ge0,\\0,&u^\top x<0.\end{cases}

The hyperplane through the origin, with normal uu, divides the space into two sides. Nonzero vectors xx and yy separated by angle θ∈[0,π]\theta\in[0,\pi] satisfy

P(hu(x)=hu(y))=1−θπ.\mathbb{P}(h_u(x)=h_u(y))=1-\frac{\theta}{\pi}.
This is the random-hyperplane family described by Charikar (2002), §3. For a pair at 60∘60^\circ, the same-bit probability is 2/32/3. Geometrically, rotate the normal around the circle in the pair’s plane: the two disagreement intervals span 120∘120^\circ altogether, leaving 240∘240^\circ of agreement out of 360∘360^\circ. Boundary orientations have probability zero under the continuous draw; the rule above assigns a zero projection to bit 1.
Now take a separate, fixed direction u=(1,−1)u=(1,-1) and three synthetic vectors:
VectorProjection u⊤xu^\top xBit
A=(2,1)A=(2,1)11
B=(3,1)B=(3,1)21
C=(1,2)C=(1,2)−10
A and B collide under this particular hash. This table is one realized partition, not an estimate of the 2/32/3 collision probability; these vectors are not the separate 60∘60^\circ pair.
A fixed normal (1, minus 1) gives bits 1, 1 and 0 for vectors A (2,1), B (3,1) and C (1,2). For a separate pair 60 degrees apart, matching normal orientations cover 240 of 360 degrees, giving probability two thirds.
A fixed normal (1, minus 1) gives bits 1, 1 and 0 for vectors A (2,1), B (3,1) and C (1,2). For a separate pair 60 degrees apart, matching normal orientations cover 240 of 360 degrees, giving probability two thirds.
A fixed split assigns actual buckets; rotating the normal explains a probability over the angular hash family. The two panels use separate synthetic examples.

More selective buckets, more chances to find them

One bit puts everything into just two buckets. A common construction concatenates kk independently sampled hashes into a table key: all kk components must match. For a fixed pair with base collision probability pp, a table collision then has probability pkp^k.
Build LL independent tables and retrieve a point if any table matches. Its candidate-inclusion probability becomes
1−(1−pk)L.1-(1-p^k)^L.
Increasing kk reduces unwanted candidates but also loses more genuine neighbors. Increasing LL restores retrieval chances while increasing storage, hash computations and candidate work. These expressions assume independent hash draws within and across tables. They describe a fixed pair, not the probability that every desired neighbor will be found. The Andoni–Indyk–Razenshteyn survey explains concatenation, multiple tables and subsequent distance checks.

Match the geometry to the question

Random-hyperplane bits preserve angular proximity, not raw Euclidean distance. Multiplying a vector by a positive scalar leaves every bit unchanged, however large its Euclidean norm becomes. Two vectors in the same bucket can therefore be far apart in magnitude. For unit-normalized vectors, squared Euclidean distance is 2−2cos⁡θ2-2\cos\theta, so angular and Euclidean rankings agree; normalization deliberately discards magnitude. Other LSH families target other measures, including Euclidean distance without that normalization. Datar and coauthors (2004) construct a family that works directly with Euclidean-space points.

In a research workflow, define the representation and distance first, retrieve candidates, then recompute the intended similarity or distance on those candidates. Compare retrieval quality and runtime with an exact search on a manageable evaluation set. Exact reranking cannot recover a neighbor that never entered the candidate set, and LSH alone provides no unconditional exact-nearest-neighbor guarantee.

For the prediction step after neighbor retrieval, k-Nearest Neighbours and the Curse of Dimensionality develops averaging, voting and the effect of extra features. Its public page is a preview; the full lesson requires membership.

Sources

Last updated
  • locality sensitive hashing
  • similarity search
  • random hyperplanes
  • angular distance
  • approximate neighbors
← Back to the glossaryBack to top ↑