Sub topic 9
Why the Guarantee Holds: Expected Collisions and Markov's Inequality
This chapter assumes you know basic probability (expected value, independent events), how the count-min sketch updates and queries work, and the (ε, δ) guarantee. Here you will see why that guarantee is true.
What we need to prove
Let N be the total count added to the sketch. Let f(x) be the true count of item x, and let est(x) be the sketch's estimate. We want to show:
- est(x) ≥ f(x) always.
- est(x) ≤ f(x) + εN with probability at least 1 − δ.
The first claim holds because each counter only receives additions, as the previous chapters showed. The work is in the second claim.
Step 1: one row, expected extra count
Fix an item x and one row j. The counter at x's column holds f(x) plus the counts of every other item y that hashes to the same column. Call that extra amount the noise in row j:
noisej = sum of f(y) over all y ≠ x with hj(y) = h_j(x)
Write an indicator variable Xy that is 1 when y collides with x in this row and 0 otherwise. Then noisej = sum of f(y)·X_y.
With a pairwise independent hash family, two different items land in the same column with probability at most 1/w. So E[X_y] ≤ 1/w. By linearity of expectation:
E[noise_j] ≤ (1/w) · sum of f(y) over y ≠ x ≤ N/w
Linearity of expectation needs no independence between the X_y. Pairwise independence is only used to get the 1/w collision probability for each pair.
Step 2: Markov's inequality on one row
Markov's inequality says that for a non-negative random variable Z and any a > 0, P(Z ≥ a) ≤ E[Z]/a. The noise is never negative, so it qualifies.
Set w = ⌈e/ε⌉, so N/w ≤ εN/e. Choose a = εN:
P(noise_j > εN) ≤ (N/w) / (εN) ≤ 1/e
So in any single row, the overcount exceeds εN with probability at most 1/e, about 0.37.
Step 3: all rows must fail together
The estimate is the minimum over d rows. It exceeds f(x) + εN only if every row's noise exceeds εN. The rows use independently chosen hash functions, so the failure probabilities multiply:
P(est(x) > f(x) + εN) ≤ (1/e)^d = e^(−d)
Set d = ⌈ln(1/δ)⌉. Then e^(−d) ≤ e^(−ln(1/δ)) = δ. This gives the guarantee.
Checking the bound empirically
The simulation below fixes the width and depth, then measures how often the overcount exceeds εN for a test item. It uses a simple stream of 2,000 items drawn from 500 distinct values. The measured failure rate should be well below δ, because Markov's inequality is a loose bound.
import random
def run_trial(w, d, stream, target, rng):
p = (1 << 61) - 1
params = [(rng.randrange(1, p), rng.randrange(0, p)) for _ in range(d)]
table = [[0] * w for _ in range(d)]
for item in stream:
for r, (a, b) in enumerate(params):
table[r][((a * item + b) % p) % w] += 1
est = min(
table[r][((a * target + b) % p) % w]
for r, (a, b) in enumerate(params)
)
return est
rng = random.Random(1)
stream = [rng.randrange(500) for _ in range(2000)]
target = 7
true = stream.count(target)
N = len(stream)
epsilon, delta = 0.01, 0.01
w, d = 272, 5 # from epsilon = delta = 0.01
trials = 500
failures = sum(
run_trial(w, d, stream, target, rng) > true + epsilon * N
for _ in range(trials)
)
print(failures / trials, "vs delta =", delta)Common misreadings
- The bound is on the overcount relative to N, the total stream length, not relative to f(x). A rare item can have a large relative error.
- The guarantee is per query. Across many queries, some will exceed the bound. To cover Q queries at once you need δ/Q per query, which costs only a ln Q term in depth.
- The probability is over the random choice of hash functions, not over the data. The stream may be adversarial, as long as it does not depend on the hash parameters.
Exercise
A sketch has w = 100 columns, and the stream has N = 10,000 total counts. Using pairwise independent hashing, what is the upper bound on the expected noise in one row, and what does Markov's inequality give for P(noise ≥ 500)?
Show answer
Expected noise is at most N/w = 10,000/100 = 100. Markov gives P(noise ≥ 500) ≤ 100/500 = 0.2.
Exercise
Why does the proof multiply the per-row failure probabilities, and which property of the sketch does that rely on?
Show answer
The estimate is the minimum across rows, so it is too large only if every row is too large. Multiplying the probabilities is valid because each row uses an independently chosen hash function.