Sub topic 14
When to use it and when not to
This chapter assumes you have built a count-min sketch with add and estimate, and that you know the (ε, δ) guarantee: with probability at least 1 − δ, an estimate exceeds the true count by at most εN, where N is the stream length. Here you decide when that trade is worth making.
What you get
A sketch uses w × d counters, with w = ⌈e/ε⌉ and d = ⌈ln(1/δ)⌉. Those two numbers depend only on ε and δ. They do not depend on how many distinct items arrive. Each update and each query costs d hash computations. Two sketches with the same parameters merge by cell-wise addition.
You pay for this with approximate answers. Every estimate can be too high, never too low, and the possible overcount is a fixed fraction of the whole stream, not of the item's own count.
Check the size before you commit
A sketch only saves memory when the exact table would be much larger. This function compares the two.
import math
def sketch_counters(epsilon, delta):
w = math.ceil(math.e / epsilon)
d = math.ceil(math.log(1 / delta))
return w * d
def worth_sketching(distinct_items, epsilon, delta):
# An exact dictionary also stores each key, so it costs more per item
# than one counter. A factor of 2 is a rough, conservative threshold.
return distinct_items > 2 * sketch_counters(epsilon, delta)
print(sketch_counters(0.001, 0.01)) # 13595
print(worth_sketching(5_000, 0.001, 0.01)) # False
print(worth_sketching(10_000_000, 0.001, 0.01)) # TrueWith 5,000 distinct items, a plain dictionary is smaller and exact. With 10 million, the sketch uses a tiny fraction of the memory.
Good fits
- Heavy hitters. Items that make up more than εN of the stream are estimated well. Pair the sketch with a small heap, as in the top-k tracker.
- Skewed data. Word frequencies, URL hits, and network flows have a few huge counts. The εN error is small next to them.
- Distributed counting. Each machine keeps a sketch with shared parameters, and you merge them.
- Fixed memory budgets. Routers, embedded devices, and streaming jobs where the number of distinct items is unbounded.
Poor fits
- Rare items. If εN is 1,000, an item that appeared 3 times can be reported as 900. The estimate carries no useful information about it.
- Uniform data. If every item has a similar count, no item stands out above εN.
- Exact answers. Billing, auditing, and anything where an overcount is a bug.
- Other questions. Use a Bloom filter for set membership and HyperLogLog for the number of distinct items. A count-min sketch answers neither.
Choosing among alternatives
| Need | Tool | Trade-off |
|---|---|---|
| Exact counts, few distinct items | Dictionary or counter | Memory grows with distinct items |
| Frequencies, one-sided error, heavy hitters | Count-min sketch | Overcounts, error scales with N |
| Frequencies with deletions or unbiased error | Count Sketch | Uses random signs and a median, and needs a wider table for the same ε |
| Number of distinct items | HyperLogLog | Gives no per-item counts |
Count Sketch bounds its error by the L2 norm of the frequency vector instead of N. That can be much tighter on skewed data. In return you lose the guarantee that estimates never undercount.
Practical warnings
Exercise
A stream has N = 2,000,000 events. You build a sketch with ε = 0.0005 and δ = 0.01. An item appeared 600 times. Is the estimate trustworthy, and what are w and d?
Show answer
The error bound is εN = 0.0005 × 2,000,000 = 1,000. The true count of 600 is below that, so the estimate could be as high as about 1,600 and tells you little about this item. The sketch sizes are w = ⌈e/0.0005⌉ = 5,437 and d = ⌈ln(100)⌉ = 5. To estimate counts near 600 reliably, you need εN well below 600, so ε below about 0.0003, or an exact method.