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
Various integer flavours, strings, any Struct on which you have implemented the traits. 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.12-cp310-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b6925561bae2f1579bc04446d6f26c4599006101f771a89ecbfa22192292368 |
|
MD5 | 3ebcc1b0b9abefbe4204c423c701561a |
|
BLAKE2b-256 | b4aa090e7901adbae2e3b30f90f202747dd9e7b5fa45b7344a8a342624b0d676 |
Hashes for count_distinct-0.1.12-cp310-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2fd1125c5b2a2c5c56873211db24fd27bfd7f2e5a853bf1dc32860bc42bff02 |
|
MD5 | 7bdfe5588fe90a2f101287d3747466a9 |
|
BLAKE2b-256 | 34fb726c7d73a98b6fd88c3e39702f4270bb621758648de7a4f67bf29133aedd |
Hashes for count_distinct-0.1.12-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f22b47332a119b4e8cb1252e8e27be65c50b6a7fc56ffe33567692d0a3209662 |
|
MD5 | ee0ed11660adcd8b55ca137972080aca |
|
BLAKE2b-256 | 3fdfc85e028885e5b365b17fc1fff5e9c34663d5609bb14df01aab30ac5bcf1e |
Hashes for count_distinct-0.1.12-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c696c76fcd3f52a9cdfc6a098f14094c9a7f4a52392fa877eccf21850d93641f |
|
MD5 | bf236bee03eb426852bcb5483d857892 |
|
BLAKE2b-256 | 7443ad0bfa84235bbc972bebef5f771d368ee8090de4ecd0837a410c60121e3c |
Hashes for count_distinct-0.1.12-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a8bef65ff6e8f7dfd2000fa74e7a6658917cfe7224b6291fc96942942f9324f |
|
MD5 | 796fb981a8f24a9ed4e92729ae157d60 |
|
BLAKE2b-256 | c1090f5fce3bdf86e478b6902f7ef0bfa308293aef9525b83b8bcd0d7072b161 |
Hashes for count_distinct-0.1.12-cp310-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3aa87908118a4402e40733f9616995801a374387aaccef44af5b4934f43db9b6 |
|
MD5 | 23ec169995df09d871e447e4741396df |
|
BLAKE2b-256 | e9ca033cd016aecfe1eeb3385788588c7d28fcc90aaa759550049765569c5cf4 |
Hashes for count_distinct-0.1.12-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d239c7c420fe9bf87b034827e61efd41203ff22aeb96cc9ab3a7af03d7a7598d |
|
MD5 | aa218c8be26ec4b2e91ad6139184a6af |
|
BLAKE2b-256 | b5cfae1b910a42bb4d642a0dae027207fd93f89a9467e97b38bb9f15e73fdb65 |