Sub topic 6
The (ε, δ) guarantee
This chapter assumes you can read the sketch code from the earlier chapters: a table of d rows and w columns, one hash per row, and an estimate taken as the minimum across rows. Here you learn exactly what the sketch promises and what it doesn't.
The statement
Let f(x) be the true count of item x. Let N be the total of all counts added so far, which is the stream length when every add uses count 1. Let f̂(x) be the sketch's estimate. Two parameters control the guarantee:
ε(epsilon) is the error fraction. It sets how far above the truth an estimate may land, as a share ofN.δ(delta) is the failure probability. It sets how often an estimate may break theεbound.
The guarantee has two parts:
- Always:
f̂(x) ≥ f(x). The estimate never undercounts. - With probability at least
1 - δ:f̂(x) ≤ f(x) + εN.
Together, with probability at least 1 - δ, the estimate falls in the range from f(x) to f(x) + εN.
What the numbers mean in practice
The error bound is εN, not a percentage of f(x). It grows with the length of the stream, and it is the same for every item.
import math
epsilon = 0.001
delta = 0.01
N = 1_000_000
w = math.ceil(math.e / epsilon)
d = math.ceil(math.log(1 / delta))
max_overcount = epsilon * N
print(w, d) # 2719 5
print(max_overcount) # about 1000After one million events, an estimate is within about 1,000 of the truth, at least 99% of the time. For an item that appeared 200,000 times, that is a 0.5% error. For an item that appeared 50 times, the estimate could be 1,050, which is a very large relative error.
The bound is also a worst case. Most estimates are much closer to the truth, because a typical cell collides with far fewer than εN units of other traffic.
What the probability covers
The 1 - δ applies to one query for one item. It does not say that all items are correct at once. If you query many items, a fraction of them may exceed the bound. To make a whole set of m queries correct together, you can set δ' = δ/m using the union bound, at the cost of a deeper table. Since depth is ⌈ln(1/δ)⌉, the cost grows only logarithmically in m.
The probability comes from the random choice of hash functions, not from the data. The input stream can be anything, even chosen by an adversary who doesn't know your hash parameters. This is why the guarantee holds without assumptions about the data distribution.
Why the two parameters cost different amounts
The table sizes are w = ⌈e/ε⌉ and d = ⌈ln(1/δ)⌉. Halving ε doubles the width, so memory doubles. Cutting δ by a factor of 10 adds only about 2.3 rows. Confidence is cheap, and precision is expensive. Neither size depends on how many distinct items appear.
| Goal | Change | Memory effect |
|---|---|---|
| Error 10 times smaller | `ε` / 10 | about 10 times more columns |
| Failure 10 times rarer | `δ` / 10 | about 2.3 more rows |
| Stream 10 times longer | none | none, but `εN` is 10 times larger |
The last row is easy to miss. The table size stays fixed, but the absolute error εN grows with the stream. To hold the absolute error constant on a growing stream, you must shrink ε and rebuild.
Where the guarantee comes from
Each row alone overcounts by more than εN with probability at most 1/e. The hash functions are random, and the expected extra count from collisions is at most εN/e. Markov's inequality turns that expectation into the probability bound. The estimate takes the minimum across d rows, so it fails only if every row fails. With independent rows that happens with probability at most (1/e)^d, and d = ⌈ln(1/δ)⌉ makes this at most δ. A later chapter works through this argument.
Exercise
A sketch sees a stream of N = 2,000,000 events. You want estimates within ε = 0.0005 of N, with failure probability at most δ = 0.001. How many columns and rows do you need, and what is the largest overcount you should expect when the guarantee holds?
Show answer
Width w = ⌈e / 0.0005⌉ = ⌈5436.56⌉ = 5437. Depth d = ⌈ln(1 / 0.001)⌉ = ⌈6.908⌉ = 7. The largest overcount is εN = 0.0005 × 2,000,000 = 1,000. The estimate is never below the true count, and with probability at least 99.9% it is at most 1,000 above it.