Problem
Given a stream S S S of n n n elements, a server that observes the elements in S S S one by one is required to estimate the cardinality n n n with less than log n \log n log n bits, with relative error ϵ ∈ ( 0 , 1 ) \epsilon \in (0, 1) ϵ ∈ ( 0 , 1 ) and failure probability δ \delta δ .
Let w ∈ ( 1 , 2 ) w \in (1, 2) w ∈ ( 1 , 2 ) be some parameter to be determined.
Algorithm . Morris Counter
k ← 0 ; X 0 ← 0 k \leftarrow 0; \quad X_0 \leftarrow 0 k ← 0 ; X 0 ← 0 ; (introduced only for analysis)
For t ← 1 t \leftarrow 1 t ← 1 to n n n :
\qquad X t ← X t − 1 X_t \leftarrow X_{t - 1} X t ← X t − 1 ; (introduced only for analysis)
\qquad With probability p t ≐ 1 / w k p_t \doteq 1 / w^k p t ≐ 1/ w k do:
\qquad \qquad X t ← X t + w k X_t \leftarrow X_t + w^k X t ← X t + w k ;
\qquad \qquad k ← k + 1 k \leftarrow k + 1 k ← k + 1 ;
Output w k − 1 w − 1 \frac{w^k - 1}{w - 1} w − 1 w k − 1 .
It can be verified that, when the algorithm stops, X n = w k − 1 w − 1 X_n = \frac{w^k - 1}{w - 1} X n = w − 1 w k − 1 .
Theorem. ∀ 0 ≤ t ≤ n \forall 0 \le t \le n ∀0 ≤ t ≤ n , E [ X t ] = t \mathbb{E} [ X_t ] = t E [ X t ] = t .
Proof: Clearly, E [ X 0 ] = 0 \mathbb{E} [ X_0 ] = 0 E [ X 0 ] = 0 . For t ≥ 1 t \ge 1 t ≥ 1 ,
E [ X t ∣ X t − 1 ] = p t ⋅ ( X t − 1 + 1 p t ) + ( 1 − p t ) ⋅ X t − 1 = X t − 1 + 1.
\mathbb{E} [ X_t \mid X_{t - 1} ] = p_t \cdot \left( X_{t - 1} + \frac{1}{p_t} \right) + ( 1 - p_t ) \cdot X_{t - 1} = X_{t - 1} + 1.
E [ X t ∣ X t − 1 ] = p t ⋅ ( X t − 1 + p t 1 ) + ( 1 − p t ) ⋅ X t − 1 = X t − 1 + 1. Hence, by induction on t t t , we have
E [ X t ] = E [ E [ X t ∣ X t − 1 ] ] = E [ X t − 1 ] + 1 = t .
\mathbb{E} [ X_t ] = \mathbb{E} [ \mathbb{E} [ X_t \mid X_{t - 1} ] ] = \mathbb{E} [ X_{t - 1} ] + 1 = t.
E [ X t ] = E [ E [ X t ∣ X t − 1 ]] = E [ X t − 1 ] + 1 = t . □ \square □
Theorem. ∀ t ∈ [ n ] \forall t \in [n] ∀ t ∈ [ n ] , V a r [ X t ] = ( w − 1 ) ⋅ t 2 2 \mathbb{Var } [ X_t ] = (w - 1) \cdot \frac{ t^2 }{ 2 } V a r [ X t ] = ( w − 1 ) ⋅ 2 t 2 .
Proof: For t ≥ 1 t \ge 1 t ≥ 1 , it holds that X t − 1 = 1 / p t − 1 w − 1 X_{t - 1} = \frac{ 1 / p_t - 1}{w - 1} X t − 1 = w − 1 1/ p t − 1 . Therefore,
1 / p t = ( w − 1 ) ⋅ X t − 1 + 1
1 / p_t = (w - 1) \cdot X_{t - 1} + 1
1/ p t = ( w − 1 ) ⋅ X t − 1 + 1 and
E [ X t 2 ∣ X t − 1 ] = p t ⋅ ( X t − 1 + 1 / p t ) 2 + ( 1 − p t ) ⋅ ( X t − 1 ) 2 = 1 / p t + 2 ⋅ X t − 1 + ( X t − 1 ) 2 = ( w + 1 ) ⋅ X t − 1 + 1 + ( X t − 1 ) 2
\begin{aligned}
\mathbb{E} [ X_t^2 | X_{t - 1} ]
&= p_t \cdot \left( X_{t - 1} + 1 / p_t \right)^2 + ( 1 - p_t ) \cdot (X_{t - 1})^2 \\
&= 1 / p_t + 2 \cdot X_{t - 1} + (X_{t - 1})^2 \\
&= (w + 1) \cdot X_{t - 1} + 1 + (X_{t - 1})^2
\end{aligned}
E [ X t 2 ∣ X t − 1 ] = p t ⋅ ( X t − 1 + 1/ p t ) 2 + ( 1 − p t ) ⋅ ( X t − 1 ) 2 = 1/ p t + 2 ⋅ X t − 1 + ( X t − 1 ) 2 = ( w + 1 ) ⋅ X t − 1 + 1 + ( X t − 1 ) 2 Therefore, since X 0 = 0 X_0 = 0 X 0 = 0 ,
E [ X t 2 ] = E [ E [ X t 2 ∣ X t − 1 ] ] = ( w + 1 ) ⋅ E [ X t − 1 ] + 1 + E [ X t − 1 2 ] = E [ X t − 1 2 ] + ( w + 1 ) ⋅ ( t − 1 ) + 1 . . . = E [ X 0 2 ] + ( w + 1 ) ⋅ ∑ i = 0 t − 1 i + t = ( w + 1 ) ⋅ t ( t − 1 ) 2 + t ≤ ( w − 1 ) ⋅ t 2 2 + t 2 ,
\begin{aligned}
\mathbb{E} [ X_t^2 ]
&= \mathbb{E} [ \mathbb{E} [ X_t^2 | X_{t - 1} ] ] \\
&= (w + 1) \cdot \mathbb{E} [ X_{t - 1} ] + 1 + \mathbb{E} [ X_{t - 1} ^2] \\
&= \mathbb{E} [ X_{t - 1}^2 ] + (w + 1) \cdot (t - 1) + 1 \\
&... \\
&= \mathbb{E} [ X_0^2 ] + (w + 1) \cdot \sum_{i = 0}^{t - 1} i + t \\
&= (w + 1) \cdot \frac{ t (t - 1) }{ 2 } + t \\
&\le (w - 1) \cdot \frac{ t^2 }{ 2 } + t^2,
\end{aligned}
E [ X t 2 ] = E [ E [ X t 2 ∣ X t − 1 ]] = ( w + 1 ) ⋅ E [ X t − 1 ] + 1 + E [ X t − 1 2 ] = E [ X t − 1 2 ] + ( w + 1 ) ⋅ ( t − 1 ) + 1 ... = E [ X 0 2 ] + ( w + 1 ) ⋅ i = 0 ∑ t − 1 i + t = ( w + 1 ) ⋅ 2 t ( t − 1 ) + t ≤ ( w − 1 ) ⋅ 2 t 2 + t 2 , and
V a r [ X t ] = E [ X t 2 ] − ( E [ X t ] ) 2 ≤ ( w − 1 ) ⋅ t 2 2 .
\mathbb{Var } [ X_t ] = \mathbb{E} [ X_t^2 ] - (\mathbb{E} [ X_t ])^2 \le (w - 1) \cdot \frac{ t^2 }{ 2 } .
V a r [ X t ] = E [ X t 2 ] − ( E [ X t ] ) 2 ≤ ( w − 1 ) ⋅ 2 t 2 .
Corollary. By Chebyshev's inequality,
Pr [ ∣ X t − t ∣ ≥ ϵ ⋅ t ] ≤ ( w − 1 ) ⋅ t 2 2 ϵ 2 ⋅ t 2 = w − 1 2 ϵ 2 .
\Pr[ | X_t - t | \ge \epsilon \cdot t ] \le (w - 1) \cdot \frac{ t^2 }{ 2 \epsilon^2 \cdot t^2} = \frac{w - 1}{ 2 \epsilon^2 }.
Pr [ ∣ X t − t ∣ ≥ ϵ ⋅ t ] ≤ ( w − 1 ) ⋅ 2 ϵ 2 ⋅ t 2 t 2 = 2 ϵ 2 w − 1 .
Corollary. If we set w = 1 + 2 ϵ 2 δ w = 1 + 2 \epsilon^2 \delta w = 1 + 2 ϵ 2 δ , then the failure probability is at most δ \delta δ .
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 ↑