Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/examples/salmon.ipynb
Views: 531
Salmon
Modeling and Simulation in Python
Copyright 2021 Allen Downey
License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
Can we predict salmon populations?
Each year the U.S. Atlantic Salmon Assessment Committee reports estimates of salmon populations in oceans and rivers in the northeastern United States. The reports are useful for monitoring changes in these populations, but they generally do not include predictions.
The goal of this case study is to model year-to-year changes in population, evaluate how predictable these changes are, and estimate the probability that a particular population will increase or decrease in the next 10 years.
As an example, I'll use data from page 18 of the 2017 report, which provides population estimates for the Narraguagus and Sheepscot Rivers in Maine.
There are tools for extracting data from a PDF document automatically, but for this example I will keep it simple and type it in.
Here are the population estimates for the Narraguagus River:
To get this data into a Pandas Series, I'll also make a range of years to use as an index.
And here's the series.
Here's what it looks like:
Modeling changes
To see how the population changes from year-to-year, I'll use diff
to compute the absolute difference between each year and the next and shift
to align the changes with the year they happened.
We can compute relative differences by dividing by the original series elementwise.
These relative differences are observed annual net growth rates. So let's drop the NaN
and save them.
A simple way to model this system is to draw a random value from this series of observed rates each year. We can use the NumPy function choice
to make a random choice from a series.
Simulation
Now we can simulate the system by drawing random growth rates from the series of observed rates.
I'll start the simulation in 2015.
I'll create a System
object with variables t_0
, p_0
, rates
, and duration=10
years.
The series of observed rates is one big parameter of the model.
Write an update functon that takes as parameters pop
, t
, and system
. It should choose a random growth rate, compute the change in population, and return the new population.
Test your update function and run it a few times
Here's a version of run_simulation
that stores the results in a TimeSeries
and returns it.
Use run_simulation
to run generate a prediction for the next 10 years.
Then plot your prediction along with the original data. Your prediction should pick up where the data leave off.
To get a sense of how much the results vary, we can run the model several times and plot all of the results.
The plot option alpha=0.1
makes the lines semi-transparent, so they are darker where they overlap.
Run plot_many_simulations
with your update function and iters=30
. Also plot the original data.
The results are highly variable: according to this model, the population might continue to decline over the next 10 years, or it might recover and grow rapidly!
It's hard to say how seriously we should take this model. There are many factors that influence salmon populations that are not included in the model. For example, if the population starts to grow quickly, it might be limited by resource limits, predators, or fishing. If the population starts to fall, humans might restrict fishing and stock the river with farmed fish.
So these results should probably not be considered useful predictions. However, there might be something useful we can do, which is to estimate the probability that the population will increase or decrease in the next 10 years.
Distribution of net changes
To describe the distribution of net changes, write a function called run_many_simulations
that runs many simulations, saves the final populations in a SweepSeries
, and returns the SweepSeries
.
Test your function by running it with iters=5
.
Now we can run 1000 simulations and describe the distribution of the results.
If we substract off the initial population, we get the distribution of changes.
The median is negative, which indicates that the population decreases more often than it increases.
We can be more specific by counting the number of runs where net_changes
is positive.
Or we can use mean
to compute the fraction of runs where net_changes
is positive.
And here's the fraction where it's negative.
So, based on observed past changes, this model predicts that the population is more likely to decrease than increase over the next 10 years, by about 2:1.
A refined model
There are a few ways we could improve the model.
It looks like there might be cyclic behavior in the past data, with a period of 4-5 years. We could extend the model to include this effect.
Older data might not be as relevant for prediction as newer data, so we could give more weight to newer data.
The second option is easier to implement, so let's try it.
I'll use linspace
to create an array of "weights" for the observed rates. The probability that I choose each rate will be proportional to these weights.
The weights have to add up to 1, so I divide through by the total.
I'll add the weights to the System
object, since they are parameters of the model.
We can pass these weights as a parameter to np.random.choice
(see the documentation)
Write an update function that takes the weights into account.
Use plot_many_simulations
to plot the results.
Use run_many_simulations
to collect the results and describe
to summarize the distribution of net changes.
Does the refined model have much effect on the probability of population decline?