How MinHash estimates a Jaccard similarity

Short answer: MinHash estimates the Jaccard similarity of two sets. With a random hash, the probability that the element of minimum hash is the same in both sets equals the Jaccard index, the size of the intersection divided by the size of the union. k independent hashes give k trials. The fraction of hashes that agree is an unbiased estimate, and the variance falls as 1/k. A signature of k minimum hashes replaces each set.


What the problem actually asks

Jaccard similarity needs the intersection and the union. For large sets those scans dominate, and a database of sets needs a pairwise comparison that does not read every element. A random permutation of the universe puts some element first. That first element lands in the intersection exactly when it is the first element of both sets, and the chance of that event is the Jaccard index. The minimum hash simulates the first element of a random permutation.

Several independent minima concentrate the estimate. Locality-sensitive hashing for Jaccard uses bands of this signature so that similar sets collide in a bucket and dissimilar sets usually do not.


What the algorithm guarantees

Each coordinated minimum is equal with probability exactly equal to the Jaccard index, for a truly random hash, or approximately so for a practical hash family that behaves randomly on the data. The average of k independent trials has variance J(1−J)/k. Time to build a signature is proportional to the set size times k. Comparing two signatures is O(k). Extra memory is the signature, not the set.

The hashes on the two sets must be the same functions. Independent hashes per set make agreement meaningless. A single hash has variance J(1−J), which is large. k in the hundreds is a common choice when the estimate will be thresholded.


The working steps

  1. Choose k hash functions, or one hash and k random seeds, and fix them for the whole collection.
  2. For a set, the signature component i is the minimum of hash_i over the elements. Empty sets have no minimum and need a documented sentinel.
  3. The estimated Jaccard similarity is the fraction of components in which two signatures are equal.
  4. Do not compare minima that were produced by different seeds. The coordination is the algorithm.
  5. For a search index, split the signature into bands and hash each band into a bucket. A candidate pair shares a bucket. Verify or re-estimate the candidates.

The estimate is of Jaccard similarity, not of cosine similarity and not of edit distance. Uncoordinated hashes do not estimate anything useful. The variance formula assumes independent trials. Reusing one hash with correlated prefixes understates the variance reduction. Banding is a retrieval structure on top of the estimate, not a more accurate estimator.


Approaches that look simpler and fail

Approach Why it fails when each set is hashed with a fresh seed
Draw new hash functions for every set Equal minima then happen by chance across unrelated functions. The probability is no longer the Jaccard index.
Estimate from one minimum and treat it as a stable percentage One trial is a Bernoulli random variable with variance J(1−J). The percentage needs many trials.
Store the minimum of the raw identifiers with no hash The smallest identifier is one fixed permutation, and it is adversarial if identifiers are assigned by a rule. The permutation has to be random.
Use the signature equality fraction as a cosine The probability identity is specific to Jaccard. Cosine has a different locality-sensitive family.

A cardinality sketch is HyperLogLog. Membership is a Bloom filter. MinHash is the Jaccard estimator, and banding it is the locality-sensitive index for similar sets.


A working example

This Python fragment estimates Jaccard similarity by the fraction of hash trials whose minimum lands on the same element. Sets {1, 2, 3, 4} and {3, 4, 5, 6} have Jaccard 1/3. With a seeded family the estimate prints 0.32.

import random
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
rng = random.Random(0)
trials = 300
agree = 0
for _ in range(trials):
    salt = rng.randrange(10**9)
    def h(x):
        return (x * 1315423911 + salt) % 2**31
    if min(a, key=h) == min(b, key=h):
        agree += 1
est = agree / trials
truth = len(a & b) / len(a | b)
assert abs(est - truth) < 0.08
print(f"{est:.2f}")

Where it is used

MinHash is Broder’s sketch for near-duplicate web pages, and the same sketch feeds plagiarism and document-clustering pipelines. The estimate is a Jaccard similarity of the shingles. A full set intersection is exact and does not fit the same memory.


When you can drop this algorithm

Drop MinHash when the sets are small enough to compare exactly. Keep coordinated signatures when many pairs must be screened and an unbiased estimate of Jaccard similarity is the score.


Why this stays published

We publish this so a search for MinHash returns the coordinated minimum and the fact that its collision probability is the Jaccard index. MinHash estimates Jaccard similarity by the fraction of coordinated minimum hashes that agree, which equals the true index in expectation.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *