{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/optimal-approximate-sampling-from-discrete","title":"Optimal Approximate Sampling from Discrete Probability Distributions","arxiv_id":"2001.04555","date":"2020-01-13","proceeding":null,"authors":["Feras A. Saad","Cameron E. Freer","Martin C. Rinard","Vikash K. Mansinghka"],"abstract":"This paper addresses a fundamental problem in random variate generation: given access to a random source that emits a stream of independent fair bits, what is the most accurate and entropy-efficient algorithm for sampling from a discrete probability distribution $(p_1, \\dots, p_n)$, where the probabilities of the output distribution $(\\hat{p}_1, \\dots, \\hat{p}_n)$ of the sampling algorithm must be specified using at most $k$ bits of precision? We present a theoretical framework for formulating this problem and provide new techniques for finding sampling algorithms that are optimal both statistically (in the sense of sampling accuracy) and information-theoretically (in the sense of entropy consumption). We leverage these results to build a system that, for a broad family of measures of statistical accuracy, delivers a sampling algorithm whose expected entropy usage is minimal among those that induce the same distribution (i.e., is \"entropy-optimal\") and whose output distribution $(\\hat{p}_1, \\dots, \\hat{p}_n)$ is a closest approximation to the target distribution $(p_1, \\dots, p_n)$ among all entropy-optimal sampling algorithms that operate within the specified $k$-bit precision. This optimal approximate sampler is also a closer approximation than any (possibly entropy-suboptimal) sampler that consumes a bounded amount of entropy with the specified precision, a class which includes floating-point implementations of inversion sampling and related methods found in many software libraries. We evaluate the accuracy, entropy consumption, precision requirements, and wall-clock runtime of our optimal approximate sampling algorithms on a broad set of distributions, demonstrating the ways that they are superior to existing approximate samplers and establishing that they often consume significantly fewer resources than are needed by exact samplers.","url_abs":"http://arxiv.org/abs/2001.04555v1","url_pdf":"http://arxiv.org/pdf/2001.04555v1.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"links_only","authors_date_abstract":"arXiv metadata, CC0 1.0 (https://info.arxiv.org/help/license), from the Kaggle arXiv metadata snapshot of 2026-09-12"},"code_links":[{"paper_slug":"optimal-approximate-sampling-from-discrete","repo_url":"https://github.com/probcomp/optimal-approximate-sampling","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":0,"framework":"none","reach":{"status":"ok","spdx":"Apache-2.0"}},{"paper_slug":"optimal-approximate-sampling-from-discrete","repo_url":"https://github.com/maciej-bendkowski/boltzmann-brain","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"none","reach":{"status":"ok","spdx":"BSD-3-Clause"}},{"paper_slug":"optimal-approximate-sampling-from-discrete","repo_url":"https://github.com/maciej-bendkowski/generic-boltzmann-brain","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"none","reach":{"status":"ok","spdx":"BSD-3-Clause"}}],"tasks":[],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2001.04555","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2001.04555"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/maciej-bendkowski/generic-boltzmann-brain","reach":{"status":"ok","spdx":"BSD-3-Clause"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/maciej-bendkowski/boltzmann-brain","reach":{"status":"ok","spdx":"BSD-3-Clause"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/probcomp/optimal-approximate-sampling","reach":{"status":"ok","spdx":"Apache-2.0"}}],"summary":{"unverified":22},"by_repo_kind":{"official":{"samples":22,"ran":0,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":0,"samples":[{"code_sha256_prefix":"7f7a7496ddb342a9","entry":"construct_sample_ky_encoding","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/construct.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/construct.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"7f7a7496ddb342a9"}},{"code_sha256_prefix":"6f620229d6782cd4","entry":"construct_sample_ky_matrix","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/construct.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/construct.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"6f620229d6782cd4"}},{"code_sha256_prefix":"8c103a34014ef59a","entry":"construct_sample_ky_matrix_cached","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/construct.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/construct.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"8c103a34014ef59a"}},{"code_sha256_prefix":"30463725333b18bb","entry":"find_optimal_indexes","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/opt.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/opt.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"30463725333b18bb"}},{"code_sha256_prefix":"4cf28f8c3eba0421","entry":"get_common_denominator","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/utils.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/utils.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"4cf28f8c3eba0421"}},{"code_sha256_prefix":"388e095eeeca2661","entry":"get_common_numerators","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/utils.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/utils.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"388e095eeeca2661"}},{"code_sha256_prefix":"e716cfd09ac434c7","entry":"get_delta_error","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/opt.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/opt.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"e716cfd09ac434c7"}},{"code_sha256_prefix":"4fa6c82bf596ac2b","entry":"get_initial_Ms","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/opt.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/opt.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"4fa6c82bf596ac2b"}},{"code_sha256_prefix":"8e3254232604c04a","entry":"kernel_hellinger","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/divergences.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/divergences.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"8e3254232604c04a"}},{"code_sha256_prefix":"b542fca2f1e4b985","entry":"kernel_pchi2","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/divergences.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/divergences.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"b542fca2f1e4b985"}},{"code_sha256_prefix":"7f88bdd1e0d18ad7","entry":"kernel_tv","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/divergences.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/divergences.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"7f88bdd1e0d18ad7"}},{"code_sha256_prefix":"9a61b5ea009c49ab","entry":"lcm","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/utils.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/utils.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"9a61b5ea009c49ab"}},{"code_sha256_prefix":"8d3912840cbc2ad0","entry":"make_ddg_matrix","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/matrix.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/matrix.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"8d3912840cbc2ad0"}},{"code_sha256_prefix":"9473b3dc91b4a9e3","entry":"make_ddg_tree","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/tree.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/tree.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"9473b3dc91b4a9e3"}},{"code_sha256_prefix":"3ff5069ec587d49d","entry":"make_hamming_vector","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/matrix.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/matrix.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"3ff5069ec587d49d"}},{"code_sha256_prefix":"599e5c702aea357f","entry":"make_leaf_table","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/tree.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/tree.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"599e5c702aea357f"}},{"code_sha256_prefix":"18fdfd1e058d844d","entry":"make_matrix","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/matrix.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/matrix.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"18fdfd1e058d844d"}},{"code_sha256_prefix":"4303f40052d64e52","entry":"make_tree","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/tree.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/tree.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"4303f40052d64e52"}},{"code_sha256_prefix":"97a14f39df6e7aa7","entry":"pack_tree","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/packing.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/packing.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"97a14f39df6e7aa7"}},{"code_sha256_prefix":"b8308fdb20d40153","entry":"sample_ky_encoding","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/sample.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/sample.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"b8308fdb20d40153"}},{"code_sha256_prefix":"f19d056c1eb9addc","entry":"sample_ky_matrix","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/sample.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/sample.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"f19d056c1eb9addc"}},{"code_sha256_prefix":"2ff3534a77da0bc7","entry":"sample_ky_matrix_cached","repo":"probcomp/optimal-approximate-sampling","repo_kind":"official","path":"src/sample.py","file_url":"https://github.com/probcomp/optimal-approximate-sampling/blob/HEAD/src/sample.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"2ff3534a77da0bc7"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}