Algorithm Search⌘K

Proofs: MinHash

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

The MinHash estimate is unbiased and concentrated around the Jaccard similarity full

This proof shows: correctness.

We use the names of the pseudocode on the page. Fix two finite sets A,B⊆UA, B \subseteq U, not both empty, and write C=A∪BC = A \cup B and J=∣A∩B∣/∣C∣J = |A \cap B| / |C|.

Assumption (R). The functions h1,…,hkh_1, \ldots, h_k are independent. Each hih_i is injective on CC, and the order that hih_i induces on CC is uniform over the ∣C∣!|C|! orders. A uniformly random permutation of a finite universe UU has this property for every CC.

Lemma 1 (the signature). For each ii, MINHASH-SIGNATURE sets sig[i]=min⁡a∈Ahi(a)\mathrm{sig}[i] = \min_{a \in A} h_i(a), with min⁡∅=∞\min \emptyset = \infty.

Proof. Fix ii. Line L2 sets sig[i]=∞\mathrm{sig}[i] = \infty. Invariant of the loop of line L3: after the elements a1,…,aja_1, \ldots, a_j of AA have been seen, sig[i]=min⁡(∞,hi(a1),…,hi(aj))\mathrm{sig}[i] = \min(\infty, h_i(a_1), \ldots, h_i(a_j)). It holds for j=0j = 0, and line L4 keeps it, since min⁡(min⁡(X),y)=min⁡(X∪{y})\min(\min(X), y) = \min(X \cup \{y\}). At the end j=∣A∣j = |A|. The loop over ii does not change sig[i′]\mathrm{sig}[i'] for i′≠ii' \ne i. □\square

Lemma 2 (one position). Let Xi=1X_i = 1 if sigA[i]=sigB[i]\mathrm{sig}_A[i] = \mathrm{sig}_B[i] and Xi=0X_i = 0 otherwise. Under (R), Pr⁡[Xi=1]=J\Pr[X_i = 1] = J.

Proof. If one of the sets is empty, say BB, then J=0J = 0, sigB[i]=∞\mathrm{sig}_B[i] = \infty and sigA[i]\mathrm{sig}_A[i] is finite, so Xi=0X_i = 0. Now let both be nonempty. Let u∈Cu \in C be the element with the least value hi(u)h_i(u); it is unique since hih_i is injective on CC. Under (R), each element of CC is first in a uniform order with the same probability, so Pr⁡[u=c]=1/∣C∣\Pr[u = c] = 1/|C| for each c∈Cc \in C.

  • If u∈A∩Bu \in A \cap B, then hi(u)≤hi(a)h_i(u) \le h_i(a) for each a∈A⊆Ca \in A \subseteq C, so by Lemma 1, sigA[i]=hi(u)\mathrm{sig}_A[i] = h_i(u), and in the same way sigB[i]=hi(u)\mathrm{sig}_B[i] = h_i(u). So Xi=1X_i = 1.
  • If u∈A∖Bu \in A \setminus B, then sigA[i]=hi(u)\mathrm{sig}_A[i] = h_i(u). Each b∈Bb \in B is in CC and differs from uu, so hi(b)>hi(u)h_i(b) > h_i(u) by injectivity, and sigB[i]>hi(u)\mathrm{sig}_B[i] > h_i(u). So Xi=0X_i = 0. The case u∈B∖Au \in B \setminus A is the same.

So Xi=1X_i = 1 exactly when u∈A∩Bu \in A \cap B, which has probability ∣A∩B∣/∣C∣=J|A \cap B| / |C| = J. □\square

Theorem. Let J^\hat J be the value that ESTIMATE returns for the signatures of AA and BB. Under (R),

  1. E[J^]=J\mathbb{E}[\hat J] = J;
  2. Var⁡[J^]=J(1−J)/k≤1/(4k)\operatorname{Var}[\hat J] = J(1 - J)/k \le 1/(4k);
  3. for each ε>0\varepsilon > 0, Pr⁡[∣J^−J∣≥ε]≤2e−2kε2\Pr[|\hat J - J| \ge \varepsilon] \le 2 e^{-2k\varepsilon^2}.

Proof. Lines L6 to L8 count the positions with equal entries, so line L9 returns J^=1k∑i=1kXi\hat J = \frac{1}{k} \sum_{i=1}^{k} X_i. Each XiX_i depends only on hih_i, so the XiX_i are independent, and by Lemma 2 each is a Bernoulli variable with mean JJ.

  1. By linearity of expectation, E[J^]=1k∑iE[Xi]=J\mathbb{E}[\hat J] = \frac1k \sum_i \mathbb{E}[X_i] = J.
  2. A Bernoulli variable with mean JJ has variance J(1−J)J(1 - J). The variance of a sum of independent variables is the sum of their variances, so Var⁡[J^]=1k2⋅kJ(1−J)=J(1−J)/k\operatorname{Var}[\hat J] = \frac{1}{k^2} \cdot k J(1 - J) = J(1-J)/k. Since J(1−J)≤1/4J(1 - J) \le 1/4 for 0≤J≤10 \le J \le 1, the bound follows.
  3. The Hoeffding inequality for kk independent variables with values in [0,1][0, 1] gives Pr⁡[1k∑iXi−J≥ε]≤e−2kε2\Pr[\frac1k \sum_i X_i - J \ge \varepsilon] \le e^{-2k\varepsilon^2}, and the same bound for the lower tail. A union bound over the two tails gives the claim. ■\blacksquare

Corollary. For 0<δ<10 < \delta < 1 and k≥ln⁡(2/δ)/(2ε2)k \ge \ln(2/\delta) / (2\varepsilon^2), Pr⁡[∣J^−J∣≥ε]≤δ\Pr[|\hat J - J| \ge \varepsilon] \le \delta, since then 2e−2kε2≤2e−ln⁡(2/δ)=δ2 e^{-2k\varepsilon^2} \le 2 e^{-\ln(2/\delta)} = \delta.

Termination and cost. The loops of lines L1, L3 and L7 run over finite ranges, kk, ∣A∣|A| and kk times, so both procedures stop after k∣A∣k|A| and kk steps of O(1)O(1) time on the word RAM.

Back to MinHash