Suppose S={s1,s2,...,sm}S = \{ s_1, s_2, ..., s_m\} is a set of samples, with weight W={w1,w2,...,wm}W = \{ w_1, w_2, ..., w_m\} and probability P={p1,p2,...,pm}P = \{ p_1, p_2, ..., p_m\}.

Now we randomly select an element XX from SS according to probability PP. Define YY the index XX. For example, if X=siX = s_i, then Y=iY = i. The expected weight of XX is given by

E[wY]=∑i∈[1,m]wiP(Y=i)=∑i∈[1,m]wipi E[w_Y] = \sum_{i \in [1,m]} w_i P( Y = i) = \sum_{i \in [1,m]} w_i p_i

If PP is a uniform distribution, i.e., pi=1/m, ∀i∈[1,m]p_i = 1/m, \ \forall i \in [1,m], then

E[wY]=1/m∗∑i∈[1,m]wi E[w_Y] = 1 / m * \sum_{i \in [1,m]} w_i

Then mE[wY]m E[w_Y] equals just the sum of WW. We say that it is an unbiased estimator of the sum of WW. But what if PP is not uniform, i.e., ∃i,j s.t.,pi≠pj\exists i,j \ s.t., p_i \neq p_j.

One technique to remedy this is to construct a new variable ZY=wY/pYZ_Y = w_Y / p_Y.

E[ZY]=∑i∈[1,m]wi/pi∗P(Y=i)=∑i∈[1,m]wi E[Z_Y] = \sum_{i \in [1,m]} w_i / p_i * P(Y = i) = \sum_{i \in [1,m]} w_i

The strange thing is that how we could know P={p1,p2,...,pm}P = \{ p_1, p_2, ..., p_m\}. If we know it in advance, we need not to do sampling to estimate $ \sum_{i \in [1,m]} w_i$.

But in some circumstance, it is possible. To get a random sample XX from SS, we need to go through some sampling process. Once XX is returned, we can calculate the probability of XX by examining the sampling process.

[FBKZ16] gives such an example. Consider two relations R1=(A1,F)R_1 = (A_1, F) and R2=(F,A2)R_2 = (F, A_2), where R1.FR_1.F and R2.FR_2.F shares the same domain. Let R=R1⋈R1.F=R2.FR2R = R_1 \bowtie_{R_1.F = R_2.F} R_2 be the natural join result based on R1.FR_1.F and R2.FR_2.F. The object is to calculate the sum of attribute A2A_2 on R=(A1,F,A2)R = (A_1, F, A_2).

The natural join can be expensive. Is there any way to estimate the join result? Indeed we have.

First sample uniformly at random a tuple (a1,f)(a_1, f) from R1R_1. The probability of a particular tuple chosen is 1/∣R1∣1/|R_1|, where ∣R1∣|R_1| is the number of tuples in R1R_1.

We further assume that there is an index on R2.FR_2.F. So we immediately know the size of the set R2[f]≐{(f,a2)∣(f,a2)∈R2}R_2[f] \doteq \{ (f, a_2) | (f, a_2) \in R_2 \}. We choose uniformly at random a tuple (f,a2)(f, a_2) from R2[f]R_2[f] and get the join result (a1,f,a2)(a_1, f, a_2). The probability corresponding to (a1,f,a2)(a_1, f, a_2) is 1/∣R1∣∗1/R2[f]1/ |R_1| * 1 / R_2[f]. So a2∣R1∣∣R2[f]∣\frac{a_2}{|R_1| |R_2[f]|} is an unbiased estimator of sum(R.A2)sum(R.A2)

Reference
[FBKZ16] Feifei. Li, Bin. Wu, Ke. Yi, Zhuoyue. Zhao, Wander Join: Online Aggregation via Random Walks. SIGMOD'16