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/chap08.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 8
Copyright 2017 Allen Downey
Functions from the previous chapter
Reading the data
Running the quadratic model
Here's the update function for the quadratic growth model with parameters alpha
and beta
.
Extract the starting time and population.
Initialize the system object.
Run the model and plot results.
Generating projections
To generate projections, all we have to do is change t_end
The population in the model converges on the equilibrium population, -alpha/beta
Exercise: What happens if we start with an initial population above the carrying capacity, like 20 billion? Run the model with initial populations between 1 and 20 billion, and plot the results on the same axes.
Comparing projections
We can compare the projection from our model with projections produced by people who know what they are doing.
NaN
is a special value that represents missing data, in this case because some agencies did not publish projections for some years.
This function plots projections from the UN DESA and U.S. Census. It uses dropna
to remove the NaN
values from each series before plotting it.
Run the model until 2100, which is as far as the other projections go.
People who know what they are doing expect the growth rate to decline more sharply than our model projects.
Exercises
Exercise: The net growth rate of world population has been declining for several decades. That observation suggests one more way to generate projections, by extrapolating observed changes in growth rate.
The modsim
library provides a function, compute_rel_diff
, that computes relative differences of the elements in a sequence.
Here's how we can use it to compute the relative differences in the census
and un
estimates:
Other than a bump around 1990, net growth rate has been declining roughly linearly since 1965. As an exercise, you can use this data to make a projection of world population until 2100.
Define a function,
alpha_func
, that takest
as a parameter and returns an estimate of the net growth rate at timet
, based on a linear functionalpha = intercept + slope * t
. Choose values ofslope
andintercept
to fit the observed net growth rates since 1965.Call your function with a range of
ts
from 1960 to 2020 and plot the results.Create a
System
object that includesalpha_func
as a system variable.Define an update function that uses
alpha_func
to compute the net growth rate at the given timet
.Test your update function with
t_0 = 1960
andp_0 = census[t_0]
.Run a simulation from 1960 to 2100 with your update function, and plot the results.
Compare your projections with those from the US Census and UN.
Related viewing: You might be interested in this video by Hans Rosling about the demographic changes we expect in this century.