Files
Kalman-and-Bayesian-Filters…/animations/Gaussians_Animations.ipynb
2018-07-14 11:45:39 -07:00

26 KiB

In [1]:
from __future__ import division, print_function
%matplotlib inline
import sys
sys.path.insert(0,'..') # allow us to format the book
sys.path.insert(0,'../code') 
# use same formatting as rest of book so that the plots are
# consistant with that look and feel.
import book_format
book_format.load_style(directory='..')
Out[1]:
<style> @import url('http://fonts.googleapis.com/css?family=Source+Code+Pro'); @import url('http://fonts.googleapis.com/css?family=Vollkorn'); @import url('http://fonts.googleapis.com/css?family=Arimo'); div.cell{ width: 850px; margin-left: 0% !important; margin-right: auto; } div.text_cell code { background: transparent; color: #000000; font-weight: 600; font-size: 11pt; font-style: bold; font-family: 'Source Code Pro', Consolas, monocco, monospace; } h1 { font-family: 'Open sans',verdana,arial,sans-serif; } div.input_area { background: #F6F6F9; border: 1px solid #586e75; } .text_cell_render h1 { font-weight: 200; font-size: 30pt; line-height: 100%; color:#c76c0c; margin-bottom: 0.5em; margin-top: 1em; display: block; white-space: wrap; } h2 { font-family: 'Open sans',verdana,arial,sans-serif; } .text_cell_render h2 { font-weight: 200; font-size: 16pt; font-style: italic; line-height: 100%; color:#c76c0c; margin-bottom: 0.5em; margin-top: 1.5em; display: inline; white-space: wrap; } h3 { font-family: 'Open sans',verdana,arial,sans-serif; } .text_cell_render h3 { font-weight: 200; font-size: 14pt; line-height: 100%; color:#d77c0c; margin-bottom: 0.5em; margin-top: 2em; display: block; white-space: nowrap; } h4 { font-family: 'Open sans',verdana,arial,sans-serif; } .text_cell_render h4 { font-weight: 100; font-size: 14pt; color:#d77c0c; margin-bottom: 0.5em; margin-top: 0.5em; display: block; white-space: nowrap; } h5 { font-family: 'Open sans',verdana,arial,sans-serif; } .text_cell_render h5 { font-weight: 200; font-style: normal; color: #1d3b84; font-size: 16pt; margin-bottom: 0em; margin-top: 0.5em; display: block; white-space: nowrap; } div.text_cell_render{ font-family: 'Arimo',verdana,arial,sans-serif; line-height: 125%; font-size: 120%; width:740px; margin-left:auto; margin-right:auto; text-align:justify; text-justify:inter-word; } div.output_subarea.output_text.output_pyout { overflow-x: auto; overflow-y: scroll; max-height: 50000px; } div.output_subarea.output_stream.output_stdout.output_text { overflow-x: auto; overflow-y: scroll; max-height: 50000px; } div.output_wrapper{ margin-top:0.2em; margin-bottom:0.2em; } code{ font-size: 70%; } .rendered_html code{ background-color: transparent; } ul{ margin: 2em; } ul li{ padding-left: 0.5em; margin-bottom: 0.5em; margin-top: 0.5em; } ul li li{ padding-left: 0.2em; margin-bottom: 0.2em; margin-top: 0.2em; } ol{ margin: 2em; } ol li{ padding-left: 0.5em; margin-bottom: 0.5em; margin-top: 0.5em; } ul li{ padding-left: 0.5em; margin-bottom: 0.5em; margin-top: 0.2em; } a:link{ font-weight: bold; color:#447adb; } a:visited{ font-weight: bold; color: #1d3b84; } a:hover{ font-weight: bold; color: #1d3b84; } a:focus{ font-weight: bold; color:#447adb; } a:active{ font-weight: bold; color:#447adb; } .rendered_html :link { text-decoration: underline; } .rendered_html :hover { text-decoration: none; } .rendered_html :visited { text-decoration: none; } .rendered_html :focus { text-decoration: none; } .rendered_html :active { text-decoration: none; } .warning{ color: rgb( 240, 20, 20 ) } hr { color: #f3f3f3; background-color: #f3f3f3; height: 1px; } blockquote{ display:block; background: #fcfcfc; border-left: 5px solid #c76c0c; font-family: 'Open sans',verdana,arial,sans-serif; width:680px; padding: 10px 10px 10px 10px; text-align:justify; text-justify:inter-word; } blockquote p { margin-bottom: 0; line-height: 125%; font-size: 100%; } </style> <script> MathJax.Hub.Config({ TeX: { extensions: ["AMSmath.js"] }, tex2jax: { inlineMath: [ ['$','$'], ["\\(","\\)"] ], displayMath: [ ['$$','$$'], ["\\[","\\]"] ] }, displayAlign: 'center', // Change this to 'center' to center equations. "HTML-CSS": { scale:85, styles: {'.MathJax_Display': {"margin": 4}} } }); </script>
In [2]:
import numpy as np
import matplotlib.pyplot as plt
from gif_animate import animate
from stats import gaussian
import matplotlib.pylab as pylab
from matplotlib import animation
import matplotlib

def plt_g (mu, variance):
    xs = np.arange(2,8,0.05)
    ys = [gaussian (x, mu, variance) for x in xs]
    plt.plot (xs, ys)
    plt.ylim((0,1))


mu = 2
sigma = 0.6
def ganimate(frame):
    global mu, sigma
    if frame < 25:
        mu += .2
    elif frame == 25:
        mu = 5
    elif frame < 37:
        sigma -= 0.05
    else:
        sigma += 0.05

    plt.cla()
    plt_g(mu,sigma)

animate('04_gaussian_animate.gif', ganimate, 
        frames=80, interval=50, figsize=(6,4))
No description has been provided for this image
In [2]:
 
No description has been provided for this image