Documentation

CslibFrontier.Probability.LocalitySensitiveHash

Locality-sensitive hashing and AND/OR amplification #

A keyed hash family hash : ι → X → Y with seed law seed : PMF ι makes x and x' collide with probability collisionProb seed hash x x'. The family is locality sensitive for a pair of relations near and far when near pairs collide with probability at least p₁ and far pairs with probability at most p₂ (Cslib.Probability.IsLocalitySensitive). With near x x' := d x x' ≤ r₁ and far x x' := r₂ ≤ d x x' for a distance d, this is the (r₁, r₂, p₁, p₂)-sensitive family of Indyk and Motwani [IM98] ((r, cr, p₁, p₂) in [HPIM12]; (d₁, d₂, p₁, p₂) in [LRU20], Section 3.6.1). Universal hashing is the case near := (· = ·), far := (· ≠ ·), p₁ = 1 (Cslib.Probability.IsUniversalHash.isLocalitySensitive).

Collision probabilities of independent repetitions are exact:

Both constructions preserve sensitivity with the transformed probabilities. If 0 ≤ p₂ < p₁ ≤ 1, then for all δ > 0 and δ' > 0 some k and L = ⌈log (1/δ) / p₁ ^ k⌉₊ make near pairs collide with probability at least 1 - δ and far pairs with probability at most δ' (IsLocalitySensitive.exists_banded). The explicit bounds behind it are le_sCurve (near pairs: 1 - δ ≤ sCurve k L p once log (1/δ) ≤ L p₁ ^ k) and sCurve_le (far pairs: sCurve k L p ≤ L p₂ ^ k, a union bound over the bands).

Charikar's observation ([Cha02], Lemma 1) that 1 - collisionProb satisfies the triangle inequality is collisionProb_triangle.

Probabilities are real numbers, the toReal of PMF.toOuterMeasure, so that the S-curve can be manipulated by ordinary real analysis. No finiteness of the seed space is assumed; independent draws use Cslib.Probability.PMF.iid. These are information-theoretic statements. The (c, r)-approximate near-neighbour structure built from them, with its failure bound and parameters, is in CslibFrontier.DataStructure.NearNeighbor.LSH; its n ^ ρ query time and the cost of evaluating the hashes are not formalized.

Main definitions #

Main results #

References #

Collision probability #

noncomputable def Cslib.Probability.collisionProb {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (seed : PMF ι) (hash : ι → X → Y) (x x' : X) :

The probability that x and x' collide under the keyed family hash with seed law seed. Not to be confused with Cslib.Probability.PMF.collisionProbability, the probability that two independent samples of one law agree.

theorem Cslib.Probability.collisionProb_nonneg {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (seed : PMF ι) (hash : ι → X → Y) (x x' : X) :
0 ≤ collisionProb seed hash x x'
theorem Cslib.Probability.collisionProb_le_one {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (seed : PMF ι) (hash : ι → X → Y) (x x' : X) :
collisionProb seed hash x x' ≤ 1
@[simp]
theorem Cslib.Probability.collisionProb_self {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (seed : PMF ι) (hash : ι → X → Y) (x : X) :
collisionProb seed hash x x = 1
theorem Cslib.Probability.collisionProb_comm {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (seed : PMF ι) (hash : ι → X → Y) (x x' : X) :
collisionProb seed hash x x' = collisionProb seed hash x' x
theorem Cslib.Probability.collisionProb_triangle {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (seed : PMF ι) (hash : ι → X → Y) (x y z : X) :
1 - collisionProb seed hash x z ≤ 1 - collisionProb seed hash x y + (1 - collisionProb seed hash y z)

Charikar's triangle inequality ([Cha02], Lemma 1): the non-collision probability 1 - collisionProb of any keyed family satisfies the triangle inequality. If x and z are separated, then x and y, or y and z, are separated.

Locality-sensitive families #

structure Cslib.Probability.IsLocalitySensitive {X : Type u_2} (prob : X → X → ℝ) (near far : X → X → Prop) (p₁ p₂ : ℝ) :

A collision-probability function prob is (near, far, p₁, p₂)-sensitive when near pairs collide with probability at least p₁ and far pairs with probability at most p₂ ([IM98]; [LRU20], Section 3.6.1). For a keyed family, take prob := collisionProb seed hash; for a distance d, take near x x' := d x x' ≤ r₁ and far x x' := r₂ ≤ d x x'. The function is left abstract so that the OR construction, which is a collision rule rather than a hash, is covered too.

  • le_of_near (x x' : X) : near x x' → p₁ ≤ prob x x'

    Near pairs collide with probability at least p₁.

  • le_of_far (x x' : X) : far x x' → prob x x' ≤ p₂

    Far pairs collide with probability at most p₂.

theorem Cslib.Probability.IsUniversalHash.isLocalitySensitive {ι : Type u_1} {X : Type u_2} {Y : Type u_3} {seed : PMF ι} {hash : ι → X → Y} {ε : ENNReal} (hh : IsUniversalHash seed hash ε) (hε : ε ≠ ⊤) :
IsLocalitySensitive (collisionProb seed hash) (fun (x1 x2 : X) => x1 = x2) (fun (x1 x2 : X) => x1 ≠ x2) 1 ε.toReal

An ε-universal family is locality sensitive for equality and inequality with p₁ = 1 and p₂ = ε.

theorem Cslib.Probability.IsLocalitySensitive.not_far {X : Type u_2} {prob : X → X → ℝ} {near far : X → X → Prop} {p₁ p₂ : ℝ} (h : IsLocalitySensitive prob near far p₁ p₂) (hp : p₂ < p₁) {x x' : X} (hx : near x x') :
¬far x x'

A near pair is not far when p₂ < p₁.

The AND construction #

def Cslib.Probability.andHash {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (hash : ι → X → Y) (k : ℕ) (seeds : Fin k → ι) (x : X) :
Fin k → Y

The AND construction: concatenate k hashes keyed by independent seeds.

theorem Cslib.Probability.collisionProb_andHash {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (seed : PMF ι) (hash : ι → X → Y) (k : ℕ) (x x' : X) :
collisionProb (PMF.iid seed k) (andHash hash k) x x' = collisionProb seed hash x x' ^ k

k independently keyed hashes all collide with probability p ^ k.

theorem Cslib.Probability.IsLocalitySensitive.and {ι : Type u_1} {X : Type u_2} {Y : Type u_3} {seed : PMF ι} {hash : ι → X → Y} {near far : X → X → Prop} {p₁ p₂ : ℝ} (h : IsLocalitySensitive (collisionProb seed hash) near far p₁ p₂) (hp₁ : 0 ≤ p₁) (k : ℕ) :
IsLocalitySensitive (collisionProb (PMF.iid seed k) (andHash hash k)) near far (p₁ ^ k) (p₂ ^ k)

The AND construction turns a (p₁, p₂)-sensitive family into a (p₁ ^ k, p₂ ^ k)-sensitive one.

The OR construction #

noncomputable def Cslib.Probability.orCollisionProb {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (seed : PMF ι) (hash : ι → X → Y) (L : ℕ) (x x' : X) :

The OR construction: the probability that at least one of L hashes, keyed by independent seeds, makes x and x' collide.

theorem Cslib.Probability.orCollisionProb_eq {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (seed : PMF ι) (hash : ι → X → Y) (L : ℕ) (x x' : X) :
orCollisionProb seed hash L x x' = 1 - (1 - collisionProb seed hash x x') ^ L

One of L independently keyed hashes collides with probability 1 - (1 - p) ^ L.

theorem Cslib.Probability.one_sub_one_sub_pow_le_one_sub_one_sub_pow {p q : ℝ} (hpq : p ≤ q) (hq : q ≤ 1) (L : ℕ) :
1 - (1 - p) ^ L ≤ 1 - (1 - q) ^ L

p ↦ 1 - (1 - p) ^ L is monotone on probabilities.

theorem Cslib.Probability.IsLocalitySensitive.or {ι : Type u_1} {X : Type u_2} {Y : Type u_3} {seed : PMF ι} {hash : ι → X → Y} {near far : X → X → Prop} {p₁ p₂ : ℝ} (h : IsLocalitySensitive (collisionProb seed hash) near far p₁ p₂) (hp₂ : p₂ ≤ 1) (L : ℕ) :
IsLocalitySensitive (orCollisionProb seed hash L) near far (1 - (1 - p₁) ^ L) (1 - (1 - p₂) ^ L)

The OR construction turns a (p₁, p₂)-sensitive family into a (1 - (1 - p₁) ^ L, 1 - (1 - p₂) ^ L)-sensitive one.

Banding and the S-curve #

def Cslib.Probability.sCurve (k L : ℕ) (p : ℝ) :

The S-curve of banding: L bands of k rows each collide in some band with probability 1 - (1 - p ^ k) ^ L when one row collides with probability p.

theorem Cslib.Probability.orCollisionProb_andHash {ι : Type u_1} {X : Type u_2} {Y : Type u_3} (seed : PMF ι) (hash : ι → X → Y) (k L : ℕ) (x x' : X) :
orCollisionProb (PMF.iid seed k) (andHash hash k) L x x' = sCurve k L (collisionProb seed hash x x')

L bands of k independently keyed rows collide in some band with probability sCurve k L p.

theorem Cslib.Probability.sCurve_mono {k L : ℕ} {p q : ℝ} (hp : 0 ≤ p) (hpq : p ≤ q) (hq : q ≤ 1) :
sCurve k L p ≤ sCurve k L q

The S-curve is monotone on probabilities.

theorem Cslib.Probability.IsLocalitySensitive.banded {ι : Type u_1} {X : Type u_2} {Y : Type u_3} {seed : PMF ι} {hash : ι → X → Y} {near far : X → X → Prop} {p₁ p₂ : ℝ} (h : IsLocalitySensitive (collisionProb seed hash) near far p₁ p₂) (hp₁ : 0 ≤ p₁) (hp₂ : p₂ ≤ 1) (k L : ℕ) :
IsLocalitySensitive (orCollisionProb (PMF.iid seed k) (andHash hash k) L) near far (sCurve k L p₁) (sCurve k L p₂)

Banding turns a (p₁, p₂)-sensitive family into a (sCurve k L p₁, sCurve k L p₂)-sensitive one.

theorem Cslib.Probability.le_sCurve {k L : ℕ} {p p₁ δ : ℝ} (hp₁ : 0 ≤ p₁) (hp₁p : p₁ ≤ p) (hp : p ≤ 1) (hδ : 0 < δ) (hL : Real.log δ⁻¹ ≤ ↑L * p₁ ^ k) :
1 - δ ≤ sCurve k L p

Near pairs. If p₁ ≤ p ≤ 1 and log (1/δ) ≤ L p₁ ^ k, banding misses with probability at most δ: (1 - p ^ k) ^ L ≤ exp (-L p ^ k) ≤ δ.

theorem Cslib.Probability.sCurve_le {k L : ℕ} {p p₂ : ℝ} (hp : 0 ≤ p) (hpp₂ : p ≤ p₂) (hp₂ : p₂ ≤ 1) :
sCurve k L p ≤ ↑L * p₂ ^ k

Far pairs. If 0 ≤ p ≤ p₂ ≤ 1, banding collides with probability at most L p₂ ^ k, by Bernoulli's inequality 1 - L p ^ k ≤ (1 - p ^ k) ^ L.

theorem Cslib.Probability.exists_sCurve_gap {p₁ p₂ δ δ' : ℝ} (hp₂ : 0 ≤ p₂) (hp₁₂ : p₂ < p₁) (hp₁ : p₁ ≤ 1) (hδ : 0 < δ) (hδ' : 0 < δ') :
∃ (k : ℕ) (L : ℕ), L = ⌈Real.log δ⁻¹ / p₁ ^ k⌉₊ ∧ (∀ (p : ℝ), p₁ ≤ p → p ≤ 1 → 1 - δ ≤ sCurve k L p) ∧ ∀ (p : ℝ), 0 ≤ p → p ≤ p₂ → sCurve k L p ≤ δ'

The S-curve gap. If 0 ≤ p₂ < p₁ ≤ 1, then for all δ > 0 and δ' > 0 there is a number of rows k such that L = ⌈log (1/δ) / p₁ ^ k⌉₊ bands give 1 - δ ≤ sCurve k L p for every p ∈ [p₁, 1] and sCurve k L p ≤ δ' for every p ∈ [0, p₂]. The proof takes k with (p₂ / p₁) ^ k ≤ δ' / (2 (c + 1)) and p₂ ^ k ≤ δ' / 2, where c = max 0 (log (1/δ)).

theorem Cslib.Probability.IsLocalitySensitive.exists_banded {ι : Type u_1} {X : Type u_2} {Y : Type u_3} {seed : PMF ι} {hash : ι → X → Y} {near far : X → X → Prop} {p₁ p₂ δ δ' : ℝ} (h : IsLocalitySensitive (collisionProb seed hash) near far p₁ p₂) (hp₂ : 0 ≤ p₂) (hp₁₂ : p₂ < p₁) (hp₁ : p₁ ≤ 1) (hδ : 0 < δ) (hδ' : 0 < δ') :
∃ (k : ℕ) (L : ℕ), L = ⌈Real.log δ⁻¹ / p₁ ^ k⌉₊ ∧ IsLocalitySensitive (orCollisionProb (PMF.iid seed k) (andHash hash k) L) near far (1 - δ) δ'

Banded amplification. A (near, far, p₁, p₂)-sensitive family with 0 ≤ p₂ < p₁ ≤ 1 can be amplified by banding with k rows and L = ⌈log (1/δ) / p₁ ^ k⌉₊ bands so that near pairs collide with probability at least 1 - δ and far pairs with probability at most δ'.