# Notebook from kwu2u/Kalman-and-Bayesian-Filters-in-Python
Path: 02-Discrete-Bayes.ipynb

[Table of Contents](./table_of_contents.ipynb)_____no_output_____# Discrete Bayes Filter_____no_output_____

from __future__ import division, print_function
%matplotlib inline_____no_output_____#format the book
import book_format
book_format.set_style()_____no_output_____

### The Kalman filter belongs to a family of filters called *Bayesian filters*. Most textbook treatments of the Kalman filter present the Bayesian formula, perhaps shows how it factors into the Kalman filter equations, but mostly keeps the discussion at a very abstract level.

That approach requires a fairly sophisticated understanding of several fields of mathematics, and it still leaves much of the work of understanding and forming an intuitive grasp of the situation in the hands of the reader.

I will use a different way to develop the topic, to which I owe the work of Dieter Fox and Sebastian Thrun a great debt. It depends on building an intuition on how Bayesian statistics work by tracking an object through a hallway - they use a robot, I use a dog. I like dogs, and they are less predictable than robots which imposes interesting difficulties for filtering. The first published example of this that I can find seems to be Fox 1999 [1], with a fuller example in Fox 2003 [2]. Sebastian Thrun also uses this formulation in his excellent Udacity course Artificial Intelligence for Robotics [3]. In fact, if you like watching videos, I highly recommend pausing reading this book in favor of first few lessons of that course, and then come back to this book for a deeper dive into the topic.

Let's now use a simple thought experiment, much like we did with the g-h filter, to see how we might reason about the use of probabilities for filtering and tracking._____no_output_____## Tracking a Dog

Let's begin with a simple problem. We have a dog friendly workspace, and so people bring their dogs to work. Occasionally the dogs wander out of offices and down the halls. We want to be able to track them. So during a hackathon somebody invented a sonar sensor to attach to the dog's collar. It emits a signal, listens for the echo, and based on how quickly an echo comes back we can tell whether the dog is in front of an open doorway or not. It also senses when the dog walks, and reports in which direction the dog has moved. It connects to the network via wifi and sends an update once a second.

I want to track my dog Simon, so I attach the device to his collar and then fire up Python, ready to write code to track him through the building. At first blush this may appear impossible. If I start listening to the sensor of Simon's collar I might read **door**, **hall**, **hall**, and so on. How can I use that information to determine where Simon is?

To keep the problem small enough to plot easily we will assume that there are only 10 positions in the hallway, which we will number 0 to 9, where 1 is to the right of 0. For reasons that will be clear later, we will also assume that the hallway is circular or rectangular. If you move right from position 9, you will be at position 0.

When I begin listening to the sensor I have no reason to believe that Simon is at any particular position in the hallway. From my perspective he is equally likely to be in any position. There are 10 positions, so the probability that he is in any given position is 1/10.

Let's represent our belief of his position in a NumPy array. I could use a Python list, but NumPy arrays offer functionality that we will be using soon._____no_output_____

import numpy as np
belief = np.array([1./10]*10)
print(belief)[0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]

In [Bayesian statistics](https://en.wikipedia.org/wiki/Bayesian_probability) this is called a [*prior*](https://en.wikipedia.org/wiki/Prior_probability). It is the probability prior to incorporating measurements or other information. More completely, this is called the *prior probability distribution*. A [*probability distribution*](https://en.wikipedia.org/wiki/Probability_distribution) is a collection of all possible probabilities for an event. Probability distributions always sum to 1 because something had to happen; the distribution lists all possible events and the probability of each.

I'm sure you've used probabilities before - as in "the probability of rain today is 30%". The last paragraph sounds like more of that. But Bayesian statistics was a revolution in probability because it treats probability as a belief about a single event. Let's take an example. I know that if I flip a fair coin infinitely many times I will get 50% heads and 50% tails. This is called [*frequentist statistics*](https://en.wikipedia.org/wiki/Frequentist_inference) to distinguish it from Bayesian statistics. Computations are based on the frequency in which events occur.

I flip the coin one more time and let it land. Which way do I believe it landed? Frequentist probability has nothing to say about that; it will merely state that 50% of coin flips land as heads. In some ways it is meaningless to assign a probability to the current state of the coin. It is either heads or tails, we just don't know which. Bayes treats this as a belief about a single event - the strength of my belief or knowledge that this specific coin flip is heads is 50%. Some object to the term "belief"; belief can imply holding something to be true without evidence. In this book it always is a measure of the strength of our knowledge. We'll learn more about this as we go.

Bayesian statistics takes past information (the prior) into account. We observe that it rains 4 times every 100 days. From this I could state that the chance of rain tomorrow is 1/25. This is not how weather prediction is done. If I know it is raining today and the storm front is stalled, it is likely to rain tomorrow. Weather prediction is Bayesian.

In practice statisticians use a mix of frequentist and Bayesian techniques. Sometimes finding the prior is difficult or impossible, and frequentist techniques rule. In this book we can find the prior. When I talk about the probability of something I am referring to the probability that some specific thing is true given past events. When I do that I'm taking the Bayesian approach.

Now let's create a map of the hallway. We'll place the first two doors close together, and then another door further away. We will use 1 for doors, and 0 for walls:_____no_output_____

hallway = np.array([1, 1, 0, 0, 0, 0, 0, 0, 1, 0])_____no_output_____

I start listening to Simon's transmissions on the network, and the first data I get from the sensor is **door**. For the moment assume the sensor always returns the correct answer. From this I conclude that he is in front of a door, but which one? I have no reason to believe he is in front of the first, second, or third door. What I can do is assign a probability to each door. All doors are equally likely, and there are three of them, so I assign a probability of 1/3 to each door. _____no_output_____

import kf_book.book_plots as book_plots
from kf_book.book_plots import figsize, set_figsize
import matplotlib.pyplot as plt

belief = np.array([1./3, 1./3, 0, 0, 0, 0, 0, 0, 1/3, 0])
book_plots.bar_plot(belief)_____no_output_____

This distribution is called a [*categorical distribution*](https://en.wikipedia.org/wiki/Categorical_distribution), which is a discrete distribution describing the probability of observing $n$ outcomes. It is a [*multimodal distribution*](https://en.wikipedia.org/wiki/Multimodal_distribution) because we have multiple beliefs about the position of our dog. Of course we are not saying that we think he is simultaneously in three different locations, merely that we have narrowed down our knowledge to one of these three locations. My (Bayesian) belief is that there is a 33.3% chance of being at door 0, 33.3% at door 1, and a 33.3% chance of being at door 8.

This is an improvement in two ways. I've rejected a number of hallway positions as impossible, and the strength of my belief in the remaining positions has increased from 10% to 33%. This will always happen. As our knowledge improves the probabilities will get closer to 100%.

A few words about the [*mode*](https://en.wikipedia.org/wiki/Mode_(statistics))
of a distribution. Given a set of numbers, such as {1, 2, 2, 2, 3, 3, 4}, the *mode* is the number that occurs most often. For this set the mode is 2. A set can contain more than one mode. The set {1, 2, 2, 2, 3, 3, 4, 4, 4} contains the modes 2 and 4, because both occur three times. We say the former set is [*unimodal*](https://en.wikipedia.org/wiki/Unimodality), and the latter is *multimodal*.

Another term used for this distribution is a [*histogram*](https://en.wikipedia.org/wiki/Histogram). Histograms graphically depict the distribution of a set of numbers. The bar chart above is a histogram.

I hand coded the `belief` array in the code above. How would we implement this in code? We represent doors with 1, and walls as 0, so we will multiply the hallway variable by the percentage, like so;_____no_output_____

belief = hallway * (1./3)
print(belief)[0.333 0.333 0.    0.    0.    0.    0.    0.    0.333 0.   ]

## Extracting Information from Sensor Readings

Let's put Python aside and think about the problem a bit. Suppose we were to read the following from Simon's sensor:

  * door
  * move right
  * door

Can we deduce Simon's location? Of course! Given the hallway's layout there is only one place from which you can get this sequence, and that is at the left end. Therefore we can confidently state that Simon is in front of the second doorway. If this is not clear, suppose Simon had started at the second or third door. After moving to the right, his sensor would have returned 'wall'. That doesn't match the sensor readings, so we know he didn't start there. We can continue with that logic for all the remaining starting positions. The only possibility is that he is now in front of the second door. Our belief is:_____no_output_____

belief = np.array([0., 1., 0., 0., 0., 0., 0., 0., 0., 0.])_____no_output_____

I designed the hallway layout and sensor readings to give us an exact answer quickly. Real problems are not so clear cut. But this should trigger your intuition - the first sensor reading only gave us low probabilities (0.333) for Simon's location, but after a position update and another sensor reading we know more about where he is. You might suspect, correctly, that if you had a very long hallway with a large number of doors that after several sensor readings and positions updates we would either be able to know where Simon was, or have the possibilities narrowed down to a small number of possibilities. This is possible when a set of sensor readings only matches one to a few starting locations.

We could implement this solution now, but instead let's consider a real world complication to the problem._____no_output_____## Noisy Sensors

Perfect sensors are rare. Perhaps the sensor would not detect a door if Simon sat in front of it while scratching himself, or misread if he is not facing down the hallway. Thus when I get **door** I cannot use 1/3 as the probability. I have to assign less than 1/3 to each door, and assign a small probability to each blank wall position. Something like

```Python
[.31, .31, .01, .01, .01, .01, .01, .01, .31, .01]
```

At first this may seem insurmountable. If the sensor is noisy it casts doubt on every piece of data. How can we conclude anything if we are always unsure?

The answer, as for the problem above, is with probabilities. We are already comfortable assigning a probabilistic belief to the location of the dog; now we have to incorporate the additional uncertainty caused by the sensor noise.

Say we get a reading of **door**, and suppose that testing shows that the sensor is 3 times more likely to be right than wrong. We should scale the probability distribution by 3 where there is a door. If we do that the result will no longer be a probability distribution, but we will learn how to fix that in a moment.

Let's look at that in Python code. Here I use the variable `z` to denote the measurement. `z` or `y` are customary choices in the literature for the measurement. As a programmer I prefer meaningful variable names, but I want you to be able to read the literature and/or other filtering code, so I will start introducing these abbreviated names now._____no_output_____

def update_belief(hall, belief, z, correct_scale):
    for i, val in enumerate(hall):
        if val == z:
            belief[i] *= correct_scale

belief = np.array([0.1] * 10)
reading = 1 # 1 is 'door'
update_belief(hallway, belief, z=reading, correct_scale=3.)
print('belief:', belief)
print('sum =', sum(belief))
plt.figure()
book_plots.bar_plot(belief)belief: [0.3 0.3 0.1 0.1 0.1 0.1 0.1 0.1 0.3 0.1]
sum = 1.6000000000000003

This is not a probability distribution because it does not sum to 1.0. But the code is doing mostly the right thing - the doors are assigned a number (0.3) that is 3 times higher than the walls (0.1). All we need to do is normalize the result so that the probabilities correctly sum to 1.0. Normalization is done by dividing each element by the sum of all elements in the list. That is easy with NumPy:_____no_output_____

belief / sum(belief)_____no_output_____

FilterPy implements this with the `normalize` function:

```Python
from filterpy.discrete_bayes import normalize
normalize(belief)
```

It is a bit odd to say "3 times as likely to be right as wrong". We are working in probabilities, so let's specify the probability of the sensor being correct, and compute the scale factor from that. The equation for that is

$$scale =  \frac{prob_{correct}}{prob_{incorrect}} = \frac{prob_{correct}} {1-prob_{correct}}$$

Also, the `for` loop is cumbersome. As a general rule you will want to avoid using `for` loops in NumPy code. NumPy is implemented in C and Fortran, so if you avoid for loops the result often runs 100x faster than the equivalent loop.

How do we get rid of this `for` loop? NumPy lets you index arrays with boolean arrays. You create a boolean array with logical operators. We can find all the doors in the hallway with:_____no_output_____

hallway == 1_____no_output_____

When you use the boolean array as an index to another array it returns only the elements where the index is `True`. Thus we can replace the `for` loop with

```python
belief[hall==z] *= scale
```
and only the elements which equal `z` will be multiplied by `scale`.

Teaching you NumPy is beyond the scope of this book. I will use idiomatic NumPy constructs and explain them the first time I present them. If you are new to NumPy there are many blog posts and videos on how to use NumPy efficiently and idiomatically.

Here is our improved version:_____no_output_____

from filterpy.discrete_bayes import normalize

def scaled_update(hall, belief, z, z_prob):
    scale = z_prob / (1. - z_prob)
    belief[hall==z] *= scale
    normalize(belief)

belief = np.array([0.1] * 10)
scaled_update(hallway, belief, z=1, z_prob=.75)

print('sum =', sum(belief))
print('probability of door =', belief[0])
print('probability of wall =', belief[2])
book_plots.bar_plot(belief, ylim=(0, .3))sum = 1.0
probability of door = 0.1875
probability of wall = 0.06249999999999999

 We can see from the output that the sum is now 1.0, and that the probability of a door vs wall is still three times larger. The result also fits our intuition that the probability of a door must be less than 0.333, and that the probability of a wall must be greater than 0.0. Finally, it should fit our intuition that we have not yet been given any information that would allow us to distinguish between any given door or wall position, so all door positions should have the same value, and the same should be true for wall positions.

This result is called the [*posterior*](https://en.wikipedia.org/wiki/Posterior_probability), which is short for *posterior probability distribution*. All this means is a probability distribution *after* incorporating the measurement information (posterior means 'after' in this context). To review, the *prior* is the probability distribution before including the measurement's information.

Another term is the [*likelihood*](https://en.wikipedia.org/wiki/Likelihood_function). When we computed `belief[hall==z] *= scale` we were computing how *likely* each position was given the measurement. The likelihood is not a probability distribution because it does not sum to one.

The combination of these gives the equation

$$\mathtt{posterior} = \frac{\mathtt{likelihood} \times \mathtt{prior}}{\mathtt{normalization}}$$

When we talk about the filter's output we typically call the state after performing the prediction the *prior* or *prediction*, and we call the state after the update either the *posterior* or the *estimated state*.

It is very important to learn and internalize these terms as most of the literature uses them extensively.

Does `scaled_update()` perform this computation? It does. Let me recast it into this form:_____no_output_____

def scaled_update(hall, belief, z, z_prob):
    scale = z_prob / (1. - z_prob)
    likelihood = np.ones(len(hall))
    likelihood[hall==z] *= scale
    return normalize(likelihood * belief)_____no_output_____

This function is not fully general. It contains knowledge about the hallway, and how we match measurements to it. We always strive to write general functions. Here we will remove the computation of the likelihood from the function, and require the caller to compute the likelihood themselves.

Here is a full implementation of the algorithm:

```python
def update(likelihood, prior):
    return normalize(likelihood * prior)
```

Computation of the likelihood varies per problem. For example, the sensor might not return  just 1 or 0, but a `float` between 0 and 1 indicating the probability of being in front of a door. It might use computer vision and report a blob shape that you then probabilistically match to a door. It might use sonar and return a distance reading. In each case the computation of the likelihood will be different. We will see many examples of this throughout the book, and learn how to perform these calculations.

FilterPy implements `update`. Here is the previous example in a fully general form:_____no_output_____

from filterpy.discrete_bayes import update

def lh_hallway(hall, z, z_prob):
    """ compute likelihood that a measurement matches
    positions in the hallway."""

    try:
        scale = z_prob / (1. - z_prob)
    except ZeroDivisionError:
        scale = 1e8

    likelihood = np.ones(len(hall))
    likelihood[hall==z] *= scale
    return likelihood

belief = np.array([0.1] * 10)
likelihood = lh_hallway(hallway, z=1, z_prob=.75)
update(likelihood, belief)  _____no_output_____

## Incorporating Movement

Recall how quickly we were able to find an exact solution when we incorporated a series of measurements and movement updates. However, that occurred in a fictional world of perfect sensors. Might we be able to find an exact solution with noisy sensors?

Unfortunately, the answer is no. Even if the sensor readings perfectly match an extremely complicated hallway map, we cannot be 100% certain that the dog is in a specific position - there is, after all, a tiny possibility that every sensor reading was wrong! Naturally, in a more typical situation most sensor readings will be correct, and we might be close to 100% sure of our answer, but never 100% sure. This may seem complicated, but let's go ahead and program the math.

First let's deal with the simple case - assume the movement sensor is perfect, and it reports that the dog has moved one space to the right. How would we alter our `belief` array?

I hope that after a moment's thought it is clear that we should shift all the values one space to the right. If we previously thought there was a 50% chance of Simon being at position 3, then after he moved one position to the right we should believe that there is a 50% chance he is at position 4. The hallway is circular, so we will use modulo arithmetic to perform the shift._____no_output_____

def perfect_predict(belief, move):
    """ move the position by `move` spaces, where positive is
    to the right, and negative is to the left
    """
    n = len(belief)
    result = np.zeros(n)
    for i in range(n):
        result[i] = belief[(i-move) % n]
    return result

belief = np.array([.35, .1, .2, .3, 0, 0, 0, 0, 0, .05])
plt.subplot(121)
book_plots.bar_plot(belief, title='Before prediction', ylim=(0, .4))

belief = perfect_predict(belief, 1)
plt.subplot(122)
book_plots.bar_plot(belief, title='After prediction', ylim=(0, .4))_____no_output_____

We can see that we correctly shifted all values one position to the right, wrapping from the end of the array back to the beginning.

The next cell animates this so you can see it in action. Use the slider to move forwards and backwards in time. This simulates Simon walking around and around the hallway. It does not yet incorporate new measurements so the probability distribution does not change shape, only position._____no_output_____

from ipywidgets import interact, IntSlider

belief = np.array([.35, .1, .2, .3, 0, 0, 0, 0, 0, .05])
beliefs = []

for _ in range(20):
    # Simon takes one step to the right
    belief = perfect_predict(belief, 1)
    beliefs.append(belief)

def simulate(time_step):
    book_plots.bar_plot(beliefs[time_step], ylim=(0, .4))

interact(simulate, time_step=IntSlider(value=0, max=len(beliefs)-1));_____no_output_____

## Terminology

Let's pause a moment to review terminology. I introduced this terminology in the last chapter, but let's take a second to help solidify your knowledge.

The *system* is what we are trying to model or filter. Here the system is our dog. The *state* is its current configuration or value. In this chapter the state is our dog's position. We rarely know the actual state, so we say our filters produce the *estimated state* of the system. In practice this often gets called the state, so be careful to understand the context.

One cycle of prediction and updating with a measurement is called the state or system *evolution*, which is short for *time evolution* [7]. Another term is *system propagation*. It refers to how the state of the system changes over time. For filters, time is usually a discrete step, such as 1 second. For our dog tracker the system state is the position of the dog, and the state evolution is the position after a discrete amount of time has passed.

We model the system behavior with the *process model*. Here, our process model is that the dog moves one or more positions at each time step. This is not a particularly accurate model of how dogs behave. The error in the model is called the *system error* or *process error*.

The prediction is our new *prior*. Time has moved forward and we made a prediction without benefit of knowing the measurements.

Let's work an example. The current position of the dog is 17 m. Our epoch is 2 seconds long, and the dog is traveling at 15 m/s. Where do we predict he will be in two seconds?

Clearly,

$$ \begin{aligned}
\bar x &= 17 + (15*2) \\
&= 47
\end{aligned}$$

I use bars over variables to indicate that they are priors (predictions). We can write the equation for the process model like this:

$$ \bar x_{k+1} = f_x(\bullet) + x_k$$

$x_k$ is the current position or state. If the dog is at 17 m then $x_k = 17$.

$f_x(\bullet)$ is the state propagation function for x. It describes how much the $x_k$ changes over one time step. For our example it performs the computation $15 \cdot 2$ so we would define it as

$$f_x(v_x, t) = v_k t$$._____no_output_____## Adding Uncertainty to the Prediction

`perfect_predict()` assumes perfect measurements, but all sensors have noise. What if the sensor reported that our dog moved one space, but he actually moved two spaces, or zero? This may sound like an insurmountable problem, but let's model it and see what happens.

Assume that the sensor's movement measurement is 80% likely to be correct, 10% likely to overshoot one position to the right, and 10% likely to undershoot to the left. That is, if the movement measurement is 4 (meaning 4 spaces to the right), the dog is 80% likely to have moved 4 spaces to the right, 10% to have moved 3 spaces, and 10% to have moved 5 spaces.

Each result in the array now needs to incorporate probabilities for 3 different situations. For example, consider the reported movement of 2. If we are 100% certain the dog started from position 3, then there is an 80% chance he is at 5, and a 10% chance for either 4 or 6. Let's try coding that:_____no_output_____

def predict_move(belief, move, p_under, p_correct, p_over):
    n = len(belief)
    prior = np.zeros(n)
    for i in range(n):
        prior[i] = (
            belief[(i-move) % n]   * p_correct +
            belief[(i-move-1) % n] * p_over +
            belief[(i-move+1) % n] * p_under)
    return prior

belief = [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.]
prior = predict_move(belief, 2, .1, .8, .1)
book_plots.plot_belief_vs_prior(belief, prior)_____no_output_____

It appears to work correctly. Now what happens when our belief is not 100% certain?_____no_output_____

belief = [0, 0, .4, .6, 0, 0, 0, 0, 0, 0]
prior = predict_move(belief, 2, .1, .8, .1)
book_plots.plot_belief_vs_prior(belief, prior)
prior_____no_output_____

Here the results are more complicated, but you should still be able to work it out in your head. The 0.04 is due to the possibility that the 0.4 belief undershot by 1. The 0.38 is due to the following: the 80% chance that we moved 2 positions (0.4 $\times$ 0.8) and the 10% chance that we undershot (0.6 $\times$ 0.1). Overshooting plays no role here because if we overshot both 0.4 and 0.6 would be past this position. **I strongly suggest working some examples until all of this is very clear, as so much of what follows depends on understanding this step.**

If you look at the probabilities after performing the update you might be dismayed. In the example above we started with probabilities of 0.4 and 0.6 in two positions; after performing the update the probabilities are not only lowered, but they are strewn out across the map.

This is not a coincidence, or the result of a carefully chosen example - it is always true of the prediction. If the sensor is noisy we lose some information on every prediction. Suppose we were to perform the prediction an infinite number of times - what would the result be? If we lose information on every step, we must eventually end up with no information at all, and our probabilities will be equally distributed across the `belief` array. Let's try this with 100 iterations. The plot is animated; use the slider to change the step number._____no_output_____

belief = np.array([1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
beliefs = []

for i in range(100):
    belief = predict_move(belief, 1, .1, .8, .1)
    beliefs.append(belief)

print('Final Belief:', belief)

# make interactive plot
def show_prior(step):
    book_plots.bar_plot(beliefs[step-1])
    plt.title('Step {}'.format(step))

interact(show_prior, step=IntSlider(value=1, max=len(beliefs)));Final Belief: [0.104 0.103 0.101 0.099 0.097 0.096 0.097 0.099 0.101 0.103]
print('Final Belief:', belief)Final Belief: [0.104 0.103 0.101 0.099 0.097 0.096 0.097 0.099 0.101 0.103]

After 100 iterations we have lost almost all information, even though we were 100% sure that we started in position 0. Feel free to play with the numbers to see the effect of differing number of updates. For example, after 100 updates a small amount of information is left, after 50 a lot is left, but by 200 iterations essentially all information is lost._____no_output_____And, if you are viewing this online here is an animation of that output.

I will not generate these standalone animations through the rest of the book. Please see the preface for instructions to run this book on the web, for free, or install IPython on your computer. This will allow you to run all of the cells and see the animations. It's very important that you practice with this code, not just read passively._____no_output_____## Generalizing with Convolution

We made the assumption that the movement error is at most one position. But it is possible for the error to be two, three, or more positions. As programmers we always want to generalize our code so that it works for all cases.

This is easily solved with [*convolution*](https://en.wikipedia.org/wiki/Convolution). Convolution modifies one function with another function. In our case we are modifying a probability distribution with the error function of the sensor. The implementation of `predict_move()` is a convolution, though we did not call it that. Formally, convolution is defined as

$$ (f \ast g) (t) = \int_0^t \!f(\tau) \, g(t-\tau) \, \mathrm{d}\tau$$

where $f\ast g$ is the notation for convolving f by g. It does not mean multiply.

Integrals are for continuous functions, but we are using discrete functions. We replace the integral with a summation, and the parenthesis with array brackets.

$$ (f \ast g) [t] = \sum\limits_{\tau=0}^t \!f[\tau] \, g[t-\tau]$$

Comparison shows that `predict_move()` is computing this equation - it computes the sum of a series of multiplications.

[Khan Academy](https://www.khanacademy.org/math/differential-equations/laplace-transform/convolution-integral/v/introduction-to-the-convolution) [4] has a good introduction to convolution, and Wikipedia has some excellent animations of convolutions [5]. But the general idea is already clear. You slide an array called the *kernel* across another array, multiplying the neighbors of the current cell with the values of the second array. In our example above we used 0.8 for the probability of moving to the correct location, 0.1 for undershooting, and 0.1 for overshooting. We make a kernel of this with the array `[0.1, 0.8, 0.1]`. All we need to do is write a loop that goes over each element of our array, multiplying by the kernel, and summing the results. To emphasize that the belief is a probability distribution I have named it `pdf`._____no_output_____

def predict_move_convolution(pdf, offset, kernel):
    N = len(pdf)
    kN = len(kernel)
    width = int((kN - 1) / 2)

    prior = np.zeros(N)
    for i in range(N):
        for k in range (kN):
            index = (i + (width-k) - offset) % N
            prior[i] += pdf[index] * kernel[k]
    return prior_____no_output_____

This illustrates the algorithm, but it runs very slow. SciPy provides a convolution routine `convolve()` in the `ndimage.filters` module. We  need to shift the pdf by `offset` before convolution; `np.roll()` does that. The move and predict algorithm can be implemented with one line:

```python
convolve(np.roll(pdf, offset), kernel, mode='wrap')
```

FilterPy implements this with `discrete_bayes`' `predict()` function._____no_output_____

from filterpy.discrete_bayes import predict

belief = [.05, .05, .05, .05, .55, .05, .05, .05, .05, .05]
prior = predict(belief, offset=1, kernel=[.1, .8, .1])
book_plots.plot_belief_vs_prior(belief, prior, ylim=(0,0.6))_____no_output_____

All of the elements are unchanged except the middle ones. The values in position 4 and 6 should be
$$(0.1 \times 0.05)+ (0.8 \times 0.05) + (0.1 \times 0.55) = 0.1$$

Position 5 should be $$(0.1 \times 0.05) + (0.8 \times 0.55)+ (0.1 \times 0.05) = 0.45$$

Let's ensure that it shifts the positions correctly for movements greater than one and for asymmetric kernels._____no_output_____

prior = predict(belief, offset=3, kernel=[.05, .05, .6, .2, .1])
book_plots.plot_belief_vs_prior(belief, prior, ylim=(0,0.6))_____no_output_____

The position was correctly shifted by 3 positions and we give more weight to the likelihood of an overshoot vs an undershoot, so this looks correct.

Make sure you understand what we are doing. We are making a prediction of where the dog is moving, and convolving the probabilities to get the prior.

If we weren't using probabilities we would use this equation that I gave earlier:

$$ \bar x_{k+1} = x_k + f_{\mathbf x}(\bullet)$$

The prior, our prediction of where the dog will be, is the amount the dog moved plus his current position. The dog was at 10, he moved 5 meters, so he is now at 15 m. It couldn't be simpler. But we are using probabilities to model this, so our equation is:

$$ \bar{ \mathbf x}_{k+1} = \mathbf x_k \ast f_{\mathbf x}(\bullet)$$

We are *convolving* the current probabilistic position estimate with a probabilistic estimate of how much we think the dog moved. It's the same concept, but the math is slightly different. $\mathbf x$ is bold to denote that it is an array of numbers. _____no_output_____## Integrating Measurements and Movement Updates

The problem of losing information during a prediction may make it seem as if our system would quickly devolve into having no knowledge. However, each prediction is followed by an update where we incorporate the measurement into the estimate. The update improves our knowledge. The output of the update step is fed into the next prediction. The prediction degrades our certainty. That is passed into another update, where certainty is again increased.

Let's think about this intuitively. Consider a simple case - you are tracking a dog while he sits still. During each prediction you predict he doesn't move. Your filter quickly *converges* on an accurate estimate of his position. Then the microwave in the kitchen turns on, and he goes streaking off. You don't know this, so at the next prediction you predict he is in the same spot. But the measurements tell a different story. As you incorporate the measurements your belief will be smeared along the hallway, leading towards the kitchen. On every epoch (cycle) your belief that he is sitting still will get smaller, and your belief that he is inbound towards the kitchen at a startling rate of speed increases.

That is what intuition tells us. What does the math tell us?

We have already programmed the update and predict steps. All we need to do is feed the result of one into the other, and we will have implemented a dog tracker!!! Let's see how it performs. We will input measurements as if the dog started at position 0 and moved right one position each epoch. As in a real world application, we will start with no knowledge of his position by assigning equal probability to all positions. _____no_output_____

from filterpy.discrete_bayes import update

hallway = np.array([1, 1, 0, 0, 0, 0, 0, 0, 1, 0])
prior = np.array([.1] * 10)
likelihood = lh_hallway(hallway, z=1, z_prob=.75)
posterior = update(likelihood, prior)
book_plots.plot_prior_vs_posterior(prior, posterior, ylim=(0,.5))_____no_output_____

After the first update we have assigned a high probability to each door position, and a low probability to each wall position. _____no_output_____

kernel = (.1, .8, .1)
prior = predict(posterior, 1, kernel)
book_plots.plot_prior_vs_posterior(prior, posterior, True, ylim=(0,.5))_____no_output_____

The predict step shifted these probabilities to the right, smearing them about a bit. Now let's look at what happens at the next sense._____no_output_____

likelihood = lh_hallway(hallway, z=1, z_prob=.75)
posterior = update(likelihood, prior)
book_plots.plot_prior_vs_posterior(prior, posterior, ylim=(0,.5))_____no_output_____

Notice the tall bar at position 1. This corresponds with the (correct) case of starting at position 0, sensing a door, shifting 1 to the right, and sensing another door. No other positions make this set of observations as likely. Now we will add an update and then sense the wall._____no_output_____

prior = predict(posterior, 1, kernel)
likelihood = lh_hallway(hallway, z=0, z_prob=.75)
posterior = update(likelihood, prior)
book_plots.plot_prior_vs_posterior(prior, posterior, ylim=(0,.5))_____no_output_____

This is exciting! We have a very prominent bar at position 2 with a value of around 35%. It is over twice the value of any other bar in the plot, and is about 4% larger than our last plot, where the tallest bar was around 31%. Let's see one more cycle._____no_output_____

prior = predict(posterior, 1, kernel)
likelihood = lh_hallway(hallway, z=0, z_prob=.75)
posterior = update(likelihood, prior)
book_plots.plot_prior_vs_posterior(prior, posterior, ylim=(0,.5))_____no_output_____

I ignored an important issue. Earlier I assumed that we had a motion sensor for the predict step; then, when talking about the dog and the microwave I assumed that you had no knowledge that he suddenly began running. I mentioned that your belief that the dog is running would increase over time, but I did not provide any code for this. In short, how do we detect and/or estimate changes in the process model if we aren't directly measuring it?

For now I want to ignore this problem. In later chapters we will learn the mathematics behind this estimation; for now it is a large enough task just to learn this algorithm. It is profoundly important to solve this problem, but we haven't yet built enough of the mathematical apparatus that is required, and so for the remainder of the chapter we will ignore the problem by assuming we have a sensor that senses movement._____no_output_____## The Discrete Bayes Algorithm

This chart illustrates the algorithm:_____no_output_____

book_plots.predict_update_chart()_____no_output_____

This filter is a form of the g-h filter. Here we are using the percentages for the errors to implicitly compute the $g$ and $h$ parameters. We could express the discrete Bayes algorithm as a g-h filter, but that would obscure the logic of this filter.

The filter equations are:

$$\begin{aligned} \bar {\mathbf x} &= \mathbf x \ast f_{\mathbf x}(\bullet)\, \, &\text{Predict Step} \\
\mathbf x &= \|\mathcal L \cdot \bar{\mathbf x}\|\, \, &\text{Update Step}\end{aligned}$$

$\mathcal L$ is the usual way to write the likelihood function, so I use that. The $\|\|$ notation denotes taking the norm. We need to normalize the product of the likelihood with the prior to ensure $x$ is a probability distribution that sums to one.

We can express this in pseudocode.

**Initialization**

    1. Initialize our belief in the state

**Predict**

    1. Based on the system behavior, predict state for the next time step
    2. Adjust belief to account for the uncertainty in prediction

**Update**

    1. Get a measurement and associated belief about its accuracy
    2. Compute how likely it is the measurement matches each state
    3. Update state belief with this likelihood

When we cover the Kalman filter we will use this exact same algorithm; only the details of the computation will differ.

Algorithms in this form are sometimes called *predictor correctors*. We make a prediction, then correct them.

Let's animate this. First Let's write functions to perform the filtering and to plot the results at any step. I've plotted the position of the doorways in black. Prior are drawn in orange, and the posterior in blue. I draw a thick vertical line to indicate where Simon really is. This is not an output of the filter - we know where Simon is only because we are simulating his movement._____no_output_____

def discrete_bayes_sim(prior, kernel, measurements, z_prob, hallway):
    posterior = np.array([.1]*10)
    priors, posteriors = [], []
    for i, z in enumerate(measurements):
        prior = predict(posterior, 1, kernel)
        priors.append(prior)

        likelihood = lh_hallway(hallway, z, z_prob)
        posterior = update(likelihood, prior)
        posteriors.append(posterior)
    return priors, posteriors

def plot_posterior(posteriors, i):
    plt.title('Posterior')
    book_plots.bar_plot(hallway, c='k')
    book_plots.bar_plot(posteriors[i], ylim=(0, 1.0))
    plt.axvline(i % len(hallway), lw=5)

def plot_prior(priors, i):
    plt.title('Prior')
    book_plots.bar_plot(hallway, c='k')
    book_plots.bar_plot(priors[i], ylim=(0, 1.0), c='#ff8015')
    plt.axvline(i % len(hallway), lw=5)

def animate_discrete_bayes(step):
    step -= 1
    i = step // 2
    if step % 2 == 0:
        plot_prior(priors, i)
    else:
        plot_posterior(posteriors, i)_____no_output_____

Let's run the filter and animate it._____no_output_____

# change these numbers to alter the simulation
kernel = (.1, .8, .1)
z_prob = 1.0
hallway = np.array([1, 1, 0, 0, 0, 0, 0, 0, 1, 0])

# measurements with no noise
zs = [hallway[i % len(hallway)] for i in range(50)]

priors, posteriors = discrete_bayes_sim(prior, kernel, zs, z_prob, hallway)
interact(animate_discrete_bayes, step=IntSlider(value=1, max=len(zs)*2));_____no_output_____

Now we can see the results. You can see how the prior shifts the position and reduces certainty, and the posterior stays in the same position and increases certainty as it incorporates the information from the measurement. I've made the measurement perfect with the line `z_prob = 1.0`; we will explore the effect of imperfect measurements in the next section. Finally,

Another thing to note is how accurate our estimate becomes when we are in front of a door, and how it degrades when in the middle of the hallway. This should make intuitive sense. There are only a few doorways, so when the sensor tells us we are in front of a door this boosts our certainty in our position. A long stretch of no doors reduces our certainty._____no_output_____## The Effect of Bad Sensor Data

You may be suspicious of the results above because I always passed correct sensor data into the functions. However, we are claiming that this code implements a *filter* - it should filter out bad sensor measurements. Does it do that?

To make this easy to program and visualize I will change the layout of the hallway to mostly alternating doors and hallways, and run the algorithm on 6 correct measurements:_____no_output_____

hallway = np.array([1, 0, 1, 0, 0]*2)
kernel = (.1, .8, .1)
prior = np.array([.1] * 10)
zs = [1, 0, 1, 0, 0, 1]
z_prob = 0.75
priors, posteriors = discrete_bayes_sim(prior, kernel, zs, z_prob, hallway)
interact(animate_discrete_bayes, step=IntSlider(value=12, max=len(zs)*2));_____no_output_____

We have identified the likely cases of having started at position 0 or 5, because we saw this sequence of doors and walls: 1,0,1,0,0. Now I inject a bad measurement. The next measurement should be 0, but instead we get a 1:_____no_output_____

measurements = [1, 0, 1, 0, 0, 1, 1]
priors, posteriors = discrete_bayes_sim(prior, kernel, measurements, z_prob, hallway);
plot_posterior(posteriors, 6)_____no_output_____

That one bad measurement has significantly eroded our knowledge. Now let's continue with a series of correct measurements._____no_output_____

with figsize(y=5.5):
    measurements = [1, 0, 1, 0, 0, 1, 1, 1, 0, 0]
    for i, m in enumerate(measurements):
        likelihood = lh_hallway(hallway, z=m, z_prob=.75)
        posterior = update(likelihood, prior)
        prior = predict(posterior, 1, kernel)
        plt.subplot(5, 2, i+1)
        book_plots.bar_plot(posterior, ylim=(0, .4), title='step {}'.format(i+1))
    plt.tight_layout()_____no_output_____

We quickly filtered out the bad sensor reading and converged on the most likely positions for our dog._____no_output_____## Drawbacks and Limitations

Do not be mislead by the simplicity of the examples I chose. This is a robust and complete filter, and you may use the code in real world solutions. If you need a multimodal, discrete filter, this filter works.

With that said, this filter it is not used often because it has several limitations. Getting around those limitations is the motivation behind the chapters in the rest of this book.

The first problem is scaling. Our dog tracking problem used only one variable, $pos$, to denote the dog's position. Most interesting problems will want to track several things in a large space. Realistically, at a minimum we would want to track our dog's $(x,y)$ coordinate, and probably his velocity $(\dot{x},\dot{y})$ as well. We have not covered the multidimensional case, but instead of an array we use a multidimensional grid to store the probabilities at each discrete location. Each `update()` and `predict()` step requires updating all values in the grid, so a simple four variable problem would require $O(n^4)$ running time *per time step*. Realistic filters can have 10 or more variables to track, leading to exorbitant computation requirements.

The second problem is that the filter is discrete, but we live in a continuous world. The histogram requires that you model the output of your filter as a set of discrete points. A 100 meter hallway requires 10,000 positions to model the hallway to 1cm accuracy. So each update and predict operation would entail performing calculations for 10,000 different probabilities. It gets exponentially worse as we add dimensions. A 100x100 m$^2$ courtyard requires 100,000,000 bins to get 1cm accuracy.

A third problem is that the filter is multimodal. In the last example we ended up with strong beliefs that the dog was in position 4 or 9. This is not always a problem. Particle filters, which we will study later, are multimodal and are often used because of this property. But imagine if the GPS in your car reported to you that it is 40% sure that you are on D street, and 30% sure you are on Willow Avenue.

A forth problem is that it requires a measurement of the change in state. We need a motion sensor to detect how much the dog moves. There are ways to work around this problem, but it would complicate the exposition of this chapter, so, given the aforementioned problems, I will not discuss it further.

With that said, if I had a small problem that this technique could handle I would choose to use it; it is trivial to implement, debug, and understand, all virtues._____no_output_____## Tracking and Control

We have been passively tracking an autonomously moving object. But consider this very similar problem. I am automating a warehouse and want to use robots to collect all of the items for a customer's order. Perhaps the easiest way to do this is to have the robots travel on a train track. I want to be able to send the robot a destination and have it go there. But train tracks and robot motors are imperfect. Wheel slippage and imperfect motors means that the robot is unlikely to travel to exactly the position you command. There is more than one robot, and we need to know where they all are so we do not cause them to crash.

So we add sensors. Perhaps we mount magnets on the track every few feet, and use a Hall sensor to count how many magnets are passed. If we count 10 magnets then the robot should be at the 10th magnet. Of course it is possible to either miss a magnet or to count it twice, so we have to accommodate some degree of error. We can use the code from the previous section to track our robot since magnet counting is very similar to doorway sensing.

But we are not done. We've learned to never throw information away. If you have information you should use it to improve your estimate. What information are we leaving out? We know what control inputs we are feeding to the wheels of the robot at each moment in time. For example, let's say that once a second we send a movement command to the robot - move left 1 unit, move right 1 unit, or stand still.  If I send the command 'move left 1 unit' I expect that in one second from now the robot will be 1 unit to the left of where it is now. This is a simplification because I am not taking acceleration into account, but I am not trying to teach control theory. Wheels and motors are imperfect. The robot might end up 0.9 units away, or maybe 1.2 units.

Now the entire solution is clear. We assumed that the dog kept moving in whatever direction he was previously moving. That is a dubious assumption for my dog! Robots are far more predictable. Instead of making a dubious prediction based on assumption of behavior we will feed in the command that we sent to the robot! In other words, when we call `predict()` we will pass in the commanded movement that we gave the robot along with a kernel that describes the likelihood  of that movement._____no_output_____### Simulating the Train Behavior

We need to simulate an imperfect train. When we command it to move it will sometimes make a small mistake, and its sensor will sometimes return the incorrect value._____no_output_____

class Train(object):

    def __init__(self, track_len, kernel=[1.], sensor_accuracy=.9):
        self.track_len = track_len
        self.pos = 0
        self.kernel = kernel
        self.sensor_accuracy = sensor_accuracy

    def move(self, distance=1):
        """ move in the specified direction
        with some small chance of error"""

        self.pos += distance
        # insert random movement error according to kernel
        r = random.random()
        s = 0
        offset = -(len(self.kernel) - 1) / 2
        for k in self.kernel:
            s += k
            if r <= s:
                break
            offset += 1
        self.pos = int((self.pos + offset) % self.track_len)
        return self.pos

    def sense(self):
        pos = self.pos
         # insert random sensor error
        if random.random() > self.sensor_accuracy:
            if random.random() > 0.5:
                pos += 1
            else:
                pos -= 1
        return pos_____no_output_____

With that we are ready to write the filter. We will put it in a function so that we can run it with different assumptions. I will assume that the robot always starts at the beginning of the track. The track is implemented as being 10 units long, but think of it as a track of length, say 10,000, with the magnet pattern repeated every 10 units. A length of 10 makes it easier to plot and inspect._____no_output_____

def train_filter(iterations, kernel, sensor_accuracy,
             move_distance, do_print=True):
    track = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    prior = np.array([.9] + [0.01]*9)
    posterior = prior[:]
    normalize(prior)

    robot = Train(len(track), kernel, sensor_accuracy)
    for i in range(iterations):
        # move the robot and
        robot.move(distance=move_distance)

        # peform prediction
        prior = predict(posterior, move_distance, kernel)

        #  and update the filter
        m = robot.sense()
        likelihood = lh_hallway(track, m, sensor_accuracy)
        posterior = update(likelihood, prior)
        index = np.argmax(posterior)

        if do_print:
            print('''time {}: pos {}, sensed {}, '''
                  '''at position {}'''.format(
                    i, robot.pos, m, track[robot.pos]))

            print('''        estimated position is {}'''
                  ''' with confidence {:.4f}%:'''.format(
                  index, posterior[index]*100))

    book_plots.bar_plot(posterior)
    if do_print:
        print()
        print('final position is', robot.pos)
        index = np.argmax(posterior)
        print('''Estimated position is {} with '''
              '''confidence {:.4f}%:'''.format(
                index, posterior[index]*100))_____no_output_____

Read the code and make sure you understand it. Now let's do a run with no sensor or movement error. If the code is correct it should be able to locate the robot with no error. The output is a bit tedious to read, but if you are at all unsure of how the update/predict cycle works make sure you read through it carefully to solidify your understanding._____no_output_____

import random

random.seed(3)
np.set_printoptions(precision=2, suppress=True, linewidth=60)
train_filter(4, kernel=[1.], sensor_accuracy=.999,
             move_distance=4, do_print=True)time 0: pos 4, sensed 4, at position 4
        estimated position is 4 with confidence 99.9900%:
time 1: pos 8, sensed 8, at position 8
        estimated position is 8 with confidence 100.0000%:
time 2: pos 2, sensed 2, at position 2
        estimated position is 2 with confidence 100.0000%:
time 3: pos 6, sensed 6, at position 6
        estimated position is 6 with confidence 100.0000%:

final position is 6
Estimated position is 6 with confidence 100.0000%:

We can see that the code was able to perfectly track the robot so we should feel reasonably confident that the code is working. Now let's see how it fairs with some errors. _____no_output_____

random.seed(5)
train_filter(4, kernel=[.1, .8, .1], sensor_accuracy=.9,
         move_distance=4, do_print=True)time 0: pos 4, sensed 4, at position 4
        estimated position is 4 with confidence 96.0390%:
time 1: pos 8, sensed 9, at position 8
        estimated position is 9 with confidence 52.1180%:
time 2: pos 3, sensed 3, at position 3
        estimated position is 3 with confidence 88.3993%:
time 3: pos 7, sensed 8, at position 7
        estimated position is 8 with confidence 49.3174%:

final position is 7
Estimated position is 8 with confidence 49.3174%:

There was a sensing error at time 1, but we are still quite confident in our position.

Now let's run a very long simulation and see how the filter responds to errors._____no_output_____

with figsize(y=5.5):
    for i in range (4):
        random.seed(3)
        plt.subplot(221+i)
        train_filter(148+i, kernel=[.1, .8, .1],
                     sensor_accuracy=.8,
                     move_distance=4, do_print=False)
        plt.title ('iteration {}'.format(148+i))_____no_output_____

We can see that there was a problem on iteration 149 as the confidence degrades. But within a few iterations the filter is able to correct itself and regain confidence in the estimated position._____no_output_____## Bayes Theorem and the Total Probability Theorem_____no_output_____We developed the math in this chapter merely by reasoning about the information we have at each moment. In the process we discovered [*Bayes' Theorem*](https://en.wikipedia.org/wiki/Bayes%27_theorem) and the [*Total Probability Theorem*](https://en.wikipedia.org/wiki/Law_of_total_probability).

Bayes theorem tells us how to compute the probability of an event given previous information.

We implemented the `update()` function with this probability calculation:

$$ \mathtt{posterior} = \frac{\mathtt{likelihood}\times \mathtt{prior}}{\mathtt{normalization\, factor}}$$

We haven't developed the mathematics to discuss Bayes yet, but this is Bayes' theorem. Every filter in this book is an expression of Bayes' theorem. In the next chapter we will develop the mathematics, but in many ways that obscures the simple idea expressed in this equation:

$$ updated\,knowledge = \big\|likelihood\,of\,new\,knowledge\times prior\, knowledge \big\|$$

where $\| \cdot\|$ expresses normalizing the term.

We came to this with simple reasoning about a dog walking down a hallway. Yet, as we will see the same equation applies to a universe of filtering problems. We will use this equation in every subsequent chapter.

Likewise, the `predict()` step computes the total probability of multiple possible events. This is known as the *Total Probability Theorem* in statistics, and we will also cover this in the next chapter after developing some supporting math.

For now I need you to understand that Bayes' theorem is a formula to incorporate new information into existing information._____no_output_____## Summary

The code is very short, but the result is impressive! We have implemented a form of a Bayesian filter. We have learned how to start with no information and derive information from noisy sensors. Even though the sensors in this chapter are very noisy (most sensors are more than 80% accurate, for example) we quickly converge on the most likely position for our dog. We have learned how the predict step always degrades our knowledge, but the addition of another measurement, even when it might have noise in it, improves our knowledge, allowing us to converge on the most likely result.

This book is mostly about the Kalman filter. The math it uses is different, but the logic is exactly the same as used in this chapter. It uses Bayesian reasoning to form estimates from a combination of measurements and process models.

**If you can understand this chapter you will be able to understand and implement Kalman filters.** I cannot stress this enough. If anything is murky, go back and reread this chapter and play with the code. The rest of this book will build on the algorithms that we use here. If you don't understand why this filter works you will have little success with the rest of the material. However, if you grasp the fundamental insight - multiplying probabilities when we measure, and shifting probabilities when we update leads to a converging solution - then after learning a bit of math you are ready to implement a Kalman filter._____no_output_____## References

 * [1] D. Fox, W. Burgard, and S. Thrun. "Monte carlo localization: Efficient position estimation for mobile robots." In *Journal of Artifical Intelligence Research*, 1999.

 http://www.cs.cmu.edu/afs/cs/project/jair/pub/volume11/fox99a-html/jair-localize.html

 * [2] Dieter Fox, et. al. "Bayesian Filters for Location Estimation". In *IEEE Pervasive Computing*, September 2003.

 http://swarmlab.unimaas.nl/wp-content/uploads/2012/07/fox2003bayesian.pdf

 * [3] Sebastian Thrun. "Artificial Intelligence for Robotics".

 https://www.udacity.com/course/cs373

 * [4] Khan Acadamy. "Introduction to the Convolution"

 https://www.khanacademy.org/math/differential-equations/laplace-transform/convolution-integral/v/introduction-to-the-convolution

* [5] Wikipedia. "Convolution"

 http://en.wikipedia.org/wiki/Convolution

* [6] Wikipedia. "Law of total probability"

  http://en.wikipedia.org/wiki/Law_of_total_probability

* [7] Wikipedia. "Time Evolution"

 https://en.wikipedia.org/wiki/Time_evolution

* [8] We need to rethink how we teach statistics from the ground up

 http://www.statslife.org.uk/opinion/2405-we-need-to-rethink-how-we-teach-statistics-from-the-ground-up_____no_output_____


# Notebook from hidekb/pyross
Path: examples/hybrid/ex1-SIR.ipynb

## Hybrid simulation of the stochastic SIR model

The SIR model of epidemiology partitions the population into three compartments: susceptibles, S, who can catch the disease; infectives, I, who have already caught the disease and infect susceptibles; and recovered individuals, R. Since the disease is assumed not to be fatal, the sum $N=S+I+R$ remains constant. The rate at which the susceptibles get infected is
$$
\lambda(t) = \frac{\beta I}{N}
$$
where the parameter $\beta$ is the probability of infection on contact. The infected individuals recover from the disease at a rate $\gamma$.

While stochastic simulations of this model can be rather slow for large population numbers, stochastic effects are also less rather relevant when population numbers are large.

The *hybrid method* for stochastic simulation of the SIR model exploits this, by
* simulating the stochastic dynamics if any of the population numbers is below a given threshold, and
* simulating the deterministic dynamics if all population numbers are above a given threshold.

The present example creates realisations for stochastic trajectories using this algorithm. For $200$ stochastic trajectories, the mean and variance of the observed trajectories for the number of infectives is calculated, plotted as a function of time, and compared to results from the corresponding fully-stochastic SIR model._____no_output_____

%matplotlib inline
import numpy as np
import pyross
import matplotlib.pyplot as plt
from timeit import default_timer as timer
#from matplotlib import rc; rc('text', usetex=True)_____no_output_____

### Simulation of a single stochastic trajectory_____no_output_____

M = 1                  # the SIR model has no age structure
Ni = 20000*np.ones(M)   # so there is only one age group
N = np.sum(Ni)         # and the total population is the size of this age group

beta  = 0.2            # infection rate
gIa   = 0.1            # recovery rate of asymptomatic infectives
gIs   = 0.1            # recovery rate of symptomatic infectives
alpha = 0              # fraction of asymptomatic infectives
fsa   = 1              # the self-isolation parameter

Ia0 = np.array([0])     # the SIR model has only one kind of infective
Is0 = np.array([10])     # we take these to be symptomatic
R0  = np.array([0])     # and assume there are no recovered individuals initially
S0  = N-(Ia0+Is0+R0)    # so that the initial susceptibles are obtained from S + Ia + Is + R = N

# there is no contact structure
def contactMatrix(t):
    return np.identity(M)

# duration of simulation and data file
Tf = 160;  Nt=160;

# threshold values
# note that these values are just for demonstration, and in practice
# are too low to assume that stochastic effects become irrelevant
Ias = np.array([0])
Iss = np.array([1000])
Ss = np.array([1000])

# if the simulation is in the stochastic mode and all numbers pass
# the following threshold, the simulation switches to deterministic dynamics
thresholds_from_below = (Ss,Ias,Iss)

# if the simulation is in the deterministic mode and all numbers pass
# the following threshold, the simulation switches to stochastic dynamics
Ias2 = np.array([0])
Iss2 = np.array([500])
Ss2 = np.array([1000])
thresholds_from_above = (Ss2,Ias2,Iss2)

thresholds = {'from_below':thresholds_from_below,
             'from_above':thresholds_from_above}

# instantiate model
parameters = {'alpha':alpha, 'beta':beta, 'gIa':gIa, 'gIs':gIs,'fsa':fsa}
model = pyross.hybrid.SIR(parameters, M, Ni)

# simulate model
data = model.simulate(S0, Ia0, Is0, contactMatrix, Tf, thresholds)_____no_output_____# plot the data and obtain the stochastic epidemic curve
S  = data['X'][:,0].flatten()
Ia = data['X'][:,1].flatten()
Is = data['X'][:,2].flatten()
t = data['t']

fig = plt.figure(num=None, figsize=(10, 8), dpi=80, facecolor='w', edgecolor='k')
plt.rcParams.update({'font.size': 22})

plt.fill_between(t, 0, S/N, color="#348ABD", alpha=0.3)
plt.plot(t, S/N, '-', color="#348ABD", label='$S$', lw=4)

plt.fill_between(t, 0, Is/N, color='#A60628', alpha=0.3)
plt.plot(t, Is/N, '-', color='#A60628', label='$I$', lw=4)

R=N-S-Ia-Is; plt.fill_between(t, 0, R/N, color="dimgrey", alpha=0.3)
plt.plot(t, R/N, '-', color="dimgrey", label='$R$', lw=4)

plt.legend(fontsize=26); plt.grid()
plt.autoscale(enable=True, axis='x', tight=True)_____no_output_____

### Averaging over several simulations

If the simulation is run many times, mean and standard deviation of the resulting trajectories can be calculated._____no_output_____

# run 100 simulations, calculate mean and standard deviation

N_runs = 100

trajectories_h = np.zeros([N_runs,Nt+1,3],dtype=float)
start_h = timer()
for i in range(N_runs):
    print("Simulating trajectory {0} of {1}".format(i+1,N_runs),end='\r')
    cur_result = model.simulate(S0, Ia0, Is0, contactMatrix, Tf, thresholds)
    trajectories_h[i] = cur_result['X']
end_h = timer()
print("{0} simulations finished in {1:3.2f} seconds".format(N_runs,end_h-start_h))
t_h = cur_result['t']
mean_h = np.mean(trajectories_h,axis=0)
std_h = np.std(trajectories_h,axis=0)100 simulations finished in 19.48 seconds
# plot mean and standard deviation

S_h  = mean_h[:,0].flatten()
Ia_h = mean_h[:,1].flatten()
Is_h = mean_h[:,2].flatten()
dS_h = std_h[:,0].flatten()
dIa_h = std_h[:,1].flatten()
dIs_h = std_h[:,2].flatten()
# for the recovered, we still need to calculate mean and variance
R_h_trajectories = N - trajectories_h[:,:,0] - trajectories_h[:,:,1] - trajectories_h[:,:,2]
R_h = np.mean(R_h_trajectories,axis=0)
dR_h = np.std(R_h_trajectories,axis=0)

fig = plt.figure(num=None, figsize=(10, 8), dpi=80, facecolor='w', edgecolor='k')
plt.rcParams.update({'font.size': 22})

# hybrid
plt.fill_between(t_h,(S_h - dS_h)/N,(S_h + dS_h)/N,color="#348ABD", alpha=0.2)
plt.plot(t_h, S_h/N, '-', color="#348ABD", label=r'$\langle S\rangle$', lw=4)
plt.fill_between(t_h,(Is_h - dIs_h)/N,(Is_h + dIs_h)/N,color="#A60628", alpha=0.2)
plt.plot(t_h, Is_h/N, '-', color="#A60628", label=r'$\langle I\rangle$', lw=4)
plt.fill_between(t_h,(R_h - dR_h)/N,(R_h + dR_h)/N,color="dimgrey", alpha=0.2)
plt.plot(t_h, R_h/N, '-', color="dimgrey", label=r'$\langle R\rangle $', lw=4)

plt.legend(fontsize=26,loc='upper right',bbox_to_anchor=(1.3,1)); plt.grid()
plt.autoscale(enable=True, axis='x', tight=True)_____no_output_____

While the solid lines denote the instantaneous means, the colored shaded regions denote the standard deviation of the samples from the instantaneous mean._____no_output_____### Comparison of the results from the hybrid simulations with averages over stochastic simulations

We now show that running fully stochastic simulations yields the same mean as obtained from the hybrid simulations, and compare the respective times needed for the simulations.

*Note: The following cell might take some time to evaluate.*_____no_output_____

# for comparison, we also evaluate the deterministic SIR model
model_s = pyross.stochastic.SIR(parameters, M, Ni)

trajectories_s = np.zeros([N_runs,Nt+1,3],dtype=float)
start_s = timer()
for i in range(N_runs):
    print("Simulating trajectory {0} of {1}".format(i+1,N_runs),end='\r')
    cur_result = model_s.simulate(S0, Ia0, Is0, contactMatrix, Tf, Nt)
    trajectories_s[i] = cur_result['X']
end_s = timer()
print("{0} simulations finished in {1:3.2f} seconds".format(N_runs,end_s-start_s))
t_s = cur_result['t']
mean_s = np.mean(trajectories_s,axis=0)
std_s = np.std(trajectories_s,axis=0)

S_s  = mean_s[:,0].flatten()
Ia_s = mean_s[:,1].flatten()
Is_s = mean_s[:,2].flatten()
dS_s = std_s[:,0].flatten()
dIa_s = std_s[:,1].flatten()
dIs_s = std_s[:,2].flatten()
# for the recovered, we still need to calculate mean and variance
R_s_trajectories = N - trajectories_s[:,:,0] - trajectories_s[:,:,1] - trajectories_s[:,:,2]
R_s = np.mean(R_s_trajectories,axis=0)
dR_s = np.std(R_s_trajectories,axis=0)100 simulations finished in 59.72 seconds
# compare hybrid and fully stochastic model

fig = plt.figure(num=None, figsize=(10, 8), dpi=80, facecolor='w', edgecolor='k')
plt.rcParams.update({'font.size': 22})

# hybrid
plt.fill_between(t_h,(S_h - dS_h)/N,(S_h + dS_h)/N,color="#348ABD", alpha=0.2)
plt.plot(t_h, S_h/N, '-', color="#348ABD", label=r'$\langle S\rangle$', lw=4)
plt.fill_between(t_h,(Is_h - dIs_h)/N,(Is_h + dIs_h)/N,color="#A60628", alpha=0.2)
plt.plot(t_h, Is_h/N, '-', color="#A60628", label=r'$\langle I\rangle$', lw=4)
plt.fill_between(t_h,(R_h - dR_h)/N,(R_h + dR_h)/N,color="dimgrey", alpha=0.2)
plt.plot(t_h, R_h/N, '-', color="black", label=r'$\langle R\rangle $', lw=4)

# stochastic
plt.plot(t_s, S_s/N, '--', color="crimson", label='$S$', lw=4,alpha=0.8)
plt.plot(t_s, (S_s-dS_s)/N, '--', color="crimson", lw=1,alpha=0.7)
plt.plot(t_s, (S_s+dS_s)/N, '--', color="crimson", lw=1,alpha=0.7)
plt.plot(t_s, Is_s/N, '--', color='dodgerblue', label='$I$', lw=4)
plt.plot(t_s, (Is_s-dIs_s)/N, '--', color='dodgerblue',  lw=1)
plt.plot(t_s, (Is_s+dIs_s)/N, '--', color='dodgerblue',  lw=1)
plt.plot(t_s, R_s/N, '--', color="limegreen", label='$R$', lw=4)
plt.plot(t_s, (R_s-dR_s)/N, '--', color="limegreen",  lw=1)
plt.plot(t_s, (R_s+dR_s)/N, '--', color="limegreen",  lw=1)

plt.legend(fontsize=26,loc='upper right',bbox_to_anchor=(1.3,1)); plt.grid()
plt.autoscale(enable=True, axis='x', tight=True)_____no_output_____

The dashed colored lines denotes the mean and corresponding standard deviation obtained from the stochastic SIR model. As can be seen, the results from hybrid- and stochastic method are almost identical. For larger systems, and using larger thresholds in the hybrid method, the differences between hybrid method and fully stochastic simulations will be smaller.

Note that while the means plotted above are almost identical, the times needed to calculate these curves are not:_____no_output_____

print("Hybrid method needed {0:3.2f} seconds".format(end_h-start_h))
print("Stochastic method needed {0:3.2f} seconds".format(end_s-start_s))
print("Speedup-factor obtained from using hybrid method = {0:3.2f}".format((end_s-start_s)/(end_h-start_h)))Hybrid method needed 19.48 seconds
Stochastic method needed 59.72 seconds
Speedup-factor obtained from using hybrid method = 3.07


# Notebook from geosciences-data-practicum/reanalysis_getter
Path: notebooks/grid_plots_efflat.ipynb

<code>
import os
import dask
import joypy
import numpy as np
import xarray as xr
import pandas as pd
import seaborn as sns
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from pathlib import Path

# To use dask across the cluster
from dask_jobqueue import SLURMCluster
from dask.distributed import Client

import matplotlib
matplotlib.rcParams.update({'font.size': 15})_____no_output_____def group_into_winters(dates): #untested
    year_arr = np.zeros(len(dates),dtype=int)
    y=0
    for date in dates:
        if date.month < 3:
            year_arr[y] = date.year - 1
        else:
            year_arr[y] = date.year
        y+=1
    return year_arr

def joy_div(data,
            time_key='year',
            time_var='time',
            data_key='t_prime',
            decade=False):
    ''' accepts a dataframe and make a joy division plot of successive distributions in data_key.
     If decade=False, these distributions are calculated yearly. If True, they are calculated every 10 years'''

    data['year'] = group_into_winters(data[time_var])
    time_key='year'
    if decade:
        time_key='decade'
        data['decade'] = data.year.apply(lambda x: int(x)//10 * 10)
    data_filter = data.filter([time_key, data_key])

    labels=[y if y%5==0 else None for y in list(data[time_key].unique())]
    fig, axes = joypy.joyplot(data_filter,
                          by=time_key,
                              labels=labels,
                              grid='y',
                              linewidth=1,
                          column=data_key,
                          fade=True,
                          figsize=(10, 10))
    plt.xlabel("Effective Latitude [degrees]")
    plt.ylabel("year")
    plt.show()
    plt.savefig('/home/ivanhigueram/public_html/joy_plt_prj.png')
    return data_filter

def overlayed_decades(data,
                      ax=None,
                      model=None,
                      decade=None,
                      grid=False,
                      var='t_prime',
                      clr='blue'):

    if model is not None:
        data = data[data.model.isin(model)]

    if decade is not None:
        data = data[data.decade.isin(decade)]

    if grid:
        g = sns.FacetGrid(data,
                         row="model",
                         col="place",
                         hue="decade",
                         sharex=True,
                         margin_titles=True
                         )
        g.map(sns.kdeplot,
              "eff_lat_deg",
              shade=True,
              alpha=0.3,
              #Label=str(decade)+'s'
             )
        g.fig.subplots_adjust(top=0.9)
        g.fig.suptitle('Effective Latitude for a set of global locations')
        g.add_legend()
    else:
        data_in_decade = data.loc[(data['model'] == model) &
                                  (data['decade'] == decade), var]

        sns.kdeplot(data_in_decade,
                    ax=ax,
                    shade=True,
                    alpha=0.3,
                    Label=str(decade)+'s',
                    color=clr)

        quantiles = np.quantile(data_in_decade,[0.1,0.9])

        plt.axvline(quantiles[0],color=clr,linestyle='--',alpha=0.5)
        plt.axvline(quantiles[1],color=clr,linestyle='--',alpha=0.5)_____no_output_____overlayed_decades(eff_lat_all_mods,
                  decade=[2020, 2090],
                  grid=True)_____no_output_____list_df = []
for f in Path('.').glob('*.csv'):
    print(f)
    model_name = f.stem.split('_')[0]
    df = pd.read_csv(f)

    df['time'] = pd.to_datetime(df.time,
                                errors='coerce',
                                utc=True)
    df['model'] = model_name
    df = df[~ df.time.isna()]
    df['decade'] = df.time.dt.year.apply(lambda x: int(x)//10 * 10)
    df['place'] = df.place.str.capitalize()

    list_df.append(df)

eff_lat_all_mods = pd.concat(list_df)
eff_lat_all_mods_comp = eff_lat_all_mods[
    eff_lat_all_mods.decade.isin([2030, 2090])
]GFDL_eff_lat.csv
CESM2_eff_lat.csv
MPI_eff_lat.csv
UKESM1_eff_lat.csv
IPSL_eff_lat.csv
colors = ['#b2182b',
          '#d6604d',
          '#f4a582',
          '#fddbc7',
          '#f7f7f7',
          '#d1e5f0',
          '#92c5de',
          '#4393c3',
          '#2166ac']
years = [2010, 2020, 2030, 2040, 2050, 2060, 2070, 2080, 2090]

years.reverse()

fig, ax = plt.subplots(figsize=(8, 8))
for decade, color in zip(years, colors):
    overlayed_decades(eff_lat_all_mods,
                      decade=decade,
                      model='CESM2Test_test',
                      clr=color,
                      ax=ax,
                      var='eff_lat_deg')
plt.title('CESM pre-processed: > 20 degrees')
plt.xlabel("Effective Latitude [degrees]")
plt.ylabel("Density")_____no_output_____colors = ['#b2182b',
          '#d6604d',
          '#f4a582',
          '#fddbc7',
          '#f7f7f7',
          '#d1e5f0',
          '#92c5de',
          '#4393c3',
          '#2166ac']
years = [2010, 2020, 2030, 2040, 2050, 2060, 2070, 2080, 2090]

years.reverse()

fig, ax = plt.subplots(figsize=(8, 8))
for decade, color in zip(years, colors):
    overlayed_decades(eff_lat_all_mods,
                      decade=decade,
                      model='CESM2',
                      clr=color,
                      ax=ax,
                      var='eff_lat_deg')
plt.xlabel("Effective Latitude [degrees]")
plt.title('CESM raw: all the globe and winter')
plt.ylabel("Density")_____no_output_____pivot_test  = eff_lat_all_mods.filter(['decade', 'model', 'eff_lat_deg', 'time']).\
pivot(index=['decade', 'time'], columns='model', values='eff_lat_deg').reset_index()
pivot_test_____no_output_____from matplotlib import cm
fig, axes = joypy.joyplot(pivot_test[pivot_test.decade.isin([2020, 2050, 2090])],
                          fill=True,
                          legend=True,
                          #fade=True,
                          linewidth=1,
                          alpha=0.5,
                          figsize=(10, 10),
                          #range_style='own',
                          by="decade",
                          title="Effective latitude for Chicago, IL \n[42°, 82°]",
                          column=['CESM2','CESM2Test_test','CESM2v0.1']
                          #column=['CESM2','GFDL','IPSL','MPI','UKESM1'],
                          #color=["grey","salmon","tomato"]
                         )
plt.xlabel("Effective Latitude [degrees]")
plt.ylabel("Global Climate Model")_____no_output_____ colors = ['#1b9e77',
          '#d95f02',
          #'#7570b3',
          #'#e7298a',
          '#66a61e'
        ]
models = eff_lat_all_mods_comp.model.unique()

fig, ax = plt.subplots(figsize=(8, 8))
for model, color in zip(models, colors):
    overlayed_decades(eff_lat_all_mods_comp,
                      model,
                      decade=2090,
                      clr=color,
                      ax=ax,
                      var='eff_lat_deg')
plt.xlabel("Effective Latitude [degrees]")
plt.ylabel("Density")_____no_output_____joy_div(test_df,
        time_key='time',
        data_key='eff_lat_deg',
        decade=False)_____no_output_____
</code>


# Notebook from jorisvandenbossche/2018-Bordeaux-pandas-course
Path: _solved/notebooks/case2_biodiversity_analysis.ipynb

 CASE - Biodiversity data - analysis

> *DS Data manipulation, analysis and visualisation in Python*
> *December, 2017*

> *© 2016, Joris Van den Bossche and Stijn Van Hoey  (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*

---_____no_output_____

%matplotlib inline

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use('seaborn-whitegrid')_____no_output_____

## Reading in the enriched survey data set_____no_output_____

    EXERCISE:

Read in the 'survey_data_completed.csv' file and save the resulting DataFrame as variable `survey_data_processed` (if you did not complete the previous notebook, a version of the csv file is also available in the `../data` folder).

Interpret the 'eventDate' column directly as python datetime object and make sure the 'occurrenceID' column is used as the index of the resulting DataFrame (both can be done at once when reading the csv file)

Inspect the resulting frame (remember `.head()` and `.info()`) and check that the 'eventDate' indeed has a datetime data type.

_____no_output_____

# %load _solutions/case2_biodiversity_analysis2.py_____no_output_____# %load _solutions/case2_biodiversity_analysis3.py_____no_output_____# %load _solutions/case2_biodiversity_analysis4.py_____no_output_____

## Tackle missing values (NaN) and duplicate values_____no_output_____

    EXERCISE: How many records are in the data set without information on the 'species' name?

_____no_output_____

# %load _solutions/case2_biodiversity_analysis5.py_____no_output_____

    EXERCISE: How many duplicate records are present in the dataset?

_____no_output_____

# %load _solutions/case2_biodiversity_analysis6.py_____no_output_____

    EXERCISE: Extract a list of all duplicates (check documentation of `duplicated`!), sort on the columns `eventDate` and `verbatimLocality` and show the first 10 records

_____no_output_____

# %load _solutions/case2_biodiversity_analysis7.py_____no_output_____

        EXERCISE: Exclude the duplicate values from the survey data set and save the result as `survey_data_unique`

_____no_output_____

# %load _solutions/case2_biodiversity_analysis8.py_____no_output_____len(survey_data_unique)_____no_output_____

    EXERCISE: For how many records we have all the information available (i.e. no NaN values in any of the columns)?

_____no_output_____

# %load _solutions/case2_biodiversity_analysis10.py_____no_output_____

    EXERCISE: Select the subset of records without a species name, while having information on the sex and store the result as variable `not_identified`

_____no_output_____

# %load _solutions/case2_biodiversity_analysis11.py_____no_output_____not_identified.head()_____no_output_____

    EXERCISE: Select only those records that do have species information and save them as the variable `survey_data`. Make sure `survey_data` is a copy of the original DataFrame. This is the DataFrame we will use in the further analyses.

_____no_output_____

# %load _solutions/case2_biodiversity_analysis13.py_____no_output_____

    NOTE: For biodiversity studies, absence values (knowing that someting is not present) are useful as well to normalize the observations, but this is out of scope for these exercises.

_____no_output_____## Observations over time_____no_output_____

    EXERCISE: Make a plot visualizing the evolution of the number of observations for each of the individual years.

_____no_output_____

# %load _solutions/case2_biodiversity_analysis14.py_____no_output_____

To evaluate the intensity or number of occurrences during different time spans, a heatmap is an interesting representation. This functionality is provided by the plotting library [seaborn](http://seaborn.pydata.org/generated/seaborn.heatmap.html). Let's reformat this data to plot is as a heatmap._____no_output_____

    EXERCISE: Create a table, called `heatmap_prep`, based on the `survey_data` DataFrame with in the row index the years, in the column the months and as values of the table, the counts for each of these year/month combinations.

_____no_output_____

# %load _solutions/case2_biodiversity_analysis15.py_____no_output_____# %load _solutions/case2_biodiversity_analysis16.py_____no_output_____# %load _solutions/case2_biodiversity_analysis17.py_____no_output_____

    EXERCISE: Based on the `heatmap_prep` DataFrame, create a *long* format of the table with the columns `year`, `month` and `count` and call the resulting variable `heatmap_tidy`.

_____no_output_____

# %load _solutions/case2_biodiversity_analysis18.py_____no_output_____

## Species abundance for each of the plots_____no_output_____The name of the observed species consists of two parts: the 'genus' and 'species' columns. For the further analyses, we want the combined name. This is already available as the 'name' column if you completed the previous notebook, otherwise you can add this again in the following exercise._____no_output_____

    EXERCISE: Make a new column 'name' that combines the 'Genus' and 'species' columns (with a space in between).

_____no_output_____

# %load _solutions/case2_biodiversity_analysis19.py_____no_output_____

    EXERCISE: Which 8 species have been observed most of all?

_____no_output_____

# %load _solutions/case2_biodiversity_analysis20.py_____no_output_____# %load _solutions/case2_biodiversity_analysis21.py_____no_output_____

    EXERCISE: How many records are available of each of the species in each of the plots (called `verbatimLocality`)? How would you visualize this information with seaborn?

_____no_output_____

# %load _solutions/case2_biodiversity_analysis22.py_____no_output_____# %load _solutions/case2_biodiversity_analysis23.py_____no_output_____

    EXERCISE: What is the number of different species in each of the plots? Make a bar chart, using Pandas `plot` function, providing for each plot the diversity of species, by defining a matplotlib figure and ax to make the plot. Change the y-label to 'plot number'

_____no_output_____

# %load _solutions/case2_biodiversity_analysis24.py_____no_output_____

    EXERCISE: What is the number of plots each species have been observed? Make an horizontal bar chart using Pandas `plot` function providing for each species the spread amongst the plots for which the species names are sorted to the number of plots

_____no_output_____

# %load _solutions/case2_biodiversity_analysis25.py_____no_output_____

    EXERCISE: Evaluate the amount of males and females present in each of the plots. Make sure to exclude NaN-values and save the result (with the verbatimLocality as index and sex as column names in a variable `n_plot_sex`

_____no_output_____

# %load _solutions/case2_biodiversity_analysis26.py_____no_output_____# %load _solutions/case2_biodiversity_analysis27.py_____no_output_____

As such, we can use the variable `n_plot_sex` to plot the result:_____no_output_____

n_plot_sex.plot(kind='bar', figsize=(12, 6), rot=0)_____no_output_____

## Select subsets according to taxa of species_____no_output_____

survey_data["taxa"].unique()_____no_output_____survey_data['taxa'].value_counts()
#survey_data.groupby('taxa').size()_____no_output_____

    EXERCISE: Select the records for which the `taxa` is equal to 'Rabbit', 'Bird' or 'Reptile'. Call the resulting variable `non_rodent_species`.

_____no_output_____

# %load _solutions/case2_biodiversity_analysis31.py_____no_output_____len(non_rodent_species)_____no_output_____

    EXERCISE: Select the records for which the `taxa` starts with an 'ro' (make sure it does not matter if a capital character is used in the 'taxa' name). Call the resulting variable `r_species`.

_____no_output_____

# %load _solutions/case2_biodiversity_analysis33.py_____no_output_____len(r_species)_____no_output_____

    EXERCISE: Select the records that are not Birds. Call the resulting variable `non_bird_species`.

_____no_output_____

# %load _solutions/case2_biodiversity_analysis35.py_____no_output_____len(non_bird_species)_____no_output_____

## (OPTIONAL SECTION) Evolution of species during monitoring period_____no_output_____*In this section, all plots can be made with the embedded Pandas plot function, unless specificly asked*_____no_output_____

    EXERCISE: Plot using Pandas `plot` function the number of records for `Dipodomys merriami` on yearly basis during time

_____no_output_____

# %load _solutions/case2_biodiversity_analysis37.py_____no_output_____# %load _solutions/case2_biodiversity_analysis38.py_____no_output_____

    NOTE: Check the difference between the following two graphs? What is different? Which one would you use?

_____no_output_____

merriami = survey_data[survey_data["species"] == "merriami"]
fig, ax = plt.subplots(2, 1, figsize=(14, 8))
merriami.groupby(merriami['eventDate']).size().plot(ax=ax[0], style="-") # top graph
merriami.resample("D", on="eventDate").size().plot(ax=ax[1], style="-") # lower graph_____no_output_____

    EXERCISE: Plot, for the species 'Dipodomys merriami', 'Dipodomys ordii', 'Reithrodontomys megalotis' and 'Chaetodipus baileyi', the monthly number of records as a function of time for the whole monitoring period. Plot each of the individual species in a separate subplot and provide them all with the same y-axis scale (cfr. documentation pandas plot function).

_____no_output_____

# %load _solutions/case2_biodiversity_analysis40.py_____no_output_____# %load _solutions/case2_biodiversity_analysis41.py_____no_output_____# %load _solutions/case2_biodiversity_analysis42.py_____no_output_____

    EXERCISE: Evaluate the yearly amount of occurrences for each of the 'taxa' as a function of time.

_____no_output_____

# %load _solutions/case2_biodiversity_analysis43.py_____no_output_____

    EXERCISE: Calculate the number of occurrences for each weekday, grouped by each year of the monitoring campaign, without using the `pivot` functionality. Call the variable `count_weekday_years`

_____no_output_____

# %load _solutions/case2_biodiversity_analysis44.py_____no_output_____# %load _solutions/case2_biodiversity_analysis45.py_____no_output_____count_weekday_years.head()_____no_output_____count_weekday_years.plot()_____no_output_____

    EXERCISE: Based on the variable `count_weekday_years`, calculate for each weekday the median amount of records based on the yearly count values. Modify the labels of the plot to indicate the actual days of the week (instead of numbers)

_____no_output_____

# %load _solutions/case2_biodiversity_analysis48.py_____no_output_____