Sub topic 11
Conservative update to reduce overcounting
This chapter assumes you know the CountMinSketch class from the earlier chapters: its add, estimate, and _cols methods, and the idea that an estimate is the minimum over d row counters. You should also be comfortable with the one-sided error property.
The problem with the standard update
A standard add increments one counter in every row. Suppose "cherry" is rare, but in row 3 it shares a counter with a very frequent item. Its row-3 counter is large and is wasted on cherry, but other rows may hold smaller values. The row minimum already ignores the inflated counters when it answers a query. The inflated counters still hurt, though. Every later item that hashes to them inherits the extra count.
Conservative update avoids creating that extra inflation in the first place.
The idea
When you add an item, first compute its current estimate, which is the minimum of its d counters. The new estimate should be at least estimate + count, and no more is needed. So raise each of the item's counters only as far as that target. Counters that are already higher stay as they are.
Implementation
def add_conservative(self, item, count=1):
cols = list(self._cols(item))
current = min(self.table[r][c] for r, c in enumerate(cols))
target = current + count
for r, c in enumerate(cols):
self.table[r][c] = max(self.table[r][c], target)
self.total += count
CountMinSketch.add_conservative = add_conservativeThe estimate method does not change. You can use the same query code on a table built either way.
Why it is still safe
After every update, each of the item's counters is at least current + count. So the new minimum is at least the old minimum plus count. If the old estimate was at least the true count, the new estimate is at least the true count plus count. That is the never-undercount property, and it holds by induction.
Each cell is also never larger than the matching cell in a standard sketch that saw the same stream with the same hash parameters. Estimates therefore can only be equal or lower, and they stay at or above the true count.
Comparing the two
This example uses a deliberately small table so that collisions are common. Both sketches share the same seed, so they use the same hash functions.
std = CountMinSketch(epsilon=0.1, delta=0.01, seed=1)
con = CountMinSketch(epsilon=0.1, delta=0.01, seed=1)
true = {}
for i in range(200):
item = f"item{i}"
true[item] = 1 + i % 7
for _ in range(true[item]):
std.add(item)
con.add_conservative(item)
std_err = con_err = 0
for item, n in true.items():
assert n <= con.estimate(item) <= std.estimate(item)
std_err += std.estimate(item) - n
con_err += con.estimate(item) - n
print("total overcount, standard: ", std_err)
print("total overcount, conservative:", con_err)The assertion holds on every run. The exact error totals depend on Python's per-process string hashing, but the conservative total is never larger than the standard one, and it is usually much smaller.
Limits
- No guaranteed bound better than the standard one. The (ε, δ) guarantee still applies. Conservative update usually does better in practice, but the proof gives you the same bound.
- No deletions. The method relies on counters only growing. Negative counts would break the never-undercount property, so it does not work in the turnstile model.
- Merging loses exactness. Adding two conservative tables cell by cell still gives estimates at or above the true counts. The result may differ from a conservative sketch built on the combined stream.
- Slightly slower updates. Each add does a read pass before the write pass.
Exercise
A sketch has d = 3 and an item's three counters hold 10, 4, and 7. You call add_conservative(item, 2). What are the three counters afterwards?
Show answer
The current estimate is min(10, 4, 7) = 4, so the target is 4 + 2 = 6. Each counter becomes max(old, 6), giving 10, 6, and 7.