We consider the Count Sketch with single array.
Definitions
- Let S=(s1,s2,...,sn) be a stream of n elements from a domain [d].
- A: an array of size m.
- h:[d]→[m] a hash function that assigns each value in [d] an integer uniformly at random from [m].
- g:[d]→{−1,1} a hash function that assigns each value in [d] a random sign.
Algorithm
For each element s∈S, Count-Sketch updates A as follows:
Update (s):
A[h(s)]←A[h(s)]+g[s]
After processing elements in S, Count-Sketch answer query for an element's frequency as follows:
Query (v):
return f^[v]≐g[v]⋅A[h(v)]
Error Analysis
For a fixed v∈[d], let f[v] be its true frequencies in S (i.e., the number of times it appears in S). Define indicator random variables
X1,X2,...,Xn,such that for i∈[n], define
Xi=⎩⎨⎧1,0,−1, if h(si)=h(v), if h(si)=h(v), if h(si)=h(v),g(si)=g(v),g(si)=g(v),The Xi's are not independent. For i∈[n], if si=v, it holds that
E[Xi]=0.For i,j∈[n],si=v,sj=v, we have
E[XiXj]={0,m1 if si=sj, if si=sj.The estimation error of f^[v] can be written as
i∈[n],si=v∑Xi.For given ϵ>0, we want to bound.
Pri∈[n],si=v∑Xi≥ϵTo do this, we pick some parameter t>0 and define Vbad≐{u∈[d],h(u)≥t}, and consider
Pri∈[n],si=v∑Xi≥ϵ≤Pr[∃u∈Vbad:h(u)=h(v)]+Pri∈[n],si=v∑Xi≥ϵ∣∄u∈Vbad:h(u)=h(v)≤t⋅mn+Pri∈[n],si=v∑Xi≥ϵ∣∄u∈Vbad:h(u)=h(v).We consider the second probability.
Vari∈[n],si=v∑Xi∣∄u∈Vbad:h(u)=h(v)=i∈[n],si=v∑Var[Xi∣∄u∈Vbad:h(u)=h(v)]+i∈[n],j∈[n],i=j,si=v,sj=v∑E[XiXj∣∄u∈Vbad:h(u)=h(v)]The first term equals to mn. For the second term, if si=sj, then by independence of Xi and Xj, it holds that
E[XiXj∣∄u∈Vbad:h(u)=h(v)]=E[Xi∣∄u∈Vbad:h(u)=h(v)]⋅E[Xj∣∄u∈Vbad:h(u)=h(v)]=0.If si=sj, then Xi=Xj. Hence,
i∈[n],j∈[n],i=j,si=v,sj=v∑E[XiXj∣∄u∈Vbad:h(u)=h(v)]=i∈[n],j∈[n],i=j,si=v,sj=v,si=sj∑E[Xi2∣∄u∈Vbad:h(u)=h(v)]=i∈[n],j∈[n],i=j,si=v,sj=v,si=sj,f[si]≤t∑m1≤t2⋅tn⋅m1=mt⋅n.By Chebyshev's inequality,
Pri∈[n],si=v∑Xi≥ϵ≤t⋅mn+m⋅ϵ2t⋅n.By setting t=ϵ, the probability is minimized to
Pri∈[n],si=v∑Xi≥ϵ≤mn⋅ϵ2.Note that this error bound is exactly the same as Count-Min sketch with a single array of size m.