Sub topic 13
Deletions and the turnstile model
This chapter assumes you have the CountMinSketch class from earlier in this tutorial and understand why the row minimum never undercounts when all updates are positive.
Three ways a stream can change counts
So far every update added to a count. Real streams sometimes subtract: an item is unfollowed, a cache entry is evicted, or a refund cancels a sale. Streaming algorithms name these cases by the kind of update they allow.
| Model | Updates allowed | Counts can go negative? |
|---|---|---|
| Cash register | Only positive increments | No |
| Strict turnstile | Increments and decrements | No, a count never drops below zero |
| General turnstile | Increments and decrements | Yes |
The name comes from a subway turnstile, which counts people going in and out.
Deletions in the strict turnstile model
The table is just a grid of sums, so a deletion is an update with a negative count. The add method from earlier already accepts one.
cms = CountMinSketch(epsilon=0.01, delta=0.01)
cms.add("apple", 10)
cms.add("apple", -4) # delete 4 occurrences
est = cms.estimate("apple")
assert est >= 6 # true count is 6, still never an undercount
print(est)In the strict model, every counter in a cell is the sum of the true counts of all items that hash to it. Each of those true counts is zero or more. So each cell is at least the true count of your item, and the row minimum is still an upper bound. The one-sided guarantee survives.
Deletions also help accuracy. The overcount comes from collisions with other items, and it is bounded by ε times the current sum of counts. When counts are removed, that sum shrinks and so does the error.
What breaks in the general turnstile model
If counts can go negative, a cell can hold a positive count from one item and a negative count from another, and the two cancel. A cell can then fall below your item's true count. The row minimum picks the most negative noise, so it can undercount badly. The one-sided property is gone.
The usual fix is to take the median of the d row values instead of the minimum. Noise in a row is now equally likely to push the value up or down. The median ignores the few rows that are far off in either direction, as long as most rows are close.
import statistics
def estimate_median(cms, item):
return statistics.median(
row[col] for row, col in zip(cms.table, cms._cols(item))
)The median has costs:
- The error is two-sided. The result may be too high or too low.
- The bound is weaker. It is a constant multiple of ε times the sum of the absolute values of all counts, and you need a somewhat wider and deeper table to reach a given δ.
- With an even depth d, the median averages the two middle rows, which is fine but slightly changes the error analysis.
Interactions with other techniques
- Conservative update relies on counters only ever rising to the new minimum. It does not work with deletions, so use plain
addin any turnstile stream. - Merging still works, because adding tables cell by cell handles negative values the same way.
- Heavy hitters with a heap become harder. An item in the heap can drop below the threshold after a deletion, and an evicted item can become heavy again without any new arrival of that item. The simple
top_kfrom the earlier chapter assumes inserts only.
If your stream really needs signed counts with good accuracy, look at Count Sketch. It multiplies each update by a random plus or minus one sign per row, so collision noise averages to zero, and it also uses a median.
Choosing an estimator
Exercise
A sketch tracks items in stock. Each sale subtracts 1 and each restock adds a positive amount, and a sale is only accepted if stock is at least 1. Is this the strict or general turnstile model, and which estimator should you use?
Show answer
This is the strict turnstile model, because true counts never go below zero. Use the row minimum. Each cell is a sum of non-negative true counts, so the estimate never falls below the true stock. The deletions also shrink the sum that bounds the overcount.