Locality-sensitive hashing

Locality-sensitive hashing (LSH) is a method of performing probabilistic dimension reduction of high-dimensional data. The basic idea is to hash the input items so that similar items are mapped to the same buckets with high probability (the number of buckets being much smaller than the universe of possible input items). The hashing used in LSH is different from conventional hash functions, such as those used in cryptography, as in the LSH case the goal is to maximize probability of "collision" of similar items rather than avoid collisions. [1] Note how locality-sensitive hashing, in many ways, mirrors data clustering and nearest neighbor search.

Definition

An LSH family [1] [2] [3] ${\displaystyle {\mathcal {F}}}$ is defined for a metric space ${\displaystyle {\mathcal {M}}=(M,d)}$, a threshold ${\displaystyle R>0}$ and an approximation factor ${\displaystyle c>1}$. This family ${\displaystyle {\mathcal {F}}}$ is a family of functions ${\displaystyle h:{\mathcal {M}}\to S}$ which map elements from the metric space to a bucket ${\displaystyle s\in S}$. The LSH family satisfies the following conditions for any two points ${\displaystyle p,q\in {\mathcal {M}}}$, using a function ${\displaystyle h\in {\mathcal {F}}}$ which is chosen uniformly at random:

A family is interesting when ${\displaystyle P_{1}>P_{2}}$. Such a family ${\displaystyle {\mathcal {F}}}$ is called ${\displaystyle (R,cR,P_{1},P_{2})}$-sensitive.

Alternatively[4] it is defined with respect to a universe of items ${\displaystyle U}$ that have a similarity function ${\displaystyle \phi :U\times U\to [0,1]}$. An LSH scheme is a family of hash functions ${\displaystyle H}$ coupled with a probability distribution ${\displaystyle D}$ over the functions such that a function ${\displaystyle h\in H}$ chosen according to ${\displaystyle D}$ satisfies the property that ${\displaystyle Pr_{h\in H}[h(a)=h(b)]=\phi (a,b)}$ for any ${\displaystyle a,b\in U}$.

Amplification

Given a ${\displaystyle (d_{1},d_{2},p_{1},p_{2})}$-sensitive family ${\displaystyle {\mathcal {F}}}$, we can construct new families ${\displaystyle {\mathcal {G}}}$ by either the AND-construction or OR-construction of ${\displaystyle {\mathcal {F}}}$.[1]

To create an AND-construction, we define a new family ${\displaystyle {\mathcal {G}}}$ of hash functions ${\displaystyle g}$, where each function ${\displaystyle g}$ is constructed from ${\displaystyle k}$ random functions ${\displaystyle h_{1},...,h_{k}}$ from ${\displaystyle {\mathcal {F}}}$. We then say that for a hash function ${\displaystyle g\in {\mathcal {G}}}$, ${\displaystyle g(x)=g(y)}$ if and only if all ${\displaystyle h_{i}(x)=h_{i}(y)}$ for ${\displaystyle i=1,2,...,k}$. Since the members of ${\displaystyle {\mathcal {F}}}$ are independently chosen for any ${\displaystyle g\in {\mathcal {G}}}$, ${\displaystyle {\mathcal {G}}}$ is a ${\displaystyle (d_{1},d_{2},p_{1}^{k},p_{2}^{k})}$-sensitive family.

To create an OR-construction, we define a new family ${\displaystyle {\mathcal {G}}}$ of hash functions ${\displaystyle g}$, where each function ${\displaystyle g}$ is constructed from ${\displaystyle k}$ random functions ${\displaystyle h_{1},...,h_{k}}$ from ${\displaystyle {\mathcal {F}}}$. We then say that for a hash function ${\displaystyle g\in {\mathcal {G}}}$, ${\displaystyle g(x)=g(y)}$ if and only if ${\displaystyle h_{i}(x)=h_{i}(y)}$ for one or more values of ${\displaystyle i}$. Since the members of ${\displaystyle {\mathcal {F}}}$ are independently chosen for any ${\displaystyle g\in {\mathcal {G}}}$, ${\displaystyle {\mathcal {G}}}$ is a ${\displaystyle (d_{1},d_{2},1-(1-p_{1})^{k},1-(1-p_{2})^{k})}$-sensitive family.

Applications

LSH has been applied to several problem domains including{{ safesubst:#invoke:Unsubst||date=__DATE__ |\$B= {{#invoke:Category handler|main}}{{#invoke:Category handler|main}}[citation needed] }}

{{#invoke:Category handler|main}}{{#invoke:Category handler|main}}[citation needed] }}

Methods

Bit sampling for Hamming distance

One of the easiest ways to construct an LSH family is by bit sampling.[3] This approach works for the Hamming distance over d-dimensional vectors ${\displaystyle \{0,1\}^{d}}$. Here, the family ${\displaystyle {\mathcal {F}}}$ of hash functions is simply the family of all the projections of points on one of the ${\displaystyle d}$ coordinates, i.e., ${\displaystyle {\mathcal {F}}=\{h:\{0,1\}^{d}\to \{0,1\}\mid h(x)=x_{i}{\text{ for some }}i\in \{1,...,d\}\}}$, where ${\displaystyle x_{i}}$ is the ${\displaystyle i}$th coordinate of ${\displaystyle x}$. A random function ${\displaystyle h}$ from ${\displaystyle {\mathcal {F}}}$ simply selects a random bit from the input point. This family has the following parameters: ${\displaystyle P_{1}=1-R/d}$, ${\displaystyle P_{2}=1-cR/d}$.

Min-wise independent permutations

{{#invoke:main|main}}

Suppose ${\displaystyle U}$ is composed of subsets of some ground set of enumerable items ${\displaystyle S}$ and the similarity function of interest is the Jaccard index ${\displaystyle J}$. If ${\displaystyle \pi }$ is a permutation on the indices of ${\displaystyle S}$, for ${\displaystyle A\subseteq S}$ let ${\displaystyle h(A)=\min _{a\in A}\{\pi (a)\}}$. Each possible choice of ${\displaystyle \pi }$ defines a single hash function ${\displaystyle h}$ mapping input sets to elements of ${\displaystyle S}$.

Define the function family ${\displaystyle H}$ to be the set of all such functions and let ${\displaystyle D}$ be the uniform distribution. Given two sets ${\displaystyle A,B\subseteq S}$ the event that ${\displaystyle h(A)=h(B)}$ corresponds exactly to the event that the minimizer of ${\displaystyle \pi }$ over ${\displaystyle A\bigcup B}$ lies inside ${\displaystyle A\bigcap B}$. As ${\displaystyle h}$ was chosen uniformly at random, ${\displaystyle Pr[h(A)=h(B)]=J(A,B)\,}$ and ${\displaystyle (H,D)\,}$ define an LSH scheme for the Jaccard index.

Because the symmetric group on n elements has size n!, choosing a truly random permutation from the full symmetric group is infeasible for even moderately sized n. Because of this fact, there has been significant work on finding a family of permutations that is "min-wise independent" - a permutation family for which each element of the domain has equal probability of being the minimum under a randomly chosen ${\displaystyle \pi }$. It has been established that a min-wise independent family of permutations is at least of size ${\displaystyle lcm(1,2,...,n)\geq e^{n-o(n)}}$.[10] and that this bound is tight[11]

Because min-wise independent families are too big for practical applications, two variant notions of min-wise independence are introduced: restricted min-wise independent permutations families, and approximate min-wise independent families. Restricted min-wise independence is the min-wise independence property restricted to certain sets of cardinality at most k.[12] Approximate min-wise independence differs from the property by at most a fixed ${\displaystyle \epsilon }$.[13]

Nilsimsa Hash

{{#invoke:main|main}}

Nilsimsa is an anti-spam focused locality-sensitive hashing algorithm.[14] The goal of Nilsimsa is to generate a hash digest of an email message such that the digests of two similar messages are similar to each other. Nilsimsa satisfies three requirements outlined by the paper's authors:

1. The digest identifying each message should not vary significantly for changes that can be produced automatically.
2. The encoding must be robust against intentional attacks.
3. The encoding should support an extremely low risk of false positives.

Random projection

The random projection method of LSH[4] (termed arccos by Andoni and Indyk [15]) is designed to approximate the cosine distance between vectors. The basic idea of this technique is to choose a random hyperplane (defined by a normal unit vector ${\displaystyle r}$) at the outset and use the hyperplane to hash input vectors.

Given an input vector ${\displaystyle v}$ and a hyperplane defined by ${\displaystyle r}$, we let ${\displaystyle h(v)=sgn(v\cdot r)}$. That is, ${\displaystyle h(v)=\pm 1}$ depending on which side of the hyperplane ${\displaystyle v}$ lies.

Each possible choice of ${\displaystyle r}$ defines a single function. Let ${\displaystyle H}$ be the set of all such functions and let ${\displaystyle D}$ be the uniform distribution once again. It is not difficult to prove that, for two vectors ${\displaystyle u,v}$, ${\displaystyle Pr[h(u)=h(v)]=1-{\frac {\theta (u,v)}{\pi }}}$, where ${\displaystyle \theta (u,v)}$ is the angle between ${\displaystyle u}$ and ${\displaystyle v}$. ${\displaystyle 1-{\frac {\theta (u,v)}{\pi }}}$ is closely related to ${\displaystyle \cos(\theta (u,v))}$.

In this instance hashing produces only a single bit. Two vectors' bits match with probability proportional to the cosine of the angle between them.

Stable distributions

The hash function [16] ${\displaystyle h_{\mathbf {a} ,b}({\boldsymbol {\upsilon }}):{\mathcal {R}}^{d}\to {\mathcal {N}}}$ maps a d dimensional vector ${\displaystyle {\boldsymbol {\upsilon }}}$ onto a set of integers. Each hash function in the family is indexed by a choice of random ${\displaystyle \mathbf {a} }$ and ${\displaystyle b}$ where ${\displaystyle \mathbf {a} }$ is a d dimensional vector with entries chosen independently from a stable distribution and ${\displaystyle b}$ is a real number chosen uniformly from the range [0,r]. For a fixed ${\displaystyle \mathbf {a} ,b}$ the hash function ${\displaystyle h_{\mathbf {a} ,b}}$ is given by ${\displaystyle h_{\mathbf {a} ,b}({\boldsymbol {\upsilon }})=\left\lfloor {\frac {\mathbf {a} \cdot {\boldsymbol {\upsilon }}+b}{r}}\right\rfloor }$.

Other construction methods for hash functions have been proposed to better fit the data. [17] In particular k-means hash functions are better in practice than projection-based hash functions, but without any theoretical guarantee.

LSH algorithm for nearest neighbor search

One of the main applications of LSH is to provide a method for efficient approximate nearest neighbor search algorithms. Consider an LSH family ${\displaystyle {\mathcal {F}}}$. The algorithm has two main parameters: the width parameter ${\displaystyle k}$ and the number of hash tables ${\displaystyle L}$.

In the first step, we define a new family ${\displaystyle {\mathcal {G}}}$ of hash functions ${\displaystyle g}$, where each function ${\displaystyle g}$ is obtained by concatenating ${\displaystyle k}$ functions ${\displaystyle h_{1},...,h_{k}}$ from ${\displaystyle {\mathcal {F}}}$, i.e., ${\displaystyle g(p)=[h_{1}(p),...,h_{k}(p)]}$. In other words, a random hash function ${\displaystyle g}$ is obtained by concatenating ${\displaystyle k}$ randomly chosen hash functions from ${\displaystyle {\mathcal {F}}}$. The algorithm then constructs ${\displaystyle L}$ hash tables, each corresponding to a different randomly chosen hash function ${\displaystyle g}$.

In the preprocessing step we hash all ${\displaystyle n}$ points from the data set ${\displaystyle S}$ into each of the ${\displaystyle L}$ hash tables. Given that the resulting hash tables have only ${\displaystyle n}$ non-zero entries, one can reduce the amount of memory used per each hash table to ${\displaystyle O(n)}$ using standard hash functions.

Given a query point ${\displaystyle q}$, the algorithm iterates over the ${\displaystyle L}$ hash functions ${\displaystyle g}$. For each ${\displaystyle g}$ considered, it retrieves the data points that are hashed into the same bucket as ${\displaystyle q}$. The process is stopped as soon as a point within distance ${\displaystyle cR}$ from ${\displaystyle q}$ is found.

Given the parameters ${\displaystyle k}$ and ${\displaystyle L}$, the algorithm has the following performance guarantees:

For a fixed approximation ratio ${\displaystyle c=1+\epsilon }$ and probabilities ${\displaystyle P_{1}}$ and ${\displaystyle P_{2}}$, one can set ${\displaystyle k={\log n \over \log 1/P_{2}}}$ and ${\displaystyle L=n^{\rho }}$, where ${\displaystyle \rho ={\log P_{1} \over \log P_{2}}}$. Then one obtains the following performance guarantees:

References

1. Template:Cite web
2. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
3. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
4. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
5. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
6. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
7. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
8. {{#invoke:citation/CS1|citation |CitationClass=citation }}
9. {{#invoke:citation/CS1|citation |CitationClass=citation }}
10. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
11. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
12. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
13. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
14. Template:Cite web
15. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
16. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
17. {{#invoke:Citation/CS1|citation |CitationClass=journal }}