SlideShare a Scribd company logo
tech talk @ ferret
Andrii Gakhov
PROBABILISTIC DATA STRUCTURES
ALL YOU WANTED TO KNOW BUT WERE AFRAID TO ASK
PART 2: CARDINALITY
CARDINALITY
Agenda:
▸ Linear Counting
▸ LogLog, SuperLogLog, HyperLogLog, HyperLogLog++
• To determine the number of distinct elements, also
called the cardinality, of a large set of elements
where duplicates are present
Calculating the exact cardinality of a multiset requires an amount of memory
proportional to the cardinality, which is impractical for very large data sets.
THE PROBLEM
LINEAR COUNTING
LINEAR COUNTING: ALGORITHM
• Linear counter is a bit map (hash table) of size m (all
elements set to 0 at the beginning).
• Algorithm consists of a few steps:
• for every element calculate hash function and set the
appropriate bit to 1
• calculate the fraction V of empty bits in the structure 

(divide the number of empty bits by the bit map size m )
• estimate cardinality as n ≈ -m ln V
LINEAR COUNTING: EXAMPLE
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
• Consider linear counter with 16 bits (m=16)
• Consider MurmurHash3 as the hash function h

(to calculate the appropriate index, we divide result by mod 16)
• Set of 10 elements: “bernau”, “bernau”, “bernau”,
“berlin”, “kiev”, “kiev”, “new york”, “germany”, “ukraine”,
“europe” (NOTE: the real cardinality n = 7)
h(“bernau”) = 4, h(“berlin”) = 4, h(“kiev”) = 6, h(“new york”) = 6,
h(“germany”) = 14, h(“ukraine”) = 7, h(“europe”) = 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 1 0 1 1 0 1 0 0 0 0 1 0
LINEAR COUNTING: EXAMPLE
number of empty bits: 11
m = 16
V = 11 / 16 = 0.6875
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 1 0 1 1 0 1 0 0 0 0 1 0
• Cardinality estimation is
n ≈ - 16 * ln (0.6875) = 5.995
LINEAR COUNTING: READ MORE
• http://dblab.kaist.ac.kr/Prof/pdf/Whang1990(linear).pdf
• http://www.codeproject.com/Articles/569718/
CardinalityplusEstimationplusinplusLinearplusTimep
HYPERLOGLOG
HYPERLOGLOG: INTUITION
• The cardinality of a multiset of uniformly distributed numbers can be estimated
by the maximum number of leading zeros in the binary representation of each
number. If such value is k, then the number of distinct elements in the set is 2k
P(rank=1) = 1/2 - probability to find a binary representation, that starts with 1
P(rank = 2) = 1/2
2
- probability to find a binary representation, that start with 01
…
P(rank=k) = 1/2
k
rank = number of leading zeros + 1, e.g. rank(f) = 3
0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0
0 1 2
leading zeros
3
f =
• Therefore, for 2k
binary representations we shell find at least one
representation with rank = k
• If we remember the maximal rank we’ve seen and it’s equal to k, then we can
use 2k
as the approximation of the number of elements
HYPERLOGLOG
• proposed by Flajolet et. al., 2007
• an extension of the Flajolet–Martin algorithm (1985)
• HyperLogLog is described by 2 parameters:
• p – number of bits that determine a bucket to use averaging

(m = 2p
is the number of buckets/substreams)
• h - hash function, that produces uniform hash values
• The HyperLogLog algorithm is able to estimate cardinalities of
> 109
with a typical error rate of 2%, using 1.5kB of memory
(Flajolet, P. et al., 2007).
HYPERLOGLOG: ALGORITHM
• HyperLogLog uses randomization to approximate the
cardinality of a multiset.This randomization is achieved by
using hash function h
• Observe the maximum number of leading zeros that for all
hash values:
• If the bit pattern 0L−1 1 is observed at the beginning of a
hash value (so, rank = L), then a good estimation of the size
of the multiset is 2L.
HYPERLOGLOG: ALGORITHM
• Stochastic averaging is used to reduce the large variability:
• The input stream of data elements S is divided into m substreams Si using
the first p bits of the hash values (m = 2p)
.
• In each substream, the rank (after the initial p bits that are used for
substreaming) is measured independently.
• These numbers are kept in an array of registers M, where M[i] stores the
maximum rank it seen for the substream with index i.
• The cardinality estimation is calculated computes as the normalized bias
corrected harmonic mean of the estimations on the substreams
DVHLL = const(m)⋅m2
⋅ 2
−M j
j=1
m
∑
⎛
⎝⎜
⎞
⎠⎟
−1
HYPERLOGLOG: EXAMPLE
• Consider L=8 bits hash function h
• Index elements “berlin” and “ferret”:
h(“berlin”) = 0110111 h(“ferret”) = 1100011
• Define buckets and calculate values to store:

(use first p =3 bits for buckets and least L - p = 5 bits for ranks)
• bucket(“berlin”) = 011 = 3 value(“berlin”) = rank(0111) = 2
• bucket(“ferret”) = 110 = 6 value(“ferret”) = rank(0011) = 3
• Let’s use p=3 bits to define a bucket (then m=23
=8 buckets).
1 2 3 4 5 6 7
0 0 0 0 0 0 0 0
0
M
1 2 3 4 5 6 7
0 0 0 2 0 0 3 0
0
M
HYPERLOGLOG: EXAMPLE
• Estimate the cardinality be the HLL formula (C ≈ 0.66):
DVHLL ≈ 0.66 * 82
/ (2-2
+ 2-4
) = 0.66 * 204.8 ≈ 135≠3
• Index element “kharkov”:
• h(“kharkov”) = 1100001
• bucket(“kharkov”) = 110 = 6 value(“kharkov”) = rank(0001) = 4
• M[6] = max(M[6], 4) = max(3, 4) = 4
1 2 3 4 5 6 7
0 0 0 2 0 0 4 0
0
M
NOTE: For small cardinalities HLL has a strong bias!!!
HYPERLOGLOG: PROPERTIES
• Memory requirement doesn't grow linearly with L (unlike MinCount or
Linear Counting) - for hash function of L bits and precision p, required
memory:
• original HyperLogLog uses 32 bit hash codes, which requires 5 · 2
p
bits
• It’s not necessary to calculate the full hash code for the element
• first p bits and number of leading zeros of the remaining bits are
enough
• There are no evidence that some of popular hash functions (MD5, Sha1,
Sha256, Murmur3) performs significantly better than others.
log2 L +1− p( )⎡⎢ ⎤⎥⋅2p
bits
HYPERLOGLOG: PROPERTIES
• The standard error can be estimated as:
σ =
1.04
2p
so, if we use 16 bits (p=16) for bucket indices, we receive the
standard error in 0.40625%
• Algorithm has large error for small cardinalities.
• For instance, for n = 0 the algorithm always returns roughly 0.7m
• To achieve better estimates for small cardinalities, use
LinearCounting below a threshold of 5m/2
HYPERLOGLOG: APPLICATIONS
• PFCOUNT in Redis returns the approximated cardinality
computed by the HyperLogLog data structure 

(http://antirez.com/news/75)
• Redis implementation uses 12Kb per key to count with a standard
error of 0.81%, and there is no limit to the number of items you can
count, unless you approach 264 items
HYPERLOGLOG: READ MORE
• http://algo.inria.fr/flajolet/Publications/DuFl03-LNCS.pdf
• http://algo.inria.fr/flajolet/Publications/FlFuGaMe07.pdf
• https://stefanheule.com/papers/edbt13-hyperloglog.pdf
• https://highlyscalable.wordpress.com/2012/05/01/
probabilistic-structures-web-analytics-data-mining/
• https://hal.archives-ouvertes.fr/file/index/docid/465313/
filename/sliding_HyperLogLog.pdf
• http://stackoverflow.com/questions/12327004/how-does-
the-hyperloglog-algorithm-work
HYPERLOGLOG++
HYPERLOGLOG++
• proposed by Stefan Heule et. al., 2013 for Google PowerDrill
• an improved version of HyperLogLog (Flajolet et. al., 2007)
• HyperLogLog++ is described by 2 parameters:
• p – number of bits that determine a bucket to use averaging

(m = 2p
is the number of buckets/substreams)
• h - hash function, that produces uniform hash values
• The HyperLogLog++ algorithm is able to estimate cardinalities
of ~ 7.9 · 10
9
with a typical error rate of 1.625%, using 2.56KB of
memory (Micha Gorelick and Ian Ozsvald, High Performance
Python, 2014).
HYPERLOGLOG++: IMPROVEMENTS
• use 64-bit hash function
• algorithm that only uses the hash code of the input values is limited by the
number of bits of the hash codes when it comes to accurately estimating
large cardinalities
• In particular, a hash function of L bits can at most distinguish 2L
different
values, and as the cardinality n approaches 2L
hash collisions become
more and more likely and accurate estimation gets impossible
• if the cardinality approaches 264 ≈ 1.8 · 1019, hash collisions become a problem
• bias correction
• original algorithm overestimates the real cardinality for small sets, but
most of the error is due to bias.
• storage efficiency
• uses different encoding strategies for hash values, variable length
encoding for integers, difference encoding
HYPERLOGLOG++ VS HYPERLOGLOG
• accuracy is significantly better for large range of cardinalities
and equally good on the rest
• sparse representation allows for a more adaptive use of memory
• if the cardinality n is much smaller then m, then HyperLogLog++
requires significantly less memory
• For cardinalities between 12000 and 61000, the bias correction
allows for a lower error and avoids a spike in the error when
switching between sub-algorithms.
• 64 bit hash codes allow the algorithm to estimate cardinalities well
beyond 1 billion
HYPERLOGLOG++: APPLICATIONS
• cardinality metric in Elasticsearch is based on the
HyperLogLog++ algorithm for big cardinalities (adaptive
counting)
• Apache DataFu, collection of libraries for working with
large-scale data in Hadoop, has an implementation of
HyperLogLog++ algorithm
HYPERLOGLOG++: READ MORE
• http://static.googleusercontent.com/media/
research.google.com/en//pubs/archive/40671.pdf
• https://research.neustar.biz/2013/01/24/hyperloglog-
googles-take-on-engineering-hll/
▸ @gakhov
▸ linkedin.com/in/gakhov
▸ www.datacrucis.com
THANK YOU
Ad

Recommended

Seastar metrics
Seastar metrics
ScyllaDB
 
Drilling fluids
Drilling fluids
Narendra Kumar Dewangan
 
Project Hydrogen and Spark Graph - 分散処理 × AIをより身近にする、Apache Sparkの新機能 - (NTTデ...
Project Hydrogen and Spark Graph - 分散処理 × AIをより身近にする、Apache Sparkの新機能 - (NTTデ...
NTT DATA Technology & Innovation
 
Sontaneous Heating Characteristics of Coal
Sontaneous Heating Characteristics of Coal
Anurag Jha
 
Chaos Engineering for Docker
Chaos Engineering for Docker
Alexei Ledenev
 
34. Horizontal Drilling.ppt
34. Horizontal Drilling.ppt
ssuser835d832
 
Drill bit types advantages and disadvantages
Drill bit types advantages and disadvantages
Jyoti Khatiwada
 
Shaft sinking 1
Shaft sinking 1
Er.Sunil Wasade
 
HBase for Architects
HBase for Architects
Nick Dimiduk
 
Extending Open Cast Mine to Underground Mine Planning
Extending Open Cast Mine to Underground Mine Planning
Jasmeet Singh Saluja
 
PySpark Training | PySpark Tutorial for Beginners | Apache Spark with Python ...
PySpark Training | PySpark Tutorial for Beginners | Apache Spark with Python ...
Edureka!
 
drillingfluids-121124044021-phpapp02.pdf
drillingfluids-121124044021-phpapp02.pdf
MuammerAlakary
 
Wilfley table / shaking table for mineral Processing (extractive metallurgy)
Wilfley table / shaking table for mineral Processing (extractive metallurgy)
SOHINI MONDAL
 
PSR-3 logs using Monolog and Graylog
PSR-3 logs using Monolog and Graylog
OCoderFest
 
Probabilistic data structures
Probabilistic data structures
shrinivasvasala
 
Using Simplicity to Make Hard Big Data Problems Easy
Using Simplicity to Make Hard Big Data Problems Easy
nathanmarz
 
Анализ количества посетителей на сайте [Считаем уникальные элементы]
Анализ количества посетителей на сайте [Считаем уникальные элементы]
Qrator Labs
 
HyperLogLog in Hive - How to count sheep efficiently?
HyperLogLog in Hive - How to count sheep efficiently?
bzamecnik
 
Big Data Day LA 2015 - Large Scale Distinct Count -- The HyperLogLog algorith...
Big Data Day LA 2015 - Large Scale Distinct Count -- The HyperLogLog algorith...
Data Con LA
 
ReqLabs PechaKucha Евгений Сафроненко
ReqLabs PechaKucha Евгений Сафроненко
PechaKucha Ukraine
 
Big Data aggregation techniques
Big Data aggregation techniques
Valentin Logvinskiy
 
Hyper loglog
Hyper loglog
nybon
 
Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017
Roman Elizarov
 
Count-Distinct Problem
Count-Distinct Problem
Kai Zhang
 
2013 open analytics_countingv3
2013 open analytics_countingv3
Open Analytics
 
HyperLogLog and friends
HyperLogLog and friends
Simon Lia-Jonassen
 
Distributed algorithms for big data @ GeeCon
Distributed algorithms for big data @ GeeCon
Duyhai Doan
 
2013 open analytics_countingv3
2013 open analytics_countingv3
abramsm
 
Hyper loglog
Hyper loglog
Eugene Kostieiev
 
Too Much Data? - Just Sample, Just Hash, ...
Too Much Data? - Just Sample, Just Hash, ...
Andrii Gakhov
 

More Related Content

What's hot (6)

HBase for Architects
HBase for Architects
Nick Dimiduk
 
Extending Open Cast Mine to Underground Mine Planning
Extending Open Cast Mine to Underground Mine Planning
Jasmeet Singh Saluja
 
PySpark Training | PySpark Tutorial for Beginners | Apache Spark with Python ...
PySpark Training | PySpark Tutorial for Beginners | Apache Spark with Python ...
Edureka!
 
drillingfluids-121124044021-phpapp02.pdf
drillingfluids-121124044021-phpapp02.pdf
MuammerAlakary
 
Wilfley table / shaking table for mineral Processing (extractive metallurgy)
Wilfley table / shaking table for mineral Processing (extractive metallurgy)
SOHINI MONDAL
 
PSR-3 logs using Monolog and Graylog
PSR-3 logs using Monolog and Graylog
OCoderFest
 
HBase for Architects
HBase for Architects
Nick Dimiduk
 
Extending Open Cast Mine to Underground Mine Planning
Extending Open Cast Mine to Underground Mine Planning
Jasmeet Singh Saluja
 
PySpark Training | PySpark Tutorial for Beginners | Apache Spark with Python ...
PySpark Training | PySpark Tutorial for Beginners | Apache Spark with Python ...
Edureka!
 
drillingfluids-121124044021-phpapp02.pdf
drillingfluids-121124044021-phpapp02.pdf
MuammerAlakary
 
Wilfley table / shaking table for mineral Processing (extractive metallurgy)
Wilfley table / shaking table for mineral Processing (extractive metallurgy)
SOHINI MONDAL
 
PSR-3 logs using Monolog and Graylog
PSR-3 logs using Monolog and Graylog
OCoderFest
 

Viewers also liked (9)

Probabilistic data structures
Probabilistic data structures
shrinivasvasala
 
Using Simplicity to Make Hard Big Data Problems Easy
Using Simplicity to Make Hard Big Data Problems Easy
nathanmarz
 
Анализ количества посетителей на сайте [Считаем уникальные элементы]
Анализ количества посетителей на сайте [Считаем уникальные элементы]
Qrator Labs
 
HyperLogLog in Hive - How to count sheep efficiently?
HyperLogLog in Hive - How to count sheep efficiently?
bzamecnik
 
Big Data Day LA 2015 - Large Scale Distinct Count -- The HyperLogLog algorith...
Big Data Day LA 2015 - Large Scale Distinct Count -- The HyperLogLog algorith...
Data Con LA
 
ReqLabs PechaKucha Евгений Сафроненко
ReqLabs PechaKucha Евгений Сафроненко
PechaKucha Ukraine
 
Big Data aggregation techniques
Big Data aggregation techniques
Valentin Logvinskiy
 
Hyper loglog
Hyper loglog
nybon
 
Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017
Roman Elizarov
 
Probabilistic data structures
Probabilistic data structures
shrinivasvasala
 
Using Simplicity to Make Hard Big Data Problems Easy
Using Simplicity to Make Hard Big Data Problems Easy
nathanmarz
 
Анализ количества посетителей на сайте [Считаем уникальные элементы]
Анализ количества посетителей на сайте [Считаем уникальные элементы]
Qrator Labs
 
HyperLogLog in Hive - How to count sheep efficiently?
HyperLogLog in Hive - How to count sheep efficiently?
bzamecnik
 
Big Data Day LA 2015 - Large Scale Distinct Count -- The HyperLogLog algorith...
Big Data Day LA 2015 - Large Scale Distinct Count -- The HyperLogLog algorith...
Data Con LA
 
ReqLabs PechaKucha Евгений Сафроненко
ReqLabs PechaKucha Евгений Сафроненко
PechaKucha Ukraine
 
Hyper loglog
Hyper loglog
nybon
 
Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017
Roman Elizarov
 
Ad

Similar to Probabilistic data structures. Part 2. Cardinality (20)

Count-Distinct Problem
Count-Distinct Problem
Kai Zhang
 
2013 open analytics_countingv3
2013 open analytics_countingv3
Open Analytics
 
HyperLogLog and friends
HyperLogLog and friends
Simon Lia-Jonassen
 
Distributed algorithms for big data @ GeeCon
Distributed algorithms for big data @ GeeCon
Duyhai Doan
 
2013 open analytics_countingv3
2013 open analytics_countingv3
abramsm
 
Hyper loglog
Hyper loglog
Eugene Kostieiev
 
Too Much Data? - Just Sample, Just Hash, ...
Too Much Data? - Just Sample, Just Hash, ...
Andrii Gakhov
 
An introduction to probabilistic data structures
An introduction to probabilistic data structures
Miguel Ping
 
Tech talk Probabilistic Data Structure
Tech talk Probabilistic Data Structure
Rishabh Dugar
 
HyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardin...
HyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardin...
Sunny Kr
 
Large-scale real-time analytics for everyone
Large-scale real-time analytics for everyone
Pavel Kalaidin
 
Probabilistic Data Structures and Approximate Solutions
Probabilistic Data Structures and Approximate Solutions
Oleksandr Pryymak
 
Approximate methods for scalable data mining
Approximate methods for scalable data mining
Andrew Clegg
 
Probabilistic Data Structures and Approximate Solutions Oleksandr Pryymak
Probabilistic Data Structures and Approximate Solutions Oleksandr Pryymak
PyData
 
Beyond PFCount: Shrif Nada
Beyond PFCount: Shrif Nada
Redis Labs
 
Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...
Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...
Andrii Gakhov
 
Hyperloglog Project
Hyperloglog Project
Kendrick Lo
 
Counting (Using Computer)
Counting (Using Computer)
roshmat
 
Probabilistic data structure
Probabilistic data structure
Thinh Dang
 
Distributed count(distinct) with hyper loglog on postgresql | PGConf EU 2017)...
Distributed count(distinct) with hyper loglog on postgresql | PGConf EU 2017)...
Citus Data
 
Count-Distinct Problem
Count-Distinct Problem
Kai Zhang
 
2013 open analytics_countingv3
2013 open analytics_countingv3
Open Analytics
 
Distributed algorithms for big data @ GeeCon
Distributed algorithms for big data @ GeeCon
Duyhai Doan
 
2013 open analytics_countingv3
2013 open analytics_countingv3
abramsm
 
Too Much Data? - Just Sample, Just Hash, ...
Too Much Data? - Just Sample, Just Hash, ...
Andrii Gakhov
 
An introduction to probabilistic data structures
An introduction to probabilistic data structures
Miguel Ping
 
Tech talk Probabilistic Data Structure
Tech talk Probabilistic Data Structure
Rishabh Dugar
 
HyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardin...
HyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardin...
Sunny Kr
 
Large-scale real-time analytics for everyone
Large-scale real-time analytics for everyone
Pavel Kalaidin
 
Probabilistic Data Structures and Approximate Solutions
Probabilistic Data Structures and Approximate Solutions
Oleksandr Pryymak
 
Approximate methods for scalable data mining
Approximate methods for scalable data mining
Andrew Clegg
 
Probabilistic Data Structures and Approximate Solutions Oleksandr Pryymak
Probabilistic Data Structures and Approximate Solutions Oleksandr Pryymak
PyData
 
Beyond PFCount: Shrif Nada
Beyond PFCount: Shrif Nada
Redis Labs
 
Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...
Exceeding Classical: Probabilistic Data Structures in Data Intensive Applicat...
Andrii Gakhov
 
Hyperloglog Project
Hyperloglog Project
Kendrick Lo
 
Counting (Using Computer)
Counting (Using Computer)
roshmat
 
Probabilistic data structure
Probabilistic data structure
Thinh Dang
 
Distributed count(distinct) with hyper loglog on postgresql | PGConf EU 2017)...
Distributed count(distinct) with hyper loglog on postgresql | PGConf EU 2017)...
Citus Data
 
Ad

More from Andrii Gakhov (20)

Let's start GraphQL: structure, behavior, and architecture
Let's start GraphQL: structure, behavior, and architecture
Andrii Gakhov
 
DNS Delegation
DNS Delegation
Andrii Gakhov
 
Implementing a Fileserver with Nginx and Lua
Implementing a Fileserver with Nginx and Lua
Andrii Gakhov
 
Pecha Kucha: Ukrainian Food Traditions
Pecha Kucha: Ukrainian Food Traditions
Andrii Gakhov
 
Probabilistic data structures. Part 4. Similarity
Probabilistic data structures. Part 4. Similarity
Andrii Gakhov
 
Probabilistic data structures. Part 3. Frequency
Probabilistic data structures. Part 3. Frequency
Andrii Gakhov
 
Вероятностные структуры данных
Вероятностные структуры данных
Andrii Gakhov
 
Recurrent Neural Networks. Part 1: Theory
Recurrent Neural Networks. Part 1: Theory
Andrii Gakhov
 
Apache Big Data Europe 2015: Selected Talks
Apache Big Data Europe 2015: Selected Talks
Andrii Gakhov
 
Swagger / Quick Start Guide
Swagger / Quick Start Guide
Andrii Gakhov
 
API Days Berlin highlights
API Days Berlin highlights
Andrii Gakhov
 
ELK - What's new and showcases
ELK - What's new and showcases
Andrii Gakhov
 
Apache Spark Overview @ ferret
Apache Spark Overview @ ferret
Andrii Gakhov
 
Data Mining - lecture 8 - 2014
Data Mining - lecture 8 - 2014
Andrii Gakhov
 
Data Mining - lecture 7 - 2014
Data Mining - lecture 7 - 2014
Andrii Gakhov
 
Data Mining - lecture 6 - 2014
Data Mining - lecture 6 - 2014
Andrii Gakhov
 
Data Mining - lecture 5 - 2014
Data Mining - lecture 5 - 2014
Andrii Gakhov
 
Data Mining - lecture 4 - 2014
Data Mining - lecture 4 - 2014
Andrii Gakhov
 
Data Mining - lecture 3 - 2014
Data Mining - lecture 3 - 2014
Andrii Gakhov
 
Decision Theory - lecture 1 (introduction)
Decision Theory - lecture 1 (introduction)
Andrii Gakhov
 
Let's start GraphQL: structure, behavior, and architecture
Let's start GraphQL: structure, behavior, and architecture
Andrii Gakhov
 
Implementing a Fileserver with Nginx and Lua
Implementing a Fileserver with Nginx and Lua
Andrii Gakhov
 
Pecha Kucha: Ukrainian Food Traditions
Pecha Kucha: Ukrainian Food Traditions
Andrii Gakhov
 
Probabilistic data structures. Part 4. Similarity
Probabilistic data structures. Part 4. Similarity
Andrii Gakhov
 
Probabilistic data structures. Part 3. Frequency
Probabilistic data structures. Part 3. Frequency
Andrii Gakhov
 
Вероятностные структуры данных
Вероятностные структуры данных
Andrii Gakhov
 
Recurrent Neural Networks. Part 1: Theory
Recurrent Neural Networks. Part 1: Theory
Andrii Gakhov
 
Apache Big Data Europe 2015: Selected Talks
Apache Big Data Europe 2015: Selected Talks
Andrii Gakhov
 
Swagger / Quick Start Guide
Swagger / Quick Start Guide
Andrii Gakhov
 
API Days Berlin highlights
API Days Berlin highlights
Andrii Gakhov
 
ELK - What's new and showcases
ELK - What's new and showcases
Andrii Gakhov
 
Apache Spark Overview @ ferret
Apache Spark Overview @ ferret
Andrii Gakhov
 
Data Mining - lecture 8 - 2014
Data Mining - lecture 8 - 2014
Andrii Gakhov
 
Data Mining - lecture 7 - 2014
Data Mining - lecture 7 - 2014
Andrii Gakhov
 
Data Mining - lecture 6 - 2014
Data Mining - lecture 6 - 2014
Andrii Gakhov
 
Data Mining - lecture 5 - 2014
Data Mining - lecture 5 - 2014
Andrii Gakhov
 
Data Mining - lecture 4 - 2014
Data Mining - lecture 4 - 2014
Andrii Gakhov
 
Data Mining - lecture 3 - 2014
Data Mining - lecture 3 - 2014
Andrii Gakhov
 
Decision Theory - lecture 1 (introduction)
Decision Theory - lecture 1 (introduction)
Andrii Gakhov
 

Recently uploaded (20)

AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Safe Software
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
Precisely
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Safe Software
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
Precisely
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 

Probabilistic data structures. Part 2. Cardinality

  • 1. tech talk @ ferret Andrii Gakhov PROBABILISTIC DATA STRUCTURES ALL YOU WANTED TO KNOW BUT WERE AFRAID TO ASK PART 2: CARDINALITY
  • 2. CARDINALITY Agenda: ▸ Linear Counting ▸ LogLog, SuperLogLog, HyperLogLog, HyperLogLog++
  • 3. • To determine the number of distinct elements, also called the cardinality, of a large set of elements where duplicates are present Calculating the exact cardinality of a multiset requires an amount of memory proportional to the cardinality, which is impractical for very large data sets. THE PROBLEM
  • 5. LINEAR COUNTING: ALGORITHM • Linear counter is a bit map (hash table) of size m (all elements set to 0 at the beginning). • Algorithm consists of a few steps: • for every element calculate hash function and set the appropriate bit to 1 • calculate the fraction V of empty bits in the structure 
 (divide the number of empty bits by the bit map size m ) • estimate cardinality as n ≈ -m ln V
  • 6. LINEAR COUNTING: EXAMPLE 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 • Consider linear counter with 16 bits (m=16) • Consider MurmurHash3 as the hash function h
 (to calculate the appropriate index, we divide result by mod 16) • Set of 10 elements: “bernau”, “bernau”, “bernau”, “berlin”, “kiev”, “kiev”, “new york”, “germany”, “ukraine”, “europe” (NOTE: the real cardinality n = 7) h(“bernau”) = 4, h(“berlin”) = 4, h(“kiev”) = 6, h(“new york”) = 6, h(“germany”) = 14, h(“ukraine”) = 7, h(“europe”) = 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 0 0 1 0 1 1 0 1 0 0 0 0 1 0
  • 7. LINEAR COUNTING: EXAMPLE number of empty bits: 11 m = 16 V = 11 / 16 = 0.6875 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 0 0 1 0 1 1 0 1 0 0 0 0 1 0 • Cardinality estimation is n ≈ - 16 * ln (0.6875) = 5.995
  • 8. LINEAR COUNTING: READ MORE • http://dblab.kaist.ac.kr/Prof/pdf/Whang1990(linear).pdf • http://www.codeproject.com/Articles/569718/ CardinalityplusEstimationplusinplusLinearplusTimep
  • 10. HYPERLOGLOG: INTUITION • The cardinality of a multiset of uniformly distributed numbers can be estimated by the maximum number of leading zeros in the binary representation of each number. If such value is k, then the number of distinct elements in the set is 2k P(rank=1) = 1/2 - probability to find a binary representation, that starts with 1 P(rank = 2) = 1/2 2 - probability to find a binary representation, that start with 01 … P(rank=k) = 1/2 k rank = number of leading zeros + 1, e.g. rank(f) = 3 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 2 leading zeros 3 f = • Therefore, for 2k binary representations we shell find at least one representation with rank = k • If we remember the maximal rank we’ve seen and it’s equal to k, then we can use 2k as the approximation of the number of elements
  • 11. HYPERLOGLOG • proposed by Flajolet et. al., 2007 • an extension of the Flajolet–Martin algorithm (1985) • HyperLogLog is described by 2 parameters: • p – number of bits that determine a bucket to use averaging
 (m = 2p is the number of buckets/substreams) • h - hash function, that produces uniform hash values • The HyperLogLog algorithm is able to estimate cardinalities of > 109 with a typical error rate of 2%, using 1.5kB of memory (Flajolet, P. et al., 2007).
  • 12. HYPERLOGLOG: ALGORITHM • HyperLogLog uses randomization to approximate the cardinality of a multiset.This randomization is achieved by using hash function h • Observe the maximum number of leading zeros that for all hash values: • If the bit pattern 0L−1 1 is observed at the beginning of a hash value (so, rank = L), then a good estimation of the size of the multiset is 2L.
  • 13. HYPERLOGLOG: ALGORITHM • Stochastic averaging is used to reduce the large variability: • The input stream of data elements S is divided into m substreams Si using the first p bits of the hash values (m = 2p) . • In each substream, the rank (after the initial p bits that are used for substreaming) is measured independently. • These numbers are kept in an array of registers M, where M[i] stores the maximum rank it seen for the substream with index i. • The cardinality estimation is calculated computes as the normalized bias corrected harmonic mean of the estimations on the substreams DVHLL = const(m)⋅m2 ⋅ 2 −M j j=1 m ∑ ⎛ ⎝⎜ ⎞ ⎠⎟ −1
  • 14. HYPERLOGLOG: EXAMPLE • Consider L=8 bits hash function h • Index elements “berlin” and “ferret”: h(“berlin”) = 0110111 h(“ferret”) = 1100011 • Define buckets and calculate values to store:
 (use first p =3 bits for buckets and least L - p = 5 bits for ranks) • bucket(“berlin”) = 011 = 3 value(“berlin”) = rank(0111) = 2 • bucket(“ferret”) = 110 = 6 value(“ferret”) = rank(0011) = 3 • Let’s use p=3 bits to define a bucket (then m=23 =8 buckets). 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 M 1 2 3 4 5 6 7 0 0 0 2 0 0 3 0 0 M
  • 15. HYPERLOGLOG: EXAMPLE • Estimate the cardinality be the HLL formula (C ≈ 0.66): DVHLL ≈ 0.66 * 82 / (2-2 + 2-4 ) = 0.66 * 204.8 ≈ 135≠3 • Index element “kharkov”: • h(“kharkov”) = 1100001 • bucket(“kharkov”) = 110 = 6 value(“kharkov”) = rank(0001) = 4 • M[6] = max(M[6], 4) = max(3, 4) = 4 1 2 3 4 5 6 7 0 0 0 2 0 0 4 0 0 M NOTE: For small cardinalities HLL has a strong bias!!!
  • 16. HYPERLOGLOG: PROPERTIES • Memory requirement doesn't grow linearly with L (unlike MinCount or Linear Counting) - for hash function of L bits and precision p, required memory: • original HyperLogLog uses 32 bit hash codes, which requires 5 · 2 p bits • It’s not necessary to calculate the full hash code for the element • first p bits and number of leading zeros of the remaining bits are enough • There are no evidence that some of popular hash functions (MD5, Sha1, Sha256, Murmur3) performs significantly better than others. log2 L +1− p( )⎡⎢ ⎤⎥⋅2p bits
  • 17. HYPERLOGLOG: PROPERTIES • The standard error can be estimated as: σ = 1.04 2p so, if we use 16 bits (p=16) for bucket indices, we receive the standard error in 0.40625% • Algorithm has large error for small cardinalities. • For instance, for n = 0 the algorithm always returns roughly 0.7m • To achieve better estimates for small cardinalities, use LinearCounting below a threshold of 5m/2
  • 18. HYPERLOGLOG: APPLICATIONS • PFCOUNT in Redis returns the approximated cardinality computed by the HyperLogLog data structure 
 (http://antirez.com/news/75) • Redis implementation uses 12Kb per key to count with a standard error of 0.81%, and there is no limit to the number of items you can count, unless you approach 264 items
  • 19. HYPERLOGLOG: READ MORE • http://algo.inria.fr/flajolet/Publications/DuFl03-LNCS.pdf • http://algo.inria.fr/flajolet/Publications/FlFuGaMe07.pdf • https://stefanheule.com/papers/edbt13-hyperloglog.pdf • https://highlyscalable.wordpress.com/2012/05/01/ probabilistic-structures-web-analytics-data-mining/ • https://hal.archives-ouvertes.fr/file/index/docid/465313/ filename/sliding_HyperLogLog.pdf • http://stackoverflow.com/questions/12327004/how-does- the-hyperloglog-algorithm-work
  • 21. HYPERLOGLOG++ • proposed by Stefan Heule et. al., 2013 for Google PowerDrill • an improved version of HyperLogLog (Flajolet et. al., 2007) • HyperLogLog++ is described by 2 parameters: • p – number of bits that determine a bucket to use averaging
 (m = 2p is the number of buckets/substreams) • h - hash function, that produces uniform hash values • The HyperLogLog++ algorithm is able to estimate cardinalities of ~ 7.9 · 10 9 with a typical error rate of 1.625%, using 2.56KB of memory (Micha Gorelick and Ian Ozsvald, High Performance Python, 2014).
  • 22. HYPERLOGLOG++: IMPROVEMENTS • use 64-bit hash function • algorithm that only uses the hash code of the input values is limited by the number of bits of the hash codes when it comes to accurately estimating large cardinalities • In particular, a hash function of L bits can at most distinguish 2L different values, and as the cardinality n approaches 2L hash collisions become more and more likely and accurate estimation gets impossible • if the cardinality approaches 264 ≈ 1.8 · 1019, hash collisions become a problem • bias correction • original algorithm overestimates the real cardinality for small sets, but most of the error is due to bias. • storage efficiency • uses different encoding strategies for hash values, variable length encoding for integers, difference encoding
  • 23. HYPERLOGLOG++ VS HYPERLOGLOG • accuracy is significantly better for large range of cardinalities and equally good on the rest • sparse representation allows for a more adaptive use of memory • if the cardinality n is much smaller then m, then HyperLogLog++ requires significantly less memory • For cardinalities between 12000 and 61000, the bias correction allows for a lower error and avoids a spike in the error when switching between sub-algorithms. • 64 bit hash codes allow the algorithm to estimate cardinalities well beyond 1 billion
  • 24. HYPERLOGLOG++: APPLICATIONS • cardinality metric in Elasticsearch is based on the HyperLogLog++ algorithm for big cardinalities (adaptive counting) • Apache DataFu, collection of libraries for working with large-scale data in Hadoop, has an implementation of HyperLogLog++ algorithm
  • 25. HYPERLOGLOG++: READ MORE • http://static.googleusercontent.com/media/ research.google.com/en//pubs/archive/40671.pdf • https://research.neustar.biz/2013/01/24/hyperloglog- googles-take-on-engineering-hll/
  • 26. ▸ @gakhov ▸ linkedin.com/in/gakhov ▸ www.datacrucis.com THANK YOU