Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a method to do arithmetic with SciPy's random variables?

SciPy's stats module have objects of the type "random variable" (they call it rv_frozen). It makes it easy to plot, say, cdf's of random variables of a given distribution. Here's a very simple example:

import scipy.stats as stats
n = stats.norm()
x = linspace(-3, 3)
y = n.cdf(x)
plot(x, y)

I wondered whether there's a way of doing basic arithmetic manipulations on such random variables. The following example is a wishful thinking (it doesn't work).

du_list = [stats.randint(2, 5) for _ in xrange(100)]
du_avg = sum(du_list) / len(du_list)
x = linspace(0, 10)
y = du_avg.cdf(x)
plot(x, y)

This wishful-thinking example should produce the graph of the cumulative distribution function of the random variable which is the average of 100 i.i.d. random variables, each is distributed uniformly on the set {2,3,4}.

like image 278
Bach Avatar asked Jun 24 '14 12:06

Bach


People also ask

How to generate bernoulli random variables in Python?

You can generate a bernoulli distributed discrete random variable using scipy. stats module's bernoulli. rvs() method which takes $p$ (probability of success) as a shape parameter. To shift distribution use the loc parameter.

Which of the following method is used to generate random numbers for a defined distribution available in Scipy stats module?

stats module is used to generate a random sample of any size from poisson distribution. The norm. rvs() method from the scipy. stats module can be used to generate a random sample of any size from Normal Distribution.


1 Answers

I realize this is a bit late, but I figured I'd answer in case anyone else needs this in the future. I needed the same functionality recently and even thought about extending scipy's rv_discrete to implement this, but then I found PaCAL.

PaCAL is a Python software package for doing arithmetic on random variables and it supports quite a few distributions, including continuous distributions. There is even some support for bivariate joint distributions. Available as a package on PyPI. Only for Python 2.x though.

EDIT: The PaCAL repo on Github now supports Python 3.x as well.

like image 129
dkasak Avatar answered Sep 23 '22 07:09

dkasak