Algorithm Search⌘K

Proofs: SimHash

These are the human proofs of SimHash, at full length.

SimHash bits are random hyperplane bits, and their Hamming distance concentrates around θ/π full

This proof shows: correctness.

We use the names of the pseudocode on the page. A document is a finite set FF of features with weights w(f)>0w(f) > 0, viewed as the vector xx with xf=w(f)x_f = w(f) on FF and xf=0x_f = 0 on the other features.

Lemma 1 (the counters). For the ±1\pm 1 version, after the loop of line L2, V[i]=∑f∈Fw(f) σi(f)V[i] = \sum_{f \in F} w(f)\,\sigma_i(f), where σi(f)=+1\sigma_i(f) = +1 if bit ii of H(f)H(f) is 1 and −1-1 otherwise. For the Gaussian variant, V[i]=∑f∈Fw(f) gi(f)V[i] = \sum_{f \in F} w(f)\, g_i(f).

Proof. Line L1 sets V[i]=0V[i] = 0. Line L5, executed once for each pair (f,i)(f, i), adds w(f)σi(f)w(f)\sigma_i(f) to V[i]V[i] and changes no other counter. Addition is commutative, so after all features V[i]V[i] is the sum. The Gaussian variant is the same with gi(f)g_i(f). □\square

So bit ii of the fingerprint is 11 exactly when x⋅σi>0x \cdot \sigma_i > 0 (or x⋅gi>0x \cdot g_i > 0), where σi=(σi(f))f\sigma_i = (\sigma_i(f))_f. For two documents xx and yy, only the features in G=Fx∪FyG = F_x \cup F_y matter, so we work in RG\mathbb{R}^G.

Theorem 1 (Gaussian variant). Assume that the values gi(f)g_i(f), for f∈Gf \in G and 1≤i≤b1 \le i \le b, are independent N(0,1)N(0, 1). Let θ\theta be the angle between xx and yy in RG\mathbb{R}^G, and let Xi=1X_i = 1 if bit ii of the two fingerprints differs. Then the XiX_i are independent, Pr⁡[Xi=1]=θ/π\Pr[X_i = 1] = \theta/\pi, and the Hamming distance D=∑iXiD = \sum_i X_i satisfies E[D]=bθ/π\mathbb{E}[D] = b\theta/\pi and Pr⁡[∣D/b−θ/π∣≥ε]≤2e−2bε2\Pr[|D/b - \theta/\pi| \ge \varepsilon] \le 2e^{-2b\varepsilon^2} for every ε>0\varepsilon > 0.

Proof. Fix ii. The vector gi=(gi(f))f∈Gg_i = (g_i(f))_{f \in G} has independent N(0,1)N(0,1) coordinates. By Lemma 1 of the proof on the random hyperplane LSH page, the events gi⋅x≥0g_i \cdot x \ge 0 and gi⋅y≥0g_i \cdot y \ge 0 disagree with probability θ/π\theta/\pi. Line L6 uses >0> 0 instead of ≥0\ge 0; the two rules differ only when gi⋅x=0g_i \cdot x = 0 or gi⋅y=0g_i \cdot y = 0, and each is a linear form with a nonzero coefficient vector in Gaussian variables, which is 00 with probability 00. So Pr⁡[Xi=1]=θ/π\Pr[X_i = 1] = \theta/\pi. The variable XiX_i is a function of gig_i alone, and the vectors g1,…,gbg_1, \ldots, g_b are independent, so the XiX_i are independent. Linearity of expectation gives E[D]=bθ/π\mathbb{E}[D] = b\theta/\pi. The XiX_i take values in [0,1][0, 1], so Hoeffding's inequality gives Pr⁡[∣D−ED∣≥bε]≤2exp⁡(−2(bε)2/b)=2e−2bε2\Pr[|D - \mathbb{E}D| \ge b\varepsilon] \le 2\exp(-2(b\varepsilon)^2/b) = 2e^{-2b\varepsilon^2}. □\square

Theorem 2 (±1\pm 1 version). Assume that the bits of H(f)H(f), for f∈Gf \in G and all positions, are independent fair coins. Then the XiX_i are independent and identically distributed with some probability δ(x,y)=Pr⁡[Xi=1]\delta(x, y) = \Pr[X_i = 1] that does not depend on ii, E[D]=b δ\mathbb{E}[D] = b\,\delta, and Pr⁡[∣D/b−δ∣≥ε]≤2e−2bε2\Pr[|D/b - \delta| \ge \varepsilon] \le 2e^{-2b\varepsilon^2}. In general δ(x,y)≠θ/π\delta(x, y) \ne \theta/\pi.

Proof. XiX_i is the same function of the vector σi=(σi(f))f∈G\sigma_i = (\sigma_i(f))_{f \in G} for every ii, and σ1,…,σb\sigma_1, \ldots, \sigma_b are independent and identically distributed, uniform on {−1,+1}G\{-1, +1\}^G. So the XiX_i are independent with a common mean δ\delta, and the expectation and the Hoeffding bound follow as in Theorem 1. For the last claim take G={1,2,3}G = \{1, 2, 3\}, x=(1,0,0)x = (1, 0, 0) and y=(1,1,1)y = (1, 1, 1). Bit ii of xx is 1 when σi(1)=+1\sigma_i(1) = +1; bit ii of yy is 1 when σi(1)+σi(2)+σi(3)>0\sigma_i(1) + \sigma_i(2) + \sigma_i(3) > 0, that is, when at least two of the three signs are +1+1. The two bits disagree exactly when σi(2)\sigma_i(2) and σi(3)\sigma_i(3) are both opposite to σi(1)\sigma_i(1), which has probability 1/41/4. So δ=1/4\delta = 1/4, while θ/π=arccos⁡(1/3)/π≈0.3041\theta/\pi = \arccos(1/\sqrt{3})/\pi \approx 0.3041. ■\blacksquare

Back to SimHash