Sub topic 7
Sizing a sketch: w = ⌈e/ε⌉ and d = ⌈ln(1/δ)⌉
This chapter assumes you know the sketch layout (d rows of w counters, one hash per row) and the estimate rule (the minimum across rows). It also assumes you know the guarantee: an estimate is never low, and it is high by at most εN with probability at least 1 − δ. Here you turn those two numbers into a table size.
From error targets to table dimensions
You pick two numbers before building the sketch:
- ε (epsilon) is the allowed overcount as a fraction of the total stream length N.
- δ (delta) is the probability that an estimate exceeds that allowance.
Two formulas give the dimensions:
- Width: w = ⌈e / ε⌉, where e ≈ 2.718 is Euler's number.
- Depth: d = ⌈ln(1/δ)⌉.
Width controls how many items share a counter in one row. A wider row means fewer collisions, so less overcounting. Depth controls how many independent chances you get to find a row with little collision noise. Each extra row cuts the failure probability by a constant factor.
Computing the sizes
The constructor in the earlier code already does this. The snippet below isolates the calculation for a tighter target.
import math
epsilon = 0.001 # overcount at most 0.1% of total stream length N
delta = 0.01 # allowed failure probability
w = math.ceil(math.e / epsilon)
d = math.ceil(math.log(1 / delta))
print(w, d, w * d) # 2719 columns, 5 rows, 13595 countersWith 8-byte counters, 13,595 cells use about 109 KB. The stream can contain a billion distinct items and the table stays the same size. Memory depends only on ε and δ.
Now check the guarantee on a small sketch. With ε = 0.01 and δ = 0.01, the table is 272 by 5.
cms = CountMinSketch(epsilon=0.01, delta=0.01)
for _ in range(1000):
cms.add("apple")
for _ in range(50):
cms.add("banana")
cms.add("cherry")
print(cms.w, cms.d) # table dimensions: 272 columns, 5 rows
for word, true_count in [("apple", 1000), ("banana", 50), ("cherry", 1)]:
est = cms.estimate(word)
assert est >= true_count # never an undercount
print(word, true_count, est)Here N = 1051, so the allowed overcount is εN ≈ 10.5. The three items are few enough that collisions are unlikely, so the estimates will usually equal the true counts.
How the cost scales
| Change | Effect on w | Effect on d | Effect on memory |
|---|---|---|---|
| Halve ε | Doubles | None | Doubles |
| Square δ (for example 0.1 to 0.01) | None | Doubles | Doubles |
| Tenfold more distinct items | None | None | None |
Accuracy in ε is expensive, because memory grows as 1/ε. Confidence in δ is cheap, because memory grows only as ln(1/δ). Going from δ = 0.01 to δ = 0.0001 adds only five rows (d from 5 to 10).
Common errors
- Treating δ as a per-sketch failure rate. The guarantee holds for each query separately. If you query many items, a few may exceed the bound. To make all of m queries hold together, use δ/m, which costs only ln(m) extra rows.
- Rounding down. Both formulas use the ceiling, so round up. A width of 271 would not meet ε = 0.01.
- Forgetting that N grows. The absolute error εN grows as the stream grows, while the table stays the same size. For a long-running stream, either choose ε for the final N or start a new sketch per time window.
- Using Python's
hash()across processes. Sizing only holds if the hash functions behave as intended. If you share or merge sketches, use a stable hash.
Exercise
You need estimates that overcount by at most 0.5% of the stream length, and a failure probability of at most 0.1%. What are w and d, and how many counters does the table hold?
Show answer
ε = 0.005 gives w = ⌈e / 0.005⌉ = ⌈543.66⌉ = 544. δ = 0.001 gives d = ⌈ln(1000)⌉ = ⌈6.908⌉ = 7. The table holds 544 × 7 = 3808 counters.
Exercise
A sketch is sized for ε = 0.01. Your team halves ε to 0.005 and also tightens δ from 0.01 to 0.0001. By what factor does the memory grow?
Show answer
Width doubles (272 to 544). Depth goes from ⌈ln 100⌉ = 5 to ⌈ln 10000⌉ = ⌈9.21⌉ = 10, so it also doubles. Memory grows by a factor of 4.