Simple set cardinality estimation through random sampling

Marco Bressan, Enoch Peserico, Luca PrettoarXiv 2015

Introduction

Estimating the size of massive sets—such as the number of nodes in a large social network or a web graph—is a common problem where counting every single element is computationally prohibitive. While the basic statistical approach, often called the "capture-recapture" method, has been known since the 1930s, obtaining tight, rigorous mathematical guarantees on the number of samples needed for a specific precision ($\epsilon$) and error probability ($\delta$) remains challenging.

This paper presents a simple, mathematically rigorous algorithm for estimating set cardinality using uniform random sampling. By applying modern martingale tail inequalities to a classic estimator, the authors prove that their method achieves high accuracy using only $O(\sqrt{n})$ samples, providing a reliable "black box" tool for algorithm designers.

The CARDAPPROX Algorithm

The authors introduce a short algorithm called CARDAPPROX. The procedure is straightforward: it repeatedly draws uniform, independent random samples from the target set $V$ and tracks unique items and collisions (repeats).

The CARDAPPROX algorithm pseudocode.

As shown in the pseudocode, the procedure follows these steps:

  1. Initialize the set of seen items $S$, a weighted sum $w$, and a repeat counter $r$.
  2. Calculate a stopping threshold $k$ based on the desired precision $\epsilon$ and error rate $\delta$.
  3. Repeatedly sample from $V$. For each sample $e$:
  4. Update the weighted sum $w$ by adding the current size of $S$.
  5. If $e$ has been seen before ($e \in S$), increment the repeat counter $r$.
  6. Add $e$ to the set of seen items $S$.
  7. Stop as soon as the number of repeats $r$ reaches the threshold $k$.
  8. Return the estimate $\hat{n} = w/r$.

Mathematical Innovation

The algorithm's novelty lies in its mathematical analysis. To prove the algorithm works within strict bounds, the authors model the sampling process as a martingale—a sequence of random variables where the expectation of the next value, given all prior observations, is equal to the current value. They then apply a powerful, modern martingale tail inequality (originally from Freedman, 1975) to tightly bound the probability of the estimate straying too far from the true value.

Key Insights

  • The Birthday Paradox in Action: You do not need to see a large fraction of a set to guess its size accurately. Because of the mathematics behind the Birthday Paradox, collisions (repeats) start happening frequently after roughly $\sqrt{n}$ samples. This algorithm formalizes that intuition into a precise stopping rule.
  • Data-Driven Stopping: The algorithm doesn't need to know the true size $n$ in advance to know when to stop sampling. It stops based entirely on the observed number of collisions, making it highly practical for sets of completely unknown sizes.
  • Bridging Old and New: The paper demonstrates how classic statistical estimators can be upgraded with modern theoretical computer science tools to yield strict, non-asymptotic guarantees.

Key Results

  • Accuracy Guarantee: The algorithm guarantees an estimate $\hat{n}$ that is within a $(1 \pm \epsilon)$ factor of the true size $n$, with a failure probability of at most $\delta$.
  • Sub-linear Sample Complexity: To achieve this accuracy, the algorithm requires at most $O(\sqrt{n} \cdot \epsilon^{-1} \sqrt{\log(\delta^{-1})})$ samples. This is vastly smaller than the total size of the set $n$.
  • Strict Upper Bound: The authors prove that the maximum number of times the algorithm will ever need to draw a sample is strictly bounded by $\min(n, 2\lceil\sqrt{kn}\rceil) + k$, where $k \approx \frac{2}{\epsilon^2} \ln(\frac{3}{\delta})$.

Limitations

An inherent limitation of the algorithm is the strict assumption that elements can be sampled uniformly and independently at random. In real-world applications, such as estimating the size of a social network via API crawling, obtaining truly uniform independent samples is notoriously difficult, as random walks tend to be biased toward highly connected nodes.

Summary updated

Abstract

We present a simple algorithm that estimates the cardinality $n$ of a set $V$ when allowed to sample elements of $V$ uniformly and independently at random. Our algorithm with probability $(1-\delta)$ returns a $(1\pm\epsilon)-$approximation of $n$ drawing $O\big(\sqrt{n} \cdot \epsilon^{-1}\sqrt{\log(\delta^{-1})}\big)$ samples (for $\epsilon^{-1}\sqrt{\log(\delta^{-1})} = O(\sqrt{n})$).
BibTeX
@misc{bressan2015simple,
  author = {Marco Bressan and Enoch Peserico and Luca Pretto},
  title = {{Simple set cardinality estimation through random sampling}},
  year = {2015},
  eprint = {1512.07901},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1512.07901},
}
Figure