A stream of items S={a1,a2,...,am} where ai∈[n]. Let the frequencies of elements in [n] be f=⟨f1,f2,...,fn⟩, where fj=∣{ai∈S:ai=j}∣.
Count-Sketch
It returns an estimation f^ of f with space O(ϵ21logn) words, such that with probability at least 1−1/n, for every i∈[n],
∣f^i−fi∣≤ϵ∥f∥2,
where ∥f∥2=∑i=1nfi2.
Problem
In general, a sketch is a concise representation of f. Suppose that we limit our space usage to k words (i.e., ∥f^∥0=k). Then the sketch f^ that minimizes the ℓ2 error
err2k(f)≐∥f−f^∥2
keeps the set H of k items with highest frequencies. In such case, the error is given by
err2k(f)=i∈/H∑fi2
In practice, however, S is not given offline. As we don't know H, we can only compute an f^ with k words that is as good as possible. The question is, how far ∥f−f^∥2 can be from the minimum possible value, namely err2k(f)?
Algorithm
One possible solution, proposed by Cormode and Muthukrishnan [1], is to construct f^ vis Count-Sketch. In particular,
It uses d=O(logn) hash tables.
Each hash table has size w=ϵ23k.
Frequency Oracle Error
Lemma 1. For every i∈[n], with probability 1−1/n2,
∣fi−f^i∣≤kϵerr2k(f).
Remark:If f is k sparse, i.e., the number of non-zero elements of f is at most k, then err2k(f)=0 and the algorithm recovers f exactly.
Proof: Consider a fixed t∈[k]. For i∈[n], let ht(i) the bucket of item i in the t-th hash table. Define
Ai:∃x∈H, s.t. hj(x)=hj(i),
be the event such that some heavy item collides with i in the t-th table. Then by union bound,
Pr[Ai]≤wk=3ϵ2.
Therefore, by the law of total probability and monotonicity of probability, we have
As long as ϵ2/3<1/6, this probability is smaller than 1/2. We can boost the failure probability to 1/n2 by repeating the hash tables d=O(logn) times and taking the median as estimation.
■
Sparse Vector Construction
The next step is keep only set H^ of the k largest items in f^. Note that H^ might not equals to H. To bound the value ∥f−f^∥2, we partition [n] into three parts:
H^,H∖H^,[n]∖(H∪H^).
Lemma 2. ∑i∈H^(fi−f^i)2≤ϵ2(err2k(f))2.
Proof. By lemma 1, each term (fi−f^i)2 has error at most kϵ2(err2k(f))2. The claim then follows from that ∣H^∣=k.
If H∖H^=∅, the claim is trivial. Otherwise, we can construct a bijection between H∖H^ and H^∖H. Then for i∈H∖H^, let j be its corresponding index in H^∖H. Then
[1] G. Cormode and S. Muthukrishnan. Combinatorial algorithms for Compressed Sensing.
[2] Moses Charikar, Lecture 9, CS 369G: Algorithmic Techniques for Big Data