{ "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "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": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "from sympy import *\n", "from sympy.stats import *\n", "init_printing()" ], "metadata": {} }, { "source": [ "## Rolling dice" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "Let's roll two dices X and Y." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "X, Y = Die('X', 6), Die('Y', 6)" ], "metadata": {} }, { "source": [ "We can compute probabilities defined by equalities (with the Eq operator) or inequalities..." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "P(Eq(X, 3))" ], "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "P(X>3)" ], "metadata": {} }, { "source": [ "Conditions can also involve multiple random variables..." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "P(X>Y)" ], "metadata": {} }, { "source": [ "Conditional probabilities..." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "P(X+Y>6, X<5)" ], "metadata": {} }, { "source": [ "## Continuous random variables" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "We can also work with arbitrary discrete or continuous random variables." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "Z = Normal('Z', 0, 1) # Gaussian variable" ], "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "P(Z>pi)" ], "metadata": {} }, { "source": [ "We can compute expectancies and variances..." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "E(Z**2), variance(Z**2)" ], "metadata": {} }, { "source": [ "as well as densities." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "f = density(Z)" ], "metadata": {} }, { "source": [ "This is a lambda function, it can be evaluated on a SymPy symbol:" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "var('x')\n", "f(x)" ], "metadata": {} }, { "source": [ "We can plot this density." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%matplotlib inline\n", "plot(f(x), (x, -6, 6));" ], "metadata": {} }, { "source": [ "SymPy.stats works by using integrals and summations for computing probabilistic quantities. For example, P(Z>pi) is:" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "Eq(Integral(f(x), (x, pi, oo)), \n", " simplify(integrate(f(x), (x, pi, oo))))" ], "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)." ], "cell_type": "markdown", "metadata": {} } ], "metadata": {} } ], "metadata": { "name": "", "signature": "sha256:8d70e596876185b9a9dc98566e1eb7a4cf5807b5051fc8587e79fef95456269f" } }