#!/usr/bin/env python # coding: utf-8 # > This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), 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 * init_printing() # ## 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[ ]: P(X>3) # Conditions can also involve multiple random variables... # In[ ]: P(X>Y) # 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[ ]: 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[ ]: get_ipython().run_line_magic('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](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages).