Sub topic 5
Why the estimate can only be too high
This chapter assumes you know basic Python, hash tables, and expected value. It also assumes you have seen the sketch's structure: a table of d rows and w columns, with one hash function per row. You should also know how add and estimate work. Here we explain why estimate never returns less than the true count.
The claim
For any item x, the sketch's estimate satisfies:
- estimate(x) ≥ true count of x
The estimate can be too high. It is never too low. This is called one-sided error. The property holds with certainty, for every item and every stream, not just with high probability.
Why a single cell never undercounts
Each add(x, c) call increases one counter in every row by c. So in row j, the cell at column h_j(x) always holds at least the total of all counts added for x.
That cell can also hold counts from other items. Another item y with h_j(y) = h_j(x) is a collision. A collision adds y's count to the same cell. Counts are only ever added, so a collision can only push the cell upward.
For row j, the cell value is:
- count of
x+ counts of all colliding items in that row
The colliding part is never negative, so every row's cell is at least the true count of x.
Why the minimum keeps the property
The estimate takes the minimum across the d cells. Every one of those cells is at least the true count. The smallest of values that are all at least c is also at least c. So the minimum cannot fall below the true count.
The minimum is also the best choice. It picks the row with the least collision noise. If any one row has no collisions for x, the estimate is exact.
Checking it in code
This example uses the CountMinSketch class from the earlier chapters. It uses a very small table on purpose, so that collisions are common. The assertion never fails, even when the estimates are far off.
import math
import random
class CountMinSketch:
def __init__(self, epsilon, delta, seed=0):
self.w = math.ceil(math.e / epsilon)
self.d = math.ceil(math.log(1 / delta))
self.p = (1 << 61) - 1
rng = random.Random(seed)
self.params = [
(rng.randrange(1, self.p), rng.randrange(0, self.p))
for _ in range(self.d)
]
self.table = [[0] * self.w for _ in range(self.d)]
self.total = 0
def _cols(self, item):
x = hash(item) % self.p
for a, b in self.params:
yield ((a * x + b) % self.p) % self.w
def add(self, item, count=1):
for row, col in zip(self.table, self._cols(item)):
row[col] += count
self.total += count
def estimate(self, item):
return min(row[col] for row, col in zip(self.table, self._cols(item)))
# Epsilon of 0.5 gives a tiny table, so collisions are frequent.
cms = CountMinSketch(epsilon=0.5, delta=0.1)
print(cms.w, cms.d) # 6 columns, 3 rows
true_counts = {}
rng = random.Random(1)
for _ in range(2000):
item = f"item{rng.randrange(100)}"
cms.add(item)
true_counts[item] = true_counts.get(item, 0) + 1
overcounts = []
for item, true in true_counts.items():
est = cms.estimate(item)
assert est >= true # holds for every item
overcounts.append(est - true)
print("max overcount:", max(overcounts))
print("min overcount:", min(overcounts))With 100 items sharing 6 columns, most estimates are well above the truth. The smallest overcount is still zero or more.
When the property breaks
The guarantee depends on counts only ever increasing. Two situations can break it:
- Negative updates. If
add(y, -3)is allowed, a collision can lower a cell belowx's true count, so the estimate can undercount. Handling deletions needs a different estimator, covered in a later chapter. - Mismatched merges. Adding tables from sketches with different hash parameters mixes counts from unrelated cells, and the one-sided guarantee no longer applies.
Conservative update, covered later, keeps the property for non-negative counts. It only raises a cell as far as the new minimum estimate requires.
Why this matters in practice
Because estimates only err upward, you can draw safe conclusions from them:
- If the estimate for
xis below a threshold, the true count is also below it. - If the estimate is above a threshold, the true count might still be lower. That is a false positive.
So a sketch never misses an item that really is frequent, but it can wrongly flag a rare one. This is the basis for heavy-hitter detection.
Exercise
A sketch has 3 rows. For item x, the three cells hold 40, 55, and 90. The true count of x is 38. What does estimate(x) return, and could the true count be 45?
Show answer
estimate(x) returns 40, the minimum of the three cells. The true count cannot be 45, because every cell is at least the true count and the smallest cell is 40. The true count must be at most 40 and, here, it is 38.
Exercise
Suppose a sketch allowed a call add("y", -5) and "y" collides with "x" in one row. Explain why estimate("x") could then return less than the true count of "x".
Show answer
The cell shared by x and y would drop by 5. If y's earlier counts were smaller than 5 in total, or if the cell was otherwise dominated by x's own count, the cell would fall below x's true count. The minimum would then pick that cell, so the estimate would undercount. The proof relied on collisions adding only non-negative amounts.