Documentation

CslibFrontier.Probability.LocalitySensitiveHash.SimHash

SimHash: random-hyperplane locality-sensitive hashing #

Charikar's random-hyperplane LSH scheme ([Cha02]), commonly called SimHash, draws a random vector r from the standard Gaussian measure stdGaussian E on a finite-dimensional real inner product space E and maps a vector u : E to the sign bit decide (0 ≤ ⟪u, r⟫). Two nonzero vectors u and v agree in sign unless the threshold 0 lies between ⟪u, r⟫ and ⟪v, r⟫. By Sheppard's formula (ProbabilityTheory.measureReal_stdGaussian_zero_mem_uIoc_eq_angle), their collision probability is exactly 1 - θ(u, v) / π (Cslib.Probability.measureCollisionProb_simHash), or 1 - arccos ⟪u, v⟫ / π on the unit sphere (measureCollisionProb_simHash_of_norm_eq_one).

Consequently SimHash is (θ₁, θ₂, 1 - θ₁ / π, 1 - θ₂ / π)-sensitive for angular distance on nonzero vectors (isLocalitySensitive_simHash) and (1 - arccos s₁ / π, 1 - arccos s₂ / π)-sensitive for cosine similarity ⟪u, v⟫ on unit vectors (isLocalitySensitive_simHash_inner).

Main definitions #

Main results #

References #

noncomputable def Cslib.Probability.simHash {E : Type u_1} [NormedAddCommGroup E] [InnerProductSpace ℝ E] (r u : E) :

SimHash ([Cha02]): map u to the side of the hyperplane orthogonal to r on which it lies, decide (0 ≤ ⟪u, r⟫).

theorem Cslib.Probability.simHash_eq_iff {E : Type u_1} [NormedAddCommGroup E] [InnerProductSpace ℝ E] (r u v : E) :
simHash r u = simHash r v ↔ 0 ∉ Set.uIoc (inner ℝ u r) (inner ℝ v r)

Two vectors share the same SimHash bit for r exactly when 0 does not lie strictly on one side of ⟪u, r⟫ and weakly on the other.

The SimHash collision event {r | simHash r u = simHash r v} is measurable.

Charikar's SimHash collision law ([Cha02]): under the standard Gaussian measure, two nonzero vectors collide with probability 1 - angle u v / π.

SimHash collision law on unit vectors: for ‖u‖ = 1 and ‖v‖ = 1, the collision probability equals 1 - arccos ⟪u, v⟫ / π.

theorem Cslib.Probability.measureCollisionProb_simHash_pi {ι : Type u_2} [Fintype ι] {u v : EuclideanSpace ℝ ι} (hu : u ≠ 0) (hv : v ≠ 0) :

SimHash under independent standard Gaussian coordinates on EuclideanSpace ℝ ι.

theorem Cslib.Probability.isLocalitySensitive_simHash_subtype {E : Type u_1} [NormedAddCommGroup E] [InnerProductSpace ℝ E] [MeasurableSpace E] [BorelSpace E] [FiniteDimensional ℝ E] (θ₁ θ₂ : ℝ) :
IsLocalitySensitive (measureCollisionProb (ProbabilityTheory.stdGaussian E) fun (r : E) (u : { x : E // x ≠ 0 }) => simHash r ↑u) (fun (u v : { x : E // x ≠ 0 }) => InnerProductGeometry.angle ↑u ↑v ≤ θ₁) (fun (u v : { x : E // x ≠ 0 }) => θ₂ ≤ InnerProductGeometry.angle ↑u ↑v) (1 - θ₁ / Real.pi) (1 - θ₂ / Real.pi)

SimHash on {u : E // u ≠ 0} is (θ₁, θ₂, 1 - θ₁ / π, 1 - θ₂ / π)-sensitive for the angular distance.

SimHash on E is (1 - θ₁ / π, 1 - θ₂ / π)-sensitive for pairs of nonzero vectors with angles ≤ θ₁ vs ≥ θ₂.

theorem Cslib.Probability.isLocalitySensitive_simHash_inner {E : Type u_1} [NormedAddCommGroup E] [InnerProductSpace ℝ E] [MeasurableSpace E] [BorelSpace E] [FiniteDimensional ℝ E] (s₁ s₂ : ℝ) :
IsLocalitySensitive (measureCollisionProb (ProbabilityTheory.stdGaussian E) fun (r : E) (u : { x : E // ‖x‖ = 1 }) => simHash r ↑u) (fun (u v : { x : E // ‖x‖ = 1 }) => s₁ ≤ inner ℝ ↑u ↑v) (fun (u v : { x : E // ‖x‖ = 1 }) => inner ℝ ↑u ↑v ≤ s₂) (1 - Real.arccos s₁ / Real.pi) (1 - Real.arccos s₂ / Real.pi)

SimHash on unit vectors {u : E // ‖u‖ = 1} is (1 - arccos s₁ / π, 1 - arccos s₂ / π)-sensitive for cosine similarity ⟪u, v⟫ (s₁ ≤ ⟪u, v⟫ for near pairs and ⟪u, v⟫ ≤ s₂ for far pairs).