Use the CVM algorithm to quickly estimate the number of distinct elements in a stream
Project description
A Python implementation of the CVM Algorithm for Counting Distinct Elements
This library implements the algorithm described in
Chakraborty, S., Vinodchandran, N. V., & Meel, K. S. (2022). Distinct Elements in Streams: An Algorithm for the (Text) Book. 6 pages, 727571 bytes. https://doi.org/10.4230/LIPIcs.ESA.2022.34
The accompanying article in Quanta is here: https://www.quantamagazine.org/computer-scientists-invent-an-efficient-new-way-to-count-20240516/
This Python module leverages the cvmcount
Rust library: https://github.com/urschrei/cvmcount
What does that mean
The count-distinct problem, or cardinality-estimation problem refers to counting the number of distinct elements in a data stream with repeated elements. As a concrete example, imagine that you want to count the unique words in a book. If you have enough memory, you can keep track of every unique element you encounter. However, you may not have enough working memory due to resource constraints, or the number of potential elements may be enormous. This constraint is referred to as the bounded-storage constraint in the literature.
In order to overcome this constraint, streaming algorithms have been developed: Flajolet-Martin, LogLog, HyperLogLog. The algorithm implemented by this library is an improvement on these in one particular sense: it is extremely simple. Instead of hashing, it uses a sampling method to compute an unbiased estimate of the cardinality.
What is an Element
Any hashable Python object or primitive. Not f32
/ f64
, however, as they don't form a total ordering due to the presence of NaN.
Further Details
Don Knuth has written about the algorithm (he refers to it as Algorithm D) at https://cs.stanford.edu/~knuth/papers/cvm-note.pdf, and does a far better job than I do at explaining it. You will note that on p1 he describes the buffer he uses as a data structure – called a treap – as a binary tree
"that’s capable of holding up to s ordered pairs (a, u), where a is an element of the stream and u is a real number, 0 ≤ u < 1."
where s >= 1. Our implementation doesn't use a treap as a buffer; it uses a fast HashSet with the FxHash algorithm: we pay the hash cost when inserting, but search in step D4 is O(1)
. The library may switch to a treap implementation eventually.
Installation
pip install count_distinct
Usage
from count_distinct import CVM
# values for epsilon, delta, and stream size are described in the docstring.
counter = CVM(0.8, 0.1, 1000)
counter.add(2)
counter.add(5)
# keep adding elements
count = counter.calculate_final_result()
# you can keep adding elements if you wish
Note
If you're thinking about using this library, you presumably know that it only provides an estimate (within the specified bounds), similar to something like HyperLogLog. You are trading accuracy for speed!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for count_distinct-0.1.13-cp310-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7f1bfb0a185ce5f8a20fd067d47ba9f8413447d3a5e68439ad7f9d5735ee483 |
|
MD5 | 3c9fe3b0a6c84cac408e176d4050bb5c |
|
BLAKE2b-256 | f7ebe2517ff78d458e1759c9c90b5fe34f83b700edbd1cc68bc5f32696e618d7 |
Hashes for count_distinct-0.1.13-cp310-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a6f6a81b6e5a9abc343cf3c56c117a496b350edc8ea074267df4076ae15787a |
|
MD5 | a722e1bdfd508cf5757acedb6d7efb9e |
|
BLAKE2b-256 | b1547821e2b34f277e014e975738fd7a02ddc1f07c69de847a7b4e655db6d973 |
Hashes for count_distinct-0.1.13-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2992831a7566cbdf46663d50387f474494f7f8584cf04b69299d20f6cb403255 |
|
MD5 | 281168fab38d01775af5dd6b65b95874 |
|
BLAKE2b-256 | 00e1b58fd969a993d3270538ab41b150392c01c51e416342ac6d1429a7925036 |
Hashes for count_distinct-0.1.13-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e0eb23b5f7e14215574fa3ae455634361847a6a69eff386348fb7a570b76d87 |
|
MD5 | 92a1281543e7f5aee18617e73a680181 |
|
BLAKE2b-256 | 1da53a2c674c3cc0bb9dfc639a8d04bef6a8dd906f06e7ba94b233dd82fe3358 |
Hashes for count_distinct-0.1.13-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4494adcfb530796813e3edcf4c24ad652a45833dc041a91a5160e6c8479268de |
|
MD5 | 0b5b2dce468a97da85d90cce605b0772 |
|
BLAKE2b-256 | fbc12d67ccee8cab5a23c1fef2d2a88216814c33357bda03fc5800d46dd8aa8d |
Hashes for count_distinct-0.1.13-cp310-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 267e83b4cd85404c96ec0107b2ebbb1f210d0ede3a42a76e4fc27cbee7f330b7 |
|
MD5 | 420f1bef5b15ad0fcff6d5b35124a986 |
|
BLAKE2b-256 | 2f2b56988246a43f7bc76789c871288fe3bf8b1656cd37b8a2bb3827f6caca36 |
Hashes for count_distinct-0.1.13-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7322b4dde9203bfa800b6207befc3c2fd84199f8402a2cbd42a1abaa8a988075 |
|
MD5 | 7d8bb2c14f47ac8a7e94ea4f7a8e2ce4 |
|
BLAKE2b-256 | 96fc86e2b4010902ebbe70aff818527fee4612a78a11428ea45c7fdb0853d59f |