Documentation

CslibFrontier.Probability.LocalitySensitiveHash.MinHash

MinHash and the Jaccard similarity #

A permutation σ of a finite linear order α ranks the elements of α. The MinHash of a finite set A is the least rank min σ(A), with ⊤ for the empty set (Cslib.Probability.minHash). Broder's theorem ([Bro97]; [LRU20], Section 3.3.3) states that, for a uniformly random permutation, two sets have equal MinHash values with probability exactly their Jaccard similarity |A ∩ B| / |A ∪ B| (Cslib.Probability.collisionProb_minHash). The Jaccard similarity of two empty sets is taken to be 1 (Cslib.Probability.jaccard), which makes the identity unconditional.

Under a random permutation each element of A ∪ B is equally likely to have the least rank: transposing two elements is a bijection between the permutations ranking either one first. The MinHash values agree exactly when the element of least rank lies in A ∩ B.

Consequently MinHash is a (d₁, d₂, 1 - d₁, 1 - d₂)-sensitive family for the Jaccard distance 1 - J ([LRU20], Section 3.6.2; isLocalitySensitive_minHash). Banding b bands of r MinHash rows gives the S-curve 1 - (1 - J ^ r) ^ b ([LRU20], Section 3.4.2; orCollisionProb_andHash_minHash). Charikar's triangle inequality gives the triangle inequality for the Jaccard distance (jaccard_triangle); with jaccard_comm and jaccard_eq_one_iff, the Jaccard distance is a metric on finite sets.

The random permutation is a uniformly random element of the full symmetric group Equiv.Perm α. Smaller min-wise independent families and the hash-based approximations used in practice are not treated here.

Main definitions #

Main results #

References #

def Cslib.Probability.jaccard {α : Type u_1} [DecidableEq α] (A B : Finset α) :

The Jaccard similarity |A ∩ B| / |A ∪ B| of two finite sets, with J(∅, ∅) = 1.

theorem Cslib.Probability.jaccard_eq_div {α : Type u_1} [DecidableEq α] {A B : Finset α} (h : (A ∪ B).Nonempty) :
jaccard A B = ↑(A ∩ B).card / ↑(A ∪ B).card

For sets with a nonempty union, the Jaccard similarity is |A ∩ B| / |A ∪ B|.

theorem Cslib.Probability.jaccard_comm {α : Type u_1} [DecidableEq α] (A B : Finset α) :
jaccard A B = jaccard B A
@[simp]
theorem Cslib.Probability.jaccard_self {α : Type u_1} [DecidableEq α] (A : Finset α) :
jaccard A A = 1
theorem Cslib.Probability.jaccard_nonneg {α : Type u_1} [DecidableEq α] (A B : Finset α) :
0 ≤ jaccard A B
theorem Cslib.Probability.jaccard_le_one {α : Type u_1} [DecidableEq α] (A B : Finset α) :
jaccard A B ≤ 1
theorem Cslib.Probability.jaccard_eq_one_iff {α : Type u_1} [DecidableEq α] {A B : Finset α} :
jaccard A B = 1 ↔ A = B

Two finite sets have Jaccard similarity 1 exactly when they are equal, so the Jaccard distance 1 - J separates points.

theorem Cslib.Probability.jaccard_map {α : Type u_1} [DecidableEq α] {β : Type u_2} [DecidableEq β] (f : α ↪ β) (A B : Finset α) :

The Jaccard similarity is invariant under embeddings.

def Cslib.Probability.minHash {α : Type u_1} [LinearOrder α] (σ : Equiv.Perm α) (A : Finset α) :

The MinHash of a finite set A under the ranking σ: the least rank min σ(A), or ⊤ when A is empty.

@[simp]
theorem Cslib.Probability.minHash_eq_of_isMin {α : Type u_1} [LinearOrder α] {σ : Equiv.Perm α} {A : Finset α} {s : α} (hs : s ∈ A) (hmin : ∀ a ∈ A, σ s ≤ σ a) :
minHash σ A = ↑(σ s)

An element of least rank in A attains the MinHash of A.

Broder's theorem ([Bro97]; [LRU20], Section 3.3.3): under a uniformly random permutation of α, two finite sets have equal MinHash values with probability exactly their Jaccard similarity.

theorem Cslib.Probability.isLocalitySensitive_minHash {α : Type u_1} [LinearOrder α] [Fintype α] (d₁ d₂ : ℝ) :
IsLocalitySensitive (collisionProb (PMF.uniformOfFintype (Equiv.Perm α)) minHash) (fun (A B : Finset α) => 1 - ↑(jaccard A B) ≤ d₁) (fun (A B : Finset α) => d₂ ≤ 1 - ↑(jaccard A B)) (1 - d₁) (1 - d₂)

MinHash is (d₁, d₂, 1 - d₁, 1 - d₂)-sensitive for the Jaccard distance 1 - J ([LRU20], Section 3.6.2).

Banding MinHash signatures ([LRU20], Section 3.4.2): b bands of r independent MinHash rows agree in some band with probability 1 - (1 - J ^ r) ^ b.

theorem Cslib.Probability.jaccard_triangle {α : Type u_1} [DecidableEq α] (A B C : Finset α) :
1 - jaccard A C ≤ 1 - jaccard A B + (1 - jaccard B C)

The Jaccard distance 1 - J satisfies the triangle inequality, by Charikar's argument ([Cha02], Lemma 1) applied to MinHash. The three sets are moved into the finite universe A ∪ B ∪ C, ordered arbitrarily, where Broder's theorem applies.