{"id":17204393,"url":"https://github.com/urschrei/cvmcount_py","last_synced_at":"2025-04-13T22:10:30.517Z","repository":{"id":246038812,"uuid":"819919170","full_name":"urschrei/cvmcount_py","owner":"urschrei","description":"Python implementation of the CVM algorithm","archived":false,"fork":false,"pushed_at":"2024-10-20T13:58:39.000Z","size":58,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T03:48:14.764Z","etag":null,"topics":["algorithm","count-distinct","cvm"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/urschrei.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-25T12:45:10.000Z","updated_at":"2025-02-10T17:44:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"a52c6d58-78c6-4738-bcc2-2555317cc51e","html_url":"https://github.com/urschrei/cvmcount_py","commit_stats":null,"previous_names":["urschrei/cvmcount_py"],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/urschrei%2Fcvmcount_py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/urschrei%2Fcvmcount_py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/urschrei%2Fcvmcount_py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/urschrei%2Fcvmcount_py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/urschrei","download_url":"https://codeload.github.com/urschrei/cvmcount_py/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248788927,"owners_count":21161727,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","count-distinct","cvm"],"created_at":"2024-10-15T02:21:37.892Z","updated_at":"2025-04-13T22:10:30.496Z","avatar_url":"https://github.com/urschrei.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A Python implementation of the CVM Algorithm for Counting Distinct Elements\n\nThis library implements the algorithm described in\n\n\u003e Chakraborty, S., Vinodchandran, N. V., \u0026 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\n\nThe accompanying article in Quanta is here: https://www.quantamagazine.org/computer-scientists-invent-an-efficient-new-way-to-count-20240516/\n\nThis Python module leverages the `cvmcount` Rust library: https://github.com/urschrei/cvmcount\n\n## What does that mean\nThe 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.\n\nIn order to overcome this constraint, streaming algorithms have been developed: [Flajolet-Martin](https://en.wikipedia.org/wiki/Flajolet–Martin_algorithm), LogLog, [HyperLogLog](https://en.wikipedia.org/wiki/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](https://www.statlect.com/glossary/unbiased-estimator#:~:text=An%20estimator%20of%20a%20given,Examples) of the cardinality.\n\n# What is an Element\nAny hashable Python object or primitive. Not `f32` / `f64`, however, as they don't form a total ordering due to the presence of NaN.\n\n## Further Details\nDon 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](https://en.wikipedia.org/wiki/Treap#:~:text=7%20External%20links-,Description,(randomly%20chosen)%20numeric%20priority.) – as a binary tree\n\u003e \"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_ \u003c 1.\"\n\nwhere _s_ \u003e= 1. Our implementation doesn't use a treap as a buffer; it uses a fast HashSet with the [FxHash](https://docs.rs/fxhash/latest/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.\n\n# Installation\n`pip install count_distinct`\n\n# Usage\n```python\nfrom count_distinct import CVM\n\n# values for epsilon, delta, and stream size are described in the docstring.\ncounter = CVM(0.8, 0.1, 1000)\ncounter.add(2)\ncounter.add(5)\n# keep adding elements\ncount = counter.calculate_final_result()\n# you can keep adding elements if you wish\n```\n\n# Perf\n## Memory, 10e8 random 7-digit positive integers\nAllocation of integer array: 763 MiB\n\n`count_distinct`: 768 MiB\n\n`np.unique`: 1.7 GiB\n\n## Note\nIf 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!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Furschrei%2Fcvmcount_py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Furschrei%2Fcvmcount_py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Furschrei%2Fcvmcount_py/lists"}