Suppose there are a set of nn items with nonnegative weights w1,w2,...,wnw_1, w_2, ..., w_n. We want to maintain a subset of samples S⊂[n]S \subset [n] in order to answer queries like given arbitrary I⊂[n]I \subset [n] , what is ∑i∈Iwi\sum_{i \in I} w_i?

A trivial solution is to set S=[n]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]i \in [n] independently with probability pip_i (to be determined), and maintain a random variable

Xi={wipii is chosen0 else  \begin{aligned} X_i = \begin{cases} \frac{w_i}{p_i} & i \text{ is chosen} \\ 0 & \text{ else } \end{cases} \end{aligned}

Note that

E[Xi]=wi \mathbb{E} \left[ X_i \right] = w_i

And therefore,

E[∑i∈IXi]=∑i∈IE[Xi]=∑i∈Iwi \mathbb{E} \left[\sum_{i \in I} X_i \right] = \sum_{i \in I} \mathbb{E}[X_i] = \sum_{i \in I} w_i

Threshold Sampling

One possible implementation of this sampling scheme is called threshold sampling.

First, a threshold TT is chosen. Then, for each item i∈[n]i \in [n], if wi≥Tw_i \ge T, then the algorithm record a value Xi=wiX_i = w_i deterministically. Otherwise, the algorithm record a value Xi=TX_i = T with probability wiT\frac{w_i}{T}. If XiX_i is not recorded, it has default value 00. In both cases, E[Xi]=wi\mathbb{E} [ X_i ] = w_i.

Mathematically, the two cases can be combined into one. Let uiu_i be a random number generated between (0,1)(0, 1). Then

Xi={max⁡{wi,T}ui<wiT0 else  \begin{aligned} X_i = \begin{cases} \max \{ w_i, T \} & u_i < \frac{w_i}{T} \\ 0 & \text{ else } \end{cases} \end{aligned}

Observe that if wi≥Tw_i \ge T, then wi/T≥1w_i / T \ge 1 and Xi≡max⁡{wi,T}=wiX_i \equiv \max \{ w_i, T \} = w_i. The expectation of XiX_i is given algebraically by

E[Xi]=max⁡{wi,T}⋅Pr⁡[ui≤wiT]=max⁡{wi,T}⋅min⁡{1,wi/T}=wi. \begin{aligned} \mathbb{E} [ X_i ] = \max \{ w_i, T \} \cdot \Pr \left[ u_i \le \frac{w_i}{T} \right] = \max \{ w_i, T \} \cdot \min \{ 1, w_i / T \} = w_i. \end{aligned}

It follows by linearity of expectation that

E[∑i∈IXi]=∑i∈Iwi. \mathbb{E} \left[ \sum_{i \in I} X_i \right] = \sum_{i \in I} w_i.

Space Usage

Denote SS the set of items with Xi≠0X_i \neq 0 and let pip_i be the probability of ii being sampled. Then the expected size of SS is given by

∑i=1npi=∑i=1nmin⁡{1,wiT} \sum_{i = 1}^n p_i = \sum_{i = 1}^n \min \left\{ 1 , \frac{w_i}{T} \right\}

The problem with threshold sampling is that it is hard to control the memory usage of the sample set SS. If the nn items are known offline, and we would like ∣S∣≈k|S| \approx k for some integer k≤nk \le n, then we can search for some TT such that

∑i=1npi=k \sum_{i = 1}^n p_i = k

Then ∣S∣=k|S| = k in expectation.

The question is, is it possible to force ∣S∣=k|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 ii by checking whether a uniform sampled real number uiu_i is less than wiT\frac{w_i}{T}:

ui<wiT u_i < \frac{w_i}{T}

This is equivalent to

T<wiui T < \frac{w_i}{u_i}

Define the priority of an item i∈[n]i \in [n] as qi=wiuiq_i = \frac{w_i}{u_i}. Note that qiq_i is a random variable. In threshold sampling, item ii is kept with weight max⁡{wi,T}\max \{ w_i, T \} if

T<qi T < q_i

Now, instead of using a fixed value of TT, the Priority sampling sets

T=(k+1)-th largest {qi} T = (k + 1) \text{-th largest } \{ q_i \}

By the definition of TT, only the kk items with largest priorities qiq_i's are kept in the sample set SS:

S={k items with largest qi ’s } S = \{ k \text{ items with largest } q_i \text{ 's } \}

It can be seen that SS and TT 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}T<wiui≡ui<wiT0 else  \begin{aligned} X_i = \begin{cases} \max \{ w_i, T \} & T < \frac{w_i}{u_i} \equiv u_i < \frac{w_i}{T} \\ 0 & \text{ else } \end{cases} \end{aligned}

since now TT is itself a random variable.

Theorem

E[Xi]=wi. \mathbb{E}[X_i] = w_i.

Proof. There is an elegant proof based on conditional expectation. For i∈[n]i \in [n], define the random variable TiT_i as the kk-th largest priority of elements other than ii. If ii-th is sampled, then qi≥Tiq_i \ge T_i and TiT_i becomes the k+1k+1-th largest priority. Therefore,

EXi[Xi∣Ti]=max⁡{Xi,Ti}⋅min⁡{1,wi/Ti}=wi. \mathbb{E}_{X_i} [ X_i \mid T_i ] = \max \{ X_i, T_i \} \cdot \min \{1, w_i / T_i \} = w_i.

Finally, EXi[Xi]=ETi[EXi[Xi∣Ti]]=wi\mathbb{E}_{X_i} [X_i ] = \mathbb{E}_{T_i} [ \mathbb{E}_{X_i} [ X_i \mid T_i] ] = w_i.

Theorem

For I⊂[n]I \subset [n],

E[∏i∈IXi]={∏i∈Iwi if ∣I∣≤k0else \begin{aligned} \mathbb{E}\left[ \prod_{i \in I} X_i \right] = \begin{cases} \prod_{i \in I} w_i & \text{ if } |I| \le k \\ 0 & else \end{cases} \end{aligned}

Proof: Clearly, when ∣I∣>k|I| > k, then ∃i∈I\exists i \in I, such that qiq_i is not one of the kk largest priorities. Hence ∏i∈IXi=0\prod_{i \in I} X_i = 0 for sure.

If ∣I∣≤k|I| \le k, define TIT_I to be the k−∣I∣+1k - |I| + 1-th largest priority of elements in [n]∖I[n] \setminus I. Observe that ∏i∈IXi≠0\prod_{i \in I} X_i \neq 0, if only the priorities of all i∈Ii \in I are among the kk largest ones. In such case, TIT_I becomes the k+1k+1-th largest priority. Therefore,

E[∏i∈IXi∣TI]=∏i∈IE[Xi∣TI]=∏i∈Iwi, \mathbb{E} \left[ \prod_{i \in I} X_i \mid T_I \right] = \prod_{i \in I} \mathbb{E} [ X_i \mid T_I ] = \prod_{i \in I} w_i,

and E[∏i∈IXi]=E[E[∏i∈IXi∣TI]]=∏i∈Iwi\mathbb{E}[ \prod_{i \in I} X_i ] = \mathbb{E} [ \mathbb{E} \left[ \prod_{i \in I} X_i \mid T_I \right] ] = \prod_{i \in I} w_i.

Reference

  1. Nick Duffield, Carsten Lund, and Mikkel Thorup. Priority sampling for estimation fo arbitrary subset sums. Journal of ACM, 2007
  2. Chandra Chekuri, Lecture note, CS 598: Algorithms for Big Data