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

In [ ]:

```
from sympy import *
from sympy.stats import *
init_printing()
```

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 [ ]:

```
P(X>3)
```

Conditions can also involve multiple random variables...

In [ ]:

```
P(X>Y)
```

Conditional probabilities...

In [ ]:

```
P(X+Y>6, X<5)
```

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

In [ ]:

```
Z = Normal('Z', 0, 1) # Gaussian variable
```

In [ ]:

```
P(Z>pi)
```

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 [ ]:

```
var('x')
f(x)
```

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