Cleanup of Q material, and rename of FilterPy.

I renamed the functions that compute Q in FilterPy and had
not propogated that through the book.

I have renamed FilterPy from filterpy, and propogated that
change through the book.

Moved the material on SymPy from the short lived Appendix C
to Appendix A
This commit is contained in:
Roger Labbe 2014-11-16 09:41:41 -08:00
parent 9c0729d837
commit c1281bc47c
9 changed files with 582 additions and 725 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -261,7 +261,7 @@
"output_type": "pyout",
"prompt_number": 1,
"text": [
"<IPython.core.display.HTML at 0x7fe0265715c0>"
"<IPython.core.display.HTML at 0x7f4ddac347b8>"
]
}
],
@ -1033,7 +1033,7 @@
"source": [
"Now we can implement our filter. I have not yet designed $\\mathbf{R}$ and $\\mathbf{Q}$ which is required to get optimal performance. However, we have already covered a lot of confusing material and I want you to see concrete examples as soon as possible. Therefore I will use 'reasonable' values for $\\mathbf{R}$ and $\\mathbf{Q}$.\n",
"\n",
"The `filterpy` library provides the class `ExtendedKalmanFilter`. It works very similar to the `KalmanFilter` class we have been using, except that it allows you to provide functions that compute the Jacobian of $\\mathbf{H}$ and the function $h(\\mathbf{x})$. We have already written the code for these two functions, so let's just get going.\n",
"The `FilterPy` library provides the class `ExtendedKalmanFilter`. It works very similar to the `KalmanFilter` class we have been using, except that it allows you to provide functions that compute the Jacobian of $\\mathbf{H}$ and the function $h(\\mathbf{x})$. We have already written the code for these two functions, so let's just get going.\n",
"\n",
"We start by importing the filter and creating it. There are 3 variables in `x` and only 1 measurement. At the same time we will create our radar simulator.\n",
"\n",

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:d0f56611de4c723c8e7b5143ed3ccef719165500f23856ea8aeaaf28fcab3664"
"signature": "sha256:ac5d16f13341646cfbb0fb366e162a56628ec5cb0c19001da7a7fb23105214b0"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -615,7 +615,9 @@
" Xi[k+1] = X + U[k]\n",
" Xi[n+k+1] = X - U[k]\n",
"\n",
"The full listing from the `filterpy.kalman` library follows."
"The full listing from the `filterpy.kalman` library follows.\n",
"\n",
"(**author's note: FilterPy has been updated since this was written; it now uses an class to implement the Unscented Kalman filter, not standalone classes. Until I update this, refer to the test_ukf.py file in that library to see how to use the class**)"
]
},
{

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:c5c10c287a1d7f362dbed42c41eebc10b9bae86b387a0b10952a0824fcb7e359"
"signature": "sha256:5c842924509a6cfd6bb712a9a7e984f9b068e34de67e1bb6c963cb48309b6e72"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -277,7 +277,7 @@
"In one sentence the $H_\\infty$ (H infinity) filter is like a Kalman filter, but it is robust in the face of non-Gaussian, non-predictable inputs.\n",
"\n",
"\n",
"My filterpy library contains an H-Infinity filter. I've pasted some test code below which implements the filter designed by Simon in the article above. Hope it helps."
"My FilterPy library contains an H-Infinity filter. I've pasted some test code below which implements the filter designed by Simon in the article above. Hope it helps."
]
},
{

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:50cd0bf7b737693755e8062af84247f67e5b07744fa97aee1a0dc08618dec3f8"
"signature": "sha256:aa9b4df3b3595ff9a790185720fa062f01b70b83d6266449e2fba5baecb5af02"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -32,7 +32,7 @@
"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') # allow us to format the book\n",
"import book_format\n",
"book_format.load_style()"
],
@ -43,6 +43,8 @@
"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",
@ -129,9 +131,9 @@
" white-space: nowrap;\n",
" }\n",
" div.text_cell_render{\n",
" font-family: 'Open sans',verdana,arial,sans-serif;\n",
" font-family: 'Arimo',verdana,arial,sans-serif;\n",
" line-height: 135%;\n",
" font-size: 110%;\n",
" font-size: 125%;\n",
" width:750px;\n",
" margin-left:auto;\n",
" margin-right:auto;\n",
@ -141,12 +143,12 @@
" div.output_subarea.output_text.output_pyout {\n",
" overflow-x: auto;\n",
" overflow-y: scroll;\n",
" max-height: 300px;\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: 300px;\n",
" max-height: 50000px;\n",
" }\n",
" code{\n",
" font-size: 70%;\n",
@ -257,13 +259,13 @@
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"prompt_number": 10,
"text": [
"<IPython.core.display.HTML at 0x7f5e0d759a20>"
"<IPython.core.display.HTML at 0x7f51f8293748>"
]
}
],
"prompt_number": 1
"prompt_number": 10
},
{
"cell_type": "markdown",
@ -278,6 +280,310 @@
"Still, I know I have not downloaded some IPython Notebooks that are of interest to me. There is the free nbviewer.org site which will statically render a notebook that is hosted elsewhere. My book is hosted on github, so you can always read my book for free by going to \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## SymPy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"SymPy is a Python package for performing symbolic mathematics. The full scope of its abilities are beyond this book, but it can perform algebra, integrate and differentiate equations, find solutions to differential equations, and much more. For example, we use use to to compute the Jacobian of matrices and the expected value integral computations.\n",
"\n",
"First, a simple example. We will import SymPy, initialize its pretty print functionality (which will print equations using LaTeX). We will then declare a symbol for Numpy to use."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sympy\n",
"from sympy import init_printing\n",
"#from sympy.interactive import printing\n",
"init_printing(use_latex='mathjax')\n",
"\n",
"phi, x = sympy.symbols('\\phi, x')\n",
"phi"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\phi$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"\\phi"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice how we use a latex expression for the symbol `phi`. This is not necessary, but if you do it will render as LaTeX when output. Now let's do some math. What is the derivative of $\\sqrt{\\phi}$?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sympy.diff('sqrt(phi)')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\frac{1}{2 \\sqrt{\\phi}}$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
" 1 \n",
"\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n",
" ___\n",
"2\u22c5\u2572\u2571 \u03c6 "
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can factor equations"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sympy.factor(phi**3 -phi**2 + phi - 1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\left(\\phi - 1\\right) \\left(\\phi^{2} + 1\\right)$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
" \u239b 2 \u239e\n",
"(\\phi - 1)\u22c5\u239d\\phi + 1\u23a0"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and we can expand them."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"((phi+1)*(phi-4)).expand()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\phi^{2} - 3 \\phi - 4$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
" 2 \n",
"\\phi - 3\u22c5\\phi - 4"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find the value of an equation by substituting in a value for a variable"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"w =x**2 -3*x +4\n",
"w.subs(x,4)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$8$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"8"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also use strings for equations that use symbols that you have not defined"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = sympy.expand('(t+1)*2')\n",
"x"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$2 t + 2$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"2\u22c5t + 2"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's use SymPy to compute the Jacobian of a matrix. Let us have a function\n",
"\n",
"$$h=\\sqrt{(x^2 + z^2)}$$\n",
"\n",
"for which we want to find the Jacobian with respect to x, y, and z."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x, y, z = sympy.symbols('x y z')\n",
"\n",
"H = sympy.Matrix([sympy.sqrt(x**2 + z**2)])\n",
"\n",
"state = sympy.Matrix([x, y, z])\n",
"H.jacobian(state)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\left[\\begin{matrix}\\frac{x}{\\sqrt{x^{2} + z^{2}}} & 0 & \\frac{z}{\\sqrt{x^{2} + z^{2}}}\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"\u23a1 x z \u23a4\n",
"\u23a2\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 0 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u23a5\n",
"\u23a2 _________ _________\u23a5\n",
"\u23a2 \u2571 2 2 \u2571 2 2 \u23a5\n",
"\u23a3\u2572\u2571 x + z \u2572\u2571 x + z \u23a6"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's compute the discrete process noise matrix $\\mathbf{Q}_k$ given the continuous process noise matrix \n",
"$$\\mathbf{Q} = \\Phi_s \\begin{bmatrix}0&0&0\\\\0&0&0\\\\0&0&1\\end{bmatrix}$$\n",
"\n",
"and the equation\n",
"\n",
"$$\\mathbf{Q} = \\int_0^{\\Delta t} \\Phi(t)\\mathbf{Q}\\Phi^T(t) dt$$\n",
"\n",
"where \n",
"$$\\Phi(t) = \\begin{bmatrix}1 & \\Delta t & {\\Delta t}^2/2 \\\\ 0 & 1 & \\Delta t\\\\ 0& 0& 1\\end{bmatrix}$$"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dt = sympy.symbols('\\Delta{t}')\n",
"F_k = sympy.Matrix([[1, dt, dt**2/2],\n",
" [0, 1, dt],\n",
" [0, 0, 1]])\n",
"Q = sympy.Matrix([[0,0,0],\n",
" [0,0,0],\n",
" [0,0,1]])\n",
"\n",
"sympy.integrate(F_k*Q*F_k.T,(dt, 0, dt))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\left[\\begin{matrix}\\frac{\\Delta{t}^{5}}{20} & \\frac{\\Delta{t}^{4}}{8} & \\frac{\\Delta{t}^{3}}{6}\\\\\\frac{\\Delta{t}^{4}}{8} & \\frac{\\Delta{t}^{3}}{3} & \\frac{\\Delta{t}^{2}}{2}\\\\\\frac{\\Delta{t}^{3}}{6} & \\frac{\\Delta{t}^{2}}{2} & \\Delta{t}\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"\u23a1 5 4 3\u23a4\n",
"\u23a2\\Delta{t} \\Delta{t} \\Delta{t} \u23a5\n",
"\u23a2\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u23a5\n",
"\u23a2 20 8 6 \u23a5\n",
"\u23a2 \u23a5\n",
"\u23a2 4 3 2\u23a5\n",
"\u23a2\\Delta{t} \\Delta{t} \\Delta{t} \u23a5\n",
"\u23a2\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u23a5\n",
"\u23a2 8 3 2 \u23a5\n",
"\u23a2 \u23a5\n",
"\u23a2 3 2 \u23a5\n",
"\u23a2\\Delta{t} \\Delta{t} \u23a5\n",
"\u23a2\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \\Delta{t} \u23a5\n",
"\u23a3 6 2 \u23a6"
]
}
],
"prompt_number": 9
}
],
"metadata": {}

View File

@ -1,584 +0,0 @@
{
"metadata": {
"name": "",
"signature": "sha256:08d8da9b9be60564b5f4478ec019d413427884ab1107ce154b37e5411794ccd3"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Appendix A: Using NumPy, SciPy, and SymPy"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#format the book\n",
"%matplotlib inline\n",
"from __future__ import division, print_function\n",
"import matplotlib.pyplot as plt\n",
"import sys\n",
"sys.path.insert(0,'../code') # allow us to format the book\n",
"import book_format\n",
"book_format.load_style()"
],
"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": 1,
"text": [
"<IPython.core.display.HTML at 0x7fe692038b00>"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## NumPy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## SymPy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"SymPy is a Python package for performing symbolic mathematics. The full scope of its abilities are beyond this book, but it can perform algebra, integrate and differentiate equations, find solutions to differential equations, and much more. For example, we use use to to compute the Jacobian of matrices and the expected value integral computations.\n",
"\n",
"First, a simple example. We will import SymPy, initialize its pretty print functionality (which will print equations using LaTeX). We will then declare a symbol for Numpy to use."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sympy\n",
"from sympy import init_printing\n",
"#from sympy.interactive import printing\n",
"init_printing(use_latex='mathjax')\n",
"\n",
"phi, x = sympy.symbols('\\phi, x')\n",
"phi"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\phi$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"\\phi"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice how we use a latex expression for the symbol `phi`. This is not necessary, but if you do it will render as LaTeX when output. Now let's do some math. What is the derivative of $\\sqrt{\\phi}$?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sympy.diff('sqrt(phi)')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\frac{1}{2 \\sqrt{\\phi}}$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
" 1 \n",
"\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n",
" ___\n",
"2\u22c5\u2572\u2571 \u03c6 "
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can factor equations"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sympy.factor(phi**3 -phi**2 + phi - 1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\left(\\phi - 1\\right) \\left(\\phi^{2} + 1\\right)$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
" \u239b 2 \u239e\n",
"(\\phi - 1)\u22c5\u239d\\phi + 1\u23a0"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and we can expand them."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"((phi+1)*(phi-4)).expand()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\phi^{2} - 3 \\phi - 4$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
" 2 \n",
"\\phi - 3\u22c5\\phi - 4"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find the value of an equation by substituting in a value for a variable"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"w =x**2 -3*x +4\n",
"w.subs(x,4)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$8$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"8"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also use strings for equations that use symbols that you have not defined"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = sympy.expand('(t+1)*2')\n",
"x"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$2 t + 2$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": [
"2\u22c5t + 2"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's use SymPy to compute the Jacobian of a matrix. Let us have a function\n",
"\n",
"$$h=\\sqrt{(x^2 + z^2)}$$\n",
"\n",
"for which we want to find the Jacobian with respect to x, y, and z."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x, y, z = sympy.symbols('x y z')\n",
"\n",
"H = sympy.Matrix([sympy.sqrt(x**2 + z**2)])\n",
"\n",
"state = sympy.Matrix([x, y, z])\n",
"H.jacobian(state)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\left[\\begin{matrix}\\frac{x}{\\sqrt{x^{2} + z^{2}}} & 0 & \\frac{z}{\\sqrt{x^{2} + z^{2}}}\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
"\u23a1 x z \u23a4\n",
"\u23a2\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 0 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u23a5\n",
"\u23a2 _________ _________\u23a5\n",
"\u23a2 \u2571 2 2 \u2571 2 2 \u23a5\n",
"\u23a3\u2572\u2571 x + z \u2572\u2571 x + z \u23a6"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's compute the discrete process noise matrix $\\mathbf{Q}_k$ given the continuous process noise matrix \n",
"$$\\mathbf{Q} = \\Phi_s \\begin{bmatrix}0&0&0\\\\0&0&0\\\\0&0&1\\end{bmatrix}$$\n",
"\n",
"and the equation\n",
"\n",
"$$\\mathbf{Q} = \\int_0^{\\Delta t} \\Phi(t)\\mathbf{Q}\\Phi^T(t) dt$$\n",
"\n",
"where \n",
"$$\\Phi(t) = \\begin{bmatrix}1 & \\Delta t & {\\Delta t}^2/2 \\\\ 0 & 1 & \\Delta t\\\\ 0& 0& 1\\end{bmatrix}$$"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dt = sympy.symbols('\\Delta{t}')\n",
"F_k = sympy.Matrix([[1, dt, dt**2/2],\n",
" [0, 1, dt],\n",
" [0, 0, 1]])\n",
"Q = sympy.Matrix([[0,0,0],\n",
" [0,0,0],\n",
" [0,0,1]])\n",
"\n",
"sympy.integrate(F_k*Q*F_k.T,(dt, 0, dt))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\left[\\begin{matrix}\\frac{\\Delta{t}^{5}}{20} & \\frac{\\Delta{t}^{4}}{8} & \\frac{\\Delta{t}^{3}}{6}\\\\\\frac{\\Delta{t}^{4}}{8} & \\frac{\\Delta{t}^{3}}{3} & \\frac{\\Delta{t}^{2}}{2}\\\\\\frac{\\Delta{t}^{3}}{6} & \\frac{\\Delta{t}^{2}}{2} & \\Delta{t}\\end{matrix}\\right]$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"\u23a1 5 4 3\u23a4\n",
"\u23a2\\Delta{t} \\Delta{t} \\Delta{t} \u23a5\n",
"\u23a2\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u23a5\n",
"\u23a2 20 8 6 \u23a5\n",
"\u23a2 \u23a5\n",
"\u23a2 4 3 2\u23a5\n",
"\u23a2\\Delta{t} \\Delta{t} \\Delta{t} \u23a5\n",
"\u23a2\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u23a5\n",
"\u23a2 8 3 2 \u23a5\n",
"\u23a2 \u23a5\n",
"\u23a2 3 2 \u23a5\n",
"\u23a2\\Delta{t} \\Delta{t} \u23a5\n",
"\u23a2\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \\Delta{t} \u23a5\n",
"\u23a3 6 2 \u23a6"
]
}
],
"prompt_number": 12
}
],
"metadata": {}
}
]
}

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:39a1ae1c95d6ab87f2ac5d1b9bfa0a792d9077395fe5e39001f75bec21829a35"
"signature": "sha256:755bf581b54e38bcd9c1302e2d8fe89ef4f40acadf56df2f4f5157688eded1c0"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -401,7 +401,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"I am writing an open source bayesian filtering Python library called **filterpy**. It is available on github at (https://github.com/rlabbe/filterpy). To ensure that you have the latest release you will want to grab a copy from github, and follow your Python installation's instructions for adding it to the Python search path.\n",
"I am writing an open source bayesian filtering Python library called **FilterPy**. It is available on github at (https://github.com/rlabbe/filterpy). To ensure that you have the latest release you will want to grab a copy from github, and follow your Python installation's instructions for adding it to the Python search path.\n",
"\n",
"I have also made the project available on PyPi, the Python Package Index. I will be honest, I am not updating this as fast as I am changing the code in the library. That will change as the library and this book mature. To install from PyPi, at the command line issue the command\n",
"\n",

View File

@ -20,7 +20,7 @@ I periodically generate a PDF of the book from the Notebooks. I do not do this f
Companion Software
-----
All of the filters used in this book as well as others not in this book are implemented in my Python library filterpy, available [here](https://github/com/rlabbe/filterpy). You do not need to download or install this to read the book, but you will likely want to use this library to write your own filters. It includes Kalman filters, Fading Memory filters, H infinity filters, Extended and Unscented filters, least square filters, and many more. It also includes helper routines that simplify the designing the matrices used by some of the filters, and other code such as Kalman based smoothers.
All of the filters used in this book as well as others not in this book are implemented in my Python library FilterPy, available [here](https://github/com/rlabbe/filterpy). You do not need to download or install this to read the book, but you will likely want to use this library to write your own filters. It includes Kalman filters, Fading Memory filters, H infinity filters, Extended and Unscented filters, least square filters, and many more. It also includes helper routines that simplify the designing the matrices used by some of the filters, and other code such as Kalman based smoothers.
Downloading the book
@ -161,11 +161,11 @@ Particle filters uses a Monte Carlo technique to
description
* [**Appendix: Installation, Python, NumPy, and filterpy**](http://nbviewer.ipython.org/urls/raw.github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python/master/Appendix_A_Installation/Appendix_Installation.ipynb)
* [**Appendix: Installation, Python, NumPy, and FilterPy**](http://nbviewer.ipython.org/urls/raw.github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python/master/Appendix_A_Installation/Appendix_Installation.ipynb)
Brief introduction of Python and how it is used in this book. Description of the companion
library filterpy.
library FilterPy.
* [**Appendix: Symbols and Notations**](http://nbviewer.ipython.org/urls/raw.github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python/master/Appendix_B_Symbols_and_Notations/Appendix_Symbols_and_Notations.ipynb)
@ -173,7 +173,6 @@ library filterpy.
Symbols and notations used in this book. Comparison with notations used in the literature.
Installation and Software Requirements
-----
@ -193,7 +192,7 @@ Personally, I use the Anaconda Python distribution in all of my work, [available
Provided Libraries and Modules
-----
I am writing an open source bayesian filtering Python library called **filterpy**. It is available on github at (https://github.com/rlabbe/filterpy). To ensure that you have the latest release you will want to grab a copy from github, and follow your Python installation's instructions for adding it to the Python search path.
I am writing an open source bayesian filtering Python library called **FilterPy**. It is available on github at (https://github.com/rlabbe/filterpy). To ensure that you have the latest release you will want to grab a copy from github, and follow your Python installation's instructions for adding it to the Python search path.
I have also made the project available on PyPi, the Python Package Index. I will be honest, I am not updating this as fast as I am changing the code in the library. That will change as the library and this book mature. To install from PyPi, at the command line issue the command