Sub topic 11
Limits and variants of MinHash
This chapter assumes you know how a MinHash signature is built, that the chance two signatures match at one position equals the Jaccard similarity, and that the estimator averages k such matches. It covers where that machinery stops working and which variants address each problem.
What MinHash cannot do
MinHash answers one question: how large is the overlap of two sets relative to their union? Four limits follow from that.
The error shrinks slowly. The estimator is an average of k independent yes/no matches. Its standard error is sqrt(s(1 - s) / k), where s is the true Jaccard similarity. At most you get 1/(2 sqrt(k)). Halving the error needs four times as many hash functions, so k = 256 gives about 0.03 at worst and k = 1024 gives about 0.016.
Cost grows with k. Building a signature takes k hash evaluations per element, so the work is k times the set size. For large sets or many documents, this is the main expense.
Jaccard punishes size differences. Suppose A has 100 elements and all of them are in B, which has 1,000 elements. The Jaccard similarity is 100 / 1000 = 0.1, even though A is entirely contained in B. If your real question is "is A part of B?", Jaccard gives a low number and MinHash faithfully estimates it. You need a containment measure instead.
Sets only. Ordinary MinHash ignores how many times an element occurs. A word that appears 50 times counts the same as one that appears once. It also ignores order, except through whatever shingling captured.
Variants and when to use them
Each variant fixes one limit above.
| Variant | Problem it addresses | Idea |
|---|---|---|
| Bottom-k sketch | Cost of k hash functions | Use one hash function and keep the k smallest values |
| One permutation hashing | Cost of k hash functions | Hash once, split the hash range into k bins, keep the minimum per bin |
| b-bit MinHash | Signature storage | Store only the lowest b bits of each value, then correct for chance matches |
| Weighted MinHash | Multisets and weights | Sample so the match probability equals the weighted Jaccard similarity |
| Containment sketches | Unequal set sizes | Estimate the fraction of A that lies in B |
One permutation hashing leaves some bins empty for small sets. Implementations fill them from neighboring bins, which is called densification.
A bottom-k sketch
A bottom-k sketch uses a single hash function. It keeps the k smallest hash values of the set. To compare two sketches, take the k smallest values of their union. Then compute the fraction of those values that appear in both sketches.
This works because a value among the k smallest of the union, if it belongs to A, is also among A's own k smallest. So it appears in both sketches exactly when the element is in both sets.
import zlib
def bottom_k(items, k):
return sorted({zlib.crc32(x.encode()) for x in items})[:k]
def estimate_bottom_k(sk1, sk2, k):
union_k = sorted(set(sk1) | set(sk2))[:k]
shared = set(sk1) & set(sk2)
return sum(h in shared for h in union_k) / len(union_k)
A = {str(i) for i in range(0, 150)}
B = {str(i) for i in range(50, 200)}
k = 64
print(estimate_bottom_k(bottom_k(A, k), bottom_k(B, k), k)) # close to 0.5Each element is hashed once instead of k times. The result is an estimate from k samples, so the error scale is still about 1/sqrt(k). Because the sketch is the k smallest values, you can also build the sketch of A union B directly from the two sketches. This is not possible with a plain signature of a different set size.
Limits of banding
Banding inherits the limits above and adds its own. The probability that a pair becomes a candidate is 1 - (1 - s^r)^b. This curve is steep but not a perfect step. Pairs just below your threshold still become candidates sometimes, and pairs just above it are sometimes missed. You must verify candidates with the exact Jaccard similarity, or accept false positives. Choosing b and r also fixes a threshold, roughly (1/b)^(1/r). To change the threshold, you must rebuild the index with different band settings, unless you stored enough signature rows to re-band.
Related methods that are not MinHash
If your data are vectors rather than sets, MinHash is the wrong tool. SimHash uses random hyperplanes to approximate cosine similarity between vectors. Plain sampling of elements works for equal-size sets but fails on skewed ones. Choose the method by the similarity measure you need, then choose the sketch.
Exercise
Set A has 200 elements and is fully contained in set B, which has 2,000 elements. What is the Jaccard similarity, and why might a MinHash estimate of it mislead you if you want to know whether A is a subset of B?
Show answer
The intersection is 200 and the union is 2,000, so the Jaccard similarity is 200 / 2000 = 0.1. MinHash estimates this value correctly, but 0.1 looks like "mostly different". The question about subsets is a containment question (the fraction of A found in B is 1.0), so a containment sketch is the better fit.
Exercise
A signature with k = 100 gives a standard error of at most 0.05. How many hash functions do you need to cut the worst-case error to 0.025?
Show answer
The error scales as 1/sqrt(k). Halving it requires four times as many hash functions, so k = 400.