Utilities for randomly sampling from statistical distributions
Project description
Simple Random Distribution Sampling
SRDS is mainly a wrapper around scipy's statistical functions (scipy.stats). It makes it easier to sample from parameterized distributions and provides tools that accelerate random sampling.
Examples
Truncation or rejection sampling
srds adds several classes that make it easier to utilize scipy statistical distributions.
To sample from a log-normal distribution with σ = 0.25
, but truncate at 0.5
and 2
, the BoundRejectionSampler
helps:
from srds import ParameterizedDistribution as PDist, BoundRejectionSampler
dist = PDist.lognorm(0.25)
sampler = BoundRejectionSampler(dist, 0.5, 2)
x = sampler.sample(10)
Fast sampling of single values
calling dist.rvs
on a scipy statistical distribution is computationally expensive. This is problematic for code that
does something like:
# will be slow (calls dist.rvs 10000 times)
for i in range(10000):
x = dist.sample()
# ...
srds provides a BufferedSampler
that draws a larger sample from a distribution, and subsequently returns from that
sample.
from srds import BufferedSampler
dist = BufferedSampler(dist)
# will be much faster! (calls dist.rvs only 10 times with a sample size of 1k)
for i in range(10000):
x = dist.sample()
# ...
Sampling from populations
You can use the PopulationSampler
to draw from a discrete set, and also bias the sampling with weights.
from srds import PopulationSampler
sampler = PopulationSampler(['a', 'b', 'c'], [8, 1, 1])
sampler.sample() # will return 'a' 8 out of 10 times on average
sampler.sample(10) # returns a list containing items from ['a', 'b', 'c'] in random order
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 Distribution
File details
Details for the file srds-0.1.0.tar.gz
.
File metadata
- Download URL: srds-0.1.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 353301a06cc93687aea21ccd617c3787fa92e8d67e87c1fbe92667af80b43939 |
|
MD5 | 8f1d3b0ae46482f4e9c3435cb9c53fee |
|
BLAKE2b-256 | 1ad54190867931a2993cb009047d2ef6dc891a49d5b903689bc36c5a54158671 |
File details
Details for the file srds-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: srds-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 587d18796867d161348298b4de288700f91cd2b51f2e286812286820654833a0 |
|
MD5 | e10157cc8bfdf58947dfe532b9ff979f |
|
BLAKE2b-256 | 0250eae1e45eb34c2ebf827766518777846e7331ffbcd88534d4fb66f05201ed |