Problem
A stream of n n n elements indexed by [ n ] ≐ { 1 , 2 , 3 , . . . , n } [n] \doteq \{ 1, 2, 3, ..., n \} [ n ] ≐ { 1 , 2 , 3 , ... , n } . Each element i i i is associated with a weight w i w_i w i .
The goal is to maintain a set S S S of m m m samples, such that
S S S is initialized with { 1 , 2 , . . . , m } \{1, 2, ..., m \} { 1 , 2 , ... , m } ;
∀ t > m \forall t > m ∀ t > m , when element t t t arrives, ∀ i ∈ [ t ] \forall i \in [t] ∀ i ∈ [ t ] ,
the i i i -th element is kept with probabilityp i ≐ min { 1 , w i z t } ,
p_i \doteq \min \left\{ 1, \frac{w_i}{ z_t } \right\},
p i ≐ min { 1 , z t w i } , where z t z_t z t is the unique solution to the equation ∑ i ∈ [ t ] p i = m \sum_{i \in [t] } p_i = m ∑ i ∈ [ t ] p i = m , i.e.,∣ { i ∈ [ t ] ∧ w i ≥ z t } ∣ + ∑ i ∈ [ t ] ∧ w i < z t w i z t = m .
\left| \{ i \in [t] \wedge w_i \ge z_t \} \right| + \sum_{ i \in [t] \wedge w_i < z_t } \frac{w_i}{ z_t } = m.
∣ { i ∈ [ t ] ∧ w i ≥ z t } ∣ + i ∈ [ t ] ∧ w i < z t ∑ z t w i = m .
Definition. L ( t , x ) ≐ { i ∈ [ t ] : w i ≥ x } L(t, x) \doteq \{ i \in [t ] : w_i \ge x \} L ( t , x ) ≐ { i ∈ [ t ] : w i ≥ x } .
Definition. W ( t , x ) ≐ ∑ i ∈ [ t ] , w i < x w i W(t, x) \doteq \sum_{ i \in [t] , w_i < x } w_i W ( t , x ) ≐ ∑ i ∈ [ t ] , w i < x w i .
Definition.
f ( t , x ) ≐ ∣ { i ∈ [ t ] ∧ w i ≥ x } ∣ + ∑ i ∈ [ t ] , w i < x w i x = ∣ L ( t , x ) ∣ + 1 x ⋅ W ( t , x ) .
\begin{aligned}
f(t, x)
&\doteq \left| \{ i \in [t] \wedge w_i \ge x \} \right| + \sum_{ i \in [t] , w_i < x } \frac{w_i}{ x } \\
&= \left| L(t, x) \right| + \frac{1}{x} \cdot W(t, x).
\end{aligned}
f ( t , x ) ≐ ∣ { i ∈ [ t ] ∧ w i ≥ x } ∣ + i ∈ [ t ] , w i < x ∑ x w i = ∣ L ( t , x ) ∣ + x 1 ⋅ W ( t , x ) .
For given t ∈ [ n ] t \in [n] t ∈ [ n ] , z t z_t z t is the solution to f ( t , z t ) = m f(t, z_t) = m f ( t , z t ) = m .
Algorithm
Initially, we can set z t = 0 z_t = 0 z t = 0 for t ∈ [ m ] t \in [m] t ∈ [ m ] , so that p t = 1 p_t = 1 p t = 1 and element t t t is added to S S S .
Next, we discuss how to compute z t + 1 z_{t + 1} z t + 1 based on z t z_t z t and how to maintain S S S when the t + 1 t + 1 t + 1 -th element arrives.
Computing z t + 1 z_{t + 1} z t + 1
We want to find the solution z t + 1 z_{t + 1} z t + 1 for the equation f ( t + 1 , x ) = m f(t + 1, x ) = m f ( t + 1 , x ) = m (in which t + 1 t + 1 t + 1 is fixed and x x x is the variable). First, observe
f ( t + 1 , z t ) = f ( t , z t ) + min { w t + 1 z t , 1 } > m .
f(t + 1, z_t) = f(t, z_t ) + \min \left\{ \frac{w_{t + 1} }{ z_t}, 1 \right\} > m.
f ( t + 1 , z t ) = f ( t , z t ) + min { z t w t + 1 , 1 } > m . Hence, it holds that z t + 1 > z t z_{t + 1} > z_t z t + 1 > z t .
W.L.O.G, assume that L ( t + 1 , z t ) L(t + 1, z_t) L ( t + 1 , z t ) contains k k k elements whose weights { x 1 , x 2 , . . , x k } \{ x_1, x_2, .., x_k \} { x 1 , x 2 , .. , x k } are sorted in non-decreasing order:
z t ≐ x 0 ≤ x 1 ≤ x 2 ≤ . . . ≤ x k < x k + 1 ≐ ∞ .
z_t \doteq x_0 \le x_1 \le x_2 \le ... \le x_k < x_{k + 1} \doteq \infty.
z t ≐ x 0 ≤ x 1 ≤ x 2 ≤ ... ≤ x k < x k + 1 ≐ ∞. If we fix t + 1 t + 1 t + 1 , then for x > z t x > z_t x > z t , f ( t + 1 , x ) f(t + 1, x) f ( t + 1 , x ) is a decreasing and piece-wise linear function on intervals
( x 0 , x 1 ] , ( x 1 , x 2 ] , . . . ( x k − 1 , x k ] , ( x k , x k + 1 ) .
( x_0 , x_1 ], ( x_1 , x_2 ], ... ( x_{k - 1} , x_k ] ,( x_k , x_{k + 1} ).
( x 0 , x 1 ] , ( x 1 , x 2 ] , ... ( x k − 1 , x k ] , ( x k , x k + 1 ) . Here if x j − 1 = x j x_{j - 1} = x_j x j − 1 = x j for some j ∈ [ k ] j \in [ k ] j ∈ [ k ] , then we consider the interval ( x j − 1 , x j ] (x_{j -1}, x_j ] ( x j − 1 , x j ] an empty set and ignore it. If z t + 1 ∈ ( x j − 1 , x j ] z_{t + 1} \in (x_{j -1}, x_j ] z t + 1 ∈ ( x j − 1 , x j ] , it holds that
f ( t + 1 , x j − 1 ) > m ⟺ ∣ L ( t + 1 , x j − 1 ) ∣ + 1 x j − 1 ⋅ W ( t + 1 , x j − 1 ) > m , f ( t + 1 , x j ) ≤ m ⟺ ∣ L ( t + 1 , x j ) ∣ + 1 x j ⋅ W ( t + 1 , x j ) ≤ m .
\begin{aligned}
&f(t + 1, x_{ j - 1 } ) > m &\Longleftrightarrow & \quad | L(t + 1, x_{ j - 1 } ) | + \frac{ 1 }{ x_{ j - 1 } } \cdot W (t + 1, x_{ j - 1 } ) > m, \\
&f(t + 1, x_j ) \le m &\Longleftrightarrow & \quad | L(t + 1, x_j ) | + \frac{ 1 }{ x_{ j } } \cdot W (t + 1, x_{ j } ) \le m .
\end{aligned}
f ( t + 1 , x j − 1 ) > m f ( t + 1 , x j ) ≤ m ⟺ ⟺ ∣ L ( t + 1 , x j − 1 ) ∣ + x j − 1 1 ⋅ W ( t + 1 , x j − 1 ) > m , ∣ L ( t + 1 , x j ) ∣ + x j 1 ⋅ W ( t + 1 , x j ) ≤ m . When x ∈ ( x j − 1 , x j ] x \in(x_{j -1}, x_j ] x ∈ ( x j − 1 , x j ] , ∣ L ( t + 1 , x ) ∣ | L (t + 1, x) | ∣ L ( t + 1 , x ) ∣ is a constant that equals to ∣ L ( t + 1 , x j ) ∣ |L(t + 1, x_j )| ∣ L ( t + 1 , x j ) ∣ . We have
∣ L ( t + 1 , z t + 1 ) ∣ + 1 z t + 1 ⋅ W ( t + 1 , z t + 1 ) = ∣ L ( t + 1 , x j ) ∣ + 1 z t + 1 ⋅ W ( t + 1 , x j ) = m .
| L(t + 1, z_{t + 1} ) | + \frac{ 1 }{ z_{ t + 1 } } \cdot W(t + 1, z_{ t + 1 } )= | L(t + 1, x_j ) | + \frac{ 1 }{ z_{ t + 1 } } \cdot W(t + 1, x_{ j } ) = m.
∣ L ( t + 1 , z t + 1 ) ∣ + z t + 1 1 ⋅ W ( t + 1 , z t + 1 ) = ∣ L ( t + 1 , x j ) ∣ + z t + 1 1 ⋅ W ( t + 1 , x j ) = m . Therefore,
z t + 1 = W ( t + 1 , x j ) m − ∣ L ( t + 1 , x j ) ∣ = ∑ i ∈ [ t + 1 ] , w i < x j w i m − ∣ L ( t + 1 , x j ) ∣ .
z_{t + 1} = \frac{ W(t + 1, x_{ j } ) }{ m - \left| L(t + 1, x_j ) \right| } = \frac{ \sum_{ i \in [t + 1], w_i < x_j } w_i }{ m - \left| L(t + 1, x_j ) \right| }.
z t + 1 = m − ∣ L ( t + 1 , x j ) ∣ W ( t + 1 , x j ) = m − ∣ L ( t + 1 , x j ) ∣ ∑ i ∈ [ t + 1 ] , w i < x j w i . The following code shows the detailed process for the search of z t + 1 z_{t + 1} z t + 1 . It maintains
A set L L L , such that at the beginning of the algorithm, L = L ( t , z t ) L = L(t, z_t) L = L ( t , z t ) , and when the algorithm stops, L = L ( t + 1 , z t + 1 ) L = L(t + 1, z_{t + 1} ) L = L ( t + 1 , z t + 1 ) ;
The sum of weights W W W , which equals to W ( t , z t ) W(t, z_t) W ( t , z t ) at the beginning of the algorithm, and equals to W ( t + 1 , z t + 1 ) W(t + 1, z_{t + 1} ) W ( t + 1 , z t + 1 ) when the algorithm stops;
A set C C C , which contains elements L ( t , z t ) ∪ { t + 1 } ∖ L ( t + 1 , z t + 1 ) L(t, z_t) \cup \{ t + 1 \} \setminus L(t + 1, z_{t + 1} ) L ( t , z t ) ∪ { t + 1 } ∖ L ( t + 1 , z t + 1 ) .
Input : a) The weight w t + 1 w_{t + 1} w t + 1 ; b) The set L = L ( t , z t ) L = L(t, z_t) L = L ( t , z t ) .
C ← ∅ C \leftarrow \emptyset C ← ∅ ;
If w t + 1 ≥ z t : w_{t + 1} \ge z_t: w t + 1 ≥ z t :
\qquad L ← L ∪ { t + 1 } L \leftarrow L \cup \{ t + 1 \} L ← L ∪ { t + 1 } .
Else:
\qquad C ← C ∪ { t + 1 } C \leftarrow C \cup \{ t + 1 \} C ← C ∪ { t + 1 } .
While ∣ L ∣ ≠ ∅ |L| \neq \emptyset ∣ L ∣ = ∅ and W / min i ∈ L w i + ∣ L ∣ > m : W / \min_{i \in L } w_i + | L | > m: W / min i ∈ L w i + ∣ L ∣ > m :
\qquad W ← W + min i ∈ L w i W \leftarrow W + \min_{i \in L} w_i W ← W + min i ∈ L w i ;
\qquad move the element i ∈ L i \in L i ∈ L with minimum w i w_i w i from L L L to C C C .
z t + 1 ← W / ( m − ∣ L ∣ ) z_{t + 1} \leftarrow W / (m - |L| ) z t + 1 ← W / ( m − ∣ L ∣ ) .
Maintaining S S S
Denote S t S_t S t the sampled set after t t t elements are processed. Define
D = [ t ] ∖ L ( t , z t ) , E = S t ∖ L ( t , z t ) .
D = [t] \setminus L( t, z_t ), \\
E = S_t \setminus L( t, z_t ).
D = [ t ] ∖ L ( t , z t ) , E = S t ∖ L ( t , z t ) . By definition, it holds that ∣ E ∣ = s − ∣ L ( t , z t ) ∣ = W ( t , z t ) / z t = ∑ i ∈ D w i / z t |E| = s - |L(t, z_t) | = W(t, z_t ) / { z_t } = \sum_{i \in D} w_i / z_t ∣ E ∣ = s − ∣ L ( t , z t ) ∣ = W ( t , z t ) / z t = ∑ i ∈ D w i / z t .
When the element t + 1 t + 1 t + 1 comes, let S ′ ≐ S t ∪ { t + 1 } S' \doteq S_t \cup \{ t + 1 \} S ′ ≐ S t ∪ { t + 1 } elements. To obtain a set S t + 1 S_{t + 1} S t + 1 , we need to discard one element in S ′ S' S ′ . Suppose that we have computed z t + 1 z_{t + 1} z t + 1 . The elements ∈ [ t + 1 ] \in [t + 1] ∈ [ t + 1 ] can be partitioned into three sets:
[ t + 1 ] = L ( t + 1 , z t + 1 ) ∪ C ∪ D .
[ t + 1 ] = L(t + 1, z_{t + 1} ) \cup C \cup D.
[ t + 1 ] = L ( t + 1 , z t + 1 ) ∪ C ∪ D . Similarly, the set S ′ S' S ′ can be decomposed into three parts:
S ′ = L ( t + 1 , z t + 1 ) ∪ C ∪ E .
S' = L(t + 1, z_{t + 1} ) \cup C \cup E.
S ′ = L ( t + 1 , z t + 1 ) ∪ C ∪ E .
If i ∈ L ( t + 1 , z t + 1 ) i \in L(t + 1, z_{t + 1} ) i ∈ L ( t + 1 , z t + 1 ) , it must be kept in S t + 1 S_{t + 1} S t + 1 .
If i ∈ C i \in C i ∈ C , it should be removed with probability 1 − w i / z t + 1 1 - w_i / z_{t + 1} 1 − w i / z t + 1 . Since i ∈ C i \in C i ∈ C , either i = t + 1 i = t + 1 i = t + 1 or i ∈ L ( t , z t ) i \in L(t , z_t) i ∈ L ( t , z t ) . In both case, i i i appears in S ′ S' S ′ with probability 1 1 1 . On the other hand, we only keep it with probability w i / z t + 1 < 1 w_i / z_{ t + 1} < 1 w i / z t + 1 < 1 . Let p p p be the probability of discarding i i i . Then
1 ⋅ ( 1 − p ) = w i z t + 1 .
1 \cdot (1 - p) = \frac{w_i}{ z_{t + 1} }.
1 ⋅ ( 1 − p ) = z t + 1 w i . It follows that p = 1 − w i / z t + 1 p = 1 - w_i / z_{t + 1} p = 1 − w i / z t + 1 .
If if i ∈ E i \in E i ∈ E , then it should be removed with probability p = 1 − z t / z t + 1 p = 1 - z_t / z_{t + 1} p = 1 − z t / z t + 1 . Since i ∈ E i \in E i ∈ E , it comes from D D D and appears in S ′ S' S ′ with probability w i / z t w_i / z_t w i / z t . As the target probability to keep i i i should be w i / z t + 1 w_i / z_{t + 1} w i / z t + 1 , it holds that
w i z t ⋅ ( 1 − p ) = w i z t + 1 .
\frac{w_i}{ z_t } \cdot (1 - p) = \frac{ w_i }{ z_{t + 1} }.
z t w i ⋅ ( 1 − p ) = z t + 1 w i . Therefore, it should be discarded with probability p = 1 − z t / z t + 1 p = 1 - z_t / z_{t + 1} p = 1 − z t / z t + 1 .
Finally, we claim that the sum of probabilities to discard an element equals to 1:
∑ i ∈ C ( 1 − w i z t + 1 ) + ∣ E ∣ ⋅ ( 1 − z t z t + 1 ) = 1.
\sum_{i \in C} \left( 1 - \frac{w_i}{ z_{ t + 1} } \right) + |E| \cdot \left( 1 - \frac{z_t}{ z_{t + 1} } \right) = 1.
i ∈ C ∑ ( 1 − z t + 1 w i ) + ∣ E ∣ ⋅ ( 1 − z t + 1 z t ) = 1.
Proof:
∣ S ′ ∣ − m = f ( t , z t ) + 1 − f ( t + 1 , z t + 1 ) = ∣ L ( t , z t ) ∣ + 1 z t ⋅ ∑ i ∈ [ t ] , w i < z t w i + 1 − ∣ L ( t + 1 , z t + 1 ∣ − 1 z t ⋅ ∑ i ∈ [ t + 1 ] , w i < z t + 1 w i = ∑ i ∈ C ( 1 − w i z t + 1 ) + ∑ i ∈ D w i z t ( 1 − z t z t + 1 ) = ∑ i ∈ C ( 1 − w i z t + 1 ) + ∣ E ∣ ⋅ ( 1 − z t z t + 1 ) = 1.
\begin{aligned}
|S'| - m
&= f(t, z_t ) + 1 - f(t + 1, z_{ t + 1} ) \\
&= | L (t, z_t ) | + \frac{1}{z_t} \cdot \sum_{i \in [t], w_i < z_t} w_i + 1 - | L (t + 1, z_{t + 1} | - \frac{1}{z_t} \cdot \sum_{i \in [t + 1], w_i < z_{t + 1} } w_i \\
&= \sum_{i \in C} \left( 1 - \frac{w_i}{ z_{ t + 1} } \right) + \sum_{ i \in D} \frac{w_i }{ z_t } \left( 1 - \frac{z_t}{ z_{t + 1} } \right) \\
&= \sum_{i \in C} \left( 1 - \frac{w_i}{ z_{ t + 1} } \right) + |E| \cdot \left( 1 - \frac{z_t}{ z_{t + 1} } \right) \\
&= 1.
\end{aligned}
∣ S ′ ∣ − m = f ( t , z t ) + 1 − f ( t + 1 , z t + 1 ) = ∣ L ( t , z t ) ∣ + z t 1 ⋅ i ∈ [ t ] , w i < z t ∑ w i + 1 − ∣ L ( t + 1 , z t + 1 ∣ − z t 1 ⋅ i ∈ [ t + 1 ] , w i < z t + 1 ∑ w i = i ∈ C ∑ ( 1 − z t + 1 w i ) + i ∈ D ∑ z t w i ( 1 − z t + 1 z t ) = i ∈ C ∑ ( 1 − z t + 1 w i ) + ∣ E ∣ ⋅ ( 1 − z t + 1 z t ) = 1. □ \square □
To decide which element from C ∪ E C \cup E C ∪ E to discard, we can run the following pseudo-code:
Sample a real number r ∈ [ 0 , 1 ] r \in [0, 1] r ∈ [ 0 , 1 ] uniformly at random.
For i ∈ C i \in C i ∈ C do:
\qquad if r < 1 − w i / z t + 1 r < 1 - w_i / z_{t + 1} r < 1 − w i / z t + 1 :
\qquad \qquad discard i i i and return;
\qquad else:
\qquad \qquad r ← 1 − w i / z t + 1 r \leftarrow 1 - w_i / z_{t + 1} r ← 1 − w i / z t + 1 ;
Discard one element from E E E uniformly at random;
Reference
[1] G. Cormode and K. Yi, Small Summaries for Big Data, 1st ed. Cambridge University Press, 2020.
← Back to all notes Back to top ↑