ArticleslgStudy

computer science

Count sketch

Count sketch is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Count sketch rather than just read about it. In short: Count sketch is a type of dimensionality reduction that is particularly efficient in statistics, machine learning and algorithms. It was invented by Moses Charikar, Kevin Chen and Martin Farach-Colton in an effort to speed up the AMS Sketch by Alon, Matias and Szegedy for approximating the frequency moments of streams (these calculations require counting of the number of occurrences for the distinct elements of the…

Key takeaways

  • Count sketch belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Count sketch to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Count sketch from memory before moving on to harder problems.

Reference excerpt

Count sketch is a type of dimensionality reduction that is particularly efficient in statistics, machine learning and algorithms. It was invented by Moses Charikar, Kevin Chen and Martin Farach-Colton in an effort to speed up the AMS Sketch by Alon, Matias and Szegedy for approximating the frequency moments of streams (these calculations require counting of the number of occurrences for the distinct elements of the stream). The sketch is nearly identical to the Feature hashing algorithm by John Moody, but differs in its use of hash functions with low dependence, which makes it more practical. In order to still have a high probability of success, the median trick is used to aggregate multiple count sketches, rather than the mean. These properties allow use for explicit kernel methods, bilinear pooling in neural networks and is a cornerstone in many numerical linear algebra algorithms.

Intuitive explanation The inventors of this data structure offer the following iterative explanation of its operation:

at the simplest level, the output of a single hash function s mapping stream elements q into {+1, -1} is feeding a single up/down counter C. After a single pass over the data, the frequency n(q) of a stream element q can be approximated, although extremely poorly, by the expected value 𝐄[C⋅s(q)]; a straightforward way to improve the variance of the previous estimate is to use an array of different hash functions si, each connected to its own counter Ci. For each i, the 𝐄[Ci⋅si(q)]=n(q) still holds, so averaging across the i range will tighten the approximation; the previous construct still has a major deficiency: if a lower-frequency-but-still-important output element a exhibits a hash collision with a high-frequency element even for one of the si hashes, n(a) estimate can be significantly affected. Avoiding this requires reducing the frequency of collision counter updates between any two distinct elements. This is achieved by replacing each Ci in the previous construct with an array of m counters (making the counter set into a two-dimensional matrix Ci,j), with index j of a particular counter to be incremented/decremented selected via another set of hash functions hi that map element q into the range {1..m}. Since 𝐄[Ci,hi(q)⋅si(q)]=n(q), averaging across all values of i will work.

Mathematical definition 1. For constants w and t (to be defined later) independently choose d=2t+1 random hash functions h1,…,hd and s1,…,sd such that hi:[n]→[w] and si:[n]→{±1}. It is necessary that the hash families from which hi and si are chosen be pairwise independent. 2. For each item qi in the stream, add sj(qi) to the hj(qi)th bucket of the jth hash. At the end of this process, one has wd sums (Cij) where

Ci,j=∑hi(k)=jsi(k). To estimate the count of qs one computes the following value:

rq=mediani=1dsi(q)⋅Ci,hi(q). The values si(q)⋅Ci,hi(q) are unbiased estimates of how many times q has appeared in the stream. The estimate rq has variance O(min{m12/w2,m22/w}), where m1 is the length of the stream and m22 is ∑q(∑i[qi=q])2. Furthermore, rq is guaranteed to never be more than 2m2/w off from the true value, with probability 1−e−O(t).

Vector formulation Alternatively Count-Sketch can be seen as a linear mapping with a non-linear reconstruction function. Let M(i∈[d])∈{−1,0,1}w×n, be a collection of d=2t+1 matrices, defined by

Mhi(j),j(i)=si(j) for j∈[w] and 0 everywhere else. Then a vector v∈ℝn is sketched by C(i)=M(i)v∈ℝw. To reconstruct v we take vj∗=medianiCj(i)si(j). This gives the same guarantees as stated above, if we take m1=‖v‖1 and m2=‖v‖2.

Relation to Tensor sketch The count sketch projection of the outer product of two vectors is equivalent to the convolution of two component count sketches. The count sketch computes a vector convolution C(1)x∗C(2)xT, where C(1) and C(2) are independent count sketch matrices. Pham and Pagh show that this equals C(x⊗xT) – a count sketch C of the outer product of vectors, where ⊗ denotes Kronecker product. The fast Fourier transform can be used to do fast convolution of count sketches. By using the face-splitting product such structures can be computed much faster than normal matrices.

See also Count–min sketch is a version of algorithm with smaller memory requirements (and weaker error guarantees as a tradeoff). Tensor sketch

References

Further reading Charikar, Moses; Chen, Kevin; Farach-Colton, Martin (2004). "Finding frequent items in data streams" (PDF). Theoretical Computer Science. 312 (1). Elsevier BV: 3–15. doi:10.1016/s0304-3975(03)00400-6. ISSN 0304-3975. Faisal M. Algashaam; Kien Nguyen; Mohamed Alkanhal; Vinod Chandran; Wageeh Boles. "Multispectral Periocular Classification WithMultimodal Compact Multi-Linear Pooling" [1]. IEEE Access, Vol. 5. 2017. Ahle, Thomas; Knudsen, Jakob (2019-09-03). "Almost Optimal Tensor Sketch". ResearchGate. Retrieved 2020-07-11.

Worked examples

Example 1 — a first encounter with Count sketch

Start with the simplest possible case. Write down what Count sketch claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Count sketch before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Count sketch ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Count sketch

In research
Count sketch appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Count sketch in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Count sketch is common in secondary-school and first-year university syllabi. It links to neighbouring topics Dimension reduction, Hash-based data structures, Probabilistic data structures, so understanding it makes those chapters shorter.
In everyday life
Look for Count sketch outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Count sketch” →

How to study Count sketch in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Count sketch means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Count sketch out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Count sketch in simple terms?

Count sketch is a type of dimensionality reduction that is particularly efficient in statistics, machine learning and algorithms. It was invented by Moses Charikar, Kevin Chen and Martin Farach-Colton in an effort to speed up the AMS Sketch by Alon, Matias and Szegedy for approximating the frequenc…

Why does Count sketch matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Count sketch?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Count sketch.

Tags

  • Dimension reduction
  • Hash-based data structures
  • Probabilistic data structures

Keep exploring