• No results found

More examples

In document Kalman and Bayesian Filters in Python (Page 125-129)

This result is worse than the example where only the measurement sensor was noisy. Instead of being mostly straight, this time the filter’s output is distinctly jagged. But, it still mostly tracks the dog. What is happening here?

This illustrates the effects of multi-sensor fusion. Suppose we get a position reading of -28.78 followed by 31.43. From that information alone it is impossible to tell if the dog is standing still during very noisy measurements, or perhaps sprinting from -29 to 31 and being accurately measured. But we have a second source of information, his velocity. Even when the velocity is also noisy, it constrains what our beliefs might be. For example, suppose that with the 31.43 position reading we get a velocity reading of 59. That matches the difference between the two positions quite well, so this will lead us to believe the RFID sensor and the velocity sensor. Now suppose we got a velocity reading of 1.7. This doesn’t match our RFID reading very well - it suggests that the dog is standing still or moving slowly.

When sensors measure different aspects of the system and they all agree we have strong evidence that the sensors are accurate. And when they do not agree it is a strong indication that one or more of them are inaccurate.

We will formalize this mathematically in the next chapter; for now trust this intuitive explanation. We use this sort of reasoning every day in our lives. If one person tells us something that seems far fetched we are inclined to doubt them. But if several people independently relay the same information we attach higher credence to the data. If one person disagrees with several other people, we tend to distrust the outlier. If we know the people that might alter our belief. If a friend is inclined to practical jokes and tall tales we may put very little trust in what they say. If one lawyer and three lay people opine on some fact of law, and the lawyer disagrees with the three you’ll probably lend more credence to what the lawyer says because of her expertise. In the next chapter we will learn how to mathematical model this sort of reasoning.

5.11

More examples

5.11.1

Example: Extreme Amounts of Noise

So I didn’t put a lot of noise in the signal, and I also ‘correctly guessed’ that the dog was at position 0. How does the filter perform in real world conditions? Let’s explore and find out. I will start by injecting a lot of noise in the RFID sensor. I will inject an extreme amount of noise - noise that apparently swamps the actual measurement. What does your intuition tell about how the filter will perform if the noise is allowed to be anywhere from -300 or 300. In other words, an actual position of 1.0 might be reported as 287.9, or -189.6, or any other number in that range. Think about it before you scroll down.

In [26]: sensor_variance = 30000

movement_variance = 2

pos = (0,500)

dog = DogSensor(pos[0], velocity=movement, measurement_variance=sensor_variance) zs = []

ps = []

for i in range(1000):

pos = predict(pos[0], pos[1], movement, movement_variance) Z = dog.sense_position()

zs.append(Z)

pos = update(pos[0], pos[1], Z, sensor_variance) ps.append(pos[0])

bp.plot_measurements(zs, lw=1) bp.plot_filter(ps)

plt.legend(loc=’best’) plt.show()

In this example the noise is extreme yet the filter still outputs a nearly straight line! This is an astonishing result! What do you think might be the cause of this performance? If you are not sure, don’t worry, we will discuss it latter.

5.11.2

Example: Bad Initial Estimate

Now let’s lets look at the results when we make a bad initial estimate of position. To avoid obscuring the results I’ll reduce the sensor variance to 30, but set the initial position to 1000m. Can the filter recover from a 1000m initial error?

5.11. MORE EXAMPLES 127

In [27]: sensor_variance = 30

movement_variance = 2

pos = (1000,500)

dog = DogSensor(0, velocity=movement, measurement_variance=sensor_variance) zs = []

ps = []

for i in range(100):

pos = predict(pos[0], pos[1], movement, movement_variance) Z = dog.sense_position()

zs.append(Z)

pos = update(pos[0], pos[1], Z, sensor_variance) ps.append(pos[0]) bp.plot_filter(ps) bp.plot_measurements(zs) plt.legend(loc=’best’) plt.gca().set_xlim(0,100) plt.show()

Again the answer is yes! Because we are relatively sure about our belief in the sensor (σ = 30) even after the first step we have changed our belief in the first position from 1000 to somewhere around 60.0 or so. After another 5-10 measurements we have converged to the correct value! So this is how we get around the chicken and egg problem of initial guesses. In practice we would probably just assign the first measurement from the sensor as the initial value, but you can see it doesn’t matter much if we wildly guess at the initial conditions - the Kalman filter still converges very quickly.

5.11.3

Example: Large Noise and Bad Initial Estimate

What about the worst of both worlds, large noise and a bad initial estimate?

In [28]: sensor_variance = 30000

movement_variance = 2

pos = (1000,500)

dog = DogSensor(0, velocity=movement, measurement_variance=sensor_variance) zs = []

ps = []

for i in range(1000):

pos = predict(pos[0], pos[1], movement, movement_variance) Z = dog.sense_position()

zs.append(Z)

pos = update(pos[0], pos[1], Z, sensor_variance) ps.append(pos[0])

bp.plot_measurements(zs, lw=1) bp.plot_filter(ps)

plt.legend(loc=’best’) plt.show()

This time the filter does struggle. Notice that the previous example only computed 100 updates, whereas this example uses 1000. By my eye it takes the filter 400 or so iterations to become reasonable accurate, but maybe over 600 before the results are good. Kalman filters are good, but we cannot expect miracles. If we have extremely noisy data and extremely bad initial conditions, this is as good as it gets.

Finally, let’s make the suggest change of making our initial position guess just be the first sensor mea- surement.

In [29]: sensor_variance = 30000

movement_variance = 2

pos = None

5.12. EXERCISE: INTERACTIVE PLOTS 129

In document Kalman and Bayesian Filters in Python (Page 125-129)