Problem

A stream SS (viewed as a multi-set) of nn elements each from a domain U=[1,,2,...,u]U = [1, , 2, ..., u]. Estimate the number of times each element appears in the SS.

If we use a balanced binary search, this can be done in O(nlog⁡min⁡{n,u})O(n \log \min\{n, u\}) time and Θ(min⁡{n,u})\Theta(\min\{n, u\}) space. If we use a hash table, this can be done in Θ(min⁡{n,u})\Theta(\min\{n, u\}) time and space.

Question to ponder: can we do it with sub-linear space?

By allowing errors, this is possible. Let a=<a1,a2,...,au>a = \left< a_1, a_2, ..., a_{u} \right> be the frequency of elements in UU and b=<b1,...,bu>b = \left< b_1, ..., b_{u} \right> be our estimation. Our goal is to minimize the ℓ∞\ell_\infty error:

∣∣a−b∣∣∞≐max⁡i∣bi−ai∣. || a - b ||_\infty \doteq \max_i |b_i - a_i|.

Count-Min Sketch

We use array AA with size m<min⁡{n,u}m < \min\{n, u\} such that each element in UU is allocated a bucket in AA to keep its frequency. Some elements must share the buckets. Now we apply the following strategy:

  1. Pick a perfect hash function h:U→[m]h : U \rightarrow [m] (this condition can be weaken, but for convenience of discussion, we assume perfectness here), that maps the elements uniformly to every bucket in the array.
  2. For ∀s∈S\forall s \in S,
    A[h(s)]←A[h(s)]+1\qquad A[h(s)] \leftarrow A[h(s)] + 1 .
  3. Return bi=A[h(i)]b_i = A[h(i)] as the estimation of aia_i.

Intuitively, each slot obtains 1m\frac{1}{m} fraction of the total mass ∑iai=∣a∣\sum_i a_i = |a| (where ∣⋅∣|\cdot | denotes the ℓ1\ell_1 norm). Conditioned on ii-th element is hashed to the h(i)h(i) slots, 1m\frac{1}{m} fraction of the rest mass ∑j≠iaj=n−ai\sum_{j \neq i} a_j = n - a_i is hashed to the h(i)h(i)-th slot. Therefore, in expectation,

0≤E[bi−ai]≤∣a∣m 0 \le \mathbb{E}[b_i - a_i] \le \frac{|a|}{m}

An interesting property is that, if some aja_j (j≠ij \neq i) (or a small subset of aja_j's) constitutes, say 0.990.99 percent of the mass ∣a∣|a|, as long as it is not hashed to h(i)h(i), then the expected error is less than 0.01∣a∣m0.01 \frac{|a|}{m}.

Formally, define the binary random variables XjX_j for j≠ij \neq i such that

Xj={ajifh(i)=h(j)0otherwise X_j = \begin{cases} a_j &\quad \text{if} \quad h(i) = h(j)\\ 0 &\quad \text{otherwise} \end{cases}

Then by perfectness of hh, Pr⁡[Xj=aj]=1m\Pr[X_j = a_j] = \frac{1}{m} and E[Xj]=ajm\mathbb{E}[X_j] = \frac{a_j}{m}. (Indeed, pairwise independence of hh suffices to guarantee this condition).

Then

E[bi−ai]=E[∑j≠iXj]=∑j≠iE[Xj]≤∣a∣m \mathbb{E} [b_i - a_i] = \mathbb{E} \left[ \sum_{j \neq i} X_j \right] = \sum_{j \neq i} \mathbb{E}[X_j] \le \frac{|a|}{m}

As bi−aib_i - a_i is non-negative, by Markov inequality,

Pr⁡[bi−ai≥ϵ∣a∣]≤E[bi−ai]ϵ∣a∣≤1ϵm \Pr \left[ b_i - a_i \ge \epsilon |a| \right] \le \frac{\mathbb{E}[b_i - a_i]}{\epsilon |a|} \le \frac{1}{\epsilon m}

If we would like to achieve failure probability δ\delta, we can set m=ϵ−1δ−1m = \epsilon^{-1} \delta^{-1}.

Question to ponder: can we improve this?

Yes, by repetition. Instead of using just one array, we set dd arrays (to be set latter) and choose dd hash functions independently. Then the probability that all estimations deviate more than ϵ∣a∣\epsilon |a| is at most

(1ϵm)d \left( \frac{1}{\epsilon m} \right)^d

When d=ln⁡δ/ln⁡1ϵm=ln⁡1δ/ln⁡ϵmd = \ln{\delta} / \ln \frac{1}{\epsilon m} = \ln{ \frac{1}{\delta} } / \ln \epsilon m, the failure probability decreases to δ\delta. To minimize the space usage, we solve the following optimization problem:

min⁡ln⁡1δmln⁡m+ln⁡ϵs.t. m≥1ϵ \begin{aligned} &\min &\ln \frac{1} {\delta} \frac{m}{\ln m + \ln \epsilon} \\ &\text{s.t. } &m \ge \frac{1}{\epsilon} \end{aligned}

Solving the equation gives m=eϵ−1m = e \epsilon^{-1}. The space consumption is eϵln⁡1δ\frac{e}{\epsilon} \ln \frac{1}{\delta}.

Note: suppose that ∣a∣=10000|a| = 10000 and ϵ=0.1\epsilon = 0.1, δ=0.01\delta = 0.01, then with 10⋅eln⁡100≈12510 \cdot e \ln 100 \approx 125 words, we can estimate a particular aia_i with the absolute error less than 10001000. If ai=9000a_i = 9000, the relative error is small. However, if ai=1a_i = 1, basically our estimation if rather inaccurate.

Question to ponder: what if we analyze this by Chernoff style inequality?

If we make dd repetition, then the confidence interval of the mean estimation is given by (Hoeffding's inequality)

∣a∣log⁡2δ2d |a|\sqrt{\frac{\log \frac{2}{\delta} }{2 d} }

If we want this interval to be smaller than ϵ∣a∣\epsilon |a|, then we need d=log⁡2δ2ϵ2d = \frac{ \log \frac{2}{\delta} }{2 \epsilon^2}, which is a 1ϵ\frac{1}{\epsilon } worse than the above analysis. Intuitively, we require stronger condition for concentration of mean value than that for existence of near-min value.

Count-Sketch

One drawback of Count-Min Sketch is that the estimator constructed is one-sided and not unbiased. This can be fixed by introducing an additional function g:U→{−1,1}g : U \rightarrow \{ - 1, 1 \} that gives each elements in UU a sign ("+" or "-") independently and uniformly at random. Now,

  1. For each element s∈Ss \in S, we increase the counter h(s)h(s) in the array by 1 if g(s)>0g(s) > 0 or −1-1 if g(s)<0g(s) < 0.
  2. Finally, we set bi=A[h(i)]⋅g(i)b_i = A[h(i)] \cdot g(i) as the estimation of aia_i.

In expectation, each slot obtains 1m\frac{1}{m} fraction of the total mass ∑iai=∣a∣\sum_i a_i = |a|. But as each element "flips" it sign randomly, the expected value obtained is 0. Now, conditioned on ii-th element is hashed to the h(i)h(i) slots, 1m\frac{1}{m} fraction of the rest mass ∑j≠iaj=n−ai\sum_{j \neq i} a_j = n - a_i is hashed to the h(i)h(i)-th slot. With their signs flipped randomly, they contribute 0 to the h(i)h(i)-th slot. Hence,

E[bi−ai]=0 \mathbb{E}[b_i - a_i] = 0

Formally, define the binary random variables XjX_j for j≠ij \neq i such that

Xj={ajifh(i)=h(j)∧g(j)=g(i)−ajifh(i)=h(j)∧g(j)≠g(i)0otherwise X_j = \begin{cases} a_j &\text{if} \quad h(i) = h(j) \wedge g(j) = g(i) \\ - a_j &\text{if} \quad h(i) = h(j) \wedge g(j) \neq g(i) \\ 0 &\text{otherwise} \end{cases}

Then by perfectness and independence of hh and gg,

Pr⁡[Xj=aj]=12mPr⁡[Xj=−aj]=12mPr⁡[Xj=0]=1−1m \Pr[X_j = a_j] = \frac{1}{2m} \\ \Pr[X_j = -a_j] = \frac{1}{2m} \\ \Pr[X_j = 0] = 1 - \frac{1}{m}

and

E[Xj]=0E[bi−ai]=E[∑j≠iXj]=∑j≠iE[Xj]=0Var[Xj]=E[Xj2]−(E[Xj])2=aj2mVar[∑j≠iXj]=∑j≠iVar[Xj]=∑j≠iaj2m≤∣a∣22m \begin{aligned} \mathbb{E}[X_j] &= 0 \\ \mathbb{E}[b_i - a_i] &= \mathbb{E}[\sum_{j \neq i} X_j] = \sum_{j \neq i} \mathbb{E}[X_j] = 0 \\ \mathbb{Var} \left[ X_j \right] &= \mathbb{E} [X_j^2] - (\mathbb{E}[X_j])^2 = \frac{a_j^2}{m} \\ \mathbb{Var} \left[ \sum_{j \neq i} X_j \right] &= \sum_{j \neq i} \mathbb{Var} [X_j] = \sum_{j \neq i} \frac{a_j^2}{m} \le \frac{|a|_2^2}{m} \end{aligned}

where ∣a∣22|a|_2^2 is the l2l_2 norm of aa. Note that the above analysis requires only pair independence. The random variable XjX_j is two sided, therefore we can not use Markov Inequality for analysis.

Question to ponder: is it possible to analyze this directly by Chernoff style inequality?

The tool we resort to is Chebyshev's inequality:

Pr⁡[∣∑j≠iXj−0∣≥ϵ∣a∣2]≤Var[∑j≠iXj]ϵ2∣a∣22≤1mϵ2 \Pr \left[ |\sum_{j \neq i} X_j - 0| \ge \epsilon |a|_2 \right] \le \frac{\mathbb{Var} [\sum_{j \neq i} X_j]}{\epsilon^2 |a|_2^2 } \le \frac{1}{m\epsilon^2 }

we take m=kϵ−2m = k\epsilon^{-2} for some integer k>2k> 2.

How do we amplify the successful probability? We repeat the experiment dd times, and take the median. The only way that the median makes a mistake is when more than half of the estimations deviate more ϵ∣a∣2\epsilon |a|_2. Denote YY the number of estimation such that the error is more than ϵ∣a∣2\epsilon |a|_2. By Chernoff,

Pr⁡[Y−dk≥d2−dk]=Pr⁡[Y−dk≥(12−1k)⋅k⋅dk]≤exp⁡(−d3k(k2−1)2)=δ \begin{aligned} \Pr \left[ Y - \frac{d}{k}\ge \frac{d}{2} - \frac{d}{k} \right] &= \Pr \left[ Y - \frac{d}{k}\ge (\frac{1}{2} - \frac{1}{k})\cdot k \cdot \frac{d}{k} \right] \\ &\le \exp \big( -\frac{d}{3 k} (\frac{k}{2} - 1)^2 \big) \\ &= \delta \end{aligned}

which solves to

d=3k(k/2−1)2log⁡1δ d= \frac{3k}{(k/2 -1)^2} \log \frac{1}{\delta}

The overall space usage is therefore

md=3k2ϵ2(k/2−1)2log⁡1δ=3ϵ2(1/2−1k)2log⁡1δ md = \frac{3k^2}{\epsilon^2 (k/2 -1)^2} \log \frac{1}{\delta} = \frac{3}{\epsilon^2 (1/2 - \frac{1}{k} )^2} \log \frac{1}{\delta}

Question to ponder: the expression is wired, which implies that k→∞k \rightarrow \infty, the space usage converges to 12ϵ2log⁡1δ\frac{12}{\epsilon^2} \log \frac{1}{\delta}, which is worse than count-min hash than a 1ϵ\frac{1}{\epsilon} factor.

Relation between ∣a∣1|a|_1 and ∣a∣2|a|_2.

Their relation is given as follows (by concavity of the ⋅\sqrt \cdot function)

∣a∣2≤∣a∣1≤∣u∣∣a∣2 |a|_2 \le |a|_1 \le \sqrt{ |u| } |a|_2

In particular, when ∣u∣=2|u| = 2, the inequality becomes

∣a1+a2∣≤2a12+a22 |a_1 + a_2| \le \sqrt 2 \sqrt{a_1^2 + a_2^2 }

When the dimension is higher than 22, the factor in the RHS can not bounded to 2\sqrt 2.