Modelintro
Logistic growth model
Also called Verhulst equation, logistic equation.
Population growth dN/dt = r N (1 - N/K), exponential at first and levelling off at the carrying capacity K.
The logistic growth model is the differential equation for the size of one population, with an intrinsic growth rate and a carrying capacity . Verhulst proposed it in 1838. A small population grows almost exponentially, the growth slows down as comes near , and every positive solution tends to without oscillation. It is the single-species part of the Lotka-Volterra competition model, the Levins metapopulation model and the Fisher-KPP equation.
The machine
The state. is the size of the population at time : a number of individuals, a biomass or a density. The model treats it as a real number.
The parameters.
- is the intrinsic growth rate, in units of 1 per time. It is the per capita birth rate minus the per capita death rate when the population is very small.
- is the carrying capacity, in the unit of . It is the size at which births and deaths balance.
The equation.
The per capita growth rate falls linearly in . It is at , zero at and negative above . One way to get this form: let the per capita birth rate be and the per capita death rate be , with and . Then and .
The assumptions. The population is closed (no migration). All individuals are the same, so there is no age, stage or sex structure. Crowding acts at once, with no delay, and it lowers the per capita rate linearly. The environment is constant. The population is large, so a deterministic equation for a real-valued size is a good description and chance births and deaths average out.
A numerical solution. The model has a closed form (below), so a numerical method is only needed for variants of it. The implementation uses the classical Runge-Kutta method with a fixed step and checks it against the closed form.
LOGISTIC-RK4(N0, r, K, dt, steps) ▷ f(N) = r N (1 - N/K)- 1
N ← N0 - 2
repeat steps times - 3
k1 ← f(N) - 4
k2 ← f(N + dt k1 / 2) - 5
k3 ← f(N + dt k2 / 2) - 6
k4 ← f(N + dt k3) - 7
N ← N + dt (k1 + 2 k2 + 2 k3 + k4) / 6 - 8
return N
Each step costs four evaluations of , so a run to time costs arithmetic operations. With per day, and , the error at days is for and for . The ratio is , close to , as a fourth order method gives.
What it counts
- The size at each time, and the time to reach a given size.
- The equilibria and , and how fast a population returns to after a small change.
- The time of fastest growth (the inflection point of the S-shaped curve) and the largest growth rate .
- With a constant harvest per unit of time, the largest harvest that the population can sustain.
An example. Take per day, and . The closed form gives these sizes:
| (days) | 0 | 1 | 2 | 3 | 5 | 10 |
|---|---|---|---|---|---|---|
| 10 | 23.20 | 45.09 | 69.06 | 94.28 | 99.96 |
The growth is fastest at days, when , and the rate is then per day. In the first day the size grows by a factor 2.32, a little less than the factor of exponential growth at rate .
Relation to other models
- Exponential growth. As the equation becomes , with the solution . The logistic solution stays close to it while .
- The logistic map and the Ricker model. These are discrete-time models with the same idea of a growth rate that falls with density. They behave in a different way: for large growth rates they oscillate and can become chaotic. The continuous model cannot oscillate, because a solution of a one-dimensional autonomous equation is monotone.
- The exact time step is a Beverton-Holt map. Sampling the solution at times gives with (see the results below). This map is monotone and converges to for each step .
- The Euler method gives a logistic map. One forward Euler step of size is . This is not the exact time step, and for large it is a different dynamical system.
- A stochastic version. A birth-death process with per capita rates and has the logistic equation as its large-population limit. If the birth rate is zero above some size, the chain has finitely many states and can be reached from each state, so it dies out with probability 1. The time to extinction can be very long, and the logistic equation describes the long phase before it. The Gillespie algorithm simulates this chain exactly.
- Models with more than one species or with space. The Lotka-Volterra competition model couples two logistic equations. The Levins metapopulation model is a logistic equation for the fraction of occupied patches. The Fisher-KPP equation adds diffusion in space.
- The logistic function in statistics. The solution is a shifted and scaled logistic function , the same function that logistic regression uses. The two uses share the curve and nothing else.
Known results in the model
Closed form. For each and ,
Proof. For the solution is , and the formula gives 0. For , the solution stays positive, because is a solution and solutions of this smooth equation cannot cross. Put . Then . This linear equation has the solution . Take the inverse and multiply the numerator and the denominator by .
Monotone convergence to . If , then increases to . If , it decreases to . If , it stays at . The approach is never an oscillation.
Proof. In the closed form, the term has the sign of , and its absolute value decreases to 0. So the denominator moves monotonically to , and moves monotonically to .
Stability of the equilibria. Write . Then , so is unstable, and , so is stable. Near , a small deviation decays like , so the return time is . This is Jacobian linearization in one dimension.
Time to reach a size. For and , solving the closed form for gives
In the example, the population reaches 80 after days. The time tends to infinity as : the population never reaches in finite time.
The inflection point. is a parabola in with its maximum at . If , the curve is convex until it reaches and concave after, so its inflection is at , the value of . If , there is no inflection point after time 0.
The exact time- map. For each , with .
Proof. The equation is autonomous, so is the closed form with the start value and the time : . Multiply the numerator and the denominator by . This gives .
The Euler map is a logistic map. Let . The substitution turns the Euler step into .
Proof. The Euler step is . Then . Since , this is .
So the Euler iteration converges to only for , because the fixed point has multiplier . With , and , the iteration settles on the 2-cycle . With it does not settle at all. These are artefacts of a step that is too large, not properties of the model; the logistic map page gives the bifurcation values.
Harvesting. With a constant harvest , the equation has an equilibrium if and only if .
Proof. An equilibrium solves . Its discriminant is , which is non-negative if and only if .
For there are two equilibria; the larger one is stable and the smaller one is a threshold below which the population collapses. With , and they are and . At the two meet at , and for a larger every solution reaches 0 in finite time. The value is the maximum sustainable yield of this model. It is a result inside the model: a harvest close to it leaves the population near a fold, where a small error in or removes the equilibrium.
A delay changes the behaviour. If crowding acts with a delay , as in , the equilibrium loses stability when and the population oscillates (Murray, chapter 1). Monotone convergence is a property of the equation without delay.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/logistic-growth-model/. The authors are listed in prov/SN0SST.jsonl.
"""The logistic growth model dN/dt = r N (1 - N / K).
N is the population size (or density), r > 0 is the intrinsic growth rate
per unit of time and K > 0 is the carrying capacity, in the unit of N.
The module gives the closed form solution, a fixed step RK4 integrator,
the time to reach a level, the exact time-h map (a Beverton-Holt map) and
the forward Euler map, which is a logistic map in disguise.
"""
import math
def rate(n: float, r: float, k: float) -> float:
"""The right-hand side r N (1 - N / K)."""
return r * n * (1.0 - n / k)
def closed_form(n0: float, r: float, k: float, t: float) -> float:
"""N(t) = K N0 / (N0 + (K - N0) e^(-r t)), valid for every N0 >= 0."""
if n0 == 0.0:
return 0.0
return k * n0 / (n0 + (k - n0) * math.exp(-r * t))
def rk4(n0: float, r: float, k: float, dt: float, steps: int) -> float:
"""N after `steps` classical Runge-Kutta steps of size dt from N0."""
n = n0 # @line L1
for _ in range(steps): # @line L2
k1 = rate(n, r, k) # @line L3
k2 = rate(n + dt * k1 / 2.0, r, k) # @line L4
k3 = rate(n + dt * k2 / 2.0, r, k) # @line L5
k4 = rate(n + dt * k3, r, k) # @line L6
n = n + dt * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0 # @line L7
return n # @line L8
def time_to_reach(n0: float, n: float, r: float, k: float) -> float:
"""The time at which the solution from N0 reaches N (both on the same side of K)."""
return math.log(n * (k - n0) / (n0 * (k - n))) / r
def inflection_time(n0: float, r: float, k: float) -> float:
"""The time at which N = K/2, where growth is fastest (needs 0 < N0 < K/2)."""
return math.log((k - n0) / n0) / r
def exact_step(n: float, r: float, k: float, h: float) -> float:
"""The exact time-h map N -> lambda N / (1 + (lambda - 1) N / K), lambda = e^(r h)."""
lam = math.exp(r * h)
return lam * n / (1.0 + (lam - 1.0) * n / k)
def euler_step(n: float, r: float, k: float, h: float) -> float:
"""One forward Euler step N + h r N (1 - N / K)."""
return n + h * rate(n, r, k)
def harvest_equilibria(r: float, k: float, harvest: float):
"""The equilibria of dN/dt = r N (1 - N/K) - H, as a sorted list (empty if H > r K / 4)."""
disc = r * r - 4.0 * r * harvest / k
if disc < 0.0:
return []
root = math.sqrt(disc)
return sorted({k * (r - root) / (2.0 * r), k * (r + root) / (2.0 * r)})
test_logistic_growth_model.py 106 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/logistic-growth-model/. The authors are listed in prov/SN0SST.jsonl.
import math
from logistic_growth_model import (
closed_form,
euler_step,
exact_step,
harvest_equilibria,
inflection_time,
rate,
rk4,
time_to_reach,
)
R, K, N0 = 1.0, 100.0, 10.0
def test_closed_form_values() -> None:
# r = 1 per day, K = 100, N0 = 10.
expected = {1: 23.196932, 2: 45.085306, 3: 69.056786, 5: 94.282562, 10: 99.959157}
for t, v in expected.items():
assert abs(closed_form(N0, R, K, t) - v) < 1e-6
assert closed_form(0.0, R, K, 5.0) == 0.0
assert closed_form(K, R, K, 5.0) == K
def test_closed_form_solves_the_ode() -> None:
# A centered difference of the closed form matches r N (1 - N / K).
h = 1e-5
for n0 in (1.0, 10.0, 60.0, 150.0):
for t in (0.3, 1.0, 2.5, 4.0):
d = (closed_form(n0, R, K, t + h) - closed_form(n0, R, K, t - h)) / (2 * h)
assert abs(d - rate(closed_form(n0, R, K, t), R, K)) < 1e-4
def test_rk4_against_closed_form_and_order() -> None:
# dt = 0.1 and 0.05 up to t = 5; RK4 is fourth order, so the error ratio is near 16.
exact = closed_form(N0, R, K, 5.0)
e1 = abs(rk4(N0, R, K, 0.1, 50) - exact)
e2 = abs(rk4(N0, R, K, 0.05, 100) - exact)
assert e1 < 1e-5 and e2 < 1e-6
assert 12.0 < e1 / e2 < 20.0
def test_monotone_convergence_from_both_sides() -> None:
for n0 in (1.0, 50.0, 99.0, 101.0, 300.0):
values = [closed_form(n0, R, K, t / 2) for t in range(60)]
diffs = [b - a for a, b in zip(values, values[1:])]
if n0 < K:
assert all(d > 0 for d in diffs)
else:
assert all(d < 0 for d in diffs)
assert abs(values[-1] - K) < 1e-6 * K * max(1.0, abs(n0 - K))
def test_time_to_reach_and_inflection() -> None:
assert abs(inflection_time(N0, R, K) - math.log(9.0)) < 1e-12
assert abs(closed_form(N0, R, K, inflection_time(N0, R, K)) - K / 2) < 1e-9
t = time_to_reach(N0, 80.0, R, K)
assert abs(closed_form(N0, R, K, t) - 80.0) < 1e-9
# The fastest growth, r K / 4, happens at N = K / 2.
best = max(rate(K * i / 1000, R, K) for i in range(1001))
assert abs(best - R * K / 4) < 1e-12
def test_exact_step_is_the_time_h_map() -> None:
n = N0
for _ in range(10):
n = exact_step(n, R, K, 0.5)
assert abs(n - closed_form(N0, R, K, 5.0)) < 1e-9
def test_euler_map_is_a_logistic_map() -> None:
# x = h r N / ((1 + h r) K) turns the Euler step into x -> (1 + h r) x (1 - x).
for h in (0.5, 2.2, 2.7):
mu = 1 + h * R
for n in (5.0, 40.0, 90.0, 120.0):
x = h * R * n / (mu * K)
x_next = h * R * euler_step(n, R, K, h) / (mu * K)
assert abs(x_next - mu * x * (1 - x)) < 1e-12
def test_euler_large_step_oscillates() -> None:
# h r = 2.2 > 2: K is unstable for the Euler map, and a 2-cycle appears.
n = N0
for _ in range(400):
n = euler_step(n, R, K, 2.2)
a = n
b = euler_step(a, R, K, 2.2)
assert abs(euler_step(b, R, K, 2.2) - a) < 1e-9
assert abs(a - b) > 40.0
# h r = 0.5: the Euler iteration converges to K.
n = N0
for _ in range(200):
n = euler_step(n, R, K, 0.5)
assert abs(n - K) < 1e-9
def test_harvest_threshold() -> None:
assert harvest_equilibria(R, K, 30.0) == []
eq = harvest_equilibria(R, K, 16.0)
assert len(eq) == 2 and abs(eq[0] - 20.0) < 1e-9 and abs(eq[1] - 80.0) < 1e-9
eq = harvest_equilibria(R, K, R * K / 4)
assert len(eq) == 1 and abs(eq[0] - K / 2) < 1e-9
References
- Verhulst 1838, the equation with a growth rate that falls as the population grows, and its solution
- James D. Murray, Mathematical Biology I, An Introduction, third edition, Springer, 2002, Chapter 1, Continuous Population Models for Single Species (section 1.1 on growth models and section 1.3 on delay models), and Chapter 2 for the discrete models
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag SN0SST Edit this page History Report an error Markdown