Algorithm Search⌘K

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 dNdt=rN(1−NK)\frac{dN}{dt} = r N \left(1 - \frac{N}{K}\right) for the size N(t)N(t) of one population, with an intrinsic growth rate rr and a carrying capacity KK. Verhulst proposed it in 1838. A small population grows almost exponentially, the growth slows down as NN comes near KK, and every positive solution tends to KK 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. N(t)≥0N(t) \ge 0 is the size of the population at time tt: a number of individuals, a biomass or a density. The model treats it as a real number.

The parameters.

  • r>0r > 0 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.
  • K>0K > 0 is the carrying capacity, in the unit of NN. It is the size at which births and deaths balance.

The equation.

dNdt=rN(1−NK),N(0)=N0≥0.\frac{dN}{dt} = r N \left(1 - \frac{N}{K}\right), \qquad N(0) = N_0 \ge 0.

The per capita growth rate 1NdNdt=r(1−N/K)\frac{1}{N}\frac{dN}{dt} = r(1 - N/K) falls linearly in NN. It is rr at N=0N = 0, zero at N=KN = K and negative above KK. One way to get this form: let the per capita birth rate be b0−b1Nb_0 - b_1 N and the per capita death rate be d0+d1Nd_0 + d_1 N, with b0>d0b_0 > d_0 and b1+d1>0b_1 + d_1 > 0. Then r=b0−d0r = b_0 - d_0 and K=(b0−d0)/(b1+d1)K = (b_0 - d_0)/(b_1 + d_1).

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 dtdt and checks it against the closed form.

  1. LOGISTIC-RK4(N0, r, K, dt, steps) ▷ f(N) = r N (1 - N/K)
  2. 1N ← N0
  3. 2repeat steps times
  4. 3k1 ← f(N)
  5. 4k2 ← f(N + dt k1 / 2)
  6. 5k3 ← f(N + dt k2 / 2)
  7. 6k4 ← f(N + dt k3)
  8. 7N ← N + dt (k1 + 2 k2 + 2 k3 + k4) / 6
  9. 8return N

Each step costs four evaluations of ff, so a run to time TT costs O(T/dt)O(T/dt) arithmetic operations. With r=1r = 1 per day, K=100K = 100 and N0=10N_0 = 10, the error at t=5t = 5 days is 7.27×10−67.27 \times 10^{-6} for dt=0.1dt = 0.1 and 4.50×10−74.50 \times 10^{-7} for dt=0.05dt = 0.05. The ratio is 16.216.2, close to 242^4, as a fourth order method gives.

What it counts

  • The size N(t)N(t) at each time, and the time to reach a given size.
  • The equilibria 00 and KK, and how fast a population returns to KK after a small change.
  • The time of fastest growth (the inflection point of the S-shaped curve) and the largest growth rate rK/4rK/4.
  • With a constant harvest HH per unit of time, the largest harvest that the population can sustain.

An example. Take r=1r = 1 per day, K=100K = 100 and N0=10N_0 = 10. The closed form gives these sizes:

tt (days) 0 1 2 3 5 10
N(t)N(t) 10 23.20 45.09 69.06 94.28 99.96

The growth is fastest at t∗=ln⁡9≈2.197t^* = \ln 9 \approx 2.197 days, when N=50N = 50, and the rate is then rK/4=25rK/4 = 25 per day. In the first day the size grows by a factor 2.32, a little less than the factor e≈2.72e \approx 2.72 of exponential growth at rate rr.

Relation to other models

  • Exponential growth. As K→∞K \to \infty the equation becomes dN/dt=rNdN/dt = rN, with the solution N0ertN_0 e^{rt}. The logistic solution stays close to it while N≪KN \ll K.
  • 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 0,h,2h,…0, h, 2h, \ldots gives Nj+1=λNj/(1+(λ−1)Nj/K)N_{j+1} = \lambda N_j / (1 + (\lambda - 1) N_j / K) with λ=erh\lambda = e^{rh} (see the results below). This map is monotone and converges to KK for each step hh.
  • The Euler method gives a logistic map. One forward Euler step of size hh is N↦N+hrN(1−N/K)N \mapsto N + hrN(1 - N/K). This is not the exact time step, and for large hh it is a different dynamical system.
  • A stochastic version. A birth-death process with per capita rates b0−b1Nb_0 - b_1 N and d0+d1Nd_0 + d_1 N 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 00 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 1/(1+e−x)1/(1 + e^{-x}), 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 N0≥0N_0 \ge 0 and t≥0t \ge 0,

N(t)=KN0N0+(K−N0) e−rt.N(t) = \frac{K N_0}{N_0 + (K - N_0)\, e^{-rt}}.

Proof. For N0=0N_0 = 0 the solution is N≡0N \equiv 0, and the formula gives 0. For N0>0N_0 > 0, the solution stays positive, because N≡0N \equiv 0 is a solution and solutions of this smooth equation cannot cross. Put y=1/Ny = 1/N. Then y′=−N′/N2=−rN(1−N/K)=−ry+r/Ky' = -N'/N^2 = -\frac{r}{N}(1 - N/K) = -ry + r/K. This linear equation has the solution y(t)=1/K+(1/N0−1/K)e−rty(t) = 1/K + (1/N_0 - 1/K) e^{-rt}. Take the inverse and multiply the numerator and the denominator by KN0K N_0. □\square

Monotone convergence to KK. If 0<N0<K0 < N_0 < K, then N(t)N(t) increases to KK. If N0>KN_0 > K, it decreases to KK. If N0=KN_0 = K, it stays at KK. The approach is never an oscillation.

Proof. In the closed form, the term (K−N0)e−rt(K - N_0) e^{-rt} has the sign of K−N0K - N_0, and its absolute value decreases to 0. So the denominator moves monotonically to N0N_0, and N(t)N(t) moves monotonically to KN0/N0=KK N_0 / N_0 = K. □\square

Stability of the equilibria. Write f(N)=rN(1−N/K)f(N) = rN(1 - N/K). Then f′(0)=r>0f'(0) = r > 0, so 00 is unstable, and f′(K)=−r<0f'(K) = -r < 0, so KK is stable. Near KK, a small deviation decays like e−rte^{-rt}, so the return time is 1/r1/r. This is Jacobian linearization in one dimension.

Time to reach a size. For 0<N0<K0 < N_0 < K and N0<N<KN_0 < N < K, solving the closed form for tt gives

t(N)=1rln⁡N(K−N0)N0(K−N).t(N) = \frac{1}{r} \ln \frac{N (K - N_0)}{N_0 (K - N)}.

In the example, the population reaches 80 after ln⁡36≈3.58\ln 36 \approx 3.58 days. The time tends to infinity as N→KN \to K: the population never reaches KK in finite time.

The inflection point. f(N)f(N) is a parabola in NN with its maximum rK/4rK/4 at N=K/2N = K/2. If N0<K/2N_0 < K/2, the curve N(t)N(t) is convex until it reaches K/2K/2 and concave after, so its inflection is at t∗=1rln⁡K−N0N0t^* = \frac{1}{r}\ln\frac{K - N_0}{N_0}, the value of t(K/2)t(K/2). If N0≥K/2N_0 \ge K/2, there is no inflection point after time 0.

The exact time-hh map. For each h>0h > 0, N(t+h)=λN(t)1+(λ−1)N(t)/KN(t + h) = \frac{\lambda N(t)}{1 + (\lambda - 1) N(t)/K} with λ=erh\lambda = e^{rh}.

Proof. The equation is autonomous, so N(t+h)N(t + h) is the closed form with the start value N(t)N(t) and the time hh: KNN+(K−N)e−rh\frac{K N}{N + (K - N) e^{-rh}}. Multiply the numerator and the denominator by erh/Ke^{rh}/K. This gives λNλN/K+1−N/K=λN1+(λ−1)N/K\frac{\lambda N}{\lambda N/K + 1 - N/K} = \frac{\lambda N}{1 + (\lambda - 1)N/K}. □\square

The Euler map is a logistic map. Let μ=1+hr\mu = 1 + hr. The substitution x=hrNμKx = \frac{hrN}{\mu K} turns the Euler step N↦N+hrN(1−N/K)N \mapsto N + hrN(1 - N/K) into x↦μx(1−x)x \mapsto \mu x (1 - x).

Proof. The Euler step is N′=μN−hrN2/KN' = \mu N - hrN^2/K. Then x′=hrN′μK=hrNK−(hr)2N2μK2x' = \frac{hr N'}{\mu K} = \frac{hrN}{K} - \frac{(hr)^2 N^2}{\mu K^2}. Since hrNK=μx\frac{hrN}{K} = \mu x, this is μx−(μx)2μ=μx(1−x)\mu x - \frac{(\mu x)^2}{\mu} = \mu x (1 - x). □\square

So the Euler iteration converges to KK only for hr<2hr < 2, because the fixed point KK has multiplier 1−hr1 - hr. With r=1r = 1, K=100K = 100 and h=2.2h = 2.2, the iteration settles on the 2-cycle 116.28,74.63116.28, 74.63. With h=2.7h = 2.7 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 H≥0H \ge 0, the equation dNdt=rN(1−N/K)−H\frac{dN}{dt} = rN(1 - N/K) - H has an equilibrium if and only if H≤rK/4H \le rK/4.

Proof. An equilibrium solves rKN2−rN+H=0\frac{r}{K} N^2 - rN + H = 0. Its discriminant is r2−4rH/Kr^2 - 4rH/K, which is non-negative if and only if H≤rK/4H \le rK/4. □\square

For H<rK/4H < rK/4 there are two equilibria; the larger one is stable and the smaller one is a threshold below which the population collapses. With r=1r = 1, K=100K = 100 and H=16H = 16 they are 8080 and 2020. At H=rK/4H = rK/4 the two meet at K/2K/2, and for a larger HH every solution reaches 0 in finite time. The value rK/4rK/4 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 rr or KK removes the equilibrium.

A delay changes the behaviour. If crowding acts with a delay TT, as in dNdt=rN(t)(1−N(t−T)K)\frac{dN}{dt} = rN(t)\left(1 - \frac{N(t - T)}{K}\right), the equilibrium KK loses stability when rT>π/2rT > \pi/2 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.

logistic_growth_model.pyI0RawGitHub
# 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

  1. Verhulst 1838, the equation with a growth rate that falls as the population grows, and its solution
  2. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/SN0SST/text.yaml