Let x∈Rn be a vector initialized with all zeros. There is a stream S of updates, each of which is a pair (t,Δ)∈[n]×R, causing xt←xt+Δ.
Define the support Supp(x) to be the set of all non-zero indexes of x:
Supp(x)≐{i∈[n]:xi=0}.
ℓ0 sampling: output an index uniformly at random from the support Supp(x).
We assume that at any time, ∀t∈[n],xt≤M for some M∈R.
Naive Solutions
We can uses an array to keep x explicitly. But this has size O(n⋅logM) bits.
Goal: design an algorithm for ℓ0 sampling with space less than O(n⋅logM) bits.
If there are only insertion updates, i.e., ∀(t,Δ)∈S, it holds that Δ>0, then we can use a hash set to keep the non-zero elements and their frequencies, which has size O(∣Supp(x)∣⋅logM). If x is dense, i.e., ∣Supp(x)∣ is large, this is still infeasible.
Sampling Game
We discuss one sampling game that illustrates the key ideas used in ℓ0-sampling.
In this game, we want to sample one integer uniformly at random from [k]. Instead of sampling it directly, we choose some p∈(0,1] and perform the following two steps:
First, we sample each i∈[k] uniformly and independent at random with probability p. Denote B the set of sampled elements.
If B=∅, then output one element from B uniformly at random.
Property.E[∣B∣]=kp.
Property.Pr[∣B∣>0]=(1−p)k≤exp(−kp).
Property. By Bernstein's Inequality, ∀δ∈(0,1), with probability at least 1−δ, we have
∣B∣−E[∣B∣]≤2⋅k⋅p⋅(1−p)⋅lnδ1+32lnδ1.
Theorem. Each element in [k] is outputted with equal probability
k1−(1−p)k.
Remark: when p=1, this reduces to sampling an element uniformly at random from [k].
Proof. The probability that B=∅ is given by (1−p)k. Conditioned on that B=∅, by symmetry, each element is sampled with equal probability.
□
Remark: We can also have a proof by algebra. For evert i∈[k], the probability that it is sampled is given by
We use the technique developed in the Sampling Game section, to avoid using O(∣Supp(x)∣logM) bits. We have seen that p controls the tradeoff between the size of B, and the successful probability of sampling an element (B=∅).
We begin with the assumption that the size of Supp(x) is known.
Insertion Only
As before, we consider insertion only model first.
Claim. There is an algorithm such that
with probability 1−δ, it returns an element from Supp(x) uniformly at random;
with probability δ, it returns nothing.
Further, the space usage is O(lnδ1) with probability at least 1−δ.
For simplicity, we assume a perfect hash function h that maps elements in [n] to {0,1} independently with probability p (to be determined). For each update (t,Δ) in the stream, we process it only if h(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)∣. As analysed before, with probability (1−p)k, the algorithm fails. To make the failure probability as small as δ, we have that (1−p)k=δ, i.e.,
p=1−δk1≤−lnδk1=k1lnδ1.
The last inequality holds since the function y=−lnx is convex, and y=1−x is the tangent line of y=−lnx at the point (1,0). By previous analysis, with probability at least 1−δ, the size of B is bounded by
(kp+2kp(1−p)lnδ1+32lnδ1)∈O(lnδ1).
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), and that h(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 1 is 0 finally, we don't this before we see the entire string.
In general, h maps np elements in [n] to 1. If we use a hashset, its size can grow to O(np).
However, there is data structure that can handle deletions while using less than O(np) space.
Sparse Recovery. Given an integer d at the beginning of the stream, if Supp(x)≤d when the stream stops, there is a data structure that returns
indices in Supp(x);
and their values,
using O(d(logn+logM))⊂O(dlog(nM)) bits. It processes each update (t,Δ) in the stream in O(d) time and recovers x in O(d2polylog(nM)) time. Its recovery can be wrong if Supp(x)>d.
It suffices to use such a data structure for the sampled set B with d∈O(lnδ1). We have seen that if we sampled each element in Supp(x) with probability p=1−δ1/k, then with probability at least 1−δ, we obtain a sampled set B with size at most d. Then we return one element from B 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 k1−(1−p)k. Here, conditioned on ∣B∣≤d, each element is sampled equally likely. However, ∣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 δ, each element is returned with probability
[k1−(1−p)k±δ]⊆[1−(1+k1)⋅δ,1+(1−k1)⋅δ].
Sampling With UnKnown Support Size
Previously, we want to sample each element in [n] with probability 1−δk1≤lnδk1=k1lnδ1, where k=∣Supp(x)∣. When this information is unavailable, we can guess by trying the values
1/2,1/4,1/8,1/16,...
We don't need to guess the value of k1lnδ1 exactly. In order that the sparse recovery data structure works, any guess within a constant factor suffices.
For simplicity, assume that n is some power of 2. Now, instead of using one sampling set B, we use logn+2 of them B1,B2,B3,..., each associated with a sparse recovery structure with parameter d∈Θ(lnδ1). Assume that we have a perfect hash function h:[n]→[logn+2] such that it holds independently for every t∈[n],