Background

A stream of items S={a1,a2,...,am}S = \{ a_1, a_2, ..., a_m \} where ai∈[n]a_i \in [n]. Let the frequencies of elements in [n][n] be f=<f1,f2,...,fn>f = \left< f_1, f_2, ..., f_n \right>, where fj=∣{ai∈S:ai=j}∣f_j = |\{ a_i \in S : a_i = j \}|.

Count-Sketch

It returns an estimation f^\hat f of ff with space O(1ϵ2log⁡n)O(\frac{1}{\epsilon^2} \log n) words, such that with probability at least 1−1/n1 - 1 / n, for every i∈[n]i \in [n],

∣f^i−fi∣≤ϵ∥f∥2, |\hat f_i - f_i| \le \epsilon \Vert f \Vert_2,

where ∥f∥2=∑i=1nfi2\Vert f \Vert_2 = \sqrt {\sum_{i = 1}^n f_i^2 }.

Problem

In general, a sketch is a concise representation of ff. Suppose that we limit our space usage to kk words (i.e., ∥f^∥0=k\Vert \hat f \Vert_0 = k). Then the sketch f^\hat f that minimizes the ℓ2\ell_2 error

err2k(f)≐∥f−f^∥2 err_2^k (f) \doteq \Vert f - \hat f \Vert_2

keeps the set HH of kk items with highest frequencies. In such case, the error is given by

err2k(f)=∑i∉Hfi2 err_2^k (f) = \sqrt {\sum_{i \notin H} f_i^2}

In practice, however, SS is not given offline. As we don't know HH, we can only compute an f^\hat f with kk words that is as good as possible. The question is, how far ∥f−f^∥2\Vert f - \hat f \Vert_2 can be from the minimum possible value, namely err2k(f)err_2^k(f)?

Algorithm

One possible solution, proposed by Cormode and Muthukrishnan [1], is to construct f^\hat f vis Count-Sketch. In particular,

  1. It uses d=O(log⁡n)d = O(\log n) hash tables.
  2. Each hash table has size w=3kϵ2w = \frac{3k}{\epsilon^2}.

Frequency Oracle Error

Lemma 1. For every i∈[n]i \in [n], with probability 1−1/n21 - 1 /n^2,

∣fi−f^i∣≤ϵkerr2k(f). |f_i - \hat f_i| \le \frac{\epsilon}{\sqrt k } err_2^k(f).

Remark: If ff is kk sparse, i.e., the number of non-zero elements of ff is at most kk, then err2k(f)=0err_2^k(f) = 0 and the algorithm recovers ff exactly.

Proof: Consider a fixed t∈[k]t \in [k]. For i∈[n]i \in [n], let ht(i)h_t(i) the bucket of item ii in the tt-th hash table. Define

Ai:∃x∈H, s.t. hj(x)=hj(i), A_i : \exist x \in H, \text{ s.t. } h_j(x) = h_j(i),

be the event such that some heavy item collides with ii in the tt-th table. Then by union bound,

Pr⁡[Ai]≤kw=ϵ23. \Pr[A_i] \le \frac{k}{w} = \frac{\epsilon^2}{3}.

Therefore, by the law of total probability and monotonicity of probability, we have

Pr⁡[∣fi−hi(j)∣≥ϵkerr2k(f)]≤Pr⁡[Ai]+Pr⁡[∣fi−hi(j)∣≥ϵkerr2k(f)∣Aˉi]≤Pr⁡[Ai]+Var[fi−hi(j)∣Aˉi](ϵkerr2k(f))2≤ϵ23+(err2k(f))2/w(ϵkerr2k(f))2≤ϵ23+13 \begin{aligned} \Pr \left[ |f_i - h_i(j)| \ge \frac{\epsilon}{\sqrt k } err_2^k(f) \right] &\le \Pr[A_i] + \Pr \left[ |f_i - h_i(j) | \ge \frac{\epsilon}{\sqrt k } err_2^k(f) \mid \bar A_i \right] \\ &\le \Pr[A_i] + \frac{ \mathbb{Var} [ f_i - h_i(j) \mid \bar A_i ] }{ (\frac{\epsilon}{\sqrt k } err_2^k(f) )^2 } \\ &\le \frac{\epsilon^2}{3} + \frac{(err_2^k(f))^2 / w }{ (\frac{\epsilon}{\sqrt k } err_2^k(f) )^2 } \\ &\le \frac{\epsilon^2}{3} + \frac{1}{3} \end{aligned}

As long as ϵ2/3<1/6\epsilon^2 / 3 < 1 / 6, this probability is smaller than 1/21 / 2. We can boost the failure probability to 1/n21/ n^2 by repeating the hash tables d=O(log⁡n)d = O(\log n) times and taking the median as estimation.

■\blacksquare

Sparse Vector Construction

The next step is keep only set H^\hat H of the kk largest items in f^\hat f. Note that H^\hat H might not equals to HH. To bound the value ∥f−f^∥2\Vert f - \hat f \Vert_2, we partition [n][n] into three parts:

H^,H∖H^,[n]∖(H∪H^). \hat H, \quad H \setminus \hat H, \quad [n] \setminus (H \cup \hat H).

Lemma 2. ∑i∈H^(fi−f^i)2≤ϵ2(err2k(f))2\sum_{i \in \hat H} (f_i - \hat f_i)^2 \le \epsilon^2 (err_2^k(f))^2.

Proof. By lemma 1, each term (fi−f^i)2(f_i - \hat f_i)^2 has error at most ϵ2k(err2k(f))2\frac{\epsilon^2}{k} (err_2^k(f))^2. The claim then follows from that ∣H^∣=k|\hat H| = k.

■\blacksquare

Lemma 3. ∑i∈H∖H^fi2≤∑j∈H^∖Hf^j2+(2ϵ+ϵ2)(err2k(f))2\sum_{i \in H \setminus \hat H} f_i ^2 \le \sum_{j \in \hat H \setminus H} \hat f_j^2 + (2 \epsilon + \epsilon^2) ( err_2^k(f))^2

Proof. ∣H∣=∣H^∣|H| = |\hat H|, we have ∣H∖H^∣=∣H^∖H∣|H \setminus \hat H| = |\hat H \setminus H|.

If H∖H^=∅H \setminus \hat H = \emptyset, the claim is trivial. Otherwise, we can construct a bijection between H∖H^H \setminus \hat H and H^∖H\hat H \setminus H. Then for i∈H∖H^i \in H \setminus \hat H, let jj be its corresponding index in H^∖H\hat H \setminus H. Then

fj=(fj−f^j)+(f^j−f^i)+(f^i−fi)+fi≥−ϵkerr2k(f)+0−ϵkerr2k(f)+fi \begin{aligned} f_j &= (f_j - \hat f_j) + (\hat f_j - \hat f_i) + (\hat f_i - f_i) + f_i \\ &\ge - \frac{\epsilon}{\sqrt k } err_2^k(f) + 0 - \frac{\epsilon}{\sqrt k } err_2^k(f) + f_i \end{aligned}

Therefore,

fi≤fj+2ϵkerr2k(f) f_i \le f_j + \frac{2\epsilon}{\sqrt k } err_2^k(f)

and

∑i∈H∖H^fi2≤∑j∈H^∖Hfj2+4ϵk(∑j∈H^∖Hfj)+4ϵ2(err2k(f))2≤∑j∈H^∖Hfj2+4ϵkerr2k(f)(∑j∈H^∖Hfj)+4ϵ2(err2k(f))2≤∑j∈H^∖Hfj2+4ϵkerr2k(f)(k1k∑j∈H^∖Hfj2)+4ϵ2(err2k(f))2=∑j∈H^∖Hfj2+4ϵ(err2k(f))2+4ϵ2(err2k(f))2=∑j∈H^∖Hfj2+(4ϵ+4ϵ2)(err2k(f))2 \begin{aligned} \sum_{i \in H \setminus \hat H} f_i^2 &\le \sum_{j \in \hat H \setminus H} f_j^2 + \frac{4 \epsilon}{\sqrt k } \left( \sum_{j \in \hat H \setminus H} f_j\right) + 4 \epsilon^2 (err_2^k(f))^2 \\ &\le \sum_{j \in \hat H \setminus H} f_j^2 + \frac{4 \epsilon}{\sqrt k } err_2^k(f) \left( \sum_{j \in \hat H \setminus H} f_j\right) + 4 \epsilon^2 (err_2^k(f))^2 \\ &\le \sum_{j \in \hat H \setminus H} f_j^2 + \frac{4 \epsilon}{\sqrt k } err_2^k(f) \left( k \sqrt{ \frac{1}{k} \sum_{j \in \hat H \setminus H} f_j^2 } \right) + 4 \epsilon^2 (err_2^k(f))^2 \\ &= \sum_{j \in \hat H \setminus H} f_j^2 + 4 \epsilon (err_2^k(f))^2 + 4 \epsilon^2 (err_2^k(f))^2 \\ &= \sum_{j \in \hat H \setminus H} f_j^2 + (4 \epsilon + 4 \epsilon^2) (err_2^k(f))^2 \end{aligned}

□\square

Reference

[1] G. Cormode and S. Muthukrishnan. Combinatorial algorithms for Compressed Sensing.
[2] Moses Charikar, Lecture 9, CS 369G: Algorithmic Techniques for Big Data