<p>For sampling-based algorithms, nearly tight upper and lower bounds are known for the query complexity of estimating the number of distinct elements in a sequence. </p>

<p><a href="http://dl.acm.org/citation.cfm?id=335230" rel="nofollow">Charikar, Chowdhuri, Motwani and Narsayya</a> showed that multiplicative approximation of the number of distinct elements to within a factor $\alpha$ can be achieved with $q= O(n/\alpha^2)$ queries into the sequence. </p>

<p>Here's the algorithm. Sample $q$ items uniformly at random from the sequence. Let $d$ be the number of distinct items in the sample, and let $f_1$ be the number of items that appear exactly once in the sample. Then, output $d + \sqrt{n/q} \cdot f_1$. </p>

<p>The analysis is also very clean. Assume without loss of generality that each element in the sequence is an integer in $[k]$, where $k$ is the number of distinct elements. Let $p_i$ be the probability of selecting $i$ when sampling uniformly from the sequence. Then, the expected output of the estimator is:</p>

<p>$$\mathbb{E}[d + \sqrt{n/q}\cdot f_1] =  \sum_{i=1}^k (1 - (1-p_i)^q) + \sqrt{n/q} \cdot p_i q (1-p_i)^{q-1}$$ </p>

<p>Now, the point is that for any valid $p_i$, the value inside the summation is between $\Omega(\sqrt{q/n})$ and $O(\sqrt{n/q})$. (This uses the fact that $1/n \leq p_i &lt; 1$.) So, the expected output of the algorithm is within $O(\sqrt{n/q})$ of $k$.</p>

<p>Charikar et al.'s estimator can also be used to get an <i>additive</i> approximation of the number of distinct elements. One can estimate the number of distinct elements upto $\pm \beta n$ by making $O((1-2\beta) n)$ samples. This was the state-of-the-art for a long while until STOC '11 when Greg and Paul Valiant <a href="http://www.cs.berkeley.edu/~gvaliant/papers/VV_stoc11.pdf" rel="nofollow">showed</a> that it's possible to reduce the sample complexity and time complexity to $O(\frac{n}{\beta^2 \log n})$. Their algorithm is based on a very general result that, given a probability distribution $D$, constructs a distribution $D'$ using $O(n/\log n)$ samples such that $D$ and $D'$ are close to each other in relative earthmover distance. So, one can make sure that the support size of $D'$ and the support size of $D$ are only an additive $\beta n$ away from each other.</p>

<p>As for lower bounds, Charikar et al. showed that for $\alpha$-multiplicative approximation, $\Omega(n/\alpha^2)$ queries are needed. The proof stems from the observation that $\Omega(n/\alpha^2)$ queries are needed to distinguish $n$ identical items from the same sequence with $\alpha^2$ unique elements inserted in random locations. For additive approximation, however, this bound is sort of trivial; for constant $\beta$, this would only give a constant lower bound on the query complexity. <a href="http://www.cse.psu.edu/~sofya/pubs/dss-sicomp.pdf" rel="nofollow">Raskhodnikova, Ron, Shpilka, and Smith</a> showed a nearly linear bound even for additive approximation. Specifically, they prove that estimating to within additive error $n/23$ requires $n/2^{\sqrt{\log n} \log \log n}$ queries. The Valiants in their above-cited paper showed a lower bound of $\Omega(n/\log n)$ for estimating to any additive error less than $n/4$, implying that their upper-bound is tight upto constant factors.</p>

CC BY-SA 3.0
2013-02-22T08:51:32.870Z
Thanks very much. I suppose if you are worried about worst case input distributions and guaranteed bounds that is what you get. What about MLEs or biased estimators or if you have some useful prior?
Simd
CC BY-SA 3.0
2013-02-22T21:14:01.703Z
You should take a look at the paper by Charikar et al. There, they discuss other heuristic estimators that do well in practice and that adapt to the input distribution. There is also this paper by [Motwani and Vassilvitskii](http://theory.stanford.edu/~sergei/papers/distinct-analco.pdf) that gives an estimator for the case when the data is drawn from a power law distribution. In this case, their sample complexity grows only with the number of distinct elements and not with $n$ !
arnab
CC BY-SA 3.0
2013-02-22T01:15:01.843Z
2013-02-24T04:25:37.873Z
2013-02-24T04:25:37.873Z
arnab
<p>What is the current state of the art for $L_0$ estimation when not in a stream?  I assume that the total number of (not necessarily distinct) objects is very large so you can only see a sample of them.</p>

<p>I found <a href="http://en.wikipedia.org/wiki/Good%E2%80%93Turing_frequency_estimation" rel="nofollow">http://en.wikipedia.org/wiki/Good%E2%80%93Turing_frequency_estimation</a> but that seems rather older than I was expecting.</p>

CC BY-SA 3.0
2013-02-21T20:06:23.293Z
Good-Turing estimation does more though: it estimates the frequencies of items. It sounds like you only want the number of distinct items.
Suresh Venkat
CC BY-SA 3.0
2013-02-21T23:06:12.217Z
@SureshVenkat Yes you are right.
Simd
<reference-request>
$L_0$ estimation, not in a stream
Simd
# $L_0$ estimation, not in a stream

(Asked by: username_0 on 2013-02-21)

What is the current state of the art for $L\_0$ estimation when not in a stream? I assume that the total number of (not necessarily distinct) objects is very large so you can only see a sample of them.

I found <http://en.wikipedia.org/wiki/Good%E2%80%93Turing_frequency_estimation> but that seems rather older than I was expecting.

**Tags:** reference-request
> **Comments:**
> **username_1**: Good-Turing estimation does more though: it estimates the frequencies of items. It sounds like you only want the number of distinct items.  
> **username_0**: @SureshVenkat Yes you are right.  

---

## 1 answers

### Answer by username_2 on 2013-02-22. Score: 7

For sampling-based algorithms, nearly tight upper and lower bounds are known for the query complexity of estimating the number of distinct elements in a sequence.

[Charikar, Chowdhuri, Motwani and Narsayya](http://dl.acm.org/citation.cfm?id=335230) showed that multiplicative approximation of the number of distinct elements to within a factor $\alpha$ can be achieved with $q= O(n/\alpha^2)$ queries into the sequence.

Here's the algorithm. Sample $q$ items uniformly at random from the sequence. Let $d$ be the number of distinct items in the sample, and let $f\_1$ be the number of items that appear exactly once in the sample. Then, output $d + \sqrt{n/q} \cdot f\_1$.

The analysis is also very clean. Assume without loss of generality that each element in the sequence is an integer in $[k]$, where $k$ is the number of distinct elements. Let $p\_i$ be the probability of selecting $i$ when sampling uniformly from the sequence. Then, the expected output of the estimator is:

$$\mathbb{E}[d + \sqrt{n/q}\cdot f\_1] = \sum\_{i=1}^k (1 - (1-p\_i)^q) + \sqrt{n/q} \cdot p\_i q (1-p\_i)^{q-1}$$

Now, the point is that for any valid $p\_i$, the value inside the summation is between $\Omega(\sqrt{q/n})$ and $O(\sqrt{n/q})$. (This uses the fact that $1/n \leq p\_i < 1$.) So, the expected output of the algorithm is within $O(\sqrt{n/q})$ of $k$.

Charikar et al.'s estimator can also be used to get an *additive* approximation of the number of distinct elements. One can estimate the number of distinct elements upto $\pm \beta n$ by making $O((1-2\beta) n)$ samples. This was the state-of-the-art for a long while until STOC '11 when Greg and Paul Valiant [showed](http://www.cs.berkeley.edu/~gvaliant/papers/VV_stoc11.pdf) that it's possible to reduce the sample complexity and time complexity to $O(\frac{n}{\beta^2 \log n})$. Their algorithm is based on a very general result that, given a probability distribution $D$, constructs a distribution $D'$ using $O(n/\log n)$ samples such that $D$ and $D'$ are close to each other in relative earthmover distance. So, one can make sure that the support size of $D'$ and the support size of $D$ are only an additive $\beta n$ away from each other.

As for lower bounds, Charikar et al. showed that for $\alpha$-multiplicative approximation, $\Omega(n/\alpha^2)$ queries are needed. The proof stems from the observation that $\Omega(n/\alpha^2)$ queries are needed to distinguish $n$ identical items from the same sequence with $\alpha^2$ unique elements inserted in random locations. For additive approximation, however, this bound is sort of trivial; for constant $\beta$, this would only give a constant lower bound on the query complexity. [Raskhodnikova, Ron, Shpilka, and Smith](http://www.cse.psu.edu/~sofya/pubs/dss-sicomp.pdf) showed a nearly linear bound even for additive approximation. Specifically, they prove that estimating to within additive error $n/23$ requires $n/2^{\sqrt{\log n} \log \log n}$ queries. The Valiants in their above-cited paper showed a lower bound of $\Omega(n/\log n)$ for estimating to any additive error less than $n/4$, implying that their upper-bound is tight upto constant factors.

> **Comments:**
> **username_0**: Thanks very much. I suppose if you are worried about worst case input distributions and guaranteed bounds that is what you get. What about MLEs or biased estimators or if you have some useful prior?  
> **username_2**: You should take a look at the paper by Charikar et al. There, they discuss other heuristic estimators that do well in practice and that adapt to the input distribution. There is also this paper by [Motwani and Vassilvitskii](http://theory.stanford.edu/~sergei/papers/distinct-analco.pdf) that gives an estimator for the case when the data is drawn from a power law distribution. In this case, their sample complexity grows only with the number of distinct elements and not with $n$ !  

---

