{ "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": [ "# 13.3. Simulating a Brownian motion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Let's import NumPy and matplotlib." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. We will simulate Brownian motions with 5000 time steps." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n = 5000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. We simulate two independent one-dimensional Brownian processes to form a single two-dimensional Brownian process. The (discrete) Brownian motion makes independent Gaussian jumps at each time step (like a random walk). Therefore, we just have to compute the cumulative sum of independent normal random variables (one for each time step)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.cumsum(np.random.randn(n))\n", "y = np.cumsum(np.random.randn(n))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Now, to display the Brownian motion, we could just do `plot(x, y)`. However, the result would be monochromatic and a bit boring. We would like to use a gradient of color to illustrate the progression of the motion in time. Matplotlib forces us to use a small hack based on `scatter`. This function allows us to assign a different color to each point at the expense of dropping out line segments between points. To work around this issue, we interpolate linearly the process to give the illusion of a continuous line." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "k = 10 # We add 10 intermediary points between two \n", " # successive points.\n", "# We interpolate x and y.\n", "x2 = np.interp(np.arange(n*k), np.arange(n)*k, x)\n", "y2 = np.interp(np.arange(n*k), np.arange(n)*k, y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Now, we draw our points with a gradient of colors.\n", "plt.scatter(x2, y2, c=range(n*k), linewidths=0,\n", " marker='o', s=3, cmap=plt.cm.jet,)\n", "plt.axis('equal');\n", "plt.xticks([]); plt.yticks([]);" ] }, { "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 }