How Often Did This Key Appear?
Picture a shop recording views of product pages. The sequence A, B, A, C, B, A contains six views, but the question “How often was A viewed?” has answer three. Keeping a counter for every possible product ID may be too expensive when most IDs are rare. A frequency sketch keeps a small grid of counters, sending each key to a position in each row. Shared positions cause interference; several rows give different chances to avoid a harmful collision.
For a tiny illustration, suppose A has count 3, B has count 2, and C has count 1. In one row A and C share a counter, so reading A there gives 4. In another row A has its own counter and gives 3. Taking the smaller reading recovers 3. This is the Count-Min idea for nonnegative updates. It does not promise that every small reading is exact: a key may collide in every row. Count Sketch uses random signs to make collision noise sometimes positive and sometimes negative, then combines row estimates by a median. Its error scale and update model differ.
The preceding chapters supply the frequency-vector model, concentration bounds, and hash-family assumptions. Here a point query asks for one coordinate of that vector. We will state whether updates can be negative, what quantity sets the error allowance, what randomness the probability covers, and whether a promised answer concerns a fixed key or many adaptively chosen keys.
A frequency sketch compresses many key counters into fewer shared counters. Collisions create query error: Count-Min uses nonnegative collision mass and a minimum, while Count Sketch uses signed collision noise and a median under a different guarantee.
| Term | Intuition and exact example | Non-example or assumption | Question answered |
|---|---|---|---|
| Frequency vector | For A, B, C, $f=(3,2,1)$ | Stream length 6 is not A's frequency | How many net updates belong to each key? |
| Point query | Request $f_A=3$ | “Find every popular key” is a different task | What is one key's count? |
| Collision | A and C share a counter of value 4 | Repeated A updates are not distinct-key collisions | Where does interference enter? |
| Cash register | Every update adds a nonnegative amount | A refund is a negative update | When can collision noise only increase a count? |
| Turnstile | Updates may add or subtract | Final nonnegative counts alone do not make every prefix nonnegative | What signed changes must be supported? |
| Error baseline | $\varepsilon\lVert f\rVert_1$ or $\varepsilon\lVert f\rVert_2$ | “Within 1%” without a denominator is incomplete | How large may error be? |
How Often Did This Key Appear?
Use arrivals A, B, A, C, B, A. The exact counts are A:3, B:2, C:1. Let row 1 put A and C together, B alone; let row 2 put A alone, B and C together. Checkpoint: row 1 counters are 4 and 2; row 2 counters are 3 and 3. Query A by taking the minimum of 4 and 3, giving 3. Query B by taking the minimum of 2 and 3, giving 2. Reflection: if C collided with A in both rows, which query would be too high, and by how much?
Point Queries in a Compressed Frequency Vector
Let $U$ be a universe of keys. Updates $(x,\Delta)$ define $f_x=\sum_{(x,\Delta)}\Delta$. In a cash-register stream every $\Delta\ge0$, so all $f_x\ge0$ and $N=\lVert f\rVert_1=\sum_xf_x$. In a general turnstile stream $\Delta$ may be negative and intermediate frequencies may be signed. A strict-turnstile stream permits negative updates while maintaining $f_x\ge0$ after every prefix. These are different premises for a proof, even when two streams end with the same final vector.
A Count-Min Sketch (CMS) has $d$ rows, $w$ nonnegative counters per row, and hash functions $h_r:U\to\{0,\ldots,w-1\}$. On $(x,\Delta)$ with $\Delta\ge0$, add $\Delta$ to counter $(r,h_r(x))$ for every row $r$. A point query returns $\widehat f_x=\min_r C_{r,h_r(x)}$. In each row the queried counter is $f_x$ plus nonnegative mass from colliding keys. Therefore $\widehat f_x\ge f_x$ deterministically for a valid cash-register sketch. The upper error bound is probabilistic under assumptions on the hashes. A direct signed decrement can make a CMS row smaller than the queried final frequency; the standard one-sided claim cannot simply be carried over to general turnstile streams.
A Count Sketch also has $d$ rows and $w$ counters, but each row uses a bucket hash $h_r$ and a sign hash $s_r:U\to\{-1,+1\}$. On update $(x,\Delta)$ add $s_r(x)\Delta$ to $C_{r,h_r(x)}$. Query row $r$ with $Y_r(x)=s_r(x)C_{r,h_r(x)}$ and return the median of row estimates. With suitable independent hash and sign families, $\mathbb E[Y_r(x)]=f_x$ and its collision-noise variance is at most $\lVert f_{-x}\rVert_2^2/w$, where $f_{-x}$ excludes coordinate $x$. Signed updates remain linear, provided arithmetic does not overflow and the update stream matches the vector model. The estimate may lie above or below truth.
An inner-product query asks for $\langle f,g\rangle=\sum_xf_xg_x$, not one coordinate. Compatible linear sketches can form estimators for such products, but a point-query guarantee alone does not supply an inner-product guarantee. State the shared randomness, which arrays are multiplied, the update models, and the resulting variance separately. Conservative update for CMS increments only the addressed counters currently at the row minimum, by an appropriate amount; it can reduce empirical overestimation for insert-only workloads, but the update is nonlinear and its merge and error claims must be checked for the chosen variant.
flowchart TD
U["Updates define frequency vector f"] --> M{"Update model"}
M -->|Nonnegative| C["Count-Min: add to hashed counters"]
C --> N["Query minimum: one-sided overestimate"]
M -->|Signed allowed| S["Count Sketch: signed counter updates"]
S --> D["Query signed rows, then median"]
N --> Q["Point query for one key"]
D --> Q
U --> I["Inner product requires separate analysis"]
classDef navy fill:#17365D,color:#fff,stroke:#17365D;
classDef teal fill:#147D84,color:#fff,stroke:#147D84;
classDef crimson fill:#C6284D,color:#fff,stroke:#C6284D;
classDef purple fill:#6C3BAA,color:#fff,stroke:#6C3BAA;
class U,M navy; class C,N,Q teal; class S,D purple; class I crimson;
| Claim | Exact scope | Needed assumptions | Counterexample or consequence |
|---|---|---|---|
| CMS one-sided reading | $\widehat f_x\ge f_x$ | Nonnegative updates and counters not overflowing | Signed deletion can make a row too small |
| CMS additive upper error | $\widehat f_x\le f_x+\varepsilon N$ with high probability | Fixed query, suitable hashes, $N=\lVert f\rVert_1$ for cash register | Small $f_x$ may have large relative error |
| Count Sketch row mean | $\mathbb E[Y_r(x)]=f_x$ | Independent, balanced signs for colliding other keys | One row can under- or overestimate |
| Count Sketch row variance | At most $\lVert f_{-x}\rVert_2^2/w$ | Suitable bucket and sign independence | A large colliding coordinate dominates noise |
| Conservative update | Raises selected minimum counters for an insert-only key | Specific variant and update model | Nonlinear state cannot be naïvely merged like standard CMS |
| Inner product | $\langle f,g\rangle$ compares two vectors | Compatible sketches and separate estimator analysis | Point-query success does not imply product success |
For cash-register updates, each CMS row queried at $x$ equals $f_x$ plus a nonnegative collision sum, so taking the minimum never underestimates $f_x$. The signed Count Sketch replaces that invariant with a centered-noise argument and a median, allowing both positive and negative point-query errors.
Collisions, Error Scales and Confidence Rows
For one CMS row and fixed key $x$, write the excess as $Z_r(x)=\sum_{y\ne x}f_y I_{\{h_r(y)=h_r(x)\}}$. Under a universal collision bound $1/w$, linearity gives $\mathbb E[Z_r(x)]\le (N-f_x)/w\le N/w$. The sum is nonnegative. Markov's inequality then gives $\Pr(Z_r(x)>\varepsilon N)\le1/(w\varepsilon)$ (using a non-strict threshold is also possible with the corresponding inequality). If $w\ge e/\varepsilon$, the bound is at most $1/e$ for each row. With independently seeded rows, the minimum exceeds $f_x+\varepsilon N$ only when every row is bad, giving probability at most $e^{-d}$. Choosing $d\ge\ln(1/\delta)$ therefore suffices for failure at most $\delta$ for that fixed query. The familiar dimensions $w=\lceil e/\varepsilon\rceil$ and $d=\lceil\ln(1/\delta)\rceil$ express a sufficient design, not a promise against seed-aware adaptive queries.
For a Count Sketch row, expand the signed reading: $Y_r(x)=f_x+\sum_{y\ne x}f_y s_r(x)s_r(y) I_{\{h_r(y)=h_r(x)\}}$. Balanced sufficiently independent signs make the noise mean zero; cross terms vanish in its second moment under the required sign independence. Pairwise bucket collision control gives $\operatorname{Var}(Y_r(x))\le\lVert f_{-x}\rVert_2^2/w$. Chebyshev makes a deviation of $\sqrt{3/w}\,\lVert f_{-x}\rVert_2$ occur with probability at most $1/3$ in a row. A median of sufficiently many independently seeded rows drives down the chance that a majority are bad exponentially in $d$. This is a two-sided $L_2$-scale statement, unlike the CMS $L_1$ one-sided statement; constants and independence requirements depend on the exact construction.
Why not infer relative error? With $N=1{,}000{,}000$ and $\varepsilon=0.01$, CMS permits absolute error up to $10{,}000$ on a count of $3$, under its successful event. Conversely, a large heavy key can dominate $\lVert f\rVert_2$ and contaminate a Count Sketch row if it collides with the queried key. Increase width or choose an appropriate heavy-hitter and recovery procedure when the target asks for small relative errors or an unknown list of important keys. A simultaneous bound over $Q$ predetermined point queries can allocate $\delta/Q$ to each and apply a union bound; it is not automatically an adaptive-query theorem.
The standard arrays are linear in updates when hashes are fixed: two compatible CMS arrays or Count Sketch arrays can be added counterwise to summarize the sum of their frequency vectors, subject to each method's update-model guarantee. Identical hash definitions, signs, seeds, width, and row order are required. Conservative-update variants alter only some counters based on current minima, so this simple linear merge proof does not apply. An inner-product estimator likewise needs its own collision and variance proof even if the arrays are mergeable.
flowchart TD
A["Fix query key x and random hashes"] --> B{"Sketch type"}
B -->|CMS, nonnegative| C["Collision mass Z is nonnegative"]
C --> D["Expected Z at most N/w"]
D --> E["Markov: one bad row at most 1/e"]
E --> F["Independent rows: min fails at most e^-d"]
B -->|Count Sketch, signs| G["Noise mean zero, variance L2²/w"]
G --> H["Chebyshev: each row usually close"]
H --> I["Median boosts confidence"]
classDef navy fill:#17365D,color:#fff,stroke:#17365D;
classDef teal fill:#147D84,color:#fff,stroke:#147D84;
classDef crimson fill:#C6284D,color:#fff,stroke:#C6284D;
classDef purple fill:#6C3BAA,color:#fff,stroke:#6C3BAA;
class A,B navy; class C,D,E,F teal; class G,H,I purple;
Collisions, Error Scales and Confidence Rows
Given: exact frequencies A:3, B:2, C:1 and a fixed CMS query A. Row 1 maps A and C together, so its counter is $4$ and collision excess is $1$; row 2 leaves A alone, so its counter is $3$ and excess is $0$. The minimum returns $3$. Checkpoint: with $N=6$, both excesses are nonnegative, illustrating the lower bound $\widehat f_A\ge f_A$. Proof exercise: with a $1/w$ collision bound, write $\mathbb E[Z_r(A)]\le (f_B+f_C)/w=3/w$, then apply Markov for a chosen tolerance. Variation: send a signed update $(-C,2)$ to the same counters after the trace. Final C frequency is $-1$; row 1 becomes $2$ while A remains $3$, so the CMS minimum can undercount A. Identify the exact assumption that failed. Count Sketch thought experiment: three sign-corrected A readings $2,5,3$ have median $3$, but a median does not certify this result without the sign and row-independence analysis.
| Proof tool | Establishes | Premise and scale | Failure case |
|---|---|---|---|
| CMS nonnegative decomposition | Each queried row is $f_x+Z_r(x)$ with $Z_r(x)\ge0$ | Cash-register updates | Negative collision mass after turnstile updates |
| Expected collision mass | $\mathbb E[Z_r(x)]\le N/w$ | Fixed key and suitable hash collision bound | Key chosen after seeing seeds |
| Markov plus minimum | Failure at most $e^{-d}$ for $w\ge e/\varepsilon$ | Independent rows; additive $\varepsilon N$ | Repeating one identical row does not amplify |
| Signed variance | $\operatorname{Var}(Y_r(x))\le\lVert f_{-x}\rVert_2^2/w$ | Balanced signs and sufficient independence | Shared or biased signs leave nonzero cross terms |
| Chebyshev plus median | Two-sided $L_2$-scale error with exponential row confidence | Independent rows and calibrated width | Median of correlated bad rows remains bad |
| Union bound over queries | Failure at most $Q\delta$ for $Q$ fixed queries | Per-key bound for predetermined queries | Adaptive query selection needs separate proof |
Network Flows, Product Views and Abuse Counters
Approachable product views. A store counts A, B, A, C, B, A. If page-view events only add one, CMS can estimate each product's views. With the two-row trace above, A and B happen to read exactly despite one collision per row. An observed estimate for a rare product should still be interpreted with an additive total-traffic bound, not as an exact small count. If traffic includes bot events, sketch accuracy says nothing about whether those events are genuine views.
Production network flows. Suppose a telemetry worker receives $100{,}000$ flow updates per second across millions of flow IDs and has $8$ MiB for hashed counters. A fixed-key CMS design for additive allowance $\varepsilon N$ and failure $\delta$ needs $w=\lceil e/\varepsilon\rceil$ and $d=\lceil\ln(1/\delta)\rceil$ counters per row. For $\varepsilon=0.001$ and $\delta=0.01$, that is $w=2{,}719$, $d=5$, or $13{,}595$ counters before counter widths and hash seeds; at eight bytes each, about $106.2$ KiB of counter storage. This is comfortably below the stated budget but does not solve key discovery, identity normalization, late-event correction, or exact billing. If flow updates can subtract bytes, a signed Count Sketch or another justified turnstile method may fit better than CMS's one-sided proof.
Abuse or misleading case. A public API uses a sketch to track request counts per account. A client may choose account IDs after watching responses or may split traffic across many IDs. A fixed-key randomized guarantee does not automatically handle adaptive key selection or sybil identities. A CMS overestimate may wrongly flag an innocent account if the estimate itself is treated as proof of abuse; an exact verification step or conservative escalation policy is needed. Similarly, corrections, refunds, and expirations change the update model. The mathematical bound is about the chosen vector and hash randomness, not about whether the logged events, account identities, or enforcement decision are correct.
Network Flows, Product Views and Abuse Counters: Frequency Estimation & Point Queries
Inputs: views A, B, A, C, B, A; exact counts A:3, B:2, C:1. Use row 1 buckets $(A,C)$ and $(B)$, and row 2 buckets $(A)$ and $(B,C)$. Model selection: cash-register CMS, since every view adds one. Hand calculation: row 1 values $4,2$; row 2 values $3,3$. Query A gives $\min(4,3)=3$, B gives $\min(2,3)=2$, and C gives $\min(4,3)=3$, an overestimate by 2. Expected observation: a single sketch can answer some keys exactly and others poorly. Success criterion: provide each queried bucket, the exact frequency, the returned estimate, and a one-line explanation of the nonnegative collision mass. Changed assumption: a negative correction removes that one-sided guarantee.
Network Flows, Product Views and Abuse Counters: Frequency Estimation & Point Queries
Scenario: $100{,}000$ flow updates/s, millions of possible IDs, $8$ MiB sketch budget, target $\varepsilon=0.001$ of total positive mass for fixed point queries and $\delta=0.01$. Initial CMS budget: $w=2{,}719$, $d=5$, $13{,}595$ eight-byte counters, about $106.2$ KiB before overhead, with five counter touches per update. Review: define bytes versus packets, time window, duplicate delivery, maximum counter value, hash seeds, fixed versus adaptive queries, and whether corrections are signed. If corrections occur, compare a properly parameterized Count Sketch whose $L_2$ error baseline matches the use case; do not substitute the same numerical $\varepsilon$ without recalculating. Evidence: replay exact counts for a stratified key sample, measure tail errors and update latency, test heavy colliders and cross-worker seed compatibility, and validate any enforcement decision against exact logs. Acceptance: a written model, memory and CPU budget, observed error distribution, failure behavior, and an explicit action when assumptions break.
Specify and Test a Frequency Contract
Select a point-query method by update signs, the acceptable absolute error baseline, and whether the output is informational or consequential. Then verify its hash assumptions and resource costs. The practice below moves from a tiny collision trace to a specification that can be tested on production data.
Practice: Specify and Test a Frequency Contract
Level 1 — Define (2 points). For arrivals A, B, A, C, B, A, write $f_A,f_B,f_C$, $\lVert f\rVert_1$, and $\lVert f\rVert_2$. Expected: $3,2,1$, $6$, and $\sqrt{14}$. Distinguish an A point query from finding every heavy hitter.
Level 2 — Derive (2 points). With cash-register CMS, $w\ge e/\varepsilon$ and independent $d$ rows, explain why a fixed key's excessive error probability is at most $e^{-d}$. Expected: nonnegative collision sum, expected excess at most $N/w$, Markov per row, intersection of independent bad-row events. State where each assumption enters.
Level 3 — Counterexample (2 points). Start with row 1 A/C collision and A count 3, C count 1. Apply $(-C,2)$ and recompute the row's A reading. Expected: $2$ although true A is $3$; the final signed C count is $-1$. Identify why a CMS minimum need not overestimate in general turnstile streams.
Level 4 — Interpret (2 points). For $N=1{,}000{,}000$, $\varepsilon=0.01$, true key count $3$, state CMS's permitted additive error on a successful query. Expected: up to $10{,}000$; it does not offer a 1% relative error on that key. Explain which count or decision would need exact confirmation.
Level 5 — Design (2 points). Draft a frequency contract for product views with optional refunds/corrections: exact target and time scope, update signs, $L_1$ versus $L_2$ baseline, $\delta$ and query count, hash independence, counter width, memory and update costs, seed compatibility, and downstream action. Artifacts: trace two colliding keys by hand, compare estimates against exact replay, and report error quantiles for rare and frequent keys separately. Rubric: model and target 2, proof assumptions 2, parameter/resource budget 2, counterexample 2, operational validation 2. Stretch: specify the separate estimator and proof needed for an inner-product query or cross-worker merge.
Before trusting a point query, write down the key and update model, the error norm and probability scope, hash and row independence, counter range, and whether anyone may choose queries after seeing answers. This page builds on probability and hashing; the next heavy-hitter and frequency-moment topics turn these per-key estimates into broader questions with additional proofs.
Next in the Series
In Heavy Hitters & Frequent Items, we'll explore Recover frequent items without storing the universe..