Suppose S={s1,s2,...,sm} is a set of samples, with weight W={w1,w2,...,wm} and probability P={p1,p2,...,pm}.
Now we randomly select an element X from S according to probability P. Define Y the index X. For example, if X=si, then Y=i. The expected weight of X is given by
E[wY]=i∈[1,m]∑wiP(Y=i)=i∈[1,m]∑wipi
If P is a uniform distribution, i.e., pi=1/m,∀i∈[1,m], then
E[wY]=1/m∗i∈[1,m]∑wi
Then mE[wY] equals just the sum of W. We say that it is an unbiased estimator of the sum of W. But what if P is not uniform, i.e., ∃i,js.t.,pi=pj.
One technique to remedy this is to construct a new variable ZY=wY/pY.
E[ZY]=i∈[1,m]∑wi/pi∗P(Y=i)=i∈[1,m]∑wi
The strange thing is that how we could know P={p1,p2,...,pm}. 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 X from S, we need to go through some sampling process. Once X is returned, we can calculate the probability of X by examining the sampling process.
[FBKZ16] gives such an example. Consider two relations R1=(A1,F) and R2=(F,A2), where R1.F and R2.F shares the same domain. Let R=R1⋈R1.F=R2.FR2 be the natural join result based on R1.F and R2.F. The object is to calculate the sum of attribute A2 on R=(A1,F,A2).
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) from R1. The probability of a particular tuple chosen is 1/∣R1∣, where ∣R1∣ is the number of tuples in R1.
We further assume that there is an index on R2.F. So we immediately know the size of the set R2[f]≐{(f,a2)∣(f,a2)∈R2}. We choose uniformly at random a tuple (f,a2) from R2[f] and get the join result (a1,f,a2). The probability corresponding to (a1,f,a2) is 1/∣R1∣∗1/R2[f]. So ∣R1∣∣R2[f]∣a2 is an unbiased estimator of sum(R.A2)
Reference
[FBKZ16] Feifei. Li, Bin. Wu, Ke. Yi, Zhuoyue. Zhao, Wander Join: Online Aggregation via Random Walks. SIGMOD'16