Problem

In many applications, data comes as a stream S={x1,x2,...,xn}S = \{x_1, x_2, ..., x_n \}, where each xi,i∈[n]x_i, i \in [n] belongs to the domain U\mathcal{U}, with size ∣U∣=N|\mathcal{U}| = N. With out lost of generality, suppose each element in U\mathcal{U} appear at least once in SS.

Problem. Estimate NN, i.e., the number of distinct elements in SS.

Naive solutions include

  1. Sorting. With quicksort, this can be preformed in O(nlog⁡n)O(n \log n) expected time and O(n)O(n) space.

  2. Hashing. With hashing (e.g., Cuckoo hashing), this can be done in O(n)O(n) time and O(n)O(n) space with two level hashing.

In real world application, nn could be extremely large. Therefore, the demand for O(n)O(n) space is expansive or even impossible.

Question. Can we perform this with O~(1)\tilde O(1) space?

If we allow some error, this is possible.

Flajolet and Martin Algorithm

Let H\mathcal{H} be the collection of all functions from U\mathcal{U} to [0,1][0, 1]. Suppose that we pick a function hh uniformly at randomly from H\mathcal{H}, then it maps elements in U\mathcal{U} independently and uniformly at random to real numbers in [0,1][0, 1].

Question: can we find another family of U\mathcal{U} so that the following analysis still hold?

The algorithm works as follows.

Flajolet and Martin Algorithm.

  1. Denote h(S)={h(x1),h(x2),...,h(xn)}h(S) = \{ h(x_1), h(x_2), ..., h(x_n)\};
  2. Let Z←min⁡h(S)Z \leftarrow \min h(S);
  3. Return 1/Z−11 / Z - 1.

Expectation

Observation. Pr⁡[Z≥z]=(1−z)N\Pr [ Z \ge z] = (1 - z)^N and Pr⁡[Z≤z]=1−(1−z)N\Pr[Z \le z] = 1 - (1 - z)^N.

Hence the density function is given as follows.

Density Function. Let pp be the density function of ZZ, then

p(z)=∂Pr⁡[Z≤z]∂z=N(1−z)N−1. p(z) = \frac{\partial \Pr[Z \le z] }{ \partial z} = N (1 - z)^{N - 1}.

We are ready to discuss the expectation.

Theorem. E[Z]=1/(N+1)\mathbb{E} [Z] = 1 / \big(N + 1\big).

Proof 1.

E[Z]=∫z=01z⋅p(z) dz=1N+1. \mathbb{E} [Z] = \int_{z = 0}^1 z \cdot p(z) \ dz = \frac{1}{N + 1}.

□\square

Proof 2. Another way to prove this is to introduce an additional element xn+1x_{n + 1} which is different from any other element in U\mathcal{U} and let S′=S∪{xn+1}S' = S \cup \{ x_{n + 1} \}.

What is the probability that h(xn+1)h(x_{n + 1}) is the smallest in h(S′)h(S'), i.e., min⁡h(S′)=h(xn+1)\min h(S') = h(x_{n + 1})? Conditioning on the minimum value of {h(x1),h(x2),...,h(xn)}\{ h(x_1), h(x_2), ..., h(x_n) \} (ZZ) being some zz (z∈[0,1]z \in [0, 1]), the probability becomes

Pr⁡[min⁡h(S′)=h(xn+1)∣Z=z]=Pr⁡[h(xn+1)∈[0,z]∣Z=z]=z. \begin{aligned} \Pr \big[\min h(S') = h(x_{n + 1}) \mid Z = z \big] = \Pr \big[h(x_{n + 1}) \in [0, z] \mid Z = z \big] = z. \end{aligned}

Integrating over all possible values of zz, we have

Pr⁡[min⁡h(S′)=h(xn+1)]=∫z=01Pr⁡[min⁡h(S′)=h(xn+1)∣Z=z]⋅p(z) dz=∫z=01z⋅p(z) dz=E[Z]. \begin{aligned} \Pr[\min h(S') = h(x_{n + 1})] &= \int_{z = 0}^1 \Pr \big[\min h(S') = h(x_{n + 1}) \mid Z = z \big] \cdot p(z) \ dz \\ &= \int_{z = 0}^1 z \cdot p(z) \ dz \\ &= \mathbb{E} [Z] . \end{aligned}

But, by symmetry, Pr⁡[min⁡h(S′)=h(xn+1)]=1/(N+1)\Pr[\min h(S') = h(x_{n + 1})] = 1 / \big(N + 1\big). Therefore, E[Z]=1/(N+1)\mathbb{E}[Z] = 1 / \big(N + 1\big).

□\square

Next we upper bound and lower the value of ZZ.

Variance

As we need to bound the two side error of ZZ, we can't use Markov inequality but rely on Chebyshev's inequality. In this section, we study the variance of ZZ.

Theorem. Var[Z]≤1/(N+1)2\mathbb{Var}[Z] \le 1 / \big(N + 1\big)^2.

Proof 1.

E[Z2]=∫z=01Nz2(1−z)N−1dz=∫z=01N[(z−1)2+2(z−1)+1](1−z)N−1dz=−NN+2(1−z)N+2∣01+2NN+1(1−z)N+1∣01−NN(1−z)N∣01=NN+2−2NN+1+NN=−(N−1)(N+2)+N2+N(N+2)(N+1)=2(N+2)(N+1) \begin{aligned} \mathbb{E} [ Z^2 ] &= \int_{z = 0}^1 N z^2(1 - z)^{N - 1} dz \\ &= \int_{z = 0}^1 N [(z - 1)^2 + 2(z - 1) + 1] (1 - z)^{N - 1}dz \\ &= -\frac{N}{N + 2}(1 - z)^{N + 2} \mid_{0}^1 + \frac{2N}{N + 1} (1 - z)^{N + 1} \mid_{0}^1 - \frac{N}{N} (1 - z)^{N} \mid_{0}^1 \\ &= \frac{N}{N + 2} - \frac{2N}{N + 1} + \frac{N}{N} \\ &= \frac{-(N - 1)(N + 2) + N^2 + N}{(N +2)(N + 1)} \\ &= \frac{2}{(N +2)(N + 1)} \end{aligned}

Hence, Var[Z]=2/((N+1)(N+2))−1/(N+1)2≤1/(N+1)2\mathbb{Var} [ Z ] = 2 / \big( (N + 1)(N + 2) \big) - 1 / \big(N + 1\big)^2 \le 1 / \big(N + 1\big)^2.

□\square

Proof 2.

E[Z2]=∫z=01Pr⁡[Z2≥z] dz=∫z=01Pr⁡[Z≥z] dz=∫z=01(1−z)N dz=t≐z∫t=01(1−t)N dt2=2∫t=01(1−t)N(t−1+1) dt=2∫t=01(1−t)N−(1−t)N+1 dt=2(1N+1−1N+2) \begin{aligned} \mathbb{E} [ Z^2 ] &= \int_{z = 0}^1 \Pr[ Z^2 \ge z ] \ dz \\ &= \int_{z = 0}^1 \Pr[ Z \ge \sqrt z ] \ dz \\ &= \int_{z = 0}^1 (1 - \sqrt z)^N \ dz \\ &\overset{t \doteq \sqrt z}{=} \int_{t = 0}^1 (1 - t)^N \ d t^2 \\ &= 2 \int_{t = 0}^1 (1 - t)^N (t - 1 + 1) \ d t \\ &= 2 \int_{t = 0}^1 (1 - t)^N - (1 - t)^{N + 1} \ d t \\ &= 2 \left( \frac{1}{N + 1} - \frac{1}{N + 2} \right) \end{aligned}

□\square

Question to ponder: try to apply Chebyshev's inequality directly on this single estimate. Can we get a meaningful lower bound for ZZ?

Repetition

However, this alone is not enough to give an accurate estimate. Instead, we repeat the algorithm kk times, and take the average over the ZZ's.

Theorem. Let Z1,...,ZkZ_1, ..., Z_k be kk independent copies of ZZ and define Zˉ≐(1/k)∑i∈[k]Zi\bar Z \doteq (1 / k) \sum_{i \in [k] } Z_i. Then 0<ϵ<10 < \epsilon < 1,

Pr⁡[∣Zˉ−1N+1∣≥ϵN+1]≤1k⋅ϵ2. \Pr \left[ \left| \bar Z - \frac{1}{N + 1} \right| \ge \frac{ \epsilon }{N + 1} \right] \le \frac{1}{k \cdot \epsilon^2}.

Proof. By independence of ZiZ_i's, we have

Var[Zˉ]=1k2⋅∑i∈[k]Var[Zi]≤1k(N+1)2. \mathbb{Var} [ \bar Z ] = \frac{1}{k^2} \cdot \sum_{i \in [k] } \mathbb{Var}[Z_i ] \le \frac{1}{k (N + 1)^2}.

The theorem follows directly from Chebyshev's inequality.

□\square

That is, Zˉ∈(1/(1+N))[1−ϵ,1+ϵ]\bar Z \in \big( 1 / (1 + N) \big) \big[ 1 - \epsilon, 1 + \epsilon \big] with probability 1/(kϵ2)1 / \big( k \epsilon^2 \big). It concludes that, with probability 1/(kϵ2)1 / \big( k \epsilon^2 \big),

1Zˉ∈[N+11+ϵ,N+11−ϵ]. \frac{1}{ \bar Z } \in \left[ \frac{ N + 1 }{ 1 + \epsilon }, \frac{ N + 1 }{ 1 - \epsilon } \right].

Taking Quantiles

There exists other analysis for the concentration behavior when we repeat the experiment for kk times.

We need the following inequalities:

  1. ∀x∈R\forall x \in \mathbb{R}, 1+x≤ex1 + x \le e^x;
  2. ∀x∈[0,1]\forall x \in [0, 1], ex≤1+2xe^x \le 1 + 2x.

Now, for N≥1N \ge 1,

Pr⁡[Z≥1+ϵN]=(1−1+ϵN)N≤exp⁡(−(1+ϵ))=1e⋅eϵ. \begin{aligned} \Pr \left[ Z \ge \frac{1 + \epsilon}{N} \right] = \left(1 - \frac{1 + \epsilon}{N} \right)^N \le \exp \left( - \big( 1 + \epsilon \big) \right) = \frac{1}{e \cdot e^\epsilon}. \end{aligned} Pr⁡[Z≤1−2ϵN]=1−(1−1−2ϵN)N≤1−exp⁡(−(1−ϵ))=1−eϵe. \begin{aligned} \Pr \left[ Z \le \frac{1 - 2\epsilon}{N} \right] = 1 - \left( 1 - \frac{1 - 2\epsilon}{N} \right)^N \le 1 - \exp \left( - \big( 1 - \epsilon \big) \right) = 1 - \frac{e^\epsilon}{e}. \end{aligned}

We claim that if we take the ke\frac{k}{\sqrt e}-th smallest element, with high probability,

∣X‾−1N+1∣≤ϵ1N+1. \left| \overline X - \frac{1}{N + 1} \right| \le \epsilon \frac{1}{N + 1}.

Denote z1,z2,...,zkz_1, z_2, ..., z_k a set of i.i.d Bernoulli variables with mean 1ee−ϵ/2\frac{1}{\sqrt e} e^{-\epsilon / 2}. Then by Chernoff bound,

Pr⁡[∑zi≥ke]=Pr⁡[∑zi≥(1+(eϵ/2−1))kee−ϵ/2]≤exp⁡(−(eϵ/2−1)2kee−ϵ/2/3)=exp⁡(−(eϵ−2eϵ/2+1)e−ϵ/2ke/3)=exp⁡(−(eϵ/2−2+e−ϵ/2)ke/3)≤exp⁡(−ϵ24ke/3)=δ \begin{aligned} \Pr[ \sum z_i \ge \frac{k}{\sqrt e} ] &= \Pr[ \sum z_i \ge (1 + (e^{\epsilon / 2} - 1) ) \frac{k}{\sqrt e} e^ {-\epsilon / 2}] \\ &\le \exp \left( -(e^{\epsilon / 2} - 1)^2 \frac{k}{\sqrt e} e^ {-\epsilon / 2} / 3 \right) \\ &= \exp \left( -(e ^\epsilon - 2 e^{\epsilon / 2} + 1) e^ {-\epsilon / 2} \frac{k}{\sqrt e} / 3 \right) \\ &= \exp \left( -( e^{\epsilon / 2} - 2 + e^ {-\epsilon / 2}) \frac{k}{\sqrt e} / 3 \right) \\ &\le \exp \left( - \frac{ \epsilon^2 }{4} \frac{k}{\sqrt e} / 3 \right) \\ & = \delta \end{aligned}

It suffices to set k=12eϵ2ln⁡1δk = \frac{12 \sqrt e}{\epsilon^2} \ln \frac{1}{\delta}.

By symmetry, we can prove the inequality of the other hand.

Remark 2: Suppose that we have a function hh that maps each of the NN elements to a non-negative real number according to exponential distribution exp⁡(−1)\exp(-1) independently (the same element is mapped to the same number), then the minimum of these real numbers serves also as an unbiased estimator of 1N\frac{1}{N}. To verify this,

Pr⁡[Z≤z]=1−exp⁡(−zN) \Pr[ Z \le z] = 1 - \exp(-zN)

Therefore,

p[Z=z]=∂Pr⁡[Z≤z]∂z=Nexp⁡(−Nz). p[Z = z] = \frac{\partial \Pr[ Z \le z]}{\partial z} = N \exp(-Nz).

and

E[Z]=∫0∞Nzexp⁡(−Nz)dz=−∫0∞z dexp⁡(−Nz)=−zexp⁡(−Nz)∣0∞+∫0∞exp⁡(−Nz)dz=1N. \begin{aligned} \mathbb{E}[Z] &= \int_{0}^\infty Nz \exp(-Nz) dz \\ &= -\int_{0}^\infty z \ d\exp(-Nz) \\ &= -z \exp(-Nz) \mid_{0}^\infty + \int_0^\infty \exp(-Nz) dz \\ &= \frac{1}{N}. \end{aligned}

Further,

Pr⁡[Z≥(1+ϵ)1N]=exp⁡(−1−ϵ)=1ee−ϵ,Pr⁡[Z≤(1−ϵ)1N]=1−exp⁡(−1+ϵ)=1−1eeϵ. \begin{aligned} \Pr \left[ Z \ge (1 + \epsilon) \frac{1}{N} \right] &= \exp( - 1 - \epsilon) = \frac{1}{e}e^{-\epsilon}, \\ \Pr \left[ Z \le (1 - \epsilon) \frac{1}{N} \right] &= 1 - \exp( - 1 + \epsilon) = 1 - \frac{1}{e}e^{\epsilon}. \end{aligned}

Similarly, we can show that if we repeat the experiment k=O(ln⁡1/δϵ2)k = O(\frac{\ln 1 / \delta }{\epsilon^2 } ) times, with probability at least 1−δ1 - \delta, the 1e\frac{1}{e} smallest elements is an 1±ϵ1 \pm \epsilon estimator of 1N\frac{1}{N}.

Solution 2

Suppose we only want to know whether N≥tN \ge t or N≤t/2N \le t / 2 for a given integer tt, is this possible?

The answer is yes and the solution is amazingly easy. We hash each element in U\mathcal{U} uniformly at random into the range [1,2,3,...,t][1, 2, 3, ..., t]. If N≥tN \ge t, the probability that no elements is assigned the value tt is given by

(1−1/t)N≤(1−1/t)t≤1/e (1 - 1 / t)^N \le (1 - 1 / t)^t \le 1/e

The second inequality holds since (1−1/t)≤e1/t(1 - 1 / t) \le e^{1 / t}.

On the other hand, when N≤t/2N \le t / 2, the probability that no element is assigned the value tt is greater than

(1−1/t)N≥(1−1/t)t/2≥1/e (1 - 1 / t)^N \ge (1 - 1 / t)^{t / 2} \ge 1 / e

The second inequality holds since (1−1/t)≥e−2/t=1−2t+12!(2t)2−13!(2t)3+...(1 - 1 / t) \ge e^{- 2 / t} = 1 - \frac{2}{t} + \frac{1}{2!}(\frac{2}{t})^2 - \frac{1}{3!}(\frac{2}{t})^3 + ... when 1/t≤1/21 / t \le 1/ 2.

We can boost the probability to some defined threshold 1−δ1 - \delta by repeating k=log⁡1/δk = \log 1 / \delta times and check whether the majority of the answers are "yes". Note that the expected number of "yes" is more than (1−1/e)k(1 - 1 / e)k and by chernoff bound the probability that less than 0.5k0.5 k "yes" are returned is given by δ\delta.

Now, to estimate NN, we can set up log⁡n\log n values of t:1,2,4,8,...,nt: 1, 2, 4, 8, ..., n and test for each value of tt whether N≥tN \ge t or N≤t/2N \le t / 2. We can find an interval [t1,t2][t_1, t_2], such that N∈[t1,t2]N \in [t_1, t_2] and N/2≤t1≤t2≤2⋅NN / 2 \le t_1 \le t_2 \le 2\cdot N. The space complexity is O(log⁡nlog⁡1/δ)O(\log n \log 1 / \delta).

Reference

[1] M. Charikar, T. Li, and A. Xue, “Lecture 1: Counting Distinct Elements,”, CS369G: Algorithmic Techniques for Big Data.