Skip to main content
Latest Version License Python Versions https://github.com/hgrecco/rvmath/workflows/CI/badge.svg?branch=main https://github.com/hgrecco/rvmath/workflows/Lint/badge.svg?branch=main https://coveralls.io/repos/github/hgrecco/rvmath/badge.svg?branch=main

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

  1. Which distributions are supported by rvmath?

    All continuous distributions available in Scipy, you can read the list in the SciPy Stats docs.

  2. Do you plan tu support discrete distributions?

    Yes, hopefully in the next version.

  3. 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

Release files for rvmath 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rvmath 0.1
File Size Uploaded
rvmath-0.1.tar.gz 14.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rvmath 0.1
File Interpreter ABI Platform
rvmath-0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 26.9 kB

Release files / rvmath-0.1.tar.gz

Download URL rvmath-0.1.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a836f3df36b1e6bfd7841ae55470b89fde04e85d3318617010bbbf37141a7301
BLAKE2b-256 checksum
How to use checksums
3af548da025dca86a8089d85749df0dddc143203f8713d2a02de5d30a8ffcffe
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / rvmath-0.1-py2.py3-none-any.whl

Download URL rvmath-0.1-py2.py3-none-any.whl
Size 12.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
437899f75e316f862bd4fbd0168065c380a685511d21a24ea8063fb6fa23734b
BLAKE2b-256 checksum
How to use checksums
4cbf848af8695a2dd05466aaad80de7a8c5f157092a218a16467c529a40f5792
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page