{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> 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.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 15.4. Computing exact probabilities and manipulating random variables" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sympy import *\n", "from sympy.stats import *\n", "init_printing()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rolling dice" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's roll two dices X and Y." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "X, Y = Die('X', 6), Die('Y', 6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can compute probabilities defined by equalities (with the Eq operator) or inequalities..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "P(Eq(X, 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "P(X>3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conditions can also involve multiple random variables..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "P(X>Y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conditional probabilities..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "P(X+Y>6, X<5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Continuous random variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also work with arbitrary discrete or continuous random variables." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Z = Normal('Z', 0, 1) # Gaussian variable" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "P(Z>pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can compute expectancies and variances..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "E(Z**2), variance(Z**2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "as well as densities." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f = density(Z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a lambda function, it can be evaluated on a SymPy symbol:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "var('x')\n", "f(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can plot this density." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "plot(f(x), (x, -6, 6));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SymPy.stats works by using integrals and summations for computing probabilistic quantities. For example, P(Z>pi) is:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Eq(Integral(f(x), (x, pi, oo)), \n", " simplify(integrate(f(x), (x, pi, oo))))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }