Algorithm Search⌘K

Modelintro

Lotka-Volterra predator-prey model

Also called predator-prey equations.

Prey grow and are eaten in proportion to encounters, and predators grow from what they eat, which gives closed neutral cycles.

The Lotka-Volterra predator-prey model is the pair of differential equations x˙=ax−bxy\dot x = a x - b x y, y˙=cxy−dy\dot y = c x y - d y for the densities of a prey xx and a predator yy. Prey grow exponentially when alone and are eaten at a rate proportional to the number of encounters. Predators grow from what they eat and die at a constant rate. Lotka (1925) and Volterra (1926) found the model on their own. Every positive solution is periodic: the populations cycle on closed curves around one equilibrium, and the size of the cycle depends only on the start.

The machine

The state. x(t)≥0x(t) \ge 0 is the prey density and y(t)≥0y(t) \ge 0 is the predator density, as numbers or biomass per unit of area. Both are real numbers.

The parameters. All four are positive.

  • aa, in 1 per time: the growth rate of the prey with no predators.
  • bb, in 1 per (predator density times time): the rate at which one predator kills prey, per unit of prey density.
  • cc, in 1 per (prey density times time): the growth of the predator per unit of prey density. If a fraction ee of the eaten prey is turned into predators, then c=ebc = e b when both are counted in the same unit.
  • dd, in 1 per time: the death rate of the predator with no prey.

The equations.

dxdt=ax−bxy,dydt=cxy−dy.\frac{dx}{dt} = a x - b x y, \qquad \frac{dy}{dt} = c x y - d y.

The assumptions. Without predators the prey grow without bound, at the rate aa. Without prey the predators die out, at the rate dd. Prey and predators meet at random, so the kill rate is proportional to xyx y (mass action). Each predator eats bxb x prey per unit of time, without limit, so there is no satiation and no handling time; this is the type I response of the Holling functional response. Births follow kills at once, with no delay. Both populations are well mixed, have no age structure and are large enough for a deterministic description.

A numerical solution. The equations have no closed form in elementary functions. The implementation uses fixed-step RK4 and checks the conserved quantity VV below.

  1. LV-RK4(x0, y0, dt, steps) ▷ F(x, y) = (a x - b x y, c x y - d y)
  2. 1(x, y) ← (x0, y0)
  3. 2repeat steps times
  4. 3k1 ← F(x, y)
  5. 4k2 ← F((x, y) + dt k1 / 2)
  6. 5k3 ← F((x, y) + dt k2 / 2)
  7. 6k4 ← F((x, y) + dt k3)
  8. 7(x, y) ← (x, y) + dt (k1 + 2 k2 + 2 k3 + k4) / 6
  9. 8return the list of states

Each step costs four evaluations of FF, so a run to time TT costs O(T/dt)O(T/dt) operations. With dt=0.01dt = 0.01 the value of VV changes by less than 10−910^{-9} over 4 periods in the example below.

What it counts

  • The equilibria: the extinction point (0,0)(0, 0) and the coexistence point (x∗,y∗)=(d/c, a/b)(x^*, y^*) = (d/c,\ a/b).
  • The orbit through a start point, which is the level curve of VV through it, and its period.
  • The time averages of xx and yy over one period.
  • The response of the averages to a harvest of both species.

An example. Take a=1a = 1, b=0.1b = 0.1, c=0.02c = 0.02 and d=0.5d = 0.5, in arbitrary time units. The coexistence point is (25,10)(25, 10). The orbit from (40,10)(40, 10) has V=−2.347025V = -2.347025, against the minimum V(25,10)=−2.412023V(25, 10) = -2.412023. On this orbit xx moves between 14.3214.32 and 4040 and yy between 6.816.81 and 14.0514.05. Prey peak first, and predators peak about a quarter of a cycle later. The period depends on the size of the orbit:

start (x0,10)(x_0, 10) 25.5 30 40 60 100
period 8.886 8.905 9.031 9.477 10.751

The small orbits have the period 2π/ad=8.8862\pi/\sqrt{ad} = 8.886 of the linearization. The orbit from (100,10)(100, 10) takes the prey down to 1.981.98 and the predators down to 2.012.01, values at which a real population of a few individuals can die out by chance.

Relation to other models

  • Prey with a carrying capacity. Replace axa x by ax(1−x/K)a x (1 - x/K), the logistic growth model. If d/c<Kd/c < K, the cycles disappear and every positive solution spirals into one equilibrium (proof below). The closed orbits of the classical model are fragile.
  • Predators with satiation. Replace bxyb x y by a type II Holling functional response bxy/(1+bhx)b x y / (1 + b h x). With logistic prey this gives a model in which a stable limit cycle can appear: an isolated cycle whose amplitude does not depend on the start. The Poincaré-Bendixson theorem is the usual tool to prove that such a cycle exists.
  • Lotka-Volterra competition. It uses the same quadratic interaction terms with negative signs on both, for two species that harm each other. It has no closed orbits.
  • The SIR model. The Kermack-McKendrick SIR model has the same mass-action product SIS I, with the susceptibles as the prey that are not replaced. Without births, it has one epidemic wave and no cycles.
  • Many species. The generalized Lotka-Volterra equations x˙i=xi(ri+∑jaijxj)\dot x_i = x_i (r_i + \sum_j a_{ij} x_j) hold both cases above for nn species.
  • Stochastic versions. In a model with individual births, deaths and kills, the quantity VV is no longer conserved. Nothing pulls the state back to one cycle, so the amplitude drifts at random. A large cycle takes a species down to a few individuals, where it can die out by chance.

Known results in the model

The first integral. The function

V(x,y)=cx−dln⁡x+by−aln⁡yV(x, y) = c x - d \ln x + b y - a \ln y

is constant along each solution with x,y>0x, y > 0.

Proof. By the chain rule, dVdt=(c−dx)x˙+(b−ay)y˙\frac{dV}{dt} = \left(c - \frac{d}{x}\right)\dot x + \left(b - \frac{a}{y}\right)\dot y. Since x˙=x(a−by)\dot x = x(a - b y) and y˙=y(cx−d)\dot y = y(c x - d), this is (cx−d)(a−by)+(by−a)(cx−d)=0(c x - d)(a - b y) + (b y - a)(c x - d) = 0. □\square

Every positive solution is periodic. If x0,y0>0x_0, y_0 > 0 and (x0,y0)≠(x∗,y∗)(x_0, y_0) \ne (x^*, y^*), the solution is periodic, and its orbit is the closed curve {V=V(x0,y0)}\{V = V(x_0, y_0)\}.

Proof. The axes are invariant: x=0x = 0 gives x˙=0\dot x = 0, and y=0y = 0 gives y˙=0\dot y = 0. So a positive solution stays positive. Write V(x,y)=g(x)+h(y)V(x, y) = g(x) + h(y) with g(x)=cx−dln⁡xg(x) = c x - d \ln x and h(y)=by−aln⁡yh(y) = b y - a \ln y. The function gg is strictly convex on (0,∞)(0, \infty), with its only minimum at x=d/cx = d/c, and g(x)→∞g(x) \to \infty as x→0x \to 0 or x→∞x \to \infty; the same holds for hh with its minimum at y=a/by = a/b. So VV is strictly convex with one minimum at (x∗,y∗)(x^*, y^*), and each level set {V=v}\{V = v\} with v>V(x∗,y∗)v > V(x^*, y^*) is a closed bounded convex curve. Each vertical line meets it in at most two points, and so does each horizontal line. The solution stays on this curve, and the curve holds no equilibrium, because the only positive equilibrium is the minimum point. The speed ∣(x˙,y˙)∣|(\dot x, \dot y)| is continuous and positive on the compact curve, so it has a positive minimum, and the solution moves around the whole curve in finite time. When it comes back to its start, it repeats, because the equation is autonomous. □\square

The linearization is a center. The Jacobian at (x∗,y∗)(x^*, y^*) is

J=(0−bd/cac/b0),J = \begin{pmatrix} 0 & -b d / c \\ a c / b & 0 \end{pmatrix},

with the eigenvalues ±iad\pm i \sqrt{a d}. So small orbits are close to ellipses with the period 2π/ad2\pi/\sqrt{a d}. The linearization alone does not prove that the orbits are closed, because a center of the linear system can be a spiral of the nonlinear one; the first integral proves it.

The period grows with the amplitude. The table above shows it for the example: the period increases from 8.8868.886 for small orbits to 10.75110.751 for the orbit from (100,10)(100, 10). This is a measurement with RK4 and dt=0.01dt = 0.01, not a closed form.

The law of the averages (Volterra). Over one period TT of any positive solution, the time averages are the equilibrium values:

1T∫0Tx dt=dc,1T∫0Ty dt=ab.\frac{1}{T}\int_0^T x\,dt = \frac{d}{c}, \qquad \frac{1}{T}\int_0^T y\,dt = \frac{a}{b}.

Proof. ddtln⁡x=a−by\frac{d}{dt}\ln x = a - b y. Integrate from 0 to TT. The left side is ln⁡x(T)−ln⁡x(0)=0\ln x(T) - \ln x(0) = 0, because the solution is periodic. So 0=aT−b∫0Ty dt0 = a T - b \int_0^T y\,dt. The same step with ddtln⁡y=cx−d\frac{d}{dt}\ln y = c x - d gives the average of xx. □\square

Harvesting raises the prey (Volterra's principle). Remove both species at the same per capita rate ε\varepsilon, with 0<ε<a0 < \varepsilon < a. The equations become x˙=(a−ε)x−bxy\dot x = (a - \varepsilon) x - b x y and y˙=cxy−(d+ε)y\dot y = c x y - (d + \varepsilon) y, so the averages become (d+ε)/c(d + \varepsilon)/c for the prey and (a−ε)/b(a - \varepsilon)/b for the predator. Harvest increases the mean prey and decreases the mean predator. Volterra used this to explain why the share of predatory fish in the Adriatic catches rose when fishing stopped during the First World War.

With logistic prey, the coexistence point attracts. Take x˙=ax(1−x/K)−bxy\dot x = a x (1 - x/K) - b x y and y˙=cxy−dy\dot y = c x y - d y with x∗=d/c<Kx^* = d/c < K. Then y∗=ab(1−x∗K)>0y^* = \frac{a}{b}\left(1 - \frac{x^*}{K}\right) > 0, and every solution with x0,y0>0x_0, y_0 > 0 tends to (x∗,y∗)(x^*, y^*).

Proof. Let W(x,y)=cx−dln⁡x+by−by∗ln⁡yW(x, y) = c x - d \ln x + b y - b y^* \ln y. As above, dWdt=(cx−d)(a−axK−by)+(by−by∗)(cx−d)=(cx−d)(a−axK−by∗)\frac{dW}{dt} = (c x - d)\left(a - \frac{a x}{K} - b y\right) + (b y - b y^*)(c x - d) = (c x - d)\left(a - \frac{a x}{K} - b y^*\right). Since a−by∗=ax∗/Ka - b y^* = a x^*/K, the last factor is aK(x∗−x)\frac{a}{K}(x^* - x), and dWdt=−acK(cx−d)2≤0\frac{dW}{dt} = -\frac{a}{cK}(c x - d)^2 \le 0. So WW is a Lyapunov function. Its sublevel sets in the open quadrant are compact, as in the proof above, so each solution is bounded and stays away from the axes. By LaSalle's invariance principle, the solution tends to the largest invariant set inside {W˙=0}={x=x∗}\{\dot W = 0\} = \{x = x^*\}. On the line x=x∗x = x^*, x˙=0\dot x = 0 forces y=y∗y = y^*. So that set is the point (x∗,y∗)(x^*, y^*). □\square

In the example with K=100K = 100, the solution from (40,10)(40, 10) spirals into (25,7.5)(25, 7.5).

Structural instability. The two results above show that the closed orbits are not robust. A small self-limitation of the prey, of any size 1/K>01/K > 0 with d/c<Kd/c < K, turns every cycle into a spiral. Cycles that are seen in real predator and prey data need a model in which they are robust, such as a limit cycle.

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.

lotka_volterra_predator_prey.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/lotka-volterra-predator-prey/. The authors are listed in prov/JXS149.jsonl.
"""The Lotka-Volterra predator-prey model.

    dx/dt = a x - b x y      (prey)
    dy/dt = c x y - d y      (predator)

All four parameters are positive. The equilibrium with both species is
(d / c, a / b), and V = c x - d ln x + b y - a ln y is constant on every
solution. The module integrates the model with fixed step RK4, measures
the period of an orbit and the time averages over one period, and has
the variant with logistic prey growth, which loses the closed orbits.
"""

import math


def field(x: float, y: float, a: float, b: float, c: float, d: float, k: float = math.inf):
    """The vector field; k is the prey carrying capacity (infinite in the classical model)."""
    return a * x * (1.0 - x / k) - b * x * y, c * x * y - d * y


def first_integral(x: float, y: float, a: float, b: float, c: float, d: float) -> float:
    """V(x, y) = c x - d ln x + b y - a ln y."""
    return c * x - d * math.log(x) + b * y - a * math.log(y)


def rk4_step(x: float, y: float, dt: float, p):
    """One classical Runge-Kutta step for the field with parameters p = (a, b, c, d) or (a, b, c, d, k)."""
    k1 = field(x, y, *p)  # @line L3
    k2 = field(x + dt * k1[0] / 2, y + dt * k1[1] / 2, *p)  # @line L4
    k3 = field(x + dt * k2[0] / 2, y + dt * k2[1] / 2, *p)  # @line L5
    k4 = field(x + dt * k3[0], y + dt * k3[1], *p)  # @line L6
    x = x + dt * (k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0]) / 6  # @line L7
    y = y + dt * (k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1]) / 6  # @line L7
    return x, y


def simulate(x0: float, y0: float, dt: float, steps: int, p):
    """The list of states at times 0, dt, ..., steps dt."""
    x, y = x0, y0  # @line L1
    out = [(x, y)]
    for _ in range(steps):  # @line L2
        x, y = rk4_step(x, y, dt, p)
        out.append((x, y))
    return out  # @line L8


def crossings(traj, level: float, dt: float):
    """Times at which y crosses `level` upward, by linear interpolation between steps."""
    times = []
    for i in range(len(traj) - 1):
        y0, y1 = traj[i][1], traj[i + 1][1]
        if y0 < level <= y1:
            times.append((i + (level - y0) / (y1 - y0)) * dt)
    return times


def period_and_means(x0: float, y0: float, dt: float, t_max: float, p):
    """The period of the orbit from (x0, y0), and the time averages of x and y over one period."""
    a, b, c, d = p[:4]
    traj = simulate(x0, y0, dt, int(round(t_max / dt)), p)
    ts = crossings(traj, a / b, dt)
    i0, i1 = int(math.ceil(ts[0] / dt)), int(math.floor(ts[1] / dt))
    # trapezoid rule on the grid points inside one period, plus the two end pieces
    seg = traj[i0:i1 + 1]
    sx = sum((seg[j][0] + seg[j + 1][0]) / 2 for j in range(len(seg) - 1)) * dt
    sy = sum((seg[j][1] + seg[j + 1][1]) / 2 for j in range(len(seg) - 1)) * dt
    span = (len(seg) - 1) * dt
    return ts[1] - ts[0], sx / span, sy / span


def small_orbit_period(a: float, d: float) -> float:
    """The period 2 pi / sqrt(a d) of the linearization at the equilibrium."""
    return 2 * math.pi / math.sqrt(a * d)
test_lotka_volterra_predator_prey.py 67 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/lotka-volterra-predator-prey/. The authors are listed in prov/JXS149.jsonl.

import math

from lotka_volterra_predator_prey import (
    field,
    first_integral,
    period_and_means,
    simulate,
    small_orbit_period,
)

P = (1.0, 0.1, 0.02, 0.5)  # a, b, c, d; the equilibrium is (25, 10)


def test_equilibrium_is_a_rest_point() -> None:
    assert field(25.0, 10.0, *P) == (0.0, 0.0)


def test_first_integral_is_conserved() -> None:
    # dt = 0.01 up to t = 40 (more than four periods); RK4 keeps V within 1e-9.
    traj = simulate(40.0, 10.0, 0.01, 4000, P)
    v0 = first_integral(40.0, 10.0, *P)
    assert abs(v0 - (-2.347025)) < 1e-6
    assert max(abs(first_integral(x, y, *P) - v0) for x, y in traj) < 1e-9
    xs = [x for x, _ in traj]
    ys = [y for _, y in traj]
    assert abs(min(xs) - 14.3207) < 1e-3 and abs(max(ys) - 14.0512) < 1e-3
    assert abs(min(ys) - 6.8142) < 1e-3


def test_derivative_of_v_is_zero() -> None:
    # The chain rule: dV/dt = (c - d/x) x' + (b - a/y) y' = 0 at any positive point.
    a, b, c, d = P
    for x in (1.0, 13.0, 25.0, 70.0):
        for y in (0.5, 10.0, 31.0):
            fx, fy = field(x, y, *P)
            assert abs((c - d / x) * fx + (b - a / y) * fy) < 1e-12


def test_period_and_volterra_averages() -> None:
    period, mx, my = period_and_means(40.0, 10.0, 0.01, 25.0, P)
    assert abs(period - 9.031) < 2e-3
    # The time averages over one period equal the equilibrium (25, 10).
    assert abs(mx - 25.0) < 2e-2 and abs(my - 10.0) < 2e-2


def test_small_orbits_have_the_linear_period() -> None:
    lin = small_orbit_period(1.0, 0.5)
    assert abs(lin - 8.885766) < 1e-6
    period, _, _ = period_and_means(25.5, 10.0, 0.01, 25.0, P)
    assert abs(period - lin) < 5e-3
    big, _, _ = period_and_means(60.0, 10.0, 0.01, 40.0, P)
    assert big > period + 0.5


def test_logistic_prey_damps_the_cycles() -> None:
    # With prey capacity K = 100, the coexistence point is (25, 7.5) and it attracts.
    p = P + (100.0,)
    traj = simulate(40.0, 10.0, 0.01, 20000, p)
    x, y = traj[-1]
    assert abs(x - 25.0) < 1e-3 and abs(y - 7.5) < 1e-3
    # The Lyapunov function with y* = 7.5 in place of a / b decreases along the path.
    a, b, c, d = P
    w = [c * x - d * math.log(x) + b * y - b * 7.5 * math.log(y) for x, y in traj[::100]]
    assert all(v2 <= v1 + 1e-12 for v1, v2 in zip(w, w[1:]))

References

  1. Lotka 1925, the equations for a species that feeds on another, and their periodic solutions
  2. Vito Volterra, Fluctuations in the abundance of a species considered mathematically, Nature 118(2972), 1926, pages 558 to 560 (the equations, the periodic fluctuations, the law of the averages and the effect of fishing)
  3. James D. Murray, Mathematical Biology I, An Introduction, third edition, Springer, 2002, Chapter 3, Models for Interacting Populations (the Lotka-Volterra system, its first integral, and realistic predator-prey 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/JXS149/text.yaml