{
 "metadata": {
  "name": "",
  "signature": "sha256:8f58b707cf76c184d840e41abdb14f1e584995252c4df05a742336b9f07b9b0b"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Discrete Bayes Animations"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "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='..')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<style>\n",
        "@import url('http://fonts.googleapis.com/css?family=Source+Code+Pro');\n",
        "@import url('http://fonts.googleapis.com/css?family=Vollkorn');\n",
        "@import url('http://fonts.googleapis.com/css?family=Arimo');\n",
        "\n",
        "    div.cell{\n",
        "        width: 850px;\n",
        "        margin-left: 0% !important;\n",
        "        margin-right: auto;\n",
        "    }\n",
        "    div.text_cell code {\n",
        "        background: transparent;\n",
        "        color: #000000;\n",
        "        font-weight: 600;\n",
        "        font-size: 11pt;\n",
        "        font-style: bold;\n",
        "        font-family:  'Source Code Pro', Consolas, monocco, monospace;\n",
        "   }\n",
        "    h1 {\n",
        "        font-family: 'Open sans',verdana,arial,sans-serif;\n",
        "\t}\n",
        "\t\n",
        "    div.input_area {\n",
        "        background: #F6F6F9;\n",
        "        border: 1px solid #586e75;\n",
        "    }\n",
        "\n",
        "    .text_cell_render h1 {\n",
        "        font-weight: 200;\n",
        "        font-size: 30pt;\n",
        "        line-height: 100%;\n",
        "        color:#c76c0c;\n",
        "        margin-bottom: 0.5em;\n",
        "        margin-top: 1em;\n",
        "        display: block;\n",
        "        white-space: wrap;\n",
        "    } \n",
        "    h2 {\n",
        "        font-family: 'Open sans',verdana,arial,sans-serif;\n",
        "    }\n",
        "    .text_cell_render h2 {\n",
        "        font-weight: 200;\n",
        "        font-size: 20pt;\n",
        "        font-style: italic;\n",
        "        line-height: 100%;\n",
        "        color:#c76c0c;\n",
        "        margin-bottom: 0.5em;\n",
        "        margin-top: 1.5em;\n",
        "        display: block;\n",
        "        white-space: nowrap;\n",
        "    } \n",
        "    h3 {\n",
        "        font-family: 'Open sans',verdana,arial,sans-serif;\n",
        "    }\n",
        "    .text_cell_render h3 {\n",
        "        font-weight: 300;\n",
        "        font-size: 18pt;\n",
        "        line-height: 100%;\n",
        "        color:#d77c0c;\n",
        "        margin-bottom: 0.5em;\n",
        "        margin-top: 2em;\n",
        "        display: block;\n",
        "        white-space: nowrap;\n",
        "    }\n",
        "    h4 {\n",
        "        font-family: 'Open sans',verdana,arial,sans-serif;\n",
        "    }\n",
        "    .text_cell_render h4 {\n",
        "        font-weight: 300;\n",
        "        font-size: 16pt;\n",
        "        color:#d77c0c;\n",
        "        margin-bottom: 0.5em;\n",
        "        margin-top: 0.5em;\n",
        "        display: block;\n",
        "        white-space: nowrap;\n",
        "    }\n",
        "    h5 {\n",
        "        font-family: 'Open sans',verdana,arial,sans-serif;\n",
        "    }\n",
        "    .text_cell_render h5 {\n",
        "        font-weight: 300;\n",
        "        font-style: normal;\n",
        "        color: #1d3b84;\n",
        "        font-size: 16pt;\n",
        "        margin-bottom: 0em;\n",
        "        margin-top: 1.5em;\n",
        "        display: block;\n",
        "        white-space: nowrap;\n",
        "    }\n",
        "    div.text_cell_render{\n",
        "        font-family: 'Arimo',verdana,arial,sans-serif;\n",
        "        line-height: 135%;\n",
        "        font-size: 125%;\n",
        "        width:750px;\n",
        "        margin-left:auto;\n",
        "        margin-right:auto;\n",
        "        text-align:justify;\n",
        "        text-justify:inter-word;\n",
        "    }\n",
        "    div.output_subarea.output_text.output_pyout {\n",
        "        overflow-x: auto;\n",
        "        overflow-y: scroll;\n",
        "        max-height: 50000px;\n",
        "    }\n",
        "    div.output_subarea.output_stream.output_stdout.output_text {\n",
        "        overflow-x: auto;\n",
        "        overflow-y: scroll;\n",
        "        max-height: 50000px;\n",
        "    }\n",
        "    code{\n",
        "      font-size: 70%;\n",
        "    }\n",
        "    .rendered_html code{\n",
        "    background-color: transparent;\n",
        "    }\n",
        "    ul{\n",
        "        margin: 2em;\n",
        "    }\n",
        "    ul li{\n",
        "        padding-left: 0.5em; \n",
        "        margin-bottom: 0.5em; \n",
        "        margin-top: 0.5em; \n",
        "    }\n",
        "    ul li li{\n",
        "        padding-left: 0.2em; \n",
        "        margin-bottom: 0.2em; \n",
        "        margin-top: 0.2em; \n",
        "    }\n",
        "    ol{\n",
        "        margin: 2em;\n",
        "    }\n",
        "    ol li{\n",
        "        padding-left: 0.5em; \n",
        "        margin-bottom: 0.5em; \n",
        "        margin-top: 0.5em; \n",
        "    }\n",
        "    ul li{\n",
        "        padding-left: 0.5em; \n",
        "        margin-bottom: 0.5em; \n",
        "        margin-top: 0.2em; \n",
        "    }\n",
        "    a:link{\n",
        "       font-weight: bold;\n",
        "       color:#447adb;\n",
        "    }\n",
        "    a:visited{\n",
        "       font-weight: bold;\n",
        "       color: #1d3b84;\n",
        "    }\n",
        "    a:hover{\n",
        "       font-weight: bold;\n",
        "       color: #1d3b84;\n",
        "    }\n",
        "    a:focus{\n",
        "       font-weight: bold;\n",
        "       color:#447adb;\n",
        "    }\n",
        "    a:active{\n",
        "       font-weight: bold;\n",
        "       color:#447adb;\n",
        "    }\n",
        "    .rendered_html :link {\n",
        "       text-decoration: underline; \n",
        "    }\n",
        "    .rendered_html :hover {\n",
        "       text-decoration: none; \n",
        "    }\n",
        "    .rendered_html :visited {\n",
        "      text-decoration: none;\n",
        "    }\n",
        "    .rendered_html :focus {\n",
        "      text-decoration: none;\n",
        "    }\n",
        "    .rendered_html :active {\n",
        "      text-decoration: none;\n",
        "    }\n",
        "    .warning{\n",
        "        color: rgb( 240, 20, 20 )\n",
        "    } \n",
        "    hr {\n",
        "      color: #f3f3f3;\n",
        "      background-color: #f3f3f3;\n",
        "      height: 1px;\n",
        "    }\n",
        "    blockquote{\n",
        "      display:block;\n",
        "      background: #fcfcfc;\n",
        "      border-left: 5px solid #c76c0c;\n",
        "      font-family: 'Open sans',verdana,arial,sans-serif;\n",
        "      width:680px;\n",
        "      padding: 10px 10px 10px 10px;\n",
        "      text-align:justify;\n",
        "      text-justify:inter-word;\n",
        "      }\n",
        "      blockquote p {\n",
        "        margin-bottom: 0;\n",
        "        line-height: 125%;\n",
        "        font-size: 100%;\n",
        "      }\n",
        "</style>\n",
        "<script>\n",
        "    MathJax.Hub.Config({\n",
        "                        TeX: {\n",
        "                           extensions: [\"AMSmath.js\"]\n",
        "                           },\n",
        "                tex2jax: {\n",
        "                    inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n",
        "                    displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ]\n",
        "                },\n",
        "                displayAlign: 'center', // Change this to 'center' to center equations.\n",
        "                \"HTML-CSS\": {\n",
        "                    styles: {'.MathJax_Display': {\"margin\": 4}}\n",
        "                }\n",
        "        });\n",
        "</script>\n"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "<IPython.core.display.HTML at 0x7f8ada8a3d30>"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "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",
     "collapsed": false,
     "input": [
      "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"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 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"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pos = [1.0,0,0,0,0,0,0,0,0,0]\n",
      "def bar_animate(nframe):\n",
      "    global pos\n",
      "    bar_plot(pos)\n",
      "    pos = predict(pos, 1, .8, .1, .1)\n",
      "\n",
      "\n",
      "fig = plt.figure(figsize=(6.5, 2.5))\n",
      "anim = animation.FuncAnimation(fig, bar_animate,\n",
      "                               frames=100, interval=75)\n",
      "anim.save('02_no_info.gif', writer='imagemagick')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAbgAAACsCAYAAAAJ+rmKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADrJJREFUeJzt3X+Q3Hddx/Hnq0krBMIVyVCEBgtyYKsihSPt8KNUjDZU\naZXpWCMZRxxKHU3EcdACf+AfjjLMwIBQ7ARsO+VX41hAw0haBkGggpWD/qA0cS6FYpNCodAupSCT\nyNs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olTVj6rqWXR/Ec5KcvaYIx2R5DeBb1bVjUzwFhLw/Ko6HXgJ8CfN7vNJshZ4NvD3VfVs\nukdev3a8keaX5ATgpcA/jTtLryQ/B/wZcArdPTGPTvLysYbqU1X7gDcBHwP2ADcyhheGx0LBLebD\n5hoiyfHAB4H3VdU/jzvPIM0uq38FZsadpcfzgPOa97iuBl6c5D1jzvQQVfX15v9vAR+mu4t/khwA\nDlTV55v1a+gW3iR6CfCF5ns5SWaAz1bVt6vqMPAhuo/PiVJVV1TVTFW9CLiP7rEUq+pYKLgjHzZv\nXlFdSPfD5VqkJAEuB26rqreNO898kmxIcmKz/Ejg1+i+6psIVfX6qtpYVU+hu9vqE1X1++PO1SvJ\nuiTrm+VHAb8OTNSRvVX1DeDOJE9vLtoMfHmMkQbZSvfFzKTZB5yZ5JHN7/ZmYNJ255Pk8c3/TwZ+\nmzHs6h12JpOxW+jD5mOO9ROSXA28CHhckjuBN1TVlWOO1ev5wDbgliQPlsbrquraMWbq9zPAVc1R\na8cB762qfxtzpkEmcdf5ScCHu895rAXeX1UfG2+kee0A3t+8YL2dCTw5RPMCYTMwce9jVtXNzd6D\nWbq7/b4IvGu8qeZ1TZLH0T0g5o+r6rurHWDiPyYgSdJyHAu7KCVJWjILTpLUShacJKmVLDhJUitZ\ncJKkVrLgJEmtZMFJklrp/wGkNvPGWyX77wAAAABJRU5ErkJggg==\n",
       "text": [
        "<matplotlib.figure.Figure at 0x7f8ada768cf8>"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<img src=\"02_no_info.gif\">"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "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",
      "    if nframe % 2 == 0:\n",
      "        pos = predict(pos, 1, .8, .1, .1)\n",
      "    else:\n",
      "        x = (nframe/2) % len(hallway)\n",
      "        z = hallway[x]\n",
      "        pos = update(pos, z, .6, .2)\n",
      "        \n",
      "\n",
      "fig = plt.figure(figsize=(6.5, 2.5))\n",
      "anim = animation.FuncAnimation(fig, bar_animate,\n",
      "                               frames=60, interval=75)\n",
      "anim.save('02_simulate.gif', writer='imagemagick')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAbgAAACsCAYAAAAJ+rmKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADXJJREFUeJzt3X2QXXddx/H3pwm1BMpW6FgkXSYokZRRbHlIOyAPSpSk\naquMY41UxypYR4M4jlrgD/zDGRn+YGSYYifUtlMUGocCGsYmZRBEEKwN9IHSrJMUdkwCVJ66DA91\nEvn6xz2Nl+1m7+7mbs7Nr+/XTCb3nPPL3s/sZvdzf79zzt1UFZIkteaMvgNIkrQaLDhJUpMsOElS\nkyw4SVKTLDhJUpMsOElSk0YWXJIbkzyY5LOLjHl7kgNJ7kly0XgjSpK0fEuZwd0EbD3RwSSXAs+s\nqo3A7wLXjSmbJEkrNrLgqurjwDcWGXIZcHM39g7gnCTnjSeeJEkrs3YMH2M9cGho+zBwPvDgIzvm\n5uZ8uxRJ0qqZmprK/H3jushk/ge20CRJvRpHwR0Bpoe2z+/2SZLUm3EsUe4GdgC7klwCPFRVD55o\n8NTU1BieUpL0WDc3N7fo8ZEFl+QW4KXAuUkOAX8OPA6gqnZW1W1JLk1yEPg2cNVJp5Yk6STlVPy6\nnOGLTJzBSZLGYXgGt5oXmUiSNFEsOElSkyw4SVKTLDhJUpMsOElSkyw4SVKTLDhJUpMsOElSkyw4\nSVKTLDhJUpMsOElSkyw4SVKTLDhJUpMsOElSkyw4SVKTLDhJUpMsOElSkyw4SVKTLDhJ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IAj6cZF+S1/QdZgHPAL6S5KYkn0lyfZJ1fYdaxK8B7+k7xLBudv5W4L8YXN3+UFV9uN9Uj3If\n8OIkT+6+vj/P4BayU+p0KLjJXkM9jXTLk7cCr+tmchOlqr5XVRcy+EZ4SZKX9RzpuCS/APx3Vd3F\nBM+QgBdV1UXANuAPuuXzSbIWeC7w11X1XAZXXr++30gLS3Im8IvAe/vOMizJjwJ/BGxgsBLzxCSv\n6jXUPFU1A7wF+BCwB7iLHl4Yng4Ft5SbzTVCkscB7wP+rqr+oe88i+mWrP4JeH7fWYa8ELisO8d1\nC/AzSd7Vc6ZHqaovdX9/BfgAgyX+SXIYOFxVd3bbtzIovEm0Dfh097mcJM8HPllVX6uqY8D7Gfz/\nnChVdWNVPb+qXgo8xOBailPqdCi44zebd6+ormBwc7mWKEmAG4D7q+ptfedZSJJzk5zTPX488LMM\nXvVNhKp6Y1VNV9UzGCxbfaSqfrPvXMOSrEtydvf4CcDPARN1ZW9VfRk4lOTHul1bgM/1GGkx2xm8\nmJk0M8AlSR7ffW9vASZtOZ8kP9T9/XTgl+lhqXfif6P3iW427znW90lyC/BS4ClJDgFvqqqbeo41\n7EXAlcC9SR4pjTdU1d4eM833w8DN3VVrZwB/W1X/3HOmxUzi0vl5wAcGP/NYC7y7qj7Ub6QFvRZ4\nd/eC9QEm8M0huhcIW4CJO49ZVfd0qwf7GCz7fQZ4Z7+pFnRrkqcwuCDm96vqm6c6wMTfJiBJ0kqc\nDkuUkiQtmwUnSWqSBSdJapIFJ0lqkgUnSWqSBSdJapIFJ0lq0v8ByVyjNXG2/lYAAAAASUVORK5C\nYII=\n",
       "text": [
        "<matplotlib.figure.Figure at 0x7f8ada701d30>"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      " <img src=\"02_simulate.gif\">"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}