diff --git a/Kalman_Filters.ipynb b/Kalman_Filters.ipynb index 8ef48c7..9884eda 100644 --- a/Kalman_Filters.ipynb +++ b/Kalman_Filters.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:b72e534a09c6090f7e829f0c4e13e0eff328f3c0e33ffdf08e55b7542c32ed9b" + "signature": "sha256:b52c7ef7a1a3bbbe84133e8c8ff6477ee050c6647736e3e9a5955927786e1f71" }, "nbformat": 3, "nbformat_minor": 0, @@ -249,7 +249,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Kalman Filters\n", + "## One Dimensional Kalman Filters\n", "Now that we understand the histogram filter and Gaussians we are prepared to implement a 1D Kalman filter. We will do this exactly as we did the histogram filter - rather than going into the theory we will just develop the code step by step. But first, let's set the book style." ] }, @@ -261,7 +261,7 @@ "\n", "As in the histogram chapter we will be tracking a dog in a long hallway at work. However, in our latest hackathon someone created an RFID tracker that provides a reasonable accurate position for our dog. Suppose the hallway is 100m long. The sensor returns the distance of the dog from the left end of the hallway. So, 23.4 would mean the dog is 23.4 meters from the left end of the hallway.\n", "\n", - "Naturally, the sensor is not perfect. A reading of 23.4 could correspond to a real position of 23.7, or 23.0. However, it is very unlikely to correspond to a real position of say 47.6. Testing during the hackathon confirmed this result - the sensor is accurate, and while it had errors, the errors are small.\n", + "Naturally, the sensor is not perfect. A reading of 23.4 could correspond to a real position of 23.7, or 23.0. However, it is very unlikely to correspond to a real position of say 47.6. Testing during the hackathon confirmed this result - the sensor is reasonably accurate, and while it had errors, the errors are small. Futhermore, the errors seemed to be evenly distributed on both sides of the measurement; a true position of 23m would be equally likely to be measured as 22.9 as 23.1.\n", "\n", "Implementing and/or robustly modelling an RFID system is beyond the scope of this book, so we will write a very simple model. We will start with a simulation of the dog moving from left to right at a constant speed with some random noise added. " ] @@ -418,6 +418,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "\n", "> **Note**: numpy uses a random number generator to generate the normal distribution samples. The numbers I see as I write this are unlikely to be the ones that you see. If you run the cell above multiple times, you should get a slightly different result each time. I could use *numpy.random.seed(some_value)* to force the results to be the same each time. This would simplify my explanations in some cases, but would ruin the interactive nature of this chapter. To get a real feel for how normal distributions and Kalman filters work you will probably want to run cells several times, observing what changes, and what stays roughly the same.\n", "\n", "So the output of the sensor should be a wavering blue line drawn over a dotted red line. The dotted red line shows the actual position of the dog, and the blue line is the noise signal produced by the simulated RFID sensor. Please note that the red dotted line was manually plotted - we do not yet have a filter that recovers that information! \n", @@ -743,7 +744,40 @@ "source": [ "This result bothered me quite a bit when I first learned it. If my first measurement was 10, and the next one was 50, why would I choose 30 as a result? And why would I be *more* confident? Doesn't it make sense that either one of the measurements is wrong, or that I am measuring a moving object? Shouldn't the result be nearer 50? And, shouldn't the variance be larger, not smaller?\n", "\n", - "Well, no. It will become clearer soon, but recall our discrete Bayesian filter. It had two steps: *sense*, which incorporated the new measurement, and then *update*, which incorporated the movement. In the chart above we don't have any movement information. For now, trust me that very shortly we will learn how to incorporate that information. In the meantime, reflect on the fact that if we have 2 measurements with known variance, this is the only possible result, because we do not have any information (yet) as to whether the object is moving or that one sensor might be malfunctioning. As with the discrete Bayes filter, this result just reflects our current knowledge, and this is all we know. Trust the math!" + "Well, no. Recall the g-h filter chapter. In that chapter we agreed that if I weighed myself on two scales, and the first read 160lbs while the second read 170lbs, and both were equally accurate, the best estimate was 165lbs. Futhermore I should be a bit more confident about 165lbs vs 160lbs or 170lbs because I know have two readings, both near this estimate, increasing my confidence that neither is wildly wrong. \n", + "\n", + "Let's look at the math again to convince ourselves that the physical interpretation of the Gaussian equations makes sense.\n", + "\n", + "$$\n", + "\\mu=\\frac{\\sigma_1^2 \\mu_2 + \\sigma_2^2 \\mu_1} {\\sigma_1^2 + \\sigma_2^2}\n", + "$$\n", + "\n", + "If both scales have the same accuracy, then $\\sigma_1^2 = \\sigma_2^2$, and the resulting equation is\n", + "\n", + "$$\\mu=\\frac{\\mu_1 + \\mu_2}{2}$$\n", + "\n", + "which is just the average of the two weighings. If we look at the extreme cases, assume the first scale is very much more accurate than than the second one. At the limit, we can set \n", + "$\\sigma_1^2=0$, yielding\n", + "\n", + "$$\n", + "\\begin{align*}\n", + "\\mu&=\\frac{0*\\mu_2 + \\sigma_2^2 \\mu_1} { \\sigma_2^2}, \\\\\n", + "\\text{or just}\\\\\n", + "\\mu&=\\mu_1\n", + "\\end{align*}\n", + "$$\n", + "\n", + "Finally, if we set $\\sigma_1^2 = 9\\sigma_2^2$, then the resulting equation is\n", + "\n", + "$$\n", + "\\begin{align*}\n", + "\\mu&=\\frac{9 \\sigma_2^2 \\mu_2 + \\sigma_2^2 \\mu_1} {9 \\sigma_2^2 + \\sigma_2^2} \\\\\n", + "\\text{or just}\\\\\n", + "\\mu&= \\frac{1}{10} \\mu_1 + \\frac{9}{10} \\mu_2\n", + "\\end{align*}\n", + "$$\n", + "\n", + "This again fits our physical intuition of favoring the second, accurate scale over the first, inaccurate scale." ] }, {