2014-05-09 22:56:39 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-08-01 17:46:14 +02:00
|
|
|
"""Copyright 2015 Roger R Labbe Jr.
|
|
|
|
|
|
|
|
|
|
|
|
Code supporting the book
|
|
|
|
|
|
|
|
Kalman and Bayesian Filters in Python
|
|
|
|
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
|
|
|
|
|
|
|
|
|
2015-08-01 17:52:48 +02:00
|
|
|
This is licensed under an MIT license. See the LICENSE.txt file
|
2015-08-01 17:46:14 +02:00
|
|
|
for more information.
|
2014-05-09 22:56:39 +02:00
|
|
|
"""
|
|
|
|
|
2015-08-01 17:46:14 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function,
|
|
|
|
unicode_literals)
|
|
|
|
|
|
|
|
import filterpy.stats as stats
|
2014-05-09 22:56:39 +02:00
|
|
|
import math
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import numpy as np
|
|
|
|
|
2015-04-21 02:14:51 +02:00
|
|
|
def plot_height_std(x, lw=10):
|
|
|
|
m = np.mean(x)
|
|
|
|
s = np.std(x)
|
|
|
|
|
|
|
|
for i, height in enumerate(x):
|
|
|
|
plt.plot([i+1, i+1], [0, height], color='k', lw=lw)
|
|
|
|
plt.xlim(0,len(x)+1)
|
|
|
|
plt.axhline(m-s, ls='--')
|
|
|
|
plt.axhline(m+s, ls='--')
|
2015-07-09 03:42:12 +02:00
|
|
|
plt.fill_between((0, len(x)+1), m-s, m+s,
|
2015-04-21 02:14:51 +02:00
|
|
|
facecolor='yellow', alpha=0.4)
|
|
|
|
plt.xlabel('student')
|
|
|
|
plt.ylabel('height (m)')
|
|
|
|
plt.show()
|
|
|
|
|
2015-07-09 03:42:12 +02:00
|
|
|
|
2014-05-09 22:56:39 +02:00
|
|
|
def plot_gaussian (mu, variance,
|
|
|
|
mu_line=False,
|
|
|
|
xlim=None,
|
|
|
|
xlabel=None,
|
|
|
|
ylabel=None):
|
2014-06-23 03:02:43 +02:00
|
|
|
|
2014-05-09 22:56:39 +02:00
|
|
|
xs = np.arange(mu-variance*2,mu+variance*2,0.1)
|
2014-05-10 20:25:52 +02:00
|
|
|
ys = [stats.gaussian (x, mu, variance)*100 for x in xs]
|
2014-05-09 22:56:39 +02:00
|
|
|
plt.plot (xs, ys)
|
|
|
|
if mu_line:
|
|
|
|
plt.axvline(mu)
|
|
|
|
if xlim:
|
|
|
|
plt.xlim(xlim)
|
|
|
|
if xlabel:
|
|
|
|
plt.xlabel(xlabel)
|
|
|
|
if ylabel:
|
|
|
|
plt.ylabel(ylabel)
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
def display_stddev_plot():
|
|
|
|
xs = np.arange(10,30,0.1)
|
2015-07-28 06:50:03 +02:00
|
|
|
var = 8;
|
|
|
|
stddev = math.sqrt(var)
|
2014-05-10 20:25:52 +02:00
|
|
|
p2, = plt.plot (xs,[stats.gaussian(x, 20, var) for x in xs])
|
2014-05-09 22:56:39 +02:00
|
|
|
x = 20+stddev
|
2015-07-28 06:50:03 +02:00
|
|
|
# 1std vertical lines
|
2014-05-10 20:25:52 +02:00
|
|
|
y = stats.gaussian(x, 20, var)
|
2014-05-09 22:56:39 +02:00
|
|
|
plt.plot ([x,x], [0,y],'g')
|
|
|
|
plt.plot ([20-stddev, 20-stddev], [0,y], 'g')
|
2015-07-28 06:50:03 +02:00
|
|
|
|
|
|
|
#2std vertical lines
|
|
|
|
x = 20+2*stddev
|
|
|
|
y = stats.gaussian(x, 20, var)
|
|
|
|
plt.plot ([x,x], [0,y],'g')
|
|
|
|
plt.plot ([20-2*stddev, 20-2*stddev], [0,y], 'g')
|
|
|
|
|
2014-05-10 20:25:52 +02:00
|
|
|
y = stats.gaussian(20,20,var)
|
2014-05-09 22:56:39 +02:00
|
|
|
plt.plot ([20,20],[0,y],'b')
|
2015-07-28 06:50:03 +02:00
|
|
|
|
|
|
|
x = 20+stddev
|
2014-05-09 22:56:39 +02:00
|
|
|
ax = plt.axes()
|
|
|
|
ax.annotate('68%', xy=(20.3, 0.045))
|
|
|
|
ax.annotate('', xy=(20-stddev,0.04), xytext=(x,0.04),
|
|
|
|
arrowprops=dict(arrowstyle="<->",
|
|
|
|
ec="r",
|
|
|
|
shrinkA=2, shrinkB=2))
|
2015-07-28 06:50:03 +02:00
|
|
|
ax.annotate('95%', xy=(20.3, 0.02))
|
|
|
|
ax.annotate('', xy=(20-2*stddev,0.015), xytext=(20+2*stddev,0.015),
|
|
|
|
arrowprops=dict(arrowstyle="<->",
|
|
|
|
ec="r",
|
|
|
|
shrinkA=2, shrinkB=2))
|
|
|
|
|
|
|
|
|
|
|
|
ax.xaxis.set_ticks ([20-2*stddev, 20-stddev, 20, 20+stddev, 20+2*stddev])
|
|
|
|
ax.xaxis.set_ticklabels(['$-2\sigma$', '$-1\sigma$','$\mu$','$1\sigma$', '$2\sigma$'])
|
2014-05-09 22:56:39 +02:00
|
|
|
ax.yaxis.set_ticks([])
|
2015-07-28 06:50:03 +02:00
|
|
|
ax.grid(None, 'both', lw=0)
|
2014-05-09 22:56:39 +02:00
|
|
|
plt.show()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
display_stddev_plot()
|