{"id":119,"date":"2026-11-13T12:31:53","date_gmt":"2026-11-13T04:31:53","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-count-min-sketch-estimates-a-frequency\/"},"modified":"2026-11-13T21:50:44","modified_gmt":"2026-11-13T13:50:44","slug":"how-a-count-min-sketch-estimates-a-frequency","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-count-min-sketch-estimates-a-frequency\/","title":{"rendered":"How a count-min sketch estimates a frequency"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> A <strong>count-min sketch<\/strong> 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 <strong>minimum<\/strong> across the d counters of x. The estimate never undershoots the true frequency when c is positive. With w = ceil(e\/\u03b5) and d = ceil(ln(1\/\u03b4)), the error is at most \u03b5 times the L1 norm of the frequencies, with probability at least 1\u2212\u03b4.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">For a stream of positive updates, the estimate of x is at least the true frequency and exceeds it by at most \u03b5\u00b7||f||\u2081 with probability at least 1\u2212\u03b4, 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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Choose \u03b5 and \u03b4. Set w = ceil(e\/\u03b5) and d = ceil(ln(1\/\u03b4)). Allocate a d by w table of zeros and d independent hash functions into 0..w\u22121.<\/li>\n<li>To add c &gt; 0 occurrences of x, for each row i add c to column hash_i(x).<\/li>\n<li>The estimate of x is the minimum of the d counters the hashes select.<\/li>\n<li>Do not subtract in this basic structure. A negative update can make the minimum fall below the truth.<\/li>\n<li>Compare the estimate with \u03b5\u00b7N, where N is the stream length, when deciding whether an item is heavy. The error term is on that scale.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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\/\u03b5. A tiny table does not inherit a tiny \u03b5. Conservative update, which increments a row only up to the current minimum, is a practical improvement and a slight variation of the update rule.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the estimate is the sum of the rows<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Sum or average the d counters<\/td>\n<td>Every collision adds to the sum. The minimum is what throws away a row that collided with a heavy item.<\/td>\n<\/tr>\n<tr>\n<td>Use the bound for a stream of decrements<\/td>\n<td>The proof that the estimate is at least the truth uses nonnegative additions. Decrements need a different analysis or a different sketch.<\/td>\n<\/tr>\n<tr>\n<td>Pick w by memory convenience and still quote an \u03b5 of 0.01<\/td>\n<td>\u03b5 is about e\/w. The width is the accuracy parameter, not a free constant.<\/td>\n<\/tr>\n<tr>\n<td>Treat the estimate as the exact frequency<\/td>\n<td>It is an upper bound with a probabilistic slack. Equal estimates do not prove equal frequencies.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>3 1<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import math\ncounts = {&quot;a&quot;: 3, &quot;b&quot;: 1, &quot;c&quot;: 1}\nwidth, depth = 16, 4\ntable = [[0] * width for _ in range(depth)]\n\ndef pos(row, key):\n    h = 0\n    for ch in key:\n        h = (h * 131 + ord(ch) + row * 17) % width\n    return h\n\nfor key, c in counts.items():\n    for row in range(depth):\n        table[row][pos(row, key)] += c\n\ndef estimate(key):\n    return min(table[row][pos(row, key)] for row in range(depth))\n\nassert estimate(&quot;a&quot;) &gt;= 3 and estimate(&quot;b&quot;) &gt;= 1\nprint(estimate(&quot;a&quot;), estimate(&quot;b&quot;))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A <strong>count-min sketch<\/strong> 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for the count-min sketch returns the minimum across rows and the (\u03b5, \u03b4) sizing of the table. <strong>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.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,36],"tags":[],"class_list":["post-119","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-sketches"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":518,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions\/518"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}