HyperLogLog

{{Short description|Approximate distinct counting algorithm}}

{{Probabilistic}}

HyperLogLog is an algorithm for the count-distinct problem, approximating the number of distinct elements in a multiset.{{cite journal |citeseerx=10.1.1.76.4286 |first1=Philippe |last1=Flajolet |first2=Éric |last2=Fusy |first3=Olivier |last3=Gandouet |first4=Frédéric |last4=Meunier |title=Hyperloglog: The analysis of a near-optimal cardinality estimation algorithm |url=http://algo.inria.fr/flajolet/Publications/FlFuGaMe07.pdf |access-date=2016-12-11 |year=2007 |volume=AH

|pages=137–156 |journal=Discrete Mathematics and Theoretical Computer Science Proceedings |location=Nancy, France}} Calculating the exact cardinality of the distinct elements of a multiset requires an amount of memory proportional to the cardinality, which is impractical for very large data sets. Probabilistic cardinality estimators, such as the HyperLogLog algorithm, use significantly less memory than this, but can only approximate the cardinality. The HyperLogLog algorithm is able to estimate cardinalities of > 109 with a typical accuracy (standard error) of 2%, using 1.5 kB of memory. HyperLogLog is an extension of the earlier LogLog algorithm,{{cite conference |url=http://algo.inria.fr/flajolet/Publications/DuFl03-LNCS.pdf |title=LogLog counting of large cardinalities. |last1=Durand |first1=M. |last2=Flajolet |first2=P. |date=2003 |publisher=Springer |editor=G. Di Battista and U. Zwick |conference = Annual European Symposium on Algorithms (ESA03)|book-title=Lecture Notes in Computer Science |volume=2832 |pages=605–617}} itself deriving from the 1984 Flajolet–Martin algorithm.{{Cite journal |doi=10.1016/0022-0000(85)90041-8 |title=Probabilistic counting algorithms for data base applications |journal=Journal of Computer and System Sciences |volume=31 |issue=2 |pages=182–209 |year=1985 |last1=Flajolet |first1=Philippe |last2=Martin |first2=G. Nigel |url=http://algo.inria.fr/flajolet/Publications/FlMa85.pdf}}

Terminology

In the original paper by Flajolet et al. and in related literature on the count-distinct problem, the term "cardinality" is used to mean the number of distinct elements in a data stream with repeated elements. However in the theory of multisets the term refers to the sum of multiplicities of each member of a multiset. This article chooses to use Flajolet's definition for consistency with the sources.

Algorithm

{{more footnotes|date=March 2014|section}}

The basis of the HyperLogLog algorithm is the observation that the cardinality of a multiset of uniformly distributed random numbers can be estimated by calculating the maximum number of leading zeros in the binary representation of each number in the set. If the maximum number of leading zeros observed is n, an estimate for the number of distinct elements in the set is 2n.

In the HyperLogLog algorithm, a hash function is applied to each element in the original multiset to obtain a multiset of uniformly distributed random numbers with the same cardinality as the original multiset. The cardinality of this randomly distributed set can then be estimated using the algorithm above.

The simple estimate of cardinality obtained using the algorithm above has the disadvantage of a large variance. In the HyperLogLog algorithm, the variance is minimised by splitting the multiset into numerous subsets, calculating the maximum number of leading zeros in the numbers in each of these subsets, and using a harmonic mean to combine these estimates for each subset into an estimate of the cardinality of the whole set.{{Cite web |url=https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/40671.pdf |title=HyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm |author=S Heule |author2=M Nunkesser |author3=A Hall |year=2013 |at=sec 4}}

Operations

The HyperLogLog has three main operations: add to add a new element to the set, count to obtain the cardinality of the set and merge to obtain the union of two sets. Some derived operations can be computed using the inclusion–exclusion principle like the cardinality of the intersection or the cardinality of the difference between two HyperLogLogs combining the merge and count operations.

The data of the HyperLogLog is stored in an array {{mvar|M}} of {{mvar|m}} counters (or "registers") that are initialized to 0. Array {{mvar|M}} initialized from a multiset {{mvar|S}} is called HyperLogLog sketch of S.

=Add=

The add operation consists of computing the hash of the input data {{mvar|v}} with a hash function {{mvar|h}}, getting the first {{mvar|b}} bits (where {{mvar|b}} is \log_2(m)), and adding 1 to them to obtain the address of the register to modify. With the remaining bits compute \rho(w) which returns the position of the leftmost 1, where leftmost position is 1 (in other words: number of leading zeros plus 1). The new value of the register will be the maximum between the current value of the register and \rho(w).

:

\begin{align}

x &:= h(v) \\

j &:= 1 +\langle x_{1}x_{2}...x_{b}\rangle_2 \\

w &:= x_{b+1}x_{b+2}... \\

M[j] &:= \max(M[j], \rho(w)) \\

\end{align}

=Count=

The count algorithm consists in computing the harmonic mean of the {{mvar|m}} registers, and using a constant to derive an estimate E of the count:

:

Z = \Bigg(\sum_{j=1}^{m}{2^{-M[j]}}\Bigg)^{-1}

:

\alpha_{m} = \left(m\int_{0}^{\infty}\left(\log_{2}\left(\frac{2+u}{1+u}\right)\right)^{m}\,du\right)^{-1}

:

E = \alpha_m m^2 Z

The intuition is that {{mvar|n}} being the unknown cardinality of {{mvar|M}}, each subset M_j will have n/m elements. Then

\max_{x\in M_j} \rho(x) should be close to \log_2(n/m). The harmonic mean of 2 to these quantities is mZ which should be near n/m. Thus, m^2 Z should be {{mvar|n}} approximately.

Finally, the constant \alpha_m is introduced to correct a systematic multiplicative bias present in m^2 Z due to hash collisions.

=Practical considerations=

The constant \alpha_m is not simple to calculate, and can be approximated with the formula

:

\alpha_{m}\approx\begin{cases}

0.673, & \text{for } m=16; \\

0.697, & \text{for } m=32; \\

0.709, & \text{for } m=64; \\

\frac{0.7213}{1+1.079/m}, & \text{for } m\ge128.

\end{cases}

The HyperLogLog technique, though, is biased for small cardinalities below a threshold of \frac{5}{2}m. The original paper proposes using a different algorithm for small cardinalities known as Linear Counting.{{Cite journal |title=A linear-time probabilistic counting algorithm for database applications |journal=ACM Transactions on Database Systems |volume=15 |number=2 |pages=208–229 |year=1990 |last1=Whang |first1= Kyu-Young |last2= Vander-Zanden |first2=Brad T|last3=Taylor |first3=Howard M |doi=10.1145/78922.78925 |s2cid=2939101 |doi-access=free }} In the case where the estimate provided above is less than the threshold E < \frac{5}{2}m, the alternative calculation can be used:

  1. Let V be the count of registers equal to 0.
  2. If V = 0, use the standard HyperLogLog estimator E above.
  3. Otherwise, use Linear Counting: E^{\star} = m\log\left(\frac{m}{V}\right)

Additionally, for very large cardinalities approaching the limit of the size of the registers (E > \frac{2^{32}}{30} for 32-bit registers), the cardinality can be estimated with:

:

E^{\star}=-2^{32}\log\left(1-\frac{E}{2^{32}}\right)

With the above corrections for lower and upper bounds, the error can be estimated as \sigma=1.04/\sqrt{m}.

=Merge=

The merge operation for two HLLs (\mathit{hll}_1, \mathit{hll}_2) consists in obtaining the maximum for each pair of registers j: 1..m

:

\mathit{hll}_\text{union}[j] = \max(\mathit{hll}_1[j], \mathit{hll}_2[j])

Complexity

To analyze the complexity, the data streaming (\epsilon,\delta) model{{cite web|url=http://research.google.com/pubs/pub40671.html|title=HyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm|access-date=2014-04-19}} is used, which analyzes the space necessary to get a 1\pm \epsilon approximation with a fixed success probability 1-\delta. The relative error of HLL is 1.04/\sqrt{m} and it needs O(\epsilon^{-2} \log\log n + \log n) space, where {{mvar|n}} is the set cardinality and {{mvar|m}} is the number of registers (usually less than one byte size).

The add operation depends on the size of the output of the hash function. As this size is fixed, we can consider the running time for the add operation to be O(1).

The count and merge operations depend on the number of registers {{mvar|m}} and have a theoretical cost of O(m). In some implementations (Redis){{Cite web | url=https://redis.io/commands/pfcount | title=PFCOUNT – Redis}} the number of registers is fixed and the cost is considered to be O(1) in the documentation.

HLL++

The HyperLogLog++ algorithm proposes several improvements in the HyperLogLog algorithm to reduce memory requirements and increase accuracy in some ranges of cardinalities:

  • 64-bit hash function is used instead of the 32 bits used in the original paper. This reduces the hash collisions for large cardinalities allowing to remove the large range correction.
  • Some bias is found for small cardinalities when switching from linear counting to the HLL counting. An empirical bias correction is proposed to mitigate the problem.
  • A sparse representation of the registers is proposed to reduce memory requirements for small cardinalities, which can be later transformed to a dense representation if the cardinality grows.

Streaming HLL

When the data arrives in a single stream, the Historic Inverse Probability or martingale estimator{{Cite journal|last=Cohen|first=E.|date=March 2015|title=All-distances sketches, revisited: HIP estimators for massive graphs analysis|journal=IEEE Transactions on Knowledge and Data Engineering|volume=27|issue=9|pages=2320–2334|doi=10.1109/TKDE.2015.2411606|arxiv=1306.3284}}

{{Cite book|last=Ting|first=D.|title=Proceedings of the 20th ACM SIGKDD international conference on Knowledge discovery and data mining |chapter=Streamed approximate counting of distinct elements |date=August 2014|chapter-url=https://research.fb.com/publications/streamed-approximate-counting-of-distinct-elements/|pages=442–451|doi=10.1145/2623330.2623669|isbn=978-1-4503-2956-9|s2cid=13179875 }}

significantly improves the accuracy of the HLL sketch and uses 36% less memory to achieve a given error level. This estimator is provably optimal for any duplicate insensitive approximate distinct counting sketch on a single stream.

The single stream scenario also leads to variants in the HLL sketch construction.

HLL-TailCut+ uses 45% less memory than the original HLL sketch but at the cost of being dependent on the data insertion order and not being able to merge sketches.{{Cite book|last1=Xiao|first1=Q.|last2=Zhou|first2=Y.|last3=Chen|first3=S.|title=IEEE INFOCOM 2017 - IEEE Conference on Computer Communications |chapter=Better with fewer bits: Improving the performance of cardinality estimation of large data streams |date=May 2017|pages=1–9|doi=10.1109/INFOCOM.2017.8057088|isbn=978-1-5090-5336-0|s2cid=27159273 }}

Further reading

  • {{cite web|url=https://oertl.github.io/hyperloglog-sketch-estimation-paper/paper/paper.pdf|title=New cardinality estimation algorithms for HyperLogLog sketches|access-date=2016-10-29}}

References