Explaining HyperLogLog estimation
HyperLogLog estimates cardinality by hashing each element to a bitstring and counting the number of leading zeros (the position of the first 1). The maximum leading zero count over all hashes approximates log₂ of the cardinality, but this single maximum is noisy. To reduce variance, it partitions hashes into many buckets using leading bits of the hash, tracks the maximum leading zeros per bucket, and combines these using a harmonic mean scaled by a constant αₘ. This "stochastic averaging" treats each bucket like an independent estimator, stabilizing the result. The final estimate is E = αₘ m² / Σ2^(–M[i]), where M[i] are the max zeros per bucket, and αₘ ≈ 0.7213/(1+1.079/m). This approach yields a memory-efficient, variance-reduced cardinality estimate with known error around 1.04/√m.
The family of FM/LogLog/HyperLogLog algorithms all exploit the same simple observation about uniform hash values: if you look at an infinite stream of fair coin‑flips, the event “the first 1 appears at position (k)” has probability (\Pr{ \rho = k} = 2^{-k}). Equivalently, for a uniformly distributed (b)‑bit hash (h(x)), the number of leading zeroes before the first 1 is a geometric random variable with mean on the order of (\log_2 n) when you have seen (n) independent hashes.
In the most basic Flajolet–Martin scheme you hash each distinct element, compute (\rho(h)=) “position of the leftmost 1‑bit”, and remember the maximum (\max \rho) seen. The probability that none of (n) hashes has (\rho) greater than (r) is ((1-2^{-r})^n), so the expected maximum (\max \rho) grows like (\log_2 n). A crude cardinality estimate is therefore
[
\hat{n} \approx 2^{\max \rho}.
]
This works, but it has very high variance: you’re basing your entire estimate on a single extreme order statistic.
HyperLogLog reduces that variance by running many such “max of leading zeros” estimators in parallel on independent substreams and then averaging them. In practice you don’t need multiple hash functions; you use a single (b)‑bit hash and split its bits into two fields:
- The first (p) bits select one of (m = 2^p) registers (M[0\ldots m-1]). This is the “bucket index”.
- The remaining (b-p) bits are treated as a uniform stream for that bucket, and you compute (\rho), the number of leading zeros in that suffix plus one.
For each element (x) you compute (h(x)), derive its bucket index (j) and its (\rho), and update the register with the maximum seen so far:
[
M[j] \leftarrow \max(M[j], \rho).
]
At the end you have (m) independent maxima, each based on roughly (n/m) elements. HyperLogLog combines them using a form of stochastic averaging: it computes the harmonic mean of (2^{-M[j]}) and inverts it. The canonical estimator is
[
E = \alpha_m, m^2 \left( \sum_{j=0}^{m-1} 2^{-M[j]} \right)^{-1}
]
where (\alpha_m) is a bias‑correction constant (for large (m), (\alpha_m \approx 0.7213/(1+1.079/m))). For small (E) a “linear counting” correction is applied, and for very large (E) a “saturation” correction is used, but the core idea is the harmonic mean.
The effect of this stochastic averaging is to drive down the variance: instead of relying on a single (\max \rho) with a heavy‑tailed distribution, you average over (m) such statistics. The standard error of HyperLogLog is about (1.04/\sqrt{m}), so by choosing the number of registers (m) you trade a modest amount of memory for a predictable relative error.