Proabilistic calculations. Build and evaluate mathematical expressions involving random variables.
Project description
rvmath: math with random variables, the easy way
rvmath is a Python package to build and evaluate mathematical expressions involving random variables.
Do you want to draw 10 values from a distribution resulting from a * cos(b + c) where a ~ Poisson, b ~ Uniform, and c ~ Normal? No problem:
>>> import rvmath as rvm
>>> z = rvm.poisson(mu=5) * np.cos(rvm.uniform() + rvm.norm())
>>> z.rvs(10)
It runs in Python 3.7+ depending on NumPy and SciPy. It is licensed under BSD.
It is extremely easy and natural to use:
>>> import rvmath as rvm
>>> x = rvm.uniform()
>>> y = rvm.uniform()
>>> z = x - y
>>> z.rvs(3) #doctest: +SKIP
[ 0.56791289 -0.1547692 -0.73984907]
>>> z.rvs(3) #doctest: +SKIP
[-0.33095289 -0.08664128 0.09938225]
Briefly, x and y are random variables drawn from a uniform distribution. z is a random variable drawn from a distribution obtained by subtracting two uniform distributions. z.rvs(3) draw 3 values from such distribution.
Behind the scenes, rvmath generate random variates of all random variables and perform all necessary calculations.
rvmath builds upon Scipy Stats and therefore all continuous distributions available there are also here, with the same name and arguments. rvs also follows the same API, namely:
size: int or tuple of ints, optional Defining number of random variates (default is 1).
random_state: None, int, RandomState, Generator, optional If seed is None the RandomState singleton is used. If seed is an int, a new RandomState instance is used, seeded with seed. If seed is already a RandomState or Generator instance, then that object is used. Default is None.
An important feature is that random variables have an identity and therefore the following code gives the expected result.
>>> w = x - x
>>> w.rvs(3)
[0., 0., 0.]
You can also use NumPy functions.
>>> c = np.cos(x)
>>> c.rvs(3)
Finally, you can convert the expression into a SciPy distribution:
>>> distro = c.to_distro(name="my_distro")
to obtain an object with useful methods such as rvs, pdf, cdf and others.
Quick Installation
To install rvmath, simply (soon):
$ pip install rvmath
and then simply enjoy it!
Other functionality
All rvmath objects have a few useful options and methods:
You can assign an id to the random variable:
>>> x = rvm.uniform(rvid="x")
to simplify debugging. If rvid is not given, a random string will be generated.
You can ask for dict containing all random variable names and their underlying SciPy distribution objects.
>>> x = rvm.uniform(rvid="x")
>>> y = rvm.norm(rvid="y")
>>> z = x + y
>>> dict(z.random_vars())
{'x': <scipy.stats._distn_infrastructure.rv_frozen at 0x7ff57f196220>,
'y': <scipy.stats._distn_infrastructure.rv_frozen at 0x7ff57e5a81f0>}
You can draw values from all random variables within an object.
>>> realization = z.draw(3)
>>> print(realization)
{'x': array([0.75633395, 0.99657116, 0.26853511]),
'y': array([-1.23407414, 0.5261816 , 2.62764828])}
and finally you can evaluate the object for this particular realization.
>>> z.eval(realization)
array([-0.47774019, 1.52275276, 2.89618339])
This is exactly what happens when rvs is called, but it can be particularly useful for debugging, testing and evaluating subexpressions.
In certain cases you might want to predefine the size of certain random variable. You can do it using the size parameter upon construction.
>>> z = np.sum(rvm.uniform(size=(3, 3))) * rvm.norm()
Random variables without an explicit size parameters will be evaluated to the size given when calling rvs.
Together with this feature, you can build distributions that depend on others:
>>> m = rvm.uniform(size=1)
>>> w = rvm.norm(loc=m)
You can combine distribution defined size with rvs provided size
>>> m = rvm.uniform(size=(None, 3, None)).rvs(2)
>>> m.shape
(2, 3, 2)
None values will be replaced by scalars provided to rvs.
FAQ
Which distributions are supported by rvmath?
All continuous distributions available in Scipy, you can read the list in the SciPy Stats docs.
Do you plan tu support discrete distributions?
Yes, hopefully in the next version.
I have built my own distribution subclassing ``rv_continuous``, can I use it with rvmath?
Yes, just call rvm.wrap(distro, *args, **kwargs)
rvmath is maintained by a community. See AUTHORS for a complete list.
To review an ordered list of notable changes for each version of a project, see CHANGES
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 rvmath-0.1.tar.gz
.
File metadata
- Download URL: rvmath-0.1.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a836f3df36b1e6bfd7841ae55470b89fde04e85d3318617010bbbf37141a7301 |
|
MD5 | ea3e4bd30b65a95974d488bf0a69b204 |
|
BLAKE2b-256 | 3af548da025dca86a8089d85749df0dddc143203f8713d2a02de5d30a8ffcffe |
File details
Details for the file rvmath-0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: rvmath-0.1-py2.py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 437899f75e316f862bd4fbd0168065c380a685511d21a24ea8063fb6fa23734b |
|
MD5 | 729043ac1398b60077befbe7bcfd955a |
|
BLAKE2b-256 | 4cbf848af8695a2dd05466aaad80de7a8c5f157092a218a16467c529a40f5792 |