Problem

There is a stream SS of elements, each consists of a pair (t,Δ)∈[n]×R(t, \Delta) \in [n] \times \mathbb{R}. Let f∈Rnf \in \mathbb{R}^n be the frequency vector of SS, s.t., ∀i∈[n]\forall i \in [n],

fi=∑(t,Δ)∈S∧t=iΔ. f_i = \sum_{ (t, \Delta) \in S \wedge t = i} \Delta.

Given ϵ∈(0,1)\epsilon \in (0, 1), design a data structure that outputs an (1±ϵ)(1 \pm \epsilon) estimate ∥f∥22\Vert f \Vert_2^2 with probability at least 1−δ1 - \delta, and uses space of O(1ϵ2ln⁡1δ)O( \frac{1}{\epsilon^2 } \ln \frac{1}{\delta} ).

Solution

Based on Count-Sketch.

Algorithm


Count-Sketch

  1. k←O(ln⁡1δ);m←3ϵ2k \leftarrow O \left( \ln \frac{1}{ \delta} \right); m \leftarrow \frac{3}{\epsilon^2}.
  2. h1,h2,...,hk:[n]→[m]h_1, h_2, ..., h_k: [n] \rightarrow [m], pairwise independent hash functions.
  3. g1,g2,...,gk:[n]→{−1,1}g_1, g_2, ..., g_k: [n] \rightarrow \{-1, 1\}, four-wise independent hash functions.
  4. M←{0}k×mM \leftarrow \{ 0 \}^{k \times m} be a k×mk \times m matrix initialized with zeros.
  5. For (t,Δ)∈S:(t, \Delta) \in S:
  6. \qquad For i∈[k]:i \in [k]:
  7. \qquad\qquad M[i,hi(t)]←M[i,hi(t)]+gi(t)⋅ΔM[i, h_i (t) ] \leftarrow M[i, h_i (t) ] + g_i(t) \cdot \Delta
    \qquad

Estimation of ∥f∥22\Vert f \Vert_2^2

  1. For i∈[k]:i \in [k]:
  2. \qquad Let MiM_i be the ii-th row of MM.
  3. \qquad Let ai←∥Mi∥2a_i \leftarrow \Vert M_i \Vert_2.
  4. Return the median of (ai2)i∈[k](a_i^2)_{i \in [k] }.

Analysis

Theorem 1. ∀i∈[k],E[ai2]=∥f∥22\forall i \in [k], \mathbb{E}[ a_i^2 ] = \Vert f \Vert_2^2.

Proof. Consider a fixed i∈[k]i \in [k]. ∀j∈[m]\forall j \in [m], define the set hi−1[j]h_i^{-1}[j] as elements in [n][n] hashed to jj:

hi−1[j]≐{t∈[n]:h(t)=j}. h_i^{-1}[j] \doteq \{ t \in [n] : h(t) = j \}.

Once hih_i is fixed, it holds that

M[i,j]=∑t∈hi−1[j]gi(t)⋅ft. M[i, j] = \sum_{ t \in h_i^{-1}[j] } g_i (t) \cdot f_t.

By linearity of expectation and four-wise independence of gig_i,

Egi[(M[i,j])2]=∑t,t′∈hi−1[j]Egi[gi(t)⋅gi(t)]⋅ft⋅ft′. \underset{g_i}{ \mathbb{E} }[ (M[i, j])^2 ] = \sum_{ t, t' \in h_i^{-1}[j] } \underset{g_i}{ \mathbb{E} } [ g_i (t) \cdot g_i (t) ] \cdot f_t \cdot f_{t'}.

It holds that

Egi[gi(t)⋅gi(t)]={1, if t=t′,0, otherwise . \underset{g_i}{ \mathbb{E} } [ g_i (t) \cdot g_i (t) ] = \begin{cases} 1, & \text{ if } t = t', \\ 0, & \text{ otherwise }. \end{cases}

Therefore, Egi[(M[i,j])2]=∑t∈hi−1[j]ft2\underset{g_i}{ \mathbb{E} }[ (M[i, j])^2 ] = \sum_{ t \in h_i^{-1}[j] } f_t^2.

Remark. This is a rather deep result, which says that whatever elements are hashed to bucket jj, Eg[(M[i,j])2]\underset{g}{ \mathbb{E} } [ (M[i,j] )^2 ] equals the sum of squares of these elements.

By linearity of expectation, when hih_i is fixed,

Eg[ai2]=∑j∈[m]Eg[(M[i,j])2]=∑j∈[m]∑t∈hi−1[j]ft2=∥f∥22. \underset{g}{ \mathbb{E} } [ a_i^2 ] = \sum_{j \in [m] } \underset{g}{ \mathbb{E} } [ (M[i,j])^2 ] = \sum_{j \in [m] } \sum_{ t \in h_i^{-1}[j] } f_t^2 = \Vert f \Vert_2^2.

It follows that Eh[Eg[ai2]]=∥f∥22\underset{h}{ \mathbb{E} } \left[ \underset{g}{ \mathbb{E} } [ a_i^2 ] \right] = \Vert f \Vert_2^2.

Remark. The proof holds even for m=1m = 1, i.e., we can use only one bucket to construct an unbiased estimator of ∥f∥22\Vert f \Vert_2^2.

□\square

Theorem 2. Var[ai2]=∥x∥24m\mathbb{Var}[ a_i^2 ] = \frac{\Vert x \Vert_2^4 }{ m }.

Proof.

∀t∈[n]\forall t \in [n], define Xt≐gi(t)⋅xtX_t \doteq g_i(t) \cdot x_t.

∀t∈[n],j∈[m]\forall t \in [n], j \in [m], define Yt,j≐1[hi[t]=j]⋅XtY_{t, j} \doteq \mathbb{1}[ h_i[t] = j ] \cdot X_t. Then

M[i,j]=∑t∈[n]Yt,j, M[i,j] = \sum_{t \in [n] } Y_{t, j},

and hence

Var[ai2]=Var[∑j∈[m](M[i,j])2]=Var[∑j∈[m](∑t∈[n]Yt,j)2]=Var[∑j∈[m]∑t,t′∈[n]Yt,jYt′,j]=Var[∑t,t′∈[n]∑j∈[m]Yt,jYt′,j]=Var[∑t,t′∈[n](∑j∈[m]1[hi[t]=j]⋅1[hi[t′]=j])⋅Xt⋅Xt′]=Var[∑t,t′∈[n]1[hi[t]=hi[t′]]⋅Xt⋅Xt′]=Var[∑t∈[n]ft2+∑t≠t′∈[n]1[hi[t]=hi[t′]]⋅Xt⋅Xt′]=Var[∑t≠t′∈[n]1[hi[t]=hi[t′]]⋅Xt⋅Xt′] \begin{aligned} \mathbb{Var}[a_i^2] &= \mathbb{Var} \left[ \sum_{j \in [m] } (M[i, j])^2 \right] \\ &= \mathbb{Var} \left[ \sum_{j \in [m] } \left( \sum_{t \in [n] } Y_{t, j} \right)^2 \right] \\ &= \mathbb{Var} \left[ \sum_{j \in [m] } \sum_{t, t' \in [n]} Y_{t, j} Y_{t', j} \right] \\ &= \mathbb{Var} \left[ \sum_{t, t' \in [n]} \sum_{j \in [m] } Y_{t, j} Y_{t', j} \right] \\ &= \mathbb{Var} \left[ \sum_{t, t' \in [n]} \left( \sum_{j \in [m] } \mathbb{1}[ h_i[t] = j ] \cdot \mathbb{1}[ h_i[t'] = j ] \right) \cdot X_t \cdot X_{t'} \right] \\ &= \mathbb{Var} \left[ \sum_{t, t' \in [n]} \mathbb{1}[ h_i[t] = h_i[t'] ] \cdot X_t \cdot X_{t'} \right] \\ &= \mathbb{Var} \left[ \sum_{t \in [n] } f_t^2 + \sum_{t \neq t' \in [n]} \mathbb{1}[ h_i[t] = h_i[t'] ] \cdot X_t \cdot X_{t'} \right] \\ &= \mathbb{Var} \left[ \sum_{t \neq t' \in [n]} \mathbb{1}[ h_i[t] = h_i[t'] ] \cdot X_t \cdot X_{t'} \right] \end{aligned}

For t≠t′∈[n]t \neq t' \in [n], define Zt,t′=1[hi[t]=hi[t′]]⋅Xt⋅Xt′Z_{t, t'} = \mathbb{1}[ h_i[t] = h_i[t'] ] \cdot X_t \cdot X_{t'}. Then

E[Zt,t′]=E[1[hi[t]=hi[t′]]]⋅E[Xt]⋅E[xt′]=0. \mathbb{E}[ Z_{t, t'} ] = \mathbb{E}[ \mathbb{1}[ h_i[t] = h_i[t'] ] ] \cdot \mathbb{E}[ X_t ] \cdot \mathbb{E}[ x_{t'} ] = 0.

Consider t<t′∈[n]t < t' \in [n], s<s′∈[n]s < s' \in [n]. If (t,t′)≠(s,s′)(t, t') \neq (s, s'), then either t≠st \neq s or t′≠s′t' \neq s'. Therefore, there must be an element that appears only once among the four. WLOG, suppose that tt is a unique element. Then

Zt,t′Zs,s′=Xt⋅(Xt′⋅Xs⋅Xs′⋅1[hi[t]=hi[t′]]⋅1[hi[s]=hi[s′]]). Z_{t, t'} Z_{s, s'} = X_t \cdot \left( X_{t'} \cdot X_s \cdot X_{s'} \cdot \mathbb{1}[ h_i[t] = h_i[t'] ] \cdot \mathbb{1}[ h_i[s] = h_i[s'] ] \right).

As gig_i is four-wise independent, and that t≠t′t \neq t', t≠st \neq s and t≠s′t \neq s', it holds that gi(t)g_i(t) is independent of (gi(t′)⋅gi(s)⋅gi(s′)⋅1[hi[t]=hi[t′]]⋅1[hi[s]=hi[s′]])\left( g_i(t') \cdot g_i(s) \cdot g_i(s') \cdot \mathbb{1}[ h_i[t] = h_i[t'] ] \cdot \mathbb{1}[ h_i[s] = h_i[s'] ] \right). Hence,

E[Zt,t′Zs,s′]=0. \mathbb{E}[ Z_{t, t'} Z_{s, s'}] = 0.

Therefore,

Var[∑t≠t′∈[n]Zt,t′]=E[(∑t≠t′∈[n]Zt,t′)2]=E[∑t≠t′∈[n]Zt,t′2]=∑t≠t′∈[n]xt2xt′2m=∥x∥24m. \begin{aligned} \mathbb{Var} \left[ \sum_{t \neq t' \in [n]} Z_{t, t'} \right] &= \mathbb{E} \left[ \left( \sum_{t \neq t' \in [n]} Z_{t, t'} \right)^2 \right] \\ &= \mathbb{E} \left[ \sum_{t \neq t' \in [n]} Z_{t, t'}^2 \right] \\ &= \sum_{t \neq t' \in [n]} \frac{ x_t^2 x_{t'}^2}{ m } \\ &= \frac{\Vert x \Vert_2^4 }{ m }. \end{aligned}

□\square

∀i∈[k],Pr⁡[∣ai2−∥x∥22∣≥ϵ∥x∥22]≤13\forall i \in [k], \Pr [ | a_i^2 - \Vert x \Vert_2^2 | \ge \epsilon \Vert x \Vert_2^2] \le \frac{1}{3}.

Proof. By Chebyshev's inequality.

□\square

Pr⁡[∣{i∈[k]:Pr⁡[∣ai2−∥x∥22∣≤ϵ∥x∥22]}∣>k2]≥1−δ\Pr \left[ \left| \{ i \in [k] : \Pr [ | a_i^2 - \Vert x \Vert_2^2 | \le \epsilon \Vert x \Vert_2^2] \} \right| > \frac{k}{2} \right] \ge 1 - \delta.

Proof. By Chernoff bound.

□\square

Reference

[1] G. Cormode and K. Yi, Small Summaries for Big Data, 1st ed. Cambridge University Press, 2020.