Problem

Let x∈Rnx \in \mathbb{R}^n be a vector initialized with all zeros. There is a stream SS of updates, each of which is a pair (t,Δ)∈[n]×R(t, \Delta) \in [n] \times \mathbb{R}, causing xt←xt+Δx_t \leftarrow x_t + \Delta.

Define the support Supp(x)\mathbb{Supp}(x) to be the set of all non-zero indexes of xx:

Supp(x)≐{i∈[n]:xi≠0}. \mathbb{Supp}(x) \doteq \{ i \in [n] : x_i \neq 0 \}.

ℓ0\ell_0 sampling: output an index uniformly at random from the support Supp(x)\mathbb{Supp}(x).

We assume that at any time, ∀t∈[n],xt≤M\forall t \in [n], x_t \le M for some M∈RM \in \mathbb{R}.

Naive Solutions

We can uses an array to keep xx explicitly. But this has size O(n⋅log⁡M)O( n \cdot \log M) bits.

Goal: design an algorithm for ℓ0\ell_0 sampling with space less than O(n⋅log⁡M)O( n \cdot \log M) bits.

If there are only insertion updates, i.e., ∀(t,Δ)∈S\forall (t, \Delta) \in S, it holds that Δ>0\Delta > 0, then we can use a hash set to keep the non-zero elements and their frequencies, which has size O(∣Supp(x)∣⋅log⁡M)O( |\mathbb{Supp}(x) | \cdot \log M). If xx is dense, i.e., ∣Supp(x)∣|\mathbb{Supp}(x)| is large, this is still infeasible.

Sampling Game

We discuss one sampling game that illustrates the key ideas used in ℓ0\ell_0-sampling.

In this game, we want to sample one integer uniformly at random from [k][k]. Instead of sampling it directly, we choose some p∈(0,1]p \in (0, 1] and perform the following two steps:

  1. First, we sample each i∈[k]i \in [k] uniformly and independent at random with probability pp. Denote BB the set of sampled elements.
  2. If B≠∅B \neq \emptyset, then output one element from BB uniformly at random.

Property. E[∣B∣]=kp\mathbb{E} [ |B| ] = kp.

Property. Pr⁡[∣B∣>0]=(1−p)k≤exp⁡(−kp)\Pr[ |B| > 0 ] = (1 - p)^k \le \exp(-kp).

Property. By Bernstein's Inequality, ∀δ∈(0,1)\forall \delta \in (0, 1), with probability at least 1−δ1 - \delta, we have

∣B∣−E[∣B∣]≤2⋅k⋅p⋅(1−p)⋅ln⁡1δ+2ln⁡1δ3. |B| - \mathbb{E} [ |B| ] \le \sqrt{2 \cdot k \cdot p \cdot (1 - p) \cdot \ln \frac{1}{\delta} } + \frac{2 \ln \frac{1}{\delta} }{3}.

Theorem. Each element in [k][k] is outputted with equal probability

1−(1−p)kk. \frac{1 - \left(1 - p \right)^k }{ k }.

Remark: when p=1p = 1, this reduces to sampling an element uniformly at random from [k][k].

Proof. The probability that B=∅B = \emptyset is given by (1−p)k(1 - p)^k. Conditioned on that B≠∅B \neq \emptyset, by symmetry, each element is sampled with equal probability.

□\square

Remark: We can also have a proof by algebra. For evert i∈[k]i \in [k], the probability that it is sampled is given by

∑t=1k1t⋅Pr⁡[i∈B∣∣B∣=t]⋅Pr⁡[∣B∣=t]=∑t=1k1t(k−1t−1)(kt)(kt)pt(1−p)k−t=∑t=1k1t(k−1t−1)pt(1−p)k−t=1k∑t=1k(kt)pt(1−p)k−t=1k(1−(k0)p0(1−p)k−0)=1−(1−p)kk. \begin{aligned} \sum_{t = 1}^k \frac{1}{t} \cdot \Pr [ i \in B \mid |B| = t] \cdot \Pr[ |B| = t] &= \sum_{t = 1}^k \frac{1}{t} \frac{ \binom{k - 1}{t - 1} }{ \binom{k}{t} } \binom{k}{t} p^t (1 - p)^{k - t} \\ &= \sum_{t = 1}^k \frac{1}{t} \binom{k - 1}{t - 1} p^t (1 - p)^{k - t} \\ &= \frac{1}{k} \sum_{t = 1}^k \binom{k}{t} p^t (1 - p)^{k - t} \\ &= \frac{1}{k} \left( 1 - \binom{k}{0} p^0 (1 - p)^{k - 0} \right) \\ &= \frac{1 - \left(1 - p \right)^k }{ k }. \end{aligned}

Sampling With Known Support Size

We use the technique developed in the Sampling Game section, to avoid using O(∣Supp(x)∣log⁡M)O( | \mathbb{Supp}(x) | \log M) bits. We have seen that pp controls the tradeoff between the size of BB, and the successful probability of sampling an element (B≠∅B \neq \emptyset).

We begin with the assumption that the size of Supp(x)\mathbb{Supp}(x) is known.

Insertion Only

As before, we consider insertion only model first.

Claim. There is an algorithm such that

  1. with probability 1−δ1 - \delta, it returns an element from Supp(x)\mathbb{Supp}(x) uniformly at random;
  2. with probability δ\delta, it returns nothing.

Further, the space usage is O(ln⁡1δ)O \left( \ln \frac{1}{\delta} \right) with probability at least 1−δ1 - \delta.

For simplicity, we assume a perfect hash function hh that maps elements in [n][n] to {0,1}\{0, 1\} independently with probability pp (to be determined). For each update (t,Δ)(t, \Delta) in the stream, we process it only if h(t)=1h(t) = 1. As we are dealing with insertion only model, we can use a hashset to maintain the elements with hash value 1.

Let k=∣Supp(x)∣k = | \mathbb{Supp}(x) |. As analysed before, with probability (1−p)k(1 - p)^k, the algorithm fails. To make the failure probability as small as δ\delta, we have that (1−p)k=δ(1 - p)^k = \delta, i.e.,

p=1−δ1k≤−ln⁡δ1k=1kln⁡1δ. p = 1 - \delta^{ \frac{1}{k} } \le -\ln \delta^{ \frac{1}{k} } = \frac{1}{k} \ln \frac{1}{\delta}.

The last inequality holds since the function y=−ln⁡xy = - \ln x is convex, and y=1−xy = 1 - x is the tangent line of y=−ln⁡xy = - \ln x at the point (1,0)(1, 0). By previous analysis, with probability at least 1−δ1 - \delta, the size of BB is bounded by

(kp+2kp(1−p)ln⁡1δ+23ln⁡1δ)∈O(ln⁡1δ). \left( kp + \sqrt{2 k p (1 - p) \ln \frac{1}{\delta} } + \frac{2}{3} \ln \frac{1}{\delta} \right) \in O \left( \ln \frac{1}{\delta} \right).

Handling Deletion

When deletions are possible, we can't use a hashset to keep the sampled updates. E.g., suppose we have a sequence (1,6),(1,−6),(2,5),(3,7),(4,9)(1, 6), (1, -6), (2, 5), (3, 7), (4, 9), and that h(1)=h(2)=1,h(3)=h(4)=0h(1) = h(2) = 1, h(3) = h(4) = 0. We need a hashset of size 2 to keep both the frequencies of element 1 and element 2. Even through the frequency of element 11 is 0 finally, we don't this before we see the entire string.

In general, hh maps npnp elements in [n][n] to 11. If we use a hashset, its size can grow to O(np)O(n p).

However, there is data structure that can handle deletions while using less than O(np)O(np) space.

Sparse Recovery. Given an integer dd at the beginning of the stream, if Supp(x)≤d\mathbb{Supp}(x) \le d when the stream stops, there is a data structure that returns

  1. indices in Supp(x)\mathbb{Supp}(x);
  2. and their values,

using O(d(log⁡n+log⁡M))⊂O(dlog⁡(nM))O(d (\log n + \log M) ) \subset O( d \log (nM) ) bits. It processes each update (t,Δ)(t, \Delta) in the stream in O(d)O(d) time and recovers xx in O(d2polylog(nM))O(d^2 polylog(nM)) time. Its recovery can be wrong if Supp(x)>d\mathbb{Supp}(x) > d.

It suffices to use such a data structure for the sampled set BB with d∈O(ln⁡1δ)d \in O( \ln \frac{1}{\delta} ). We have seen that if we sampled each element in Supp(x)\mathbb{Supp}(x) with probability p=1−δ1/kp = 1 - \delta^{1 / k}, then with probability at least 1−δ1 - \delta, we obtain a sampled set BB with size at most dd. Then we return one element from BB uniformly at random.

Analyzing the probability of sampling an element is more subtle than the previous section. In previous section, each element is under-sampled with probability 1−(1−p)kk\frac{1 - \left(1 - p \right)^k }{ k }. Here, conditioned on ∣B∣≤d|B|\le d, each element is sampled equally likely. However, ∣B∣>d|B| > d, the recovery of sampled elements fails, some element can be returned more frequently and some can be returned less frequently (the recovery behavior is un-defined). As the failure probability is bounded by δ\delta, each element is returned with probability

[1−(1−p)kk±δ]⊆[1−(1+1k)⋅δ,1+(1−1k)⋅δ]. \left[ \frac{1 - \left(1 - p \right)^k }{ k } \pm \delta \right] \subseteq \left[ 1 - \left( 1 + \frac{ 1 }{ k } \right) \cdot \delta, 1 + \left( 1 - \frac{ 1 }{ k } \right) \cdot \delta \right].

Sampling With UnKnown Support Size

Previously, we want to sample each element in [n][n] with probability 1−δ1k≤ln⁡δ1k=1kln⁡1δ1 - \delta^{ \frac{1}{k} } \le \ln \delta^{ \frac{1}{k} } = \frac{1}{k} \ln \frac{1}{\delta}, where k=∣Supp(x)∣k = | \mathbb{Supp}(x) |. When this information is unavailable, we can guess by trying the values

1/2,1/4,1/8,1/16,... 1 / 2, \quad 1 / 4, \quad 1 / 8, \quad 1 / 16, ...

We don't need to guess the value of 1kln⁡1δ\frac{1}{k} \ln \frac{1}{\delta} exactly. In order that the sparse recovery data structure works, any guess within a constant factor suffices.

For simplicity, assume that nn is some power of 22. Now, instead of using one sampling set BB, we use log⁡n+2\log n + 2 of them B1,B2,B3,...B_1, B_2, B_3, ..., each associated with a sparse recovery structure with parameter d∈Θ(ln⁡1δ)d \in \Theta( \ln \frac{1}{\delta} ). Assume that we have a perfect hash function h:[n]→[log⁡n+2]h: [n] \rightarrow [\log n + 2] such that it holds independently for every t∈[n]t \in [n],

Pr⁡[h(t)=i]=12i,∀i∈[log⁡n+1],Pr⁡[h(t)=log⁡n+2]=12log⁡n+1. \begin{aligned} &\Pr[ h(t) = i ] = \frac{1}{2^i}, \forall i \in [\log n + 1], \\ &\Pr[ h(t) = \log n + 2] = \frac{1}{ 2^{\log n + 1} }. \end{aligned}

The distribution is well defined, as ∑i∈[log⁡n+1]1/2i+1/2log⁡n+1=1−1/2log⁡n+1+1/2log⁡n+1=1\sum_{i \in [\log n + 1] } 1 / 2^i + 1 / 2^{ \log n + 1} = 1 - 1 / 2^{ \log n + 1} + 1 / 2^{ \log n + 1} = 1.

If k=1k = 1, some BiB_i must recovers xx successfully.

Otherwise, ∃i∈[log⁡n]\exists i \in [\log n], such that 12i≥1kln⁡1δ>12i+1\frac{1}{2^i} \ge \frac{1}{k} \ln \frac{1}{\delta} > \frac{1}{2^{i + 1} }. We study the following good events:

Event E1:E_1: for all j≥ij \ge i, it holds that ∣{t∈Supp(x):h(t)=j}∣≤d|\{ t \in \mathbb{Supp}(x) : h(t) = j \}| \le d.

Under E1E_1, the sparse recovery structure associated with BjB_j recovers BjB_j successfully for all j≥ij \ge i.

Event E2:E_2: ∣{t∈Supp(x):h(t)≥i}∣>0|\{ t \in \mathbb{Supp}(x) : h(t) \ge i \}| > 0.

Under E2E_2, there exists at least one element that is mapped to BjB_j for some j≥ij \ge i.

Under these two good events, we sample one element as follows:

For j←log⁡n+2j \leftarrow \log n + 2 downto 11 do
\qquad recover BjB_j;
\qquad if Bj≠∅B_j \neq \emptyset then
\qquad\qquad return one element from BjB_j uniformly at random.

Analysis the probability of E1E_1:

For j≥ij \ge i, it holds that E[∣Bj∣]=k/2j\mathbb{E} [ |B_j| ] = k / 2^j. Suppose that d=2⋅Cd⋅ln⁡1δd = 2 \cdot C_d \cdot \ln \frac{1}{\delta} for some constant CdC_d (to be determined). Since 1kln⁡1δ≥1/2i+1\frac{1}{k} \ln \frac{1}{\delta} \ge 1 / 2^{i + 1}, we have that

dE[∣Bj∣]≥2jk⋅2⋅Cd⋅ln⁡1δ≥Cd⋅2j−i. \frac{d}{\mathbb{E} [ |B_j| ]} \ge \frac{ 2^j }{ k } \cdot 2 \cdot C_d \cdot \ln \frac{1}{\delta} \ge C_d \cdot 2^{j - i}.

By Chernoff bound,

Pr⁡[∣Bj∣≥d]≤(e(d/E[∣Bj∣])−1(d/E[∣Bj∣])(d/E[∣Bj∣]))E[∣Bj∣]≤(eCd⋅2j−i)d(1e)E[∣Bj∣]. \begin{aligned} \Pr \left[ |B_j| \ge d \right] &\le \left( \frac{e^{ \left( {d} / { \mathbb{E} [ |B_j| ] } \right) - 1 } }{ \left( {d} / { \mathbb{E} [ |B_j| ] } \right)^{ \left( {d} / { \mathbb{E} [ |B_j| ] } \right) } } \right)^{ \mathbb{E} [ |B_j| ]} \\ &\le \left( \frac{e}{C_d \cdot 2^{j - i} } \right)^d \left( \frac{1}{e} \right)^{ \mathbb{E} [ |B_j| ]}. \end{aligned}

By union bound, when Cd≥e2C_d \ge e^2, it holds that

Pr⁡[∃j≥i:∣Bj∣≥d]≤∑j≥i(eCd⋅2j−i)d≤(eCd)d∑j=0∞(12d)j≤(eCd)d11−12d≤2⋅δ2e2. \begin{aligned} \Pr \left[ \exists j \ge i : |B_j| \ge d \right] &\le \sum_{j \ge i} \left( \frac{e}{C_d \cdot 2^{j - i} } \right)^d \\ &\le \left( \frac{e}{C_d } \right)^d \sum_{j = 0}^\infty \left( \frac{1}{2^{d} } \right)^j \\ &\le \left( \frac{e}{C_d } \right)^d \frac{1}{1 - \frac{1}{2^{d} } } \\ &\le 2 \cdot \delta^{2 e^2}. \end{aligned}

□\square

Analysis the probability of E2E_2:

For each t∈Supp(x)t \in \mathbb{Supp}(x), we have

Pr⁡[h(t)<i]=(1−12i). \Pr[ h(t) < i ] = \left( 1 - \frac{1}{2^i} \right).

Therefore,

Pr⁡[h(t)<i:∀t∈Supp(x)]=(1−12i)k≤(1−1kln⁡1δ)k≤δ. \Pr[ h(t) < i : \forall t \in \mathbb{Supp}(x) ] = \left( 1 - \frac{1}{2^i} \right)^k \le \left( 1 - \frac{1}{k} \ln \frac{1}{\delta} \right)^k \le \delta.

□\square