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 , for the densities of a prey and a predator . 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. is the prey density and is the predator density, as numbers or biomass per unit of area. Both are real numbers.
The parameters. All four are positive.
- , in 1 per time: the growth rate of the prey with no predators.
- , in 1 per (predator density times time): the rate at which one predator kills prey, per unit of prey density.
- , in 1 per (prey density times time): the growth of the predator per unit of prey density. If a fraction of the eaten prey is turned into predators, then when both are counted in the same unit.
- , in 1 per time: the death rate of the predator with no prey.
The equations.
The assumptions. Without predators the prey grow without bound, at the rate . Without prey the predators die out, at the rate . Prey and predators meet at random, so the kill rate is proportional to (mass action). Each predator eats 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 below.
LV-RK4(x0, y0, dt, steps) ▷ F(x, y) = (a x - b x y, c x y - d y)- 1
(x, y) ← (x0, y0) - 2
repeat steps times - 3
k1 ← F(x, y) - 4
k2 ← F((x, y) + dt k1 / 2) - 5
k3 ← F((x, y) + dt k2 / 2) - 6
k4 ← F((x, y) + dt k3) - 7
(x, y) ← (x, y) + dt (k1 + 2 k2 + 2 k3 + k4) / 6 - 8
return the list of states
Each step costs four evaluations of , so a run to time costs operations. With the value of changes by less than over 4 periods in the example below.
What it counts
- The equilibria: the extinction point and the coexistence point .
- The orbit through a start point, which is the level curve of through it, and its period.
- The time averages of and over one period.
- The response of the averages to a harvest of both species.
An example. Take , , and , in arbitrary time units. The coexistence point is . The orbit from has , against the minimum . On this orbit moves between and and between and . Prey peak first, and predators peak about a quarter of a cycle later. The period depends on the size of the orbit:
| start | 25.5 | 30 | 40 | 60 | 100 |
|---|---|---|---|---|---|
| period | 8.886 | 8.905 | 9.031 | 9.477 | 10.751 |
The small orbits have the period of the linearization. The orbit from takes the prey down to and the predators down to , 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 by , the logistic growth model. If , 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 by a type II Holling functional response . 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 , 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 hold both cases above for species.
- Stochastic versions. In a model with individual births, deaths and kills, the quantity 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
is constant along each solution with .
Proof. By the chain rule, . Since and , this is .
Every positive solution is periodic. If and , the solution is periodic, and its orbit is the closed curve .
Proof. The axes are invariant: gives , and gives . So a positive solution stays positive. Write with and . The function is strictly convex on , with its only minimum at , and as or ; the same holds for with its minimum at . So is strictly convex with one minimum at , and each level set with 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 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.
The linearization is a center. The Jacobian at is
with the eigenvalues . So small orbits are close to ellipses with the period . 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 for small orbits to for the orbit from . This is a measurement with RK4 and , not a closed form.
The law of the averages (Volterra). Over one period of any positive solution, the time averages are the equilibrium values:
Proof. . Integrate from 0 to . The left side is , because the solution is periodic. So . The same step with gives the average of .
Harvesting raises the prey (Volterra's principle). Remove both species at the same per capita rate , with . The equations become and , so the averages become for the prey and 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 and with . Then , and every solution with tends to .
Proof. Let . As above, . Since , the last factor is , and . So 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 . On the line , forces . So that set is the point .
In the example with , the solution from spirals into .
Structural instability. The two results above show that the closed orbits are not robust. A small self-limitation of the prey, of any size with , 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.
# 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
- Lotka 1925, the equations for a species that feeds on another, and their periodic solutions
- 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)
- 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.
| 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 JXS149 Edit this page History Report an error Markdown