Suppose there are a set of n items with nonnegative weights w1,w2,...,wn. We want to maintain a subset of samples S⊂[n] in order to answer queries like given arbitrary I⊂[n] , what is ∑i∈Iwi?
A trivial solution is to set S=[n] and we can answer the query exactly. This is an overkill if our goal is to only answer the query approximately.
Alternatively, we can sample each item i∈[n] independently with probability pi (to be determined), and maintain a random variable
Xi={piwi0i is chosen else
Note that
E[Xi]=wi
And therefore,
E[i∈I∑Xi]=i∈I∑E[Xi]=i∈I∑wi
Threshold Sampling
One possible implementation of this sampling scheme is called threshold sampling.
First, a threshold T is chosen. Then, for each item i∈[n], if wi≥T, then the algorithm record a value Xi=wi deterministically. Otherwise, the algorithm record a value Xi=T with probability Twi. If Xi is not recorded, it has default value 0. In both cases, E[Xi]=wi.
Mathematically, the two cases can be combined into one. Let ui be a random number generated between (0,1). Then
Xi={max{wi,T}0ui<Twi else
Observe that if wi≥T, then wi/T≥1 and Xi≡max{wi,T}=wi. The expectation of Xi is given algebraically by
Denote S the set of items with Xi=0 and let pi be the probability of i being sampled. Then the expected size of S is given by
i=1∑npi=i=1∑nmin{1,Twi}
The problem with threshold sampling is that it is hard to control the memory usage of the sample set S. If the n items are known offline, and we would like ∣S∣≈k for some integer k≤n, then we can search for some T such that
i=1∑npi=k
Then ∣S∣=k in expectation.
The question is, is it possible to force ∣S∣=k exactly? Further, what if the items come online (one by one)?
Priority Sampling
Priority sampling achieves this. First recall that in threshold sampling we decide whether to keep i by checking whether a uniform sampled real number ui is less than Twi:
ui<Twi
This is equivalent to
T<uiwi
Define the priority of an item i∈[n] as qi=uiwi. Note that qi is a random variable. In threshold sampling, item i is kept with weight max{wi,T} if
T<qi
Now, instead of using a fixed value of T, the Priority sampling sets
T=(k+1)-th largest {qi}
By the definition of T, only the k items with largest priorities qi's are kept in the sample set S:
S={k items with largest qi ’s }
It can be seen that S and T can be easily maintained by a binary heap when the items come in stream fashion.
The challenge is not to analyze the behaviors of the r.v.'s:
Xi={max{wi,T}0T<uiwi≡ui<Twi else
since now T is itself a random variable.
Theorem
E[Xi]=wi.
Proof. There is an elegant proof based on conditional expectation. For i∈[n], define the random variable Ti as the k-th largest priority of elements other than i. If i-th is sampled, then qi≥Ti and Ti becomes the k+1-th largest priority. Therefore,
EXi[Xi∣Ti]=max{Xi,Ti}⋅min{1,wi/Ti}=wi.
Finally, EXi[Xi]=ETi[EXi[Xi∣Ti]]=wi.
Theorem
For I⊂[n],
E[i∈I∏Xi]={∏i∈Iwi0 if ∣I∣≤kelse
Proof: Clearly, when ∣I∣>k, then ∃i∈I, such that qi is not one of the k largest priorities. Hence ∏i∈IXi=0 for sure.
If ∣I∣≤k, define TI to be the k−∣I∣+1-th largest priority of elements in [n]∖I. Observe that ∏i∈IXi=0, if only the priorities of all i∈I are among the k largest ones. In such case, TI becomes the k+1-th largest priority. Therefore,
E[i∈I∏Xi∣TI]=i∈I∏E[Xi∣TI]=i∈I∏wi,
and E[∏i∈IXi]=E[E[∏i∈IXi∣TI]]=∏i∈Iwi.
Reference
Nick Duffield, Carsten Lund, and Mikkel Thorup. Priority sampling for estimation fo arbitrary subset sums. Journal of ACM, 2007
Chandra Chekuri, Lecture note, CS 598: Algorithms for Big Data