Problem

Let x⃗=(x1,...,xn)\vec x = (x_1, ..., x_n), y⃗=(y1,...,yn)∈Rn\vec y = (y_1, ..., y_n) \in \mathbb{R}^n. Given δ∈(0,1)\delta \in (0, 1), construct a data structure with space usage O(1ϵ2log⁡1δ)O \left( \frac{1}{\epsilon^2} \log \frac{1}{\delta} \right), such that with failure probability at most δ∈(0,1)\delta \in (0, 1),

  • it returns an estimate of the inner product <x⃗,y⃗>\left< \vec x, \vec y \right> with error O(ϵ⋅∥x⃗∥2⋅∥y⃗∥2)O( \epsilon \cdot \Vert \vec x \Vert_2 \cdot \Vert \vec y \Vert_2 ).

Remark: If we have x⃗=y⃗\vec x = \vec y, we get an estimator of ∥x⃗∥22\Vert \vec x \Vert_2^2 with ϵ\epsilon-relative error.

Prototype

Let σ⃗=(σ1,...,σn)∈{−1,1}n\vec \sigma = (\sigma_1, ..., \sigma_n) \in \{ - 1, 1 \}^n a sequence of nn independent Rademachers.

Define

X≐<x⃗,σ⃗>,Y≐<y⃗,σ⃗>. X \doteq \left< \vec x, \vec \sigma \right>, \qquad Y \doteq \left< \vec y, \vec \sigma \right>.

Theorem. X⋅YX \cdot Y is an unbiased estimator of <x⃗,y⃗>\left< \vec x, \vec y \right>.

Proof. Observe ∀i,j∈[n]\forall i, j \in [n], if entries of σ\sigma are pairwise independent, then

E[σiσj]={1,i=j0,i≠j. \mathbb{E} [ \sigma_i \sigma_j ] = \begin{cases} 1, & i = j \\ 0, & i \neq j \end{cases}.

Hence,

E[X⋅Y]=E[(∑i∈[n]σixi)(∑j∈[n]σjyj)]=∑i,j∈[n]E[σiσj]xiyj=∑i∈[n]xiyi. \begin{aligned} \mathbb{E} [ X \cdot Y ] &= \mathbb{E} \left[ \left( \sum_{i \in [n] } \sigma_i x_i \right) \left( \sum_{j \in [n] } \sigma_j y_j \right) \right] \\ &= \sum_{i, j \in [n] } \mathbb{E} [ \sigma_i \sigma_j ]x_i y_j \\ &= \sum_{i \in [n] } x_i y_i. \end{aligned}

□\square

Theorem. Var[X⋅Y]≤∥x⃗∥22⋅∥y⃗∥22\mathbb{Var}[ X \cdot Y ] \le \Vert \vec x \Vert_2^2 \cdot \Vert \vec y \Vert_2^2.

Proof. If entries of σ\sigma are four-wise independent, then

Var[X⋅Y]=Var[(∑i∈[n]σixi)(∑j∈[n]σjyj)]=Var[∑i≠j∈[n]σiσjxiyj]=∑i≠jVar[σiσjxiyj]+∑i≠j,k≠ℓ,(i,j)≠(k,ℓ)Cov[σiσjxiyj,σkσℓxkyℓ]=∑i≠jxi2yj2≤∥x⃗∥22⋅∥y⃗∥22 \begin{aligned} \mathbb{Var} [ X \cdot Y ] &= \mathbb{Var} \left[ \left( \sum_{i \in [n] } \sigma_i x_i \right) \left( \sum_{j \in [n] } \sigma_j y_j \right) \right] \\ &= \mathbb{Var} \left[ \sum_{i \neq j \in [n] } \sigma_i \sigma_j x_i y_j \right] \\ &= \sum_{i \neq j } \mathbb{Var} \left[ \sigma_i \sigma_j x_i y_j \right] + \sum_{i \neq j, k \neq \ell, (i, j) \neq (k, \ell) } \mathbb{Cov} [ \sigma_i \sigma_j x_i y_j, \sigma_k \sigma_\ell x_k y_\ell ]\\ &= \sum_{i \neq j } x_i^2 y_j^2 \\ &\le \Vert \vec x \Vert_2^2 \cdot \Vert \vec y \Vert_2^2 \end{aligned}

□\square

If we have mm independent copies of XX and YY, denoted as X1,...,XmX_1, ..., X_m, and Y1,...,YmY_1, ..., Y_m, then we can compute the average (1/m)⋅∑i∈[m]Xi⋅Yi(1 / m) \cdot \sum_{i \in [m] } X_i \cdot Y_i, whose variance is bounded by (1/m)⋅∥x⃗∥22⋅∥y⃗∥22(1 / m) \cdot \Vert \vec x \Vert_2^2 \cdot \Vert \vec y \Vert_2^2. If we set m=1/ϵ2m = 1 / \epsilon^2, then it becomes ϵ2⋅∥x⃗∥22⋅∥y⃗∥22\epsilon^2 \cdot \Vert \vec x \Vert_2^2 \cdot \Vert \vec y \Vert_2^2 and by Chebyshev's inequality ∣(1/m)⋅∑i∈[m]Xi⋅Yi−<x⃗,y⃗>∣|(1 / m) \cdot \sum_{i \in [m] } X_i \cdot Y_i - \left< \vec x , \vec y \right> | is bounded by ϵ⋅∥x⃗∥2⋅∥y⃗∥2\epsilon \cdot \Vert \vec x \Vert_2 \cdot \Vert \vec y \Vert_2 with constant probability.

By Chernoff bound, we can boost the probability to 1−δ1 - \delta, by repeating the experiment O(log⁡1δ)O(\log \frac{1}{\delta}) times and taking the median of the estimation.

Computing (1/m)⋅∑i∈[m]Xi⋅Yi(1 / m) \cdot \sum_{i \in [m] } X_i \cdot Y_i takes O(1/ϵ2)O( 1 / \epsilon^2 ) time. Repeating this O(log⁡1δ)O(\log \frac{1}{ \delta}) times and taking the median results in time complexity O(log⁡(1/δ)ϵ2)O( \frac{\log (1 / \delta) }{\epsilon^2} ).

On the other hand, we construct O(ϵ2⋅log⁡1δ)O ( \epsilon^2 \cdot \log \frac{1}{\delta} ) copies of XX, which takes time

O(n⋅ϵ2⋅log⁡1δ). O ( n \cdot \epsilon^2 \cdot \log \frac{1}{\delta} ).

Algorithm

In this section, we show how to reduce the construction time to

O(n⋅log⁡1δ). O ( n \cdot \log \frac{1}{\delta} ).

Given m∈N+m \in \mathbb{N}^+, let h:[n]→[m]h : [n] \rightarrow [m] be a pairwise independent hash function.

Let X⃗=(X1,...,Xm),Y⃗=(Y1,...,Ym)∈Rm\vec X = (X_1, ..., X_m), \vec Y = (Y_1, ..., Y_m) \in \mathbb{R}^m be random vectors, such that ∀t∈[m]\forall t \in [m],

Xt=∑i∈[n]:h(i)=tσixi,Yt=∑i∈[n]:h(i)=tσiyi. X_t = \sum_{ i \in [n] : h(i) = t } \sigma_i x_i, \\ Y_t = \sum_{ i \in [n] : h(i) = t } \sigma_i y_i.

Theorem. <X⃗,Y⃗>\left< \vec X, \vec Y \right> is an unbiased estimator of <x⃗,y⃗>\left< \vec x, \vec y \right>.

Proof.

If entries of σ\sigma are pairwise independent, then

E[<X⃗,Y⃗>]=∑t∈[m]E[XtYt]=∑t∈[m]E[∑i,j∈[n]:h(i)=h(j)=tσiσjxiyj]=∑t∈[m]E[∑i∈[n]1mxiyi+∑i≠j1m2σiσjxiyj]=∑i∈[n]xiyi \begin{aligned} \mathbb{E} \left[ \left< \vec X, \vec Y \right> \right] &= \sum_{ t \in [m] } \mathbb{E}[ X_t Y_t ] \\ &= \sum_{ t \in [m] } \mathbb{E} \left[ \sum_{ i, j \in [n] : h(i) = h(j) = t } \sigma_i \sigma_j x_i y_j \right] \\ &= \sum_{ t \in [m] } \mathbb{E} \left[ \sum_{ i \in [n] } \frac{1}{m} x_i y_i + \sum_{ i \neq j } \frac{1}{m^2} \sigma_i \sigma_j x_i y_j\right] \\ &= \sum_{ i \in [n] } x_i y_i \end{aligned}

□\square

Theorem. Var[<X⃗,Y⃗>]≤1m⋅∥x⃗∥22⋅∥y⃗∥22\mathbb{Var} \left[ \left< \vec X, \vec Y \right> \right] \le \frac{1}{m} \cdot \Vert \vec x \Vert_2^2 \cdot \Vert \vec y \Vert_2^2.

Proof. If entries of σ\sigma are four-wise independent, then

Var[<X⃗,Y⃗>]=Var[∑t∈[m]XtYt]=Var[∑i,j∈[n]:h(i)=h(j)σiσjxiyj]=Var[∑i≠j,h(i)=h(j)σiσjxiyj]=∑i≠jVar[1[h(i)=h(j)]⋅σiσjxiyj]+∑i≠j,k≠ℓ,(i,j)≠(k,ℓ)Cov[1[h(i)=h(j)]⋅σiσjxiyj,1[h(k)=h(ℓ)]⋅σkσℓxkyℓ]=∑i≠jE[(1[h(i)=h(j)]⋅σiσjxiyj)2]=∑i≠j1m⋅xi2yj2≤1m⋅∥x⃗∥22⋅∥y⃗∥22 \begin{aligned} \mathbb{Var} \left[ \left< \vec X, \vec Y \right> \right] &= \mathbb{Var} \left[ \sum_{ t \in [m] } X_t Y_t \right] \\ &= \mathbb{Var} \left[ \sum_{ i, j \in [n] : h(i) = h(j) } \sigma_i \sigma_j x_i y_j \right] \\ &= \mathbb{Var} \left[ \sum_{ i \neq j, h(i) = h(j) } \sigma_i \sigma_j x_i y_j \right] \\ &= \sum_{i \neq j } \mathbb{Var} \left[ 1_{ [ h(i) = h (j) ] } \cdot \sigma_i \sigma_j x_i y_j \right] \\ &\quad+ \sum_{i \neq j, k \neq \ell, (i, j) \neq (k, \ell) } \mathbb{Cov} [ 1_{ [ h(i) = h (j) ] } \cdot \sigma_i \sigma_j x_i y_j, 1_{ [ h(k) = h (\ell ) ] } \cdot \sigma_k \sigma_\ell x_k y_\ell ] \\ &= \sum_{i \neq j } \mathbb{E} \left[ \left( 1_{ [ h(i) = h (j) ] } \cdot \sigma_i \sigma_j x_i y_j \right)^2 \right] \\ &= \sum_{i \neq j } \frac{1}{m} \cdot x_i^2 y_j^2 \\ &\le \frac{1}{m} \cdot \Vert \vec x \Vert_2^2 \cdot \Vert \vec y \Vert_2^2 \end{aligned}

□\square

By setting m=1/ϵ2m = 1/ \epsilon^2, repeating the procedure O(log⁡1δ)O(\log \frac{1}{\delta}) times and taking the median as our estimation, we can obtained the desired result.

Reference

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