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/notebooks/rabbits2.ipynb
Views: 531
Modeling and Simulation in Python
Rabbit example
Copyright 2017 Allen Downey
Rabbit Redux
This notebook starts with a version of the rabbit population growth model and walks through some steps for extending it.
In the original model, we treat all rabbits as adults; that is, we assume that a rabbit is able to breed in the season after it is born. In this notebook, we extend the model to include both juvenile and adult rabbits.
As an example, let's assume that rabbits take 3 seasons to mature. We could model that process explicitly by counting the number of rabbits that are 1, 2, or 3 seasons old. As an alternative, we can model just two stages, juvenile and adult. In the simpler model, the maturation rate is 1/3 of the juveniles per season.
To implement this model, make these changes in the System object:
Before you make any changes, run all cells and confirm your understand them.
Then, add a second initial populations:
juvenile_pop0
, with value0
.Add an additional variable,
mature_rate
, with the value0.33
.
Now update run_simulation
with the following changes:
Add a second TimeSeries, named
juveniles
, to keep track of the juvenile population, and initialize it withjuvenile_pop0
.Inside the for loop, compute the number of juveniles that mature during each time step.
Also inside the for loop, add a line that stores the number of juveniles in the new
TimeSeries
. For simplicity, let's assume that only adult rabbits die.During each time step, subtract the number of maturations from the juvenile population and add it to the adult population.
After the for loop, store the
juveniles
TimeSeries
as a variable inSystem
.
Test your changes in run_simulation
:
Next, update plot_results
to plot both the adult and juvenile TimeSeries
.
And test your updated version of plot_results
.
This notebook demonstrates the steps we recommend for starting your project:
Start with one of the examples from the book, either by copying a notebook or pasting code into a new notebook. Get the code working before you make any changes.
Make one small change, and run the code again.
Repeat step 2 until you have a basic implementation of your model.
If you start with working code that you understand and make small changes, you can avoid spending a lot of time debugging.
One you have a basic model working, you can think about what metrics to measure, what parameters to sweep, and how to use the model to predict, explain, or design.
Bonus question
Suppose you only have room for 30 adult rabbits. Whenever the adult population exceeds 30, you take any excess rabbits to market (as pets for kind children, of course). Modify run_simulation
to model this strategy. What effect does it have on the behavior of the system? You might have to run for more than 10 seasons to see what happens.