Sub topic 9
Why the Guarantee Holds: Expected Collisions and Markov's Inequality
The earlier chapters stated the count-min guarantee. This chapter proves it. You should already know how the table, the row hashes and the "minimum across rows" query work. You should also know what an expected value is, and what it means for events to be independent.
What we are proving
Let N be the total weight of the stream, which is the sum of all counts added so far. Let a_i be the true count of item i. The sketch has width w = ⌈e/ε⌉ and depth d = ⌈ln(1/δ)⌉. For any single item i, the estimate satisfies:
- a_i ≤ estimate(i), always.
- estimate(i) ≤ a_i + εN, with probability at least 1 − δ.
The first claim needs no probability. Every update adds a non-negative amount to a counter that item i also uses, so no counter that i touches can be smaller than a_i. The minimum of those counters can't be smaller either. The second claim is the one that needs the argument below.
The proof has four steps. Each step is a short calculation.
Step 1: one row is the true count plus noise
Fix an item i and one row j. Let hj be that row's hash function. The counter at column hj(i) holds the counts of every item that hashes to that column, including i itself:
counter = ai + Xj, where Xj = the sum of ak over all items k ≠ i with hj(k) = hj(i).
Xj is the noise in that row. It is a random variable because the hash function was chosen at random. The true counts ak are fixed. Two facts matter:
- X_j ≥ 0, because counts are non-negative.
- Xj is a sum of terms, each of which is either ak (item k collides with i) or 0 (it doesn't).
Step 2: the expected noise
Write 1[·] for an indicator that is 1 when the condition holds and 0 otherwise. Then Xj = Σ over k ≠ i of ak · 1[hj(k) = hj(i)].
Expectation is linear, so we can take it term by term. The expectation of an indicator is the probability of its event:
E[Xj] = Σ over k ≠ i of ak · Pr[hj(k) = hj(i)].
Here we need a property of the hash family. For any two distinct items, the collision probability must be at most 1/w. Hash families with this property are called universal. The pairwise independent family from the previous chapter has it. Substituting gives:
E[Xj] ≤ Σ over k ≠ i of ak / w = (N − a_i) / w ≤ N / w.
Because w = ⌈e/ε⌉ ≥ e/ε, we have 1/w ≤ ε/e. So the expected noise in any row is at most εN/e, which is about 0.37·εN.
Linearity of expectation does not need the collision events to be independent of each other. Only the pairwise collision probability matters, and that is why the weak assumption of pairwise independence is enough.
Step 3: Markov's inequality
Markov's inequality says that for a non-negative random variable X and any t > 0, Pr[X ≥ t] ≤ E[X] / t.
It uses only the mean and non-negativity. It says nothing about the shape of the distribution. Apply it with t = εN:
Pr[Xj ≥ εN] ≤ E[Xj] / (εN) ≤ (N/w) / (εN) = 1 / (wε).
Since w ≥ e/ε, we have wε ≥ e, so:
Pr[X_j ≥ εN] ≤ 1/e ≈ 0.368.
This is why the width has the constant e in it. It makes each row's chance of a bad overcount at most 1/e. Any constant below 1 would work for the proof, and e is the choice that gives the cleanest depth formula in Step 4.
Step 4: independent rows and the minimum
The estimate is the minimum over rows:
estimate(i) = ai + min over j of Xj.
The estimate exceeds a_i + εN only if every row's noise is at least εN, because the minimum is small as soon as one row is small. The row hash functions are drawn independently, so the row events are independent and their probabilities multiply:
Pr[estimate(i) ≥ a_i + εN] ≤ (1/e)^d = e^(−d).
With d = ⌈ln(1/δ)⌉ ≥ ln(1/δ), we get e^(−d) ≤ δ. That completes the proof.
Numbers for a concrete sketch
Take ε = 0.01 and δ = 0.01, the sketch from the worked example. Suppose the stream holds N = 1051 items in total. This is the same stream as before: 1000 apples, 50 bananas and 1 cherry.
| Quantity | Formula | Value |
|---|---|---|
| Width w | ⌈e/ε⌉ | 272 |
| Depth d | ⌈ln(1/δ)⌉ | 5 |
| Error budget εN | 0.01 × 1051 | 10.51 |
| Expected noise for cherry in one row | at most (N − 1)/w | at most 3.86 |
| Markov bound per row | 3.86 / 10.51 | about 0.367 |
| Failure chance, all 5 rows | 0.367^5 | about 0.0067, below δ |
Checking the bound by simulation
The program below measures the noise directly. It builds a skewed stream over 2000 items and picks a rare target item. For each trial it draws fresh hash parameters and computes the collision noise X for d rows. It reports how often one row, and then all rows together, reach the threshold εN.
import math
import random
P = (1 << 61) - 1 # Mersenne prime for the hash family
def overcount(rng, counts, target, w):
"""Noise X for one row: total count of other items sharing target's column."""
a = rng.randrange(1, P)
b = rng.randrange(0, P)
def col(x):
return ((a * x + b) % P) % w
target_col = col(target)
return sum(c for item, c in counts.items()
if item != target and col(item) == target_col)
rng = random.Random(1)
counts = {i: 1 + 2000 // (i + 1) for i in range(2000)} # skewed counts
N = sum(counts.values())
epsilon, delta = 0.01, 0.01
w = math.ceil(math.e / epsilon)
d = math.ceil(math.log(1 / delta))
threshold = epsilon * N
target = 1999 # a rare item
trials = 400
row_fail = all_fail = 0
total_noise = 0
for _ in range(trials):
xs = [overcount(rng, counts, target, w) for _ in range(d)]
total_noise += xs[0]
row_fail += xs[0] >= threshold
all_fail += min(xs) >= threshold
print(f"N={N}, w={w}, d={d}, threshold={threshold:.1f}")
print(f"mean noise per row: {total_noise / trials:.1f} (bound N/w = {N / w:.1f})")
print(f"one-row failure rate: {row_fail / trials:.3f} (bound {1 / math.e:.3f})")
print(f"all-rows failure rate: {all_fail / trials:.3f} (bound {delta})")The observed rates sit well below the bounds. Markov's inequality uses only the mean, so it is loose. The bounds are guarantees for any non-negative noise distribution, not predictions for this stream.
What the proof does and does not give you
Several other assumptions are built into the argument:
- Non-negative counts. Markov needs X ≥ 0. With deletions the noise can be negative and this proof no longer applies. The deletions chapter covers that case.
- N is total stream weight. The error εN scales with the whole stream, not with the item's own count. Rare items can get an error far larger than their true count. The guarantee is meaningful mainly for items whose count is a sizeable fraction of N.
- Hash parameters are random and hidden from the data. The probabilities are over the choice of hash functions. If an adversary knows your parameters, they can build a stream that collides everything. Python's built-in
hash()also differs between processes unless it is seeded. A sketch that is shared or stored must use a stable hash. - Independent rows. If you reuse the same hash function in two rows, those rows fail together. The multiplication in Step 4 then no longer holds.
- The proof is for the plain update. Conservative update, covered in a later chapter, keeps the lower bound and never increases the overcount of any counter. The same upper bound therefore still holds.
Exercise
A sketch uses ε = 0.05 and δ = 0.02, and the stream has N = 10,000 items. Compute w and d. Then give the error budget εN, a bound on the expected noise in one row, and a bound on the chance that all rows fail.
Show answer
w = ⌈e/0.05⌉ = ⌈54.37⌉ = 55 and d = ⌈ln 50⌉ = ⌈3.91⌉ = 4. The error budget is εN = 0.05 × 10,000 = 500. The expected noise in one row is at most N/w = 10,000/55 ≈ 181.8. Markov gives a per-row failure chance of at most 181.8/500 ≈ 0.364, which is below 1/e. All four rows failing has chance at most 0.364^4 ≈ 0.0175, which is below δ = 0.02.
Exercise
The proof multiplies the failure chances of the d rows. Name the assumption that makes this valid, and say what happens to the guarantee if every row uses the same hash function.
Show answer
The row hash functions must be chosen independently. If all rows share one hash function, they all have the same collisions and the same noise X. The chance that all rows fail then equals the chance that one row fails, which is up to 1/e. Depth would add no accuracy.
Exercise
You query the estimates of 1,000 different items and want all of them to be within εN of the truth with probability at least 0.99. What depth do you need?
Show answer
By the union bound, the chance that at least one query fails is at most 1000 × e^(−d). We need this to be at most 0.01, so e^(−d) ≤ 10^(−5) and d ≥ ln(100,000) ≈ 11.5. Use d = 12. This matches d = ⌈ln(m/δ)⌉ with m = 1000 and δ = 0.01.