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.
Metadata
Release files for discrete-dists 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| discrete_dists-1.2.1.tar.gz | 22.0 kB | Details |
Built distributions (wheels)
Total release size: 6.5 MB
Release files / discrete_dists-1.2.1.tar.gz
| Download URL | discrete_dists-1.2.1.tar.gz |
|---|---|
| Size | 22.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
24bb8b4b85999edd2989021d06ed947e482c3d59fd637b899815352e2e4cacfd
|
|
BLAKE2b-256 checksum How to use checksums |
4282201eec0dc60175d10f73a1d21afc2452e81d69252662e5ca431cfc16c341
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | discrete_dists-1.2.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 345.4 kB |
| Tags | Linux glibc 2.17+ x86-64 PyPy 3.11 PyPy 3.11 7.3 |
|
SHA-256 checksum How to use checksums |
6ff0217177f15e83ec7bbf7f84eb745f1981e8909c0ba86b012c59d08a655066
|
|
BLAKE2b-256 checksum How to use checksums |
ef77da9fece5b9de4be97f96aa983d6d7e88e81403cd005caeb8fed983222d0a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl
| Download URL | discrete_dists-1.2.1-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl |
|---|---|
| Size | 362.0 kB |
| Tags | Linux glibc 2.5+ x86-32 PyPy 3.11 PyPy 3.11 7.3 |
|
SHA-256 checksum How to use checksums |
465132209867002adde12ab1f00b6ad3fabae933542aaf23e136c1948cae3ec8
|
|
BLAKE2b-256 checksum How to use checksums |
7fed191a0f845950cec1210cf32cba45bbc5e9048019e1ee800153352a74b2e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp314-cp314-win_amd64.whl
| Download URL | discrete_dists-1.2.1-cp314-cp314-win_amd64.whl |
|---|---|
| Size | 186.7 kB |
| Tags | CPython 3.14 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
30d69766c933fa76b89f28d89a1185812fd1ecbe7e12d2c84a8aef83ab2b632b
|
|
BLAKE2b-256 checksum How to use checksums |
72ee0b733bb47c3698b3804bc486480bb8185941db2c4c2e78fbb6f772d1851e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp314-cp314-win32.whl
| Download URL | discrete_dists-1.2.1-cp314-cp314-win32.whl |
|---|---|
| Size | 178.0 kB |
| Tags | CPython 3.14 Windows x86-32 |
|
SHA-256 checksum How to use checksums |
cdbd33969b4e89c57ca6300d51cb2224012a95bfdf761820a759af3a01238a70
|
|
BLAKE2b-256 checksum How to use checksums |
5d2524286fc757a89a65f9c5afebf9e9686594c760c78f0512a54f676bcc2875
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | discrete_dists-1.2.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 339.9 kB |
| Tags | CPython 3.14 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
83926000be81596ea57fb0dad4918c715546a637ded6b2fcb9fd4d6fc2a9326d
|
|
BLAKE2b-256 checksum How to use checksums |
1481c5a98537c688acb19c52c2106d90b31f72a6141b17cee3a0a57ff8f16772
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp314-cp314-manylinux_2_5_i686.manylinux1_i686.whl
| Download URL | discrete_dists-1.2.1-cp314-cp314-manylinux_2_5_i686.manylinux1_i686.whl |
|---|---|
| Size | 358.3 kB |
| Tags | CPython 3.14 Linux glibc 2.5+ x86-32 |
|
SHA-256 checksum How to use checksums |
55e72158fd84f7465aa62b1e0d591e7108bfb403c06ca394870693d85be532c0
|
|
BLAKE2b-256 checksum How to use checksums |
94c9bc00ec9fd5a7478a1a20440e0396cb8a0d3038257a91b0483b3e58c93fdb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp314-cp314-macosx_11_0_arm64.whl
| Download URL | discrete_dists-1.2.1-cp314-cp314-macosx_11_0_arm64.whl |
|---|---|
| Size | 297.6 kB |
| Tags | CPython 3.14 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
40f7580afd348b1f2ed120031f684fbc2ed680925eb7ca87883a853a655a6e86
|
|
BLAKE2b-256 checksum How to use checksums |
99e8c28208e5fdd4736b10e18da18588f9f198711be951746760355af4a580b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp313-cp313-win_amd64.whl
| Download URL | discrete_dists-1.2.1-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 186.7 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
d1c9f9629b589a7b0e3471a930394c736f08bcf99ee240d92e87e7835656c6c9
|
|
BLAKE2b-256 checksum How to use checksums |
46bf16c5a0dd380f1130de69e9266251e20edbbefd5e8a6008067e7c673d552d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | discrete_dists-1.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 340.5 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
ce668f6027fd5e1beaaa64731837f6891fa2489b55390992fd992f2dac582874
|
|
BLAKE2b-256 checksum How to use checksums |
4acd061896d5c8da510a1926c6f5001dfc94d51cb3776cffe27d4d078cdc4b4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl
| Download URL | discrete_dists-1.2.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl |
|---|---|
| Size | 358.0 kB |
| Tags | CPython 3.13 Linux glibc 2.5+ x86-32 |
|
SHA-256 checksum How to use checksums |
ecb4d82e00a66db634d20f635e9c453f0bb00c83ad24843587ebcd4f5cfb1af4
|
|
BLAKE2b-256 checksum How to use checksums |
3a9783dc5ebd54f006f2334e003e5a8d019cbf5106e7f5aea42a79776b5271ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | discrete_dists-1.2.1-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 297.5 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
52643b9ab0046e6b67f4783c9fe57d0aa548b368414c8ed7303eab3c518ee299
|
|
BLAKE2b-256 checksum How to use checksums |
48495fe13ddb1bb93d61c0460c280a841f1311a39b1d785c5d4290332dc6c640
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp312-cp312-win_amd64.whl
| Download URL | discrete_dists-1.2.1-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 186.6 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
03e70828bb6c444dd9841b92b597e37b9beb60a47b01cd2ad8b0318de8cc44b2
|
|
BLAKE2b-256 checksum How to use checksums |
bcf3a95109856ebbdbff6112ac03ba6d127dbd41d87e28138f6190974fb728a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | discrete_dists-1.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 340.4 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
d44166cb64f7bf8ee0a3f5b03f575c629c9316b5ff70f83b36d592153470de73
|
|
BLAKE2b-256 checksum How to use checksums |
756fc9785e8060695944b86e52701f046c0d3c316095bd96d5c5af23a0d9938f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl
| Download URL | discrete_dists-1.2.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl |
|---|---|
| Size | 358.1 kB |
| Tags | CPython 3.12 Linux glibc 2.5+ x86-32 |
|
SHA-256 checksum How to use checksums |
c41bdc9be6b985b8586f995ac962e31e7ba2e6d28f44e758773b8ea467626481
|
|
BLAKE2b-256 checksum How to use checksums |
1419c48b37c7e30585f8b6702d277d25735fdae939de6b46f57008285bd59d21
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | discrete_dists-1.2.1-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 297.3 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
9c90aaa9fc434f4e7909d2ed442faab7e309665d1016cc386cca09f3208940a0
|
|
BLAKE2b-256 checksum How to use checksums |
93a24f420e59b63ed10c3f26dbf4cbef07f97f4e1546621abf1719642f48e417
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp311-cp311-win_amd64.whl
| Download URL | discrete_dists-1.2.1-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 188.5 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
c5d9aec64f0ac7519f84c7b7a480eda1509f6ca16f6edd53f3ad4da21b8a0991
|
|
BLAKE2b-256 checksum How to use checksums |
fe6ab42f62d9bc2c37367cb3e1274871cdea209e99d9a29d67d7ed354849ba66
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | discrete_dists-1.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 345.1 kB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
74b543c86e1b6dcaa3b0b392a3acb9a984610afe8dc2576c0593636d65b5dcf3
|
|
BLAKE2b-256 checksum How to use checksums |
71ad72a3f43cd4dd9c7313234ba8f61a97aa49c20e4cd6aad74ea625fc9aac4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl
| Download URL | discrete_dists-1.2.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl |
|---|---|
| Size | 361.9 kB |
| Tags | CPython 3.11 Linux glibc 2.5+ x86-32 |
|
SHA-256 checksum How to use checksums |
302f9da9b7446da12dda80056a2b1162801937566330b92ab7aa531c6150303a
|
|
BLAKE2b-256 checksum How to use checksums |
df6fd23bcf6b36b5be366818ba77e46039973b031abb19f5fb3b155406112d09
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | discrete_dists-1.2.1-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 299.6 kB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
69b9d5d64a76730752fc2ba6c90209747cc1fb98b40ef19739c53d77368bf03f
|
|
BLAKE2b-256 checksum How to use checksums |
c5bab2269a3100cd7e7d067ebd3cd68dabb06e1f9e1384327e3be29854d28f2c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp310-cp310-win_amd64.whl
| Download URL | discrete_dists-1.2.1-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 188.5 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
e1a7fd34e3e6e43c3775ad0a85539abdf788879e333b10c8d4674829d285d545
|
|
BLAKE2b-256 checksum How to use checksums |
908738f11126092d334ceb298de82ab24bf41ccb5f3e7ef9e64bcab3f19f2039
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | discrete_dists-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 345.3 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
79f4469a67628552a0a3f825e6da4ea704f674263e672cc0bf52eb9aabd4411f
|
|
BLAKE2b-256 checksum How to use checksums |
604a45dba39131eaf48b36ab810eb06a6d66b254019365e6c7d628de6363233f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / discrete_dists-1.2.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl
| Download URL | discrete_dists-1.2.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl |
|---|---|
| Size | 362.1 kB |
| Tags | CPython 3.10 Linux glibc 2.5+ x86-32 |
|
SHA-256 checksum How to use checksums |
e9b8cda41685c1f7003630413eac8cb586a54a8579a65820fd066936eada6679
|
|
BLAKE2b-256 checksum How to use checksums |
135fbfe8aff2a206701ba6fde0394d396ce64a15fdc2b9251fd694165942e610
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|