Let D be an order domain with size d, and f=(0,…,0)∈Nd be the frequency vector initialized with all zeros.
Suppose that there is a stream S of n elements updating f as follows:
For each x∈Sdo f[x]←f[x]+1.
When all updates in S have arrived, we have that ∣S∣=∥f∥1=n.
We study the ranks of elements in D.
Definition. For x∈D, define the rank r(x) of x to as
r(x)≐y<x∑f[y].
Approximate Rank Query. Given a parameter ϵ∈(0,1), design a data structure which, for each x∈D, outputs an estimate r^(x) of r(x), such that
r(x)−ϵ∥f∥1≤r^(x)≤r(x)+ϵ∥f∥1.
MRL Sketch
Theorem (MRL Sketch). If the size of the stream ∣S∣=n is given in advance, the MRL Sketch solves the approximate rank query with space usage
O(ϵ−1log2(ϵn)).
Structure
The structure of the algorithm comprises a sequence of L+1 arrays A0,…AL, each of size
k≐⌈ϵ−1(1+log(ϵn))⌉even
where ⌈⋅⌉even is the operation of rounding a real number up to nearest even integer.
It sets L, the largest index of the arrays, to ⌈log(n/k)⌉.
Therefore,
The algorithm COMPRESS inspects the arrays A0,…,AL−1 in increasing order.
If an array Ai, reaches its capacity, the algorithm performs a compaction on Ai.
In particular, it moves the odd-indexed elements of Ai to Ai+1, and then reset Ai to empty (by deleting all its elements).
This may cause Ai+1 to research its capacity, and trigger a sequence of compactions.
COMPRESS.
i←0;
whilei<L and ∣Ai∣=k: // no empty buckets
Sort Ai in increasing order.
Move Ai[1],Ai[3],…,Ai[k−1] into Ai+1
Delete all elements in Ai
i←i+1.
Suppose that each elements in array Ai (0≤i≤L) is associated with weight wi=2i.
It is easy to prove, by induction on the size of n, and on the number of inductions performed, the following invariant
Invariant. For 0≤i≤L, define wi=2i. Then
i=0∑Lwi⋅∣Ai∣=n.
Therefore, 2L∣AL∣=2⌈log(n/k)⌉⋅∣AL∣≤n.
Therefore, ∣AL∣≤k and AL will never exceed its capacity.
Interpretation of Weights
The weights have another interpretation.
Suppose that we associate each element inserted into A0 with a set, which contains only the element itself.
We will maintain these sets, such that
For each 0≤i≤L, and each v∈Ai, the set associated with v has size wi, and v is the only element in the set that is kept in the arrays. Further, the sets associated with different elements in the arrays are disjoint.
Clearly this holds when an element is inserted into A0.
Now, suppose we are going to perform a compaction at an array Ai, for some 0≤i<L.
We delete half of the elements in Ai, and keep the rest of the elements.
We can pair up the elements deleted and the ones kept one by one, and merge their associated sets.
For each merged set, it contains wi+1 elements, and only one element in the merge set is kept in the arrays.
Hence, we can associate it with the only element kept.
The above discussion gives a new interpretation of the weights.
If an element is associated with weight wi, it represents wi unique elements in the original stream.
Query
Definition. For each i∈{0,…,L}, and x∈D, let r(x,Ai) be the rank of x in Ai, defined as
r(x,Ai)≐∣{y∈Ai:y<x}∣,
which is the number of elements in Ai that is strictly less than x.
Given x∈D, the MRL sketch returns an weighted sum of the ranks r(x,Ai), as an estimate of r(x):
r^(x)=i=0∑Lwi⋅r(x,Ai).
Rank:x∈D
r^(x)←0;
Fori←0 to Ldo
r^(x)←r^(x)+wi⋅r(x,Ai).
Error Analysis
Now we analyze the approximation error of r^(x).
We illustrate the key idea of the analysis via an example.
In the figure below, k=4.
Before the compaction, A5 contributes 25⋅r(4,A5)=25⋅2 to the estimation of r^(4), and 25⋅r(9,A5)=25⋅3 to the estimation of r^(9).
The compaction moves elements in the odd positions of A5, namely 3 and 10, to A6.
Then it deletes all elements in A5.
After the compaction, A6 contributes 26⋅r(4,A6)=26⋅1 to the estimation of r^(4), and 26⋅r(9,A6)=26⋅1 to the estimation of r^(9).
There is no error incurred to the estimation of r^(4) at this compaction step, while the estimation error incurred to r^(9) is −25 at this compaction step.
The above discussion leads to the following observations.
Observation. Let x∈D and 0≤i<L. Denote Ai,Ai+1 the arrays before the compaction and Ai′,Ai+1′ the ones after.
If r(x,Ai) is even, then
wi+1⋅r(x,Ai+1′)=wi+1⋅r(x,Ai+1)+wi⋅r(x,Ai),
which implies that the compaction at Ai incurs no error to the estimation of r(x).
which implies that the compaction at Ai incurs an error of −2i to the estimation of r(x).
Let mi be the number of compactions performed at Ai.
For each j<i, each compaction at Aj discards k/2 elements and transfers only k/2 elements to Aj+1.
Therefore, the number of elements that have ever been inserted into Ai is bounded by n/2i, and mi is bounded by
mi≤2i⋅kn.
Given x∈D, let ηi,t be the parity of r(x,Ai) just before the t(th)compaction is performed on Ai.
The estimation error Err(x) is given by
In this section, we reduce the space usage from O(ϵ−1⋅log2(ϵn)) to
O(ϵ−1⋅ln(1/δ)⋅loglog(1/δ)ϵn).
The algorithm is a randomized one and has failure probability δ.
The algorithm is similar to the MRL sketch, but
sets k=2/ϵ⋅ln2/δ and L=⌈log(n/k)⌉ to achieve the space usage stated above.
Besides, the compaction step is different.
Random Parity
Instead of always moving the odd-indexed elements to the next array, it chooses uniformly at random either the odd-indexed elements, or even-indexed elements.
In particular, it replaces the line 4 of the COMPRESS algorithm with the following lines:
Flip an unbiased coin;
If the coin turns out head:
Move Ai[1],Ai[3],…,Ai[k−1] into Ai+1;
Else:
Move Ai[0],Ai[2],…,Ai[k−2] into Ai+1;
Unbiased Estimation
For each x∈D and 0≤i<L, if r(x,Ai) is even, then there is no error introduced after the compaction on Ai.
If r(x,Ai) is odd, then the error introduced is either 2i or −2i, both with probability 1/2.
Hence, the expected error is always 0.
The estimation error Err(x) is given by
Err(x)=i=0∑L−1t∈[mi]∑ηi,t⋅σi,t⋅2i,
where σi,t∈{−1,1} are Rademacher random variables.
Clearly, we have that
E[Err(x)]=0.
Confidence Interval of the Estimation Error
However, the random variables ηi,t⋅σi,t⋅2i are not independent, as the values of ηi,t depends on previous compressions.
Instead of using the Hoeffding's inequality for independent random variables, we resort to a martingale concentration inequality.
Martingale Difference Sequence
Let m=∑i=0L−1mi be the total number of compaction at the arrays.
For fixed n and k, m is a deterministic number (even without knowing the elements in S).
Further, for each t∈[m], the array at which the t-th compaction is performed is also deterministic.
Denote at∈[0,…,L−1] the index of the corresponding array, and let ct≐2at, which is a deterministic number since at is deterministic.
It holds that
For each t∈[m], define the random variable Xt∈{−ct,0,ct}, such that
If rank(x,Aat) is even, then Pr[Xt=0]=1.
If rank(x,Aat) is odd, then Pr[Xt=−ct]=Pr[Xt=ct]=1/2
The Xt are not independent random variables, since for t>1, when the t-th compaction happens at the at-th array Aat, the rank of x at this array depends on the previous compressions.
However, the conditional expectation of Xt is always 0:
E[Xt∣Xt−1,…,X1]=0.
Hence, X1,…,Xm is a martingale difference sequence.
Theorem.([1]) Let c1,…,cm be constants, and let X1,…,Xm be a martingale difference sequence with ∣Xt∣≤ct for each t.
Then for any λ≥0,
Pr[t∈[m]∑Xt≥λ]≤2⋅exp(−2⋅∑t∈[m]ct2λ2).
Choosing k
It follows that with probability at least 1−δ, it holds that
Setting k=⌈2/ϵ⋅ln2/δ⌉even, this error is bounded by ϵ⋅n.
The total space usage is therefore
k⋅L∈O(k⋅logkn)⊂O(ϵ1⋅lnδ1⋅logln1/δϵ⋅n).
Remark. It is kind of amazing that the size of k does not depends on n.
On the other hand, even though the size of L depends on n, we can still apply the algorithm when we don't know the value of n in advance.
We can create new arrays on the fly.
In particular, if the array with the largest index exceeds its capacity, we perform compaction at it and create a new array.
Note the we don't perform compaction at that array, when it reaches its capacity.
The entire analysis still holds.
Improved KLL Sketch
In this section, we improve the space usage to
O(ϵ1⋅lnδ1+logln1/δϵ⋅n).
Compared to the basic KLL, the improved algorithm allows the capacities of the arrays to vary.
Denote the largest index of the arrays as L.
It and maintains the following invariant:
For each 0≤i≤L, the array AL−i has capacity ⌈αi⋅k⌉even, where α∈(0.5,1) is a parameter chosen by the algorithm.
The algorithm sets k=⌈2/ϵ⋅1/(2α−1)⋅ln2/δ⌉even.
When n elements arrive, at least one compaction is performed at AL−1, as otherwise AL would be not created.
When the first compaction is performed at AL−1, it has capacity k.
As each element in AL−1 represents 2L−1 unique elements in the original stream,
k⋅2L−1≤n⟹2L−1≤kn,and L≤⌈2logkn⌉.
It follows that the total space usage is bounded by
Initially, L=0 and the algorithm creates a single array A0 with capacity k.
When AL reaches or exceeds its capacity, the algorithm increases L and creates a new array.
The new algorithm COMPRESS is as follows.
COMPRESS.
i←0
while∣Ai∣≥⌈αL−i⋅k⌉even // no empty buckets
Ifi=Lthen
L←L+1
Create a new array AL
Sort Ai in increasing order
Flip an unbiased coin
IfAi contains odd number of elements then
E← the element at the back of A
delete E from A
Else
E← null
If the coin turns out head then
Move odd index elements of Ai to Ai+1
Else:
Move even index elements of Ai to Ai+1
Delete all elements in Ai
If E= null then
Put E back into Ai
i←i+1
Analysis
Maintaining the Invariant
Lemma. The invariant is maintained.
Proof. We prove this by induction.
Initially, L=0 and A0 has capacity k.
If A0 has not reaches its capacity, the invariant holds trivially.
Now, suppose that the invariant holds before the algorithm COMPRESS.
If L is not increased and no new array is created by the algorithm, i.e., the while loop of the algorithm stops for i≤L, then the invariant still holds.
If L is increased and new arrays are created, then when the algorithm COMPRESS stops, compactions have been performed for all arrays except the one with highest index.
Hence,
$
0 \le |A_j| \le 1 < \lceil \alpha^i \cdot k \rceil_{even}.
$
Finally, when while loop stops, it holds that i=L.
The stopping condition implies that ∣AL∣<⌈α0⋅k⌉even=k.
□
Question to ponder: Prove that at most one new array can be created by COMPRESS each time it is invoked.
Bounding the Number of Compaction
Lemma. When the algorithms stops, for each i∈[L], the number of compaction performed at the array AL−i is bounded by (4/α)i.
Proof.
For each i∈[L], when a compaction is performed at AL−i, at least ⌈αi⋅k⌉even elements are removed from it.
As each element in AL−i represents 2L−i elements in the original stream, it holds that
mL−i≤2L−i⋅⌈αi⋅k⌉evenn≤2L−i⋅αi⋅kn
□
Finally, we discuss the estimation error of an element.
For fixed n, the number of compactions performed, as well as the order of the compactions performed are deterministic.
Using the notations in the previous section, we have
Setting k=⌈2/ϵ⋅2α−11⋅ln2/δ⌉even, this error is bounded by ϵ⋅n.
Reference
[1] C. McDiarmid, “Concentration,” in Probabilistic Methods for Algorithmic Discrete Mathematics, M. Habib, C. McDiarmid, J. Ramirez-Alfonsin, and B. Reed, Eds. Berlin, Heidelberg: Springer Berlin Heidelberg, 1998, pp. 195–248.
[2] Z. Karnin, K. Lang, and E. Liberty, “Optimal Quantile Approximation in Streams,” arXiv:1603.05346 [cs], Apr. 2016