No project description provided
Project description
discrete dists
Simple utilities for defining complex distributions over discrete elements. Backed by a fast sum-tree implementation written in Rust.
Getting Started
pip install discrete-dists
The formal API behavior is documented in SEMANTICS.md.
When to use which distribution
Uniform: sample evenly from a fixed interval[lo, hi).Proportional: sample discrete elements proportionally to per-element nonnegative weights.Categorical: convenience wrapper aroundProportionalusing standard probability terminology.MixtureDistribution: blend multiple sub-distributions with mixture weights.
API
Uniform Distribution
A very simple wrapper over np.random.default_rng().integers, conforming to the Distribution API defined in this library.
This wrapper additionally introduces importance sampling ratio calculations, sampling without replacement, and stratified sampling.
import numpy as np
from discrete_dists.uniform import Uniform
rng = np.random.default_rng(0)
u = Uniform(100)
# sampling
print(u.sample(rng, 10))
print(u.stratified_sample(rng, 10))
print(u.sample_without_replacement(rng, 10))
# importance sampling ratio
other = Uniform(10)
items = [0, 3, 8]
isrs = u.isr(other, items)
# out-of-support probabilities are zero
print(u.probs([-1, 0, 99, 100]))
# updating the support
u.update_single(150)
Proportional Distribution
Sample proportional to a list of values.
from discrete_dists.proportional import Proportional
from discrete_dists.categorical import Categorical
p = Proportional(5)
# set the values to sample proportional to
p.update(idxs=[0, 2], values=[1, 2])
# approximately 33% of values are 0, and 66% are 2
print(p.sample(rng, 10000))
p.update(idxs=[1], values=[2])
# approximately 20% are 0, 40% are 1, and 40% are 2
print(p.sample(rng, 10000))
# standard alias if you prefer probability terminology
c = Categorical(5)
Mixture Distribution
Mix together arbitrary distributions with arbitrary supports.
from discrete_dists.proportional import Proportional
from discrete_dists.uniform import Uniform
from discrete_dists.mixture import MixtureDistribution, SubDistribution
prop = Proportional(100)
m = MixtureDistribution([
SubDistribution(d=prop, p=0.2),
SubDistribution(d=Uniform(10), p=0.8),
])
prop.update(idxs=np.arange(100), values=100-np.arange(100))
print(m.sample(rng, 10000))
Mixtures ignore defunct children when computing probabilities and samples.
Edge cases and contracts
probs()returns0outside support.sample_without_replacement()returns exactlynunique elements or raisesValueError.isr()may returninfornanwhen supports do not overlap.Proportional.update_support()supports same-width shifts and widening that fully contains the old support.
Performance notes
- The Rust-backed sum tree powers
Proportional/Categoricaloperations. - Core update/query paths use a flat tree layout and release the GIL in hot paths.
- In the current test benchmark on this branch, SumTree update and sample paths improved substantially compared with the earlier layered implementation.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file discrete_dists-1.2.1.tar.gz.
File metadata
- Download URL: discrete_dists-1.2.1.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24bb8b4b85999edd2989021d06ed947e482c3d59fd637b899815352e2e4cacfd
|
|
| MD5 |
cca80a7bf9b20c60662b76ecd88575a9
|
|
| BLAKE2b-256 |
4282201eec0dc60175d10f73a1d21afc2452e81d69252662e5ca431cfc16c341
|
File details
Details for the file discrete_dists-1.2.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 345.4 kB
- Tags: PyPy, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ff0217177f15e83ec7bbf7f84eb745f1981e8909c0ba86b012c59d08a655066
|
|
| MD5 |
1368c7878b0a493f44590f29af9b0f1b
|
|
| BLAKE2b-256 |
ef77da9fece5b9de4be97f96aa983d6d7e88e81403cd005caeb8fed983222d0a
|
File details
Details for the file discrete_dists-1.2.1-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl.
File metadata
- Download URL: discrete_dists-1.2.1-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl
- Upload date:
- Size: 362.0 kB
- Tags: PyPy, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
465132209867002adde12ab1f00b6ad3fabae933542aaf23e136c1948cae3ec8
|
|
| MD5 |
1db0fdc8438d6faba01bdd75dfd4870d
|
|
| BLAKE2b-256 |
7fed191a0f845950cec1210cf32cba45bbc5e9048019e1ee800153352a74b2e9
|
File details
Details for the file discrete_dists-1.2.1-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 186.7 kB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30d69766c933fa76b89f28d89a1185812fd1ecbe7e12d2c84a8aef83ab2b632b
|
|
| MD5 |
a050fa4f889a2df0168767fc3e93abb9
|
|
| BLAKE2b-256 |
72ee0b733bb47c3698b3804bc486480bb8185941db2c4c2e78fbb6f772d1851e
|
File details
Details for the file discrete_dists-1.2.1-cp314-cp314-win32.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp314-cp314-win32.whl
- Upload date:
- Size: 178.0 kB
- Tags: CPython 3.14, Windows x86
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdbd33969b4e89c57ca6300d51cb2224012a95bfdf761820a759af3a01238a70
|
|
| MD5 |
e595bd4ad5b040350462534098cc6b7d
|
|
| BLAKE2b-256 |
5d2524286fc757a89a65f9c5afebf9e9686594c760c78f0512a54f676bcc2875
|
File details
Details for the file discrete_dists-1.2.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 339.9 kB
- Tags: CPython 3.14, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83926000be81596ea57fb0dad4918c715546a637ded6b2fcb9fd4d6fc2a9326d
|
|
| MD5 |
2abffc087681428780b7b328f7d1a2a3
|
|
| BLAKE2b-256 |
1481c5a98537c688acb19c52c2106d90b31f72a6141b17cee3a0a57ff8f16772
|
File details
Details for the file discrete_dists-1.2.1-cp314-cp314-manylinux_2_5_i686.manylinux1_i686.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp314-cp314-manylinux_2_5_i686.manylinux1_i686.whl
- Upload date:
- Size: 358.3 kB
- Tags: CPython 3.14, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55e72158fd84f7465aa62b1e0d591e7108bfb403c06ca394870693d85be532c0
|
|
| MD5 |
2cee9f178004bbb44ce0aead361bcb6b
|
|
| BLAKE2b-256 |
94c9bc00ec9fd5a7478a1a20440e0396cb8a0d3038257a91b0483b3e58c93fdb
|
File details
Details for the file discrete_dists-1.2.1-cp314-cp314-macosx_11_0_arm64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp314-cp314-macosx_11_0_arm64.whl
- Upload date:
- Size: 297.6 kB
- Tags: CPython 3.14, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40f7580afd348b1f2ed120031f684fbc2ed680925eb7ca87883a853a655a6e86
|
|
| MD5 |
9d7f7ad7d3ab260b03dadaf2c4612031
|
|
| BLAKE2b-256 |
99e8c28208e5fdd4736b10e18da18588f9f198711be951746760355af4a580b2
|
File details
Details for the file discrete_dists-1.2.1-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 186.7 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1c9f9629b589a7b0e3471a930394c736f08bcf99ee240d92e87e7835656c6c9
|
|
| MD5 |
e3eea3214bb094db8479ba858744e2ff
|
|
| BLAKE2b-256 |
46bf16c5a0dd380f1130de69e9266251e20edbbefd5e8a6008067e7c673d552d
|
File details
Details for the file discrete_dists-1.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 340.5 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce668f6027fd5e1beaaa64731837f6891fa2489b55390992fd992f2dac582874
|
|
| MD5 |
7c1319a85b62090d3fc20ccb0a434241
|
|
| BLAKE2b-256 |
4acd061896d5c8da510a1926c6f5001dfc94d51cb3776cffe27d4d078cdc4b4a
|
File details
Details for the file discrete_dists-1.2.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl
- Upload date:
- Size: 358.0 kB
- Tags: CPython 3.13, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ecb4d82e00a66db634d20f635e9c453f0bb00c83ad24843587ebcd4f5cfb1af4
|
|
| MD5 |
42ebd560101e6fd9718fc965c7ce2551
|
|
| BLAKE2b-256 |
3a9783dc5ebd54f006f2334e003e5a8d019cbf5106e7f5aea42a79776b5271ae
|
File details
Details for the file discrete_dists-1.2.1-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 297.5 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52643b9ab0046e6b67f4783c9fe57d0aa548b368414c8ed7303eab3c518ee299
|
|
| MD5 |
a10e1925c3acad289eb95d18612b66e7
|
|
| BLAKE2b-256 |
48495fe13ddb1bb93d61c0460c280a841f1311a39b1d785c5d4290332dc6c640
|
File details
Details for the file discrete_dists-1.2.1-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 186.6 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03e70828bb6c444dd9841b92b597e37b9beb60a47b01cd2ad8b0318de8cc44b2
|
|
| MD5 |
35921307c39b2859b9a34f3cb59e45f3
|
|
| BLAKE2b-256 |
bcf3a95109856ebbdbff6112ac03ba6d127dbd41d87e28138f6190974fb728a9
|
File details
Details for the file discrete_dists-1.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 340.4 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d44166cb64f7bf8ee0a3f5b03f575c629c9316b5ff70f83b36d592153470de73
|
|
| MD5 |
48b77095658935f5896d0a41d0009c51
|
|
| BLAKE2b-256 |
756fc9785e8060695944b86e52701f046c0d3c316095bd96d5c5af23a0d9938f
|
File details
Details for the file discrete_dists-1.2.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl
- Upload date:
- Size: 358.1 kB
- Tags: CPython 3.12, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c41bdc9be6b985b8586f995ac962e31e7ba2e6d28f44e758773b8ea467626481
|
|
| MD5 |
376d64a7574f4e926bf22cb6eb6263bc
|
|
| BLAKE2b-256 |
1419c48b37c7e30585f8b6702d277d25735fdae939de6b46f57008285bd59d21
|
File details
Details for the file discrete_dists-1.2.1-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 297.3 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c90aaa9fc434f4e7909d2ed442faab7e309665d1016cc386cca09f3208940a0
|
|
| MD5 |
9f4bd88affad8c9ad451bb1f7d4b6b50
|
|
| BLAKE2b-256 |
93a24f420e59b63ed10c3f26dbf4cbef07f97f4e1546621abf1719642f48e417
|
File details
Details for the file discrete_dists-1.2.1-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 188.5 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5d9aec64f0ac7519f84c7b7a480eda1509f6ca16f6edd53f3ad4da21b8a0991
|
|
| MD5 |
3bf565412411780d8179711fdbf11f07
|
|
| BLAKE2b-256 |
fe6ab42f62d9bc2c37367cb3e1274871cdea209e99d9a29d67d7ed354849ba66
|
File details
Details for the file discrete_dists-1.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 345.1 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74b543c86e1b6dcaa3b0b392a3acb9a984610afe8dc2576c0593636d65b5dcf3
|
|
| MD5 |
9d4c00ff9ffec282f0866833319a329c
|
|
| BLAKE2b-256 |
71ad72a3f43cd4dd9c7313234ba8f61a97aa49c20e4cd6aad74ea625fc9aac4a
|
File details
Details for the file discrete_dists-1.2.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl
- Upload date:
- Size: 361.9 kB
- Tags: CPython 3.11, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
302f9da9b7446da12dda80056a2b1162801937566330b92ab7aa531c6150303a
|
|
| MD5 |
89d6f080af7d793de3ccff5fe03e0237
|
|
| BLAKE2b-256 |
df6fd23bcf6b36b5be366818ba77e46039973b031abb19f5fb3b155406112d09
|
File details
Details for the file discrete_dists-1.2.1-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 299.6 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69b9d5d64a76730752fc2ba6c90209747cc1fb98b40ef19739c53d77368bf03f
|
|
| MD5 |
d097c1afb9754f8977eeb5b149d67dd6
|
|
| BLAKE2b-256 |
c5bab2269a3100cd7e7d067ebd3cd68dabb06e1f9e1384327e3be29854d28f2c
|
File details
Details for the file discrete_dists-1.2.1-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 188.5 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1a7fd34e3e6e43c3775ad0a85539abdf788879e333b10c8d4674829d285d545
|
|
| MD5 |
282a81056caa0dac58f58c2aecaacd15
|
|
| BLAKE2b-256 |
908738f11126092d334ceb298de82ab24bf41ccb5f3e7ef9e64bcab3f19f2039
|
File details
Details for the file discrete_dists-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 345.3 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79f4469a67628552a0a3f825e6da4ea704f674263e672cc0bf52eb9aabd4411f
|
|
| MD5 |
075230c38660db1f6a55fb4c64ca9476
|
|
| BLAKE2b-256 |
604a45dba39131eaf48b36ab810eb06a6d66b254019365e6c7d628de6363233f
|
File details
Details for the file discrete_dists-1.2.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl.
File metadata
- Download URL: discrete_dists-1.2.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl
- Upload date:
- Size: 362.1 kB
- Tags: CPython 3.10, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9b8cda41685c1f7003630413eac8cb586a54a8579a65820fd066936eada6679
|
|
| MD5 |
f739ab371390256d20b3220d9869af93
|
|
| BLAKE2b-256 |
135fbfe8aff2a206701ba6fde0394d396ce64a15fdc2b9251fd694165942e610
|