Sub topic 6
Why the match probability equals Jaccard similarity
This chapter assumes you know union, intersection, and the Jaccard similarity J(A, B) = |A ∩ B| / |A ∪ B|. It also assumes you know what an expected value is and what the previous chapter called the MinHash value of a set: the smallest element of the set under a random ordering. Here we prove the central fact of MinHash and check it by simulation.
The claim
Pick a random ordering of all possible elements. Compute the MinHash value of set A, which is its first element in that ordering. Do the same for set B. Then:
P(minhash(A) = minhash(B)) = J(A, B)
This is the whole reason MinHash works. A comparison of two small numbers becomes a coin flip whose chance of landing "match" is exactly the similarity you want to measure.
The argument in three steps
Look only at the union A ∪ B. Elements outside the union never decide which element comes first within A or B.
- Every element of the union is equally likely to come first. The ordering is uniformly random, so each of the |A ∪ B| elements has the same chance, 1 / |A ∪ B|, of being the earliest one in the union.
- The earliest element of the union is the MinHash value of at least one set. Call it x. If x is in A, it is the first element of A, because nothing in A comes before the first element of the union. The same holds for B.
- The values match exactly when x is in both sets.
- If x is in A ∩ B, both sets have x as their minimum, so the values match.
- If x is only in A, then minhash(A) = x. B does not contain x, so minhash(B) is a different element. The values differ. The same reasoning applies if x is only in B.
So a match happens when the first element of the union falls in the intersection. That event has probability |A ∩ B| / |A ∪ B|, which is J(A, B).
A worked example
Let A = {0, 1, 2, 3, 4, 5} and B = {3, 4, 5, 6, 7, 8}.
- Intersection: {3, 4, 5}, so 3 elements.
- Union: {0, 1, ..., 8}, so 9 elements.
- J(A, B) = 3 / 9 = 1/3.
In a random ordering of the nine union elements, the first one is in {3, 4, 5} with probability 3/9. Only then do the two minimums agree. Any other first element is in just one set and splits the two minimums apart.
Checking it by simulation
The code below draws many random orderings, computes both minimums under each, and counts how often they agree.
import random
A = set(range(0, 6))
B = set(range(3, 9))
universe = list(A | B)
random.seed(1)
trials = 100_000
matches = 0
for _ in range(trials):
order = universe[:]
random.shuffle(order)
rank = {x: i for i, x in enumerate(order)}
if min(A, key=rank.get) == min(B, key=rank.get):
matches += 1
print(matches / trials) # close to 3/9 = 0.333The printed value lands near 0.333. Each trial is one random ordering. The match frequency approaches J(A, B) as the number of trials grows.
From a probability to an estimator
One trial gives a yes or no answer. Define an indicator variable X that is 1 when the two MinHash values match and 0 otherwise. Its expected value is
E[X] = 1 · P(match) + 0 · P(no match) = J(A, B)
So X is an unbiased estimate of J, but a very noisy one, since it is always 0 or 1. The next chapter averages many independent copies of X. Each copy uses a different hash function and gives one position of a signature. The fraction of matching positions is then an estimate with a known error.
Common errors
- Using one ordering for many comparisons of different pairs and expecting independence. The proof holds for each single pair under one random ordering. For one pair, use different orderings to get independent trials. The same orderings can be reused across all pairs.
- Ignoring the whole universe. The ordering must be a consistent ordering of all elements, not a fresh shuffle per set. If A and B were ordered separately, the argument in step 2 would break. In code, a hash function gives every element a fixed rank that both sets share.
- Forgetting hash collisions. A hash function only approximates a random permutation. If two different elements receive the same hash value, the values can match when the elements differ. With a 61-bit modulus this is negligible. With a tiny hash range it biases the estimate upward.
Exercise
Let A = {a, b, c, d} and B = {c, d, e, f, g, h}. What is the probability that minhash(A) = minhash(B) under a uniformly random ordering of all elements? Which elements of the union make the values match?
Show answer
The intersection is {c, d}, with 2 elements. The union is {a, b, c, d, e, f, g, h}, with 8 elements. J = 2/8 = 0.25, so the probability of a match is 0.25. The values match exactly when the first element of the union is c or d.
Exercise
Why does the match probability stay the same if you add many elements to the universe that appear in neither A nor B?
Show answer
The argument looks only at the order within A ∪ B. Elements outside the union never become the minimum of either set, so they do not change which union element is first. The probability stays |A ∩ B| / |A ∪ B|.