This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.

15.4. Computing exact probabilities and manipulating random variables

In [ ]:
from sympy import *
from sympy.stats import *

Rolling dice

Let's roll two dices X and Y.

In [ ]:
X, Y = Die('X', 6), Die('Y', 6)

We can compute probabilities defined by equalities (with the Eq operator) or inequalities...

In [ ]:
P(Eq(X, 3))
In [ ]:

Conditions can also involve multiple random variables...

In [ ]:

Conditional probabilities...

In [ ]:
P(X+Y>6, X<5)

Continuous random variables

We can also work with arbitrary discrete or continuous random variables.

In [ ]:
Z = Normal('Z', 0, 1)  # Gaussian variable
In [ ]:

We can compute expectancies and variances...

In [ ]:
E(Z**2), variance(Z**2)

as well as densities.

In [ ]:
f = density(Z)

This is a lambda function, it can be evaluated on a SymPy symbol:

In [ ]:

We can plot this density.

In [ ]:
%matplotlib inline
plot(f(x), (x, -6, 6));

SymPy.stats works by using integrals and summations for computing probabilistic quantities. For example, P(Z>pi) is:

In [ ]:
Eq(Integral(f(x), (x, pi, oo)), 
   simplify(integrate(f(x), (x, pi, oo))))

You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).

IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).