{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Discrete Bayes Animations" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "ename": "AttributeError", "evalue": "module 'book_format' has no attribute 'load_style'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minsert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'../code'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mbook_format\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mbook_format\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload_style\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdirectory\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'..'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m: module 'book_format' has no attribute 'load_style'" ] } ], "source": [ "from __future__ import division, print_function\n", "import matplotlib.pyplot as plt\n", "import sys\n", "sys.path.insert(0,'..') # allow us to format the book\n", "sys.path.insert(0,'../code') \n", "import book_format\n", "book_format.load_style(directory='..')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook creates the animations for the Discrete Bayesian filters chapter. It is not really intended to be a readable part of the book, but of course you are free to look at the source code, and even modify it. However, if you are interested in running your own animations, I'll point you to the examples subdirectory of the book, which contains a number of python scripts that you can run and modify from an IDE or the command line. This module saves the animations to GIF files, which is quite slow and not very interactive. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'book_plots'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mbook_plots\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mbar_plot\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun_line_magic\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'matplotlib'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'inline'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'book_plots'" ] } ], "source": [ "from matplotlib import animation\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from book_plots import bar_plot\n", "%matplotlib inline\n", "\n", "# the predict algorithm of the discrete bayesian filter\n", "def predict(pos, move, p_correct, p_under, p_over):\n", " n = len(pos)\n", " result = np.array(pos, dtype=float)\n", " for i in range(n):\n", " result[i] = \\\n", " pos[(i-move) % n] * p_correct + \\\n", " pos[(i-move-1) % n] * p_over + \\\n", " pos[(i-move+1) % n] * p_under \n", " return result\n", "\n", "\n", "def normalize(p):\n", " s = sum(p)\n", " for i in range (len(p)):\n", " p[i] = p[i] / s\n", " \n", "# the update algorithm of the discrete bayesian filter\n", "def update(pos, measure, p_hit, p_miss):\n", " q = np.array(pos, dtype=float)\n", " for i in range(len(hallway)):\n", " if hallway[i] == measure:\n", " q[i] = pos[i] * p_hit\n", " else:\n", " q[i] = pos[i] * p_miss\n", " normalize(q)\n", " return q" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\matplotlibrc\n" ] }, { "data": { "text/plain": [ "'ffmpeg'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import matplotlib\n", "# make sure our matplotlibrc has been edited to use imagemagick\n", "print(matplotlib.matplotlib_fname())\n", "matplotlib.rcParams['animation.writer']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'gif_animate'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mgif_animate\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0manimate\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mpos\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1.0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mbar_animate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnframe\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mglobal\u001b[0m \u001b[0mpos\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'gif_animate'" ] } ], "source": [ "from gif_animate import animate\n", "\n", "pos = [1.0,0,0,0,0,0,0,0,0,0]\n", "def bar_animate(nframe):\n", " global pos\n", " plt.cla()\n", " bar_plot(pos)\n", " plt.title('Step {}'.format(nframe + 1))\n", " pos = predict(pos, 1, .8, .1, .1)\n", "\n", "for i in range(10):\n", " bar_animate(i)\n", "\n", "fig = plt.figure(figsize=(6.5, 2.5))\n", "animate('02_no_info.gif', bar_animate, fig=fig, frames=75, interval=75);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'animate' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[0mfig\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfigure\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfigsize\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m6.5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2.5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[0manimate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'02_simulate.gif'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbar_animate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfig\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfig\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mframes\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m40\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minterval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m85\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'animate' is not defined" ] }, { "data": { "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pos = np.array([.1]*10)\n", "hallway = np.array([1, 1, 0, 0, 0, 0, 0, 0, 1, 0])\n", "\n", "def bar_animate(nframe):\n", " global pos\n", " #if nframe == 0:\n", " # return\n", "\n", " bar_plot(pos, ylim=(0,1.0))\n", " plt.title('Step {}'.format(nframe + 1))\n", " if nframe % 2 == 0:\n", " pos = predict(pos, 1, .9, .05, .05)\n", " else:\n", " x = int((nframe/2) % len(hallway))\n", " z = hallway[x]\n", " pos = update(pos, z, .9, .2)\n", " \n", "\n", "fig = plt.figure(figsize=(6.5, 2.5))\n", "animate('02_simulate.gif', bar_animate, fig=fig, frames=40, interval=85);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " " ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "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.6.10" } }, "nbformat": 4, "nbformat_minor": 0 }