Sub topic 9
Estimator error and choosing the signature length
This chapter assumes you know how a MinHash signature is built and that the fraction of matching positions estimates Jaccard similarity. You should also know the variance of a coin flip. Here you will measure how far that estimate can be from the truth and pick the number of hash functions, k, to fit an error target.
Each position is a coin flip
For two sets with Jaccard similarity J, one hash function gives matching MinHash values with probability exactly J. Treat each position as a trial with two outcomes, match or no match. The estimate is the mean of k such trials:
- Expected value of the estimate:
J, so the estimator is unbiased. - Variance:
J(1 - J) / k. - Standard error:
sqrt(J(1 - J) / k).
This holds when the k hash functions behave independently. The error shrinks with the square root of k. Quadrupling k only halves the error.
The error is largest at J = 0.5, where J(1 - J) = 0.25. The worst-case standard error is therefore 0.5 / sqrt(k).
| k | Worst-case standard error |
|---|---|
| 64 | 0.0625 |
| 128 | 0.0442 |
| 256 | 0.0313 |
| 512 | 0.0221 |
| 1024 | 0.0156 |
Measuring the error yourself
The program below builds signatures with k hash functions for the two sets from earlier chapters, whose true Jaccard similarity is 0.5. It repeats this with 40 different random seeds for each k and prints the observed spread next to the predicted one. It takes several seconds to run.
import math
import random
import statistics
import zlib
P = (1 << 61) - 1
def make_hashes(k, seed=0):
rng = random.Random(seed)
return [(rng.randrange(1, P), rng.randrange(0, P)) for _ in range(k)]
def signature(ids, hashes):
return [min((a * x + b) % P for x in ids) for a, b in hashes]
def estimate(sig1, sig2):
return sum(u == v for u, v in zip(sig1, sig2)) / len(sig1)
ids_a = [zlib.crc32(str(i).encode()) for i in range(0, 150)]
ids_b = [zlib.crc32(str(i).encode()) for i in range(50, 200)]
true_j = 0.5
for k in (64, 256, 1024):
estimates = []
for seed in range(40):
hashes = make_hashes(k, seed)
estimates.append(estimate(signature(ids_a, hashes), signature(ids_b, hashes)))
observed = statistics.pstdev(estimates)
predicted = math.sqrt(true_j * (1 - true_j) / k)
print(f"k={k:5d} mean={statistics.mean(estimates):.3f} "
f"observed sd={observed:.4f} predicted sd={predicted:.4f}")The means should sit near 0.5. The observed spread should be close to 0.0625, 0.0313, and 0.0156. With only 40 repetitions, expect roughly 10 to 15 percent of noise in those spreads.
Choosing k from an error target
You usually want a guarantee like "the estimate is within ε of the truth with probability at least 1 - δ". Hoeffding's inequality bounds the mean of k independent trials:
P(|estimate - J| ≥ ε) ≤ 2 · exp(-2kε²)
Setting the right side to δ and solving for k gives:
k ≥ ln(2 / δ) / (2ε²)
import math
def signature_length(epsilon, delta):
return math.ceil(math.log(2 / delta) / (2 * epsilon ** 2))
print(signature_length(0.05, 0.05)) # 738
print(signature_length(0.02, 0.05)) # 4612This bound does not depend on J, so it is safe but conservative. If you only need a standard error of σ and you know roughly where J lies, use k = J(1 - J) / σ². For σ = 0.05 and the worst case J = 0.5, that gives k = 100.
Common errors
- Confusing absolute and relative error. The standard error is an absolute number. For
J = 0.01andk = 256, it is about 0.0062. That is 62 percent of the true value. If you need to compare very small similarities,kmust be much larger, or MinHash is the wrong tool. - Planning around the average case. Pairs near
J = 0.5have the widest spread. If your decision threshold sits there, budget for the worst case. - Using different hash functions for different sets. Positions only match meaningfully when both signatures at position
iuse the same hash function. Buildhashesonce and reuse it for every set. - Reusing correlated hash functions. Using the same
awith differentbvalues, or overlapping seeds, breaks the independence that the variance formula assumes. Draw each(a, b)pair independently. - Ignoring cost. Each signature takes
kintegers, and building it costskhash evaluations per element. Doublingkdoubles both memory and indexing time.
Exercise
You want an estimate within 0.1 of the true Jaccard similarity, with at least 90 percent probability, using the Hoeffding bound. How many hash functions do you need? Use ln(20) ≈ 2.996.
Show answer
Here ε = 0.1 and δ = 0.1, so k ≥ ln(2 / 0.1) / (2 · 0.1²) = ln(20) / 0.02 ≈ 2.996 / 0.02 ≈ 149.8. Round up to k = 150.
Exercise
A signature has k = 400 positions. What is the worst-case standard error, and at which Jaccard similarity does it occur?
Show answer
The worst case occurs at J = 0.5. The standard error is sqrt(0.25 / 400) = 0.5 / 20 = 0.025.