How a count-min sketch estimates a frequency

Short answer: A count-min sketch estimates the frequency of items in a stream with a table of d rows and w counters. Each row has its own hash into the w columns. An update of item x by c adds c to one counter in every row. The estimate is the minimum across the d counters of x. The estimate never undershoots the true frequency when c is positive. With w = ceil(e/ε) and d = ceil(ln(1/δ)), the error is at most ε times the L1 norm of the frequencies, with probability at least 1−δ.


What the problem actually asks

An exact map from items to counts costs memory proportional to the number of distinct items. A sketch fixes the memory in advance and accepts a one-sided error. Collisions add extra counts into a cell. Taking the minimum across independent rows discards some of those collisions, because a collision would have to happen in every row to inflate every candidate.

The minimum is the right reducer for positive updates. A sum or an average would mix the inflated rows back in. A maximum would prefer the worst collision.


What the algorithm guarantees

For a stream of positive updates, the estimate of x is at least the true frequency and exceeds it by at most ε·||f||₁ with probability at least 1−δ, under the width and depth above and pairwise independent hashes. Time per update or query is O(d). Memory is the d by w table. Negative updates destroy the one-sided guarantee unless a different sketch is used. The estimate is not an integer exact count, and two items can share a cell.

The L1 norm is the sum of absolute frequencies, which for a positive stream is the total number of events. A heavy hitter is an item whose estimate exceeds a threshold. The sketch finds candidates. A second pass or a heap of candidates confirms them.


The working steps

  1. Choose ε and δ. Set w = ceil(e/ε) and d = ceil(ln(1/δ)). Allocate a d by w table of zeros and d independent hash functions into 0..w−1.
  2. To add c > 0 occurrences of x, for each row i add c to column hash_i(x).
  3. The estimate of x is the minimum of the d counters the hashes select.
  4. Do not subtract in this basic structure. A negative update can make the minimum fall below the truth.
  5. Compare the estimate with ε·N, where N is the stream length, when deciding whether an item is heavy. The error term is on that scale.

The error bound is probabilistic and one-sided for positive updates. It is not a Bloom filter, which stores no counts, and it is not an exact dictionary. The width must grow as 1/ε. A tiny table does not inherit a tiny ε. Conservative update, which increments a row only up to the current minimum, is a practical improvement and a slight variation of the update rule.


Approaches that look simpler and fail

Approach Why it fails when the estimate is the sum of the rows
Sum or average the d counters Every collision adds to the sum. The minimum is what throws away a row that collided with a heavy item.
Use the bound for a stream of decrements The proof that the estimate is at least the truth uses nonnegative additions. Decrements need a different analysis or a different sketch.
Pick w by memory convenience and still quote an ε of 0.01 ε is about e/w. The width is the accuracy parameter, not a free constant.
Treat the estimate as the exact frequency It is an upper bound with a probabilistic slack. Equal estimates do not prove equal frequencies.

Membership without a count is a Bloom filter. Cardinality without frequencies is HyperLogLog. An exact count of a small domain is an array. Count-min is the frequency sketch with a one-sided additive error.


A working example

This Python fragment estimates frequencies with a count-min sketch. The reported count is the minimum across the rows, so a positive update is never underestimated. It prints 3 1.

import math
counts = {"a": 3, "b": 1, "c": 1}
width, depth = 16, 4
table = [[0] * width for _ in range(depth)]

def pos(row, key):
    h = 0
    for ch in key:
        h = (h * 131 + ord(ch) + row * 17) % width
    return h

for key, c in counts.items():
    for row in range(depth):
        table[row][pos(row, key)] += c

def estimate(key):
    return min(table[row][pos(row, key)] for row in range(depth))

assert estimate("a") >= 3 and estimate("b") >= 1
print(estimate("a"), estimate("b"))

Where it is used

A count-min sketch estimates frequencies in a stream when a full map will not fit, which is the setting of network counters and of the Count-Min structure in RedisBloom. The estimate is an upper bound. A heavy-hitter list such as Misra-Gries answers a narrower question.


When you can drop this algorithm

Drop the sketch when the domain is small enough to count exactly. Keep it for a large stream, positive updates, and an error that may overestimate and must not underestimate.


Why this stays published

We publish this so a search for the count-min sketch returns the minimum across rows and the (ε, δ) sizing of the table. A count-min sketch estimates a positive frequency by the minimum of d hashed counters, never underestimating, with an additive error set by the table width.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *