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/chapters/chap09.ipynb
Views: 531
Printed and electronic copies of Modeling and Simulation in Python are available from No Starch Press and Bookshop.org and Amazon.
Analysis of Population Growth
Modeling and Simulation in Python
Copyright 2021 Allen Downey
License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
In this chapter we'll express the models from previous chapters as difference equations and differential equations, solve the equations, and derive the functional forms of the solutions. And I'll present some thoughts about the complementary roles of mathematical analysis and simulation.
Difference Equations
The population models in the previous chapter and this one are simple enough that we didn't really need to run simulations. We could have solved them mathematically. For example, we wrote the constant growth model like this:
In mathematical notation, we would write the same model like this:
where is the population during year , is the population during year , and is constant annual growth. This way of representing the model, where future population is a function of current population, is a difference equation; see https://en.wikipedia.org/wiki/Linear_difference_equation.
For a given value of , sometimes it is possible to compute directly; that is, without computing the intervening values from through .
In the case of constant growth we can see that , and . Combining these, we get , then , and we can see that in general
So if we want to know and we don't care about the other values, we can compute it with one multiplication and one addition.
We can also write the proportional model as a difference equation:
Or more conventionally as:
Now we can see that , and , and in general
A sequence with this functional form is called a geometric progression; see http://modsimpy.com/geom. When is positive, the factor is greater than 1, so the elements of the sequence grow without bound.
Finally, we can write the quadratic model like this:
or with the more conventional parameterization like this:
There is no analytic solution to this equation, but we can approximate it with a differential equation and solve that, which is what we'll do in the next section.
Differential Equations
Starting again with the constant growth model
If we define to be the change in from one time step to the next, we can write:
If we define to be the time step, which is one year in the example, we can write the rate of change per unit of time like this:
This is a discrete model, which means time is only defined at integer values of and not in between. But in reality, people are born and die all the time, not once a year, so it might be more realistic to use a continuous model, which means time is defined at all values of , not just integers.
In a continuous model, we write the rate of change in the form of a derivative:
This way of representing the model is a differential equation, which is an equation that involves at least one derivative (see http://modsimpy.com/diffeq). To solve this equation, we multiply both sides by :
And then integrate both sides:
Similarly, we can write the proportional growth model like this:
And as a differential equation like this:
If we multiply both sides by and divide by , we get
Now we integrate both sides, yielding:
where is the natural logarithm and is the constant of integration.
Exponentiating both sides, we have
The exponential function can be written or . In this book I use the first form because it resembles the Python code. We can rewrite the previous equation as
Since is an arbitrary constant, is also an arbitrary constant, so we can write
where . There are many solutions to this differential equation, with different values of . The particular solution we want is the one that has the value when .
When , , so and the solution we want is
If you would like to see this derivation done more carefully, you might like this video: http://modsimpy.com/khan1.
Analysis and Simulation
Once you have designed a model, there are generally two ways to proceed: simulation and analysis. Simulation often comes in the form of a computer program that models changes in a system over time, like births and deaths, or bikes moving from place to place. Analysis often comes in the form of algebra and calculus; that is, symbolic manipulation using mathematical notation.
Analysis and simulation have different capabilities and limitations. Simulation is generally more versatile; it is easy to add and remove parts of a program and test many versions of a model, as we have done in the previous examples.
But there are several things we can do with analysis that are harder or impossible with simulations:
With analysis we can sometimes compute, exactly and efficiently, a value that we could only approximate, less efficiently, with simulation. For example, in the quadratic model we plotted net growth versus population and saw it crosses through zero when the population is near 14 billion. We could estimate the crossing point using a numerical search algorithm (more about that later). But with a bit of algebra, we derived the general result that .
Analysis sometimes provides "computational shortcuts", that is, the ability to jump forward in time to compute the state of a system many time steps in the future without computing the intervening states.
We can use analysis to state and prove generalizations about models; for example, we might prove that certain results will always or never occur. With simulations, we can show examples and sometimes find counterexamples, but it is hard to write proofs.
Analysis can provide insight into models and the systems they describe; for example, sometimes we can identify qualitatively different ways the system can behave and key parameters that control those behaviors.
When people see what analysis can do, they sometimes get drunk with power and imagine that it gives them a special ability to see past the veil of the material world and discern the laws of mathematics that govern the universe. When they analyze a model of a physical system, they talk about "the math behind it" as if our world is the mere shadow of a world of ideal mathematical entities (I am not making this up; see http://modsimpy.com/plato.).
This is, of course, nonsense. Mathematical notation is a language designed by humans for a purpose, specifically to facilitate symbolic manipulations like algebra. Similarly, programming languages are designed for a purpose, specifically to represent computational ideas and run programs.
Each of these languages is good for the purposes it was designed for and less good for other purposes. But they are often complementary, and one of the goals of this book is to show how they can be used together.
Analysis with WolframAlpha
Until recently, most analysis was done by rubbing graphite on wood pulp, a process that is laborious and error-prone. A useful alternative is symbolic computation. If you have used services like WolframAlpha, you have used symbolic computation.
For example, if you go to https://www.wolframalpha.com/ and enter
WolframAlpha infers that f(t)
is a function of t
and alpha
is a parameter; it classifies the query as a "first-order linear ordinary differential equation", and reports the general solution:
If you add a second equation to specify the initial condition:
WolframAlpha reports the particular solution:
WolframAlpha is based on Mathematica, a powerful programming language designed specifically for symbolic computation.
Analysis with SymPy
Python has a library called SymPy that provides symbolic computation tools similar to Mathematica. They are not as easy to use as WolframAlpha, but they have some other advantages.
To use it, we'll define Symbol
objects that represent names of variables and functions. The symbols
function takes a string and returns Symbol
objects.
Now when we use t
, Python treats it like a variable name rather than a specific number. For example, if we use t
as part of an expression, like this,
Python doesn't try to perform numerical addition; rather, it creates a new Symbol
that represents the sum of t
and 1
. We can evaluate the sum using subs
, which substitutes a value for a symbol. This example substitutes 2 for t
:
Functions in SymPy are represented by a special kind of Symbol
:
Now if we write f(t)
, we get an object that represents the evaluation of a function, , at a value, .
But again SymPy doesn't actually try to evaluate it.
Differential Equations In SymPy
SymPy provides a function, diff
, that can differentiate a function. We can apply it to f(t)
like this:
The result is a Symbol
that represents the derivative of f
with respect to t
. But again, SymPy doesn't try to compute the derivative yet.
To represent a differential equation, we use Eq
:
The result is an object that represents an equation. Now we can use dsolve
to solve this differential equation:
The result is the general solution, which still contains an unspecified constant, . To get the particular solution where , we substitute for C1
. First, we have to tell Python that C1
is a symbol.
Now we can substitute the value of for C1
. For example, if is 1000:
When , the value of is , which confirms that this is the solution we want.
Solving the Quadratic Growth Model
To solve the quadratic growth curve, we'll use the r, K
parameterization, so we'll need two more symbols:
Now we can write the differential equation.
And solve it.
The result, solution_eq
, contains rhs
, which is the right-hand side of the solution.
We can evaluate the right-hand side at
Now we want to find the value of C1
that makes f(0) = p_0
.
So we'll create the equation at_0 = p_0
and solve for C1
. Because this is just an algebraic equation, not a differential equation, we use solve
, not dsolve
.
The result from solve
is a list of solutions.
In this case, there is only one solution, but we still get a list, so we have to use the bracket operator, [0]
, to select the first one.
Now in the general solution, we want to replace C1
with the value of C1
we just figured out.
The result is complicated, but SymPy provides a function that tries to simplify it.
This function is called the logistic growth curve; see http://modsimpy.com/logistic. In the context of growth models, the logistic function is often written like this:
where .
We can use SymPy to confirm that these two forms are equivalent. First we represent the alternative version of the logistic function:
To see whether two expressions are equivalent, we can check whether their difference simplifies to 0.
This test only works one way: if SymPy says the difference reduces to 0, the expressions are definitely equivalent (and not just numerically close).
But if SymPy can't find a way to simplify the result to 0, that doesn't necessarily mean there isn't one. Testing whether two expressions are equivalent is a surprisingly hard problem; in fact, there is no algorithm that can solve it in general.
If you use SymPy to compute an expression, and then want to evaluate that expression in Python, SymPy provides a function called pycode
that generates Python code:
If you would like to see this differential equation solved by hand, you might like this video: http://modsimpy.com/khan2
Summary
In this chapter we wrote the growth models from the previous chapters in terms of difference and differential equations. We solved some of these equations by hand; for others, we used WolframAlpha and SymPy.
What I called the "constant growth" model is more commonly called linear growth because the solution is a line. If we model time as continuous, the solution is
where is net annual growth.
Similarly, the proportional growth model is usually called exponential growth because the solution is an exponential function:
Finally, the quadratic growth model is called logistic growth because the solution is a logistic function:
where .
I avoided these terms until now because they are based on results we had not derived yet.
With that, we are done modeling world population growth. The next chapter presents case studies where you can apply the tools we have learned so far.
Exercises
This chapter is available as a Jupyter notebook where you can read the text, run the code, and work on the exercises. You can access the notebooks at https://allendowney.github.io/ModSimPy/.
Exercise 1
Use SymPy to solve the quadratic growth equation using the alternative parameterization
Exercise 2
Use WolframAlpha to solve the quadratic growth model, using either or both forms of parameterization:
or
Find the general solution and also the particular solution where f(0) = p_0
.