An object-oriented approach to defining and combining random variables
Project description
RandCraft
RandCraft is a Python library for object-oriented combination and manipulation of univariate random variables, built on top of the scipy.stats module.
Usage Example
Have you ever wanted to add together random variables but can't be bothered working out an analytical solution? Randcraft makes it simple.
from randcraft import make_normal, make_coin_flip
coin_flip = make_coin_flip()
# <RandomVariable(discrete): mean=0.5, var=0.25>
norm = make_normal(mean=0, std_dev=0.2)
# <RandomVariable(normal): mean=0.0, var=0.04>
combined = coin_flip + norm
# <RandomVariable(mixture): mean=0.5, var=0.29>
combined.sample_one()
# 0.8678903828104276
combined.plot()
Features
- Object-oriented random variables: Wrap and combine distributions as Python objects.
- Distribution composition: Add, multiply, and transform random variables.
- Sampling and statistics: Easily sample from composed distributions and compute statistics.
- Extensible: Supports custom distributions via subclassing.
- Integration with scipy.stats: Use any frozen continuous distribution from scipy stats
Supported Distributions
RandCraft currently supports the following distributions:
- Normal, Uniform, Beta, Gamma + any other parametric continuous distribution from scipy.stats
- Discrete
- DiracDelta
- Gaussian kde distribution from provided observations
- Mixture distributions
- Anonymous distribution functions based on a provided sampler function
Distributions can all be combined arbitrarily with addition and subtraction. The library will simplify the new distribution analytically where possible, and use numerical approaches otherwise.
You can also extend RandCraft with your own custom distributions.
Installation
pip install randcraft
API Overview
make_normal,make_uniform...etc: Create a random variable- Addition subtraction with constants or other RVs:
+,- .sample_numpy(size): Draw samples.get_mean(),.get_variance(): Get statistics.cdf(x): Evaluate cdf at points.ppf(x): Evaluate inverse of cdf at points.plot(): Take a look at your distribution
More Examples
Combining dice rolls
from randcraft.constructors import make_die_roll
die = make_die_roll(sides=6)
# <RandomVariable(discrete): mean=3.5, var=2.92>
three_dice = die.multi_sample(3)
# <RandomVariable(discrete): mean=10.5, var=8.75>
three_dice.cdf(10.0)
# 0.5
three_dice.ppf(0.5)
# 10.0
Using arbitrary parametric continuous distribution from scipy.stats
from scipy.stats import uniform
from randcraft.constructors import make_scipy
rv = make_scipy(uniform, loc=1, scale=2)
# <RandomVariable(scipy-uniform): mean=2.0, var=0.333>
b = rv.scale(2.0)
# <RandomVariable(scipy-uniform): mean=4.0, var=1.33>
Kernel density estimation and combination
You have observations of two independent random variables. You want to use kernal density estimation to create continuous random variables for each and then add them together.
import numpy as np
from randcraft.observations import make_gaussian_kde
observations_a = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
observations_b = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0])
rv_a = make_gaussian_kde(observations=observations_a, bw_method=0.1)
# <RandomVariable(multi): mean=3.0, var=2.42>
rv_b = make_gaussian_kde(observations=observations_b)
# <RandomVariable(multi): mean=0.5, var=0.676>
rv_joined = rv_a + rv_b
# <RandomVariable(multi): mean=3.5, var=3.1>
Uses gaussian_kde by scipy.stats under the hood. You also have the option to pass arguments for gaussian_kde, or provide your own kernel as a RandomVariable.
Mixing continuous and discrete variables
You have observations of two independent random variables. You want to use kernal density estimation to create continuous random variables for each and then add them together.
from randcraft.constructors import make_normal, make_uniform, make_discrete
from randcraft.misc import mix_rvs
rv1 = make_normal(mean=0, std_dev=1)
# <RandomVariable(scipy-norm): mean=0.0, var=1.0>
rv2 = make_uniform(low=-1, high=1)
# <RandomVariable(scipy-uniform): mean=-0.0, var=0.333>
combined = rv1 + rv2
# <RandomVariable(multi): mean=0.0, var=1.33>
discrete = make_discrete(values=[1, 2, 3])
# <RandomVariable(discrete): mean=2.0, var=0.667>
# Make a new rv which has a random chance of drawing from one of the other 4 rvs
mixture = mix_rvs([rv1, rv2, combined, discrete])
# <RandomVariable(mixture): mean=0.5, var=1.58>
mixture.plot()
Extending RandCraft
You can create custom random variable classes by subclassing the base RV class and implementing required methods.
Known limitations
The library is designed to work with univariate random variables only. Multi-dimensional rvs or correlations etc are not supported.
License
MIT License
Acknowledgements
Built on scipy.stats.
Images
Double normal
Mixture
Images
Double normal
Mixture
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
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 randcraft-0.1.7.tar.gz.
File metadata
- Download URL: randcraft-0.1.7.tar.gz
- Upload date:
- Size: 185.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b3974653d915db5bdd7f7c6d7af4d596b7fcb80c36d376f0a0d026950a7ed11
|
|
| MD5 |
fce959ba4120a10ccad1b04e813faa10
|
|
| BLAKE2b-256 |
ae05be13eb21525b0e3c16f1077f4078a3c51951eb5ff86bc2e45639f7a36551
|
Provenance
The following attestation bundles were made for randcraft-0.1.7.tar.gz:
Publisher:
publish.yml on RobbieKiwi/RandCraft
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
randcraft-0.1.7.tar.gz -
Subject digest:
9b3974653d915db5bdd7f7c6d7af4d596b7fcb80c36d376f0a0d026950a7ed11 - Sigstore transparency entry: 561845050
- Sigstore integration time:
-
Permalink:
RobbieKiwi/RandCraft@c4b0c2635eaee15a5374ffa77cbad2f099562942 -
Branch / Tag:
refs/tags/v0.1.7 - Owner: https://github.com/RobbieKiwi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c4b0c2635eaee15a5374ffa77cbad2f099562942 -
Trigger Event:
release
-
Statement type:
File details
Details for the file randcraft-0.1.7-py3-none-any.whl.
File metadata
- Download URL: randcraft-0.1.7-py3-none-any.whl
- Upload date:
- Size: 24.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a1304c65b4d99c95a2037c0637e62d987aa1bfcbc7394cc2b80da926c2a6590
|
|
| MD5 |
02f3fb82ed4536a510a94bf85f6f148c
|
|
| BLAKE2b-256 |
9858816ad94d0cdf70c0fe08e7d6e9a2b2a3c1d66d4f2e3cb0e72825e55e036b
|
Provenance
The following attestation bundles were made for randcraft-0.1.7-py3-none-any.whl:
Publisher:
publish.yml on RobbieKiwi/RandCraft
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
randcraft-0.1.7-py3-none-any.whl -
Subject digest:
5a1304c65b4d99c95a2037c0637e62d987aa1bfcbc7394cc2b80da926c2a6590 - Sigstore transparency entry: 561845066
- Sigstore integration time:
-
Permalink:
RobbieKiwi/RandCraft@c4b0c2635eaee15a5374ffa77cbad2f099562942 -
Branch / Tag:
refs/tags/v0.1.7 - Owner: https://github.com/RobbieKiwi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c4b0c2635eaee15a5374ffa77cbad2f099562942 -
Trigger Event:
release
-
Statement type: