Sub topic 8
Choosing hash functions: pairwise independence
This chapter assumes you know the count-min sketch layout (d rows, w columns), basic probability (expected value, independence), and how hash tables handle collisions. Here you will see what the sketch's analysis requires of its hash functions and how to meet that requirement cheaply.
What the analysis needs
The error guarantee depends on one fact about each row. If two different items x and y are hashed into w columns, the chance that they land in the same column must be at most 1/w. The proof in the later chapter on Markov's inequality uses this fact and no other property of the hash functions.
A hash function drawn at random from a family is pairwise independent if, for any two distinct inputs x and y and any two columns i and j:
Pr[h(x) = i and h(y) = j] = 1/w²
Summing over i = j gives a collision probability of exactly 1/w. Pairwise independence is a weak requirement. It constrains how two items behave together and says nothing about three or more. That weakness is useful, because a family with this property needs only two random numbers to describe one function.
The standard family
The code in this tutorial uses the family h(x) = ((a·x + b) mod p) mod w.
pis a prime larger than any input. We use the Mersenne prime 2^61 − 1.ais chosen uniformly from 1 to p − 1.bis chosen uniformly from 0 to p − 1.
Before the final mod w, the map x → (a·x + b) mod p is pairwise independent over the integers modulo p. The final mod w slightly distorts the distribution. The result is still a universal family: for distinct inputs below p, the collision probability is at most 1/w. That is the property the sketch needs.
Each row gets its own (a, b) pair, drawn independently of the other rows. Independent rows are what make the failure probability shrink as e^(-d). If two rows shared a function, a collision in one would also be a collision in the other, and the second row would add no protection.
Checking the collision rate
You can measure the property directly. This script fixes two items, draws many random functions from the family, and counts how often the items collide.
import hashlib
import random
P = (1 << 61) - 1
def stable_int(item):
digest = hashlib.blake2b(str(item).encode(), digest_size=8).digest()
return int.from_bytes(digest, "big") % P
def make_hash(w, rng):
a = rng.randrange(1, P)
b = rng.randrange(0, P)
return lambda x: ((a * x + b) % P) % w
w = 50
rng = random.Random(1)
x, y = stable_int("apple"), stable_int("banana")
trials = 100_000
collisions = 0
for _ in range(trials):
h = make_hash(w, rng)
if h(x) == h(y):
collisions += 1
print(collisions / trials, 1 / w) # both values are close to 0.02The measured rate is close to 1/w = 0.02. The randomness is in the choice of function, and the two items stay fixed.
Gotchas
- Choose the function before you see the data. The guarantee holds over the random choice of
aandb. If an adversary knows your parameters, they can build items that all collide. - Pairwise independence starts after items become integers. Two different items that map to the same integer
xcollide in every row, and no family can separate them. Use a wide integer, such as 64 bits, to make this negligible. - Python's built-in
hash()is not stable across processes for strings, because it is salted per run. This is harmless for a single-process experiment. Sketches that are saved, shared, or merged need a stable conversion such asstable_intabove, and they must also share the same(a, b)pairs, for example by using the same seed.
Exercise
A row uses h(x) = ((a·x + b) mod p) mod w with w = 200. For two distinct items, what is the largest collision probability the sketch analysis allows in that row? If every row reused the same (a, b) pair, why would adding more rows no longer lower the failure probability?
Show answer
The analysis allows a collision probability of at most 1/w = 1/200 = 0.005 per row. With a shared (a, b), every row would put an item in the same column, so any collision in one row would occur in all rows. The minimum across rows would then equal the value from a single row, and the failure probability would not shrink as e^(-d).