Package: rvmath
rvmath: math with random variables, the easy wayrvmathis a Python package to build and evaluate
mathematical expressions involving random variables.Do you want to draw 10 values from a distribution resulting
froma * cos(b + c)wherea ~ Poisson,b ~ Uniform,
andc ~ Normal? No problem:>>>importrvmathasrvm>>>z=rvm.poisson(mu=5)*np.cos(rvm.uniform()+rvm.norm())>>>z.rvs(10)It runs in Python 3.7+ depending onNumPyandSciPy.
It is licensed under BSD.It is extremely easy and natural to use:>>>importrvmathasrvm>>>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.086641280.09938225]Briefly,xandyare random variables drawn from a uniform distribution.zis 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,rvmathgenerate random variates of all random variables
and perform all necessary calculations.rvmathbuilds uponScipy Statsand therefore all continuous distributions
available there are also here, with the same name and arguments.rvsalso 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 asrvs,pdf,cdfand others.Quick InstallationTo installrvmath, simply (soon):$pipinstallrvmathand then simply enjoy it!Other functionalityAllrvmathobjects have a few useful options and methods:You can assign an id to the random variable:>>>x=rvm.uniform(rvid="x")to simplify debugging. Ifrvidis 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_frozenat0x7ff57f196220>,'y':<scipy.stats._distn_infrastructure.rv_frozenat0x7ff57e5a81f0>}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 whenrvsis 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 thesizeparameter 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 callingrvs.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 withrvsprovided size>>>m=rvm.uniform(size=(None,3,None)).rvs(2)>>>m.shape(2,3,2)Nonevalues will be replaced by scalars provided torvs.FAQWhich distributions are supported by rvmath?All continuous distributions available in Scipy, you can read the
list in theSciPy Statsdocs.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 callrvm.wrap(distro, *args, **kwargs)rvmathis maintained by a community. SeeAUTHORSfor a complete list.To review an ordered list of notable changes for each version of a project,
seeCHANGES