A stream S (viewed as a multi-set) of n elements each from a domain U=[1,,2,...,u]. Estimate the number of times each element appears in the S.
If we use a balanced binary search, this can be done in O(nlogmin{n,u}) time and Θ(min{n,u}) space. If we use a hash table, this can be done in Θ(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⟩ be the frequency of elements in U and b=⟨b1,...,bu⟩ be our estimation. Our goal is to minimize the ℓ∞ error:
∣∣a−b∣∣∞≐imax∣bi−ai∣.
Count-Min Sketch
We use array A with size m<min{n,u} such that each element in U is allocated a bucket in A to keep its frequency. Some elements must share the buckets. Now we apply the following strategy:
Pick a perfect hash function h:U→[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.
For ∀s∈S, A[h(s)]←A[h(s)]+1 .
Return bi=A[h(i)] as the estimation of ai.
Intuitively, each slot obtains m1 fraction of the total mass ∑iai=∣a∣ (where ∣⋅∣ denotes the ℓ1 norm). Conditioned on i-th element is hashed to the h(i) slots, m1 fraction of the rest mass ∑j=iaj=n−ai is hashed to the h(i)-th slot. Therefore, in expectation,
0≤E[bi−ai]≤m∣a∣
An interesting property is that, if some aj (j=i) (or a small subset of aj's) constitutes, say 0.99 percent of the mass ∣a∣, as long as it is not hashed to h(i), then the expected error is less than 0.01m∣a∣.
Formally, define the binary random variables Xj for j=i such that
Xj={aj0ifh(i)=h(j)otherwise
Then by perfectness of h, Pr[Xj=aj]=m1 and E[Xj]=maj. (Indeed, pairwise independence of h suffices to guarantee this condition).
Then
E[bi−ai]=Ej=i∑Xj=j=i∑E[Xj]≤m∣a∣
As bi−ai is non-negative, by Markov inequality,
Pr[bi−ai≥ϵ∣a∣]≤ϵ∣a∣E[bi−ai]≤ϵm1
If we would like to achieve failure probability δ, we can set m=ϵ−1δ−1.
Question to ponder: can we improve this?
Yes, by repetition. Instead of using just one array, we set d arrays (to be set latter) and choose d hash functions independently. Then the probability that all estimations deviate more than ϵ∣a∣ is at most
(ϵm1)d
When d=lnδ/lnϵm1=lnδ1/lnϵm, the failure probability decreases to δ. To minimize the space usage, we solve the following optimization problem:
mins.t. lnδ1lnm+lnϵmm≥ϵ1
Solving the equation gives m=eϵ−1. The space consumption is ϵelnδ1.
Note: suppose that ∣a∣=10000 and ϵ=0.1, δ=0.01, then with 10⋅eln100≈125 words, we can estimate a particular ai with the absolute error less than 1000. If ai=9000, the relative error is small. However, if ai=1, basically our estimation if rather inaccurate.
Question to ponder: what if we analyze this by Chernoff style inequality?
If we make d repetition, then the confidence interval of the mean estimation is given by (Hoeffding's inequality)
∣a∣2dlogδ2
If we want this interval to be smaller than ϵ∣a∣, then we need d=2ϵ2logδ2, which is a ϵ1 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} that gives each elements in U a sign ("+" or "-") independently and uniformly at random. Now,
For each element s∈S, we increase the counter h(s) in the array by 1 if g(s)>0 or −1 if g(s)<0.
Finally, we set bi=A[h(i)]⋅g(i) as the estimation of ai.
In expectation, each slot obtains m1 fraction of the total mass ∑iai=∣a∣. But as each element "flips" it sign randomly, the expected value obtained is 0. Now, conditioned on i-th element is hashed to the h(i) slots, m1 fraction of the rest mass ∑j=iaj=n−ai is hashed to the h(i)-th slot. With their signs flipped randomly, they contribute 0 to the h(i)-th slot. Hence,
E[bi−ai]=0
Formally, define the binary random variables Xj for j=i such that
where ∣a∣22 is the l2 norm of a. Note that the above analysis requires only pair independence. The random variable Xj 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?
How do we amplify the successful probability? We repeat the experiment d 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. Denote Y the number of estimation such that the error is more than ϵ∣a∣2. By Chernoff,
Question to ponder: the expression is wired, which implies that k→∞, the space usage converges to ϵ212logδ1, which is worse than count-min hash than a ϵ1 factor.
Relation between ∣a∣1 and ∣a∣2.
Their relation is given as follows (by concavity of the ⋅ function)
∣a∣2≤∣a∣1≤∣u∣∣a∣2
In particular, when ∣u∣=2, the inequality becomes
∣a1+a2∣≤2a12+a22
When the dimension is higher than 2, the factor in the RHS can not bounded to 2.