Modelcore
Ricker model
Also called Ricker stock-recruitment model.
The discrete population model N_(t+1) = N_t e^(r (1 - N_t / K)), used for fish stocks, which shows cycles and chaos for large r.
The Ricker model is the discrete population model for a species with separate generations. Below the level the population grows, and above it falls. The fall is strong: a large population produces fewer offspring than a medium one. Ricker (1954) wrote it as a stock-recruitment curve , the number of young fish that a stock of spawners produces, for fisheries. Like the logistic map, the model has a stable equilibrium for small , then cycles of period 2, 4, 8, and chaos for large . Unlike the logistic map, the population stays positive for every , so the model is a valid population model for all its parameters.
The machine
The state. One number , the population, or the number of spawners, in generation . The scaled state is .
The parameters. The growth rate , with no unit: is the factor of growth per generation when the population is very small. The equilibrium level , in the units of (individuals, or tonnes of spawners).
The update rule. , or with . The slope is . The stock-recruitment form is the same rule with and , so and .
The assumptions. Generations are discrete and do not overlap. The population is closed and has no age structure. The number of offspring per individual falls exponentially with the density, , so it never becomes negative. A common way to get this form: if each young individual dies when it meets an adult, and the meetings are random with a mean number proportional to , then the chance to survive is the zero term of a Poisson distribution, (heuristic). The model is deterministic.
RICKER(r, K, N_0, T)- 1
N ← N_0 - 2
for t ← 1 to T do - 3
N ← N exp(r (1 - N / K)) - 4
record N_t ← N - 5
return N_1, ..., N_T
Each step costs one exponential and a few arithmetic operations, so generations cost .
What it counts
- The trajectory , the population or the stock in each generation.
- The long-run behaviour: the equilibrium , a cycle of period , or an orbit with no period.
- The multiplier of a cycle, the product of over its points. The cycle is stable when the absolute value of the multiplier is less than 1.
- The Lyapunov exponent, the mean of along the orbit, which is positive for chaos.
- In fisheries, the recruits from a stock , and the stock that gives the most recruits.
Relation to other models
- Logistic map. Both maps have one hump and the same route to chaos by period doubling. The logistic map sends values above 1 to negative values for . The Ricker map is positive for every , and its large values are followed by very small ones, not by negative ones.
- Logistic growth model. Suppose that the population grows by during one generation, with the rate per individual fixed by the density at the start of the generation. Then is constant during the generation, and the population at its end is exactly . So the Ricker model is logistic growth with a density effect that acts once per generation (theorem, by integration).
- Beverton-Holt model. The model has recruitment that rises to a plateau and never falls. It has no cycles. The Ricker curve falls at high stock, which is called overcompensation, and this is the source of the cycles.
- Leslie matrix. An age-structured linear model. If the fertilities in its first row are made to depend on the density in the Ricker form, the model has cycles and chaos as well (heuristic).
- Fixed-point iteration. The trajectory is a fixed-point iteration of , and the rate of approach to is per generation.
Known results in the model
Positive and bounded (theorem). If , then for every .
Proof. for . The slope is zero only at , positive below and negative above, so has its maximum there.
The value after the peak is . At the peak is times , and the next generation is times . Over 199000 generations at the orbit fills the range from to (measurement). A crash to 3 percent of is a real risk of extinction in a small population, which the deterministic model does not show (heuristic).
The equilibrium (theorem). The fixed point , that is , has multiplier . It is stable for . For the population approaches from one side, at the fixed point is superstable, and for the population overshoots and oscillates with decreasing size. For example, from gives , and the error is roughly halved and changes sign in each step, since . The Lyapunov exponent there is .
The 2-cycle (theorem). For the map has a 2-cycle , where solves . Its multiplier is .
Proof. Let be a 2-cycle. Multiply and to get , so . Write and . Then , which is the equation for . The left side is , convex in with slope 2 at 0 and infinite at 1, so it meets at some exactly when . The multiplier is .
At the cycle is with multiplier . The population alternates between 41 percent and 159 percent of . The cycle loses stability when the multiplier is , that is when . Together with the equation for this gives (numerical root).
The period-doubling cascade (measurement). A bisection on the period of the attractor gives the next doublings near (period 4 to 8), (8 to 16) and (16 to 32), and the onset of chaos near . The ratios of the successive intervals are about and . The map has one quadratic maximum, so by the known result of Feigenbaum they converge to , the same constant as for the logistic map. At the stable 4-cycle is .
Chaos (measurement). At the orbit from has no period up to 64, and with 1000 steps of transient and 200000 steps of average its Lyapunov exponent is . At it is , and at it is . A search on a grid of step finds a stable 3-cycle from about . Where a 3-cycle exists, the theorem of Li and Yorke gives points of every period.
Fitting the curve (theorem and practice). is linear in . So ordinary least squares of on estimates and from pairs of stock and recruits. If the noise is a factor with independent normal of constant variance, this fit is the maximum likelihood estimate (theorem). It is the usual first fit of the Ricker curve in fisheries (known practice). The errors in the stock values bias the slope, and the observed stocks depend on past recruits, so the fit is only a first estimate (heuristic).
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/ricker-model/. The authors are listed in prov/ZBTGC3.jsonl.
"""The Ricker model N_(t+1) = N_t exp(r (1 - N_t / K)).
orbit gives the trajectory. The other functions convert between the
growth form (r, K) and the stock-recruitment form R = a S exp(-b S), find
the 2-cycle u = 1 -/+ d of the scaled map u -> u exp(r (1 - u)), the value
of r where the 2-cycle loses stability, the period of the attractor, the
Lyapunov exponent, and the least-squares fit of ln(R / S) = ln a - b S.
Cost: Theta(T) operations for T steps.
"""
from __future__ import annotations
import math
from typing import List, Optional, Sequence, Tuple
def step(r: float, u: float, K: float = 1.0) -> float:
"""One generation of the model."""
return u * math.exp(r * (1 - u / K))
def orbit(r: float, n0: float, steps: int, K: float = 1.0) -> List[float]:
"""The values N_1, ..., N_steps from the start n0."""
if steps < 0 or K <= 0:
raise ValueError("need steps >= 0 and K > 0")
n = n0 # @line L1
out: List[float] = []
for _ in range(steps): # @line L2
n = n * math.exp(r * (1 - n / K)) # @line L3
out.append(n) # @line L4
return out # @line L5
def to_stock_recruitment(r: float, K: float) -> Tuple[float, float]:
"""(a, b) with a S exp(-b S) = S exp(r (1 - S / K))."""
return math.exp(r), r / K
def from_stock_recruitment(a: float, b: float) -> Tuple[float, float]:
"""(r, K) for R = a S exp(-b S), with a > 1 and b > 0."""
if a <= 1 or b <= 0:
raise ValueError("need a > 1 and b > 0")
r = math.log(a)
return r, r / b
def multiplier(r: float, cycle: Sequence[float]) -> float:
"""The product of f'(u) = exp(r (1 - u)) (1 - r u) over a cycle of the scaled map."""
m = 1.0
for u in cycle:
m *= math.exp(r * (1 - u)) * (1 - r * u)
return m
def two_cycle(r: float) -> Optional[Tuple[float, float]]:
"""The 2-cycle (1 - d, 1 + d), where ln((1 + d) / (1 - d)) = r d, or None for r <= 2."""
if r <= 2:
return None
g = lambda d: math.log((1 + d) / (1 - d)) - r * d
lo, hi = 1e-12, 1 - 1e-15
for _ in range(200):
mid = (lo + hi) / 2
if g(mid) < 0:
lo = mid
else:
hi = mid
d = (lo + hi) / 2
return 1 - d, 1 + d
def two_cycle_limit() -> float:
"""The r > 2 where the multiplier (1 - r)^2 - r^2 d^2 of the 2-cycle is -1."""
lo, hi = 2.1, 3.0
for _ in range(100):
mid = (lo + hi) / 2
if multiplier(mid, two_cycle(mid)) > -1:
lo = mid
else:
hi = mid
return (lo + hi) / 2
def attractor(r: float, u0: float = 0.3, burn: int = 100000,
max_period: int = 64, tol: float = 1e-9) -> Optional[List[float]]:
"""The sorted cycle the scaled orbit settles on, or None if no period <= max_period."""
u = u0
for _ in range(burn):
u = step(r, u)
path = [u]
for _ in range(max_period):
u = step(r, u)
path.append(u)
for p in range(1, max_period + 1):
if abs(path[p] - path[0]) < tol:
return sorted(path[:p])
return None
def lyapunov(r: float, u0: float = 0.3, steps: int = 100000, burn: int = 1000) -> float:
"""The mean of log |f'(u_t)| along the scaled orbit."""
u = u0
for _ in range(burn):
u = step(r, u)
total = 0.0
for _ in range(steps):
total += math.log(abs(math.exp(r * (1 - u)) * (1 - r * u)))
u = step(r, u)
return total / steps
def fit_stock_recruitment(stock: Sequence[float], recruits: Sequence[float]) -> Tuple[float, float]:
"""Least squares of ln(R / S) on S. Returns (a, b) of R = a S exp(-b S)."""
if len(stock) != len(recruits) or len(stock) < 2:
raise ValueError("need two or more pairs")
ys = [math.log(rr / s) for s, rr in zip(stock, recruits)]
n = len(stock)
mx = sum(stock) / n
my = sum(ys) / n
sxx = sum((s - mx) ** 2 for s in stock)
if sxx == 0:
raise ValueError("the stock values must not all be equal")
slope = sum((s - mx) * (y - my) for s, y in zip(stock, ys)) / sxx
return math.exp(my - slope * mx), -slope
test_ricker_model.py 103 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/ricker-model/. The authors are listed in prov/ZBTGC3.jsonl.
"""Tests of the Ricker model against the page values and the closed forms."""
import math
import random
import pytest
from ricker_model import (
attractor,
fit_stock_recruitment,
from_stock_recruitment,
lyapunov,
multiplier,
orbit,
step,
to_stock_recruitment,
two_cycle,
two_cycle_limit,
)
def close(xs, ys, tol=1e-6):
return len(xs) == len(ys) and all(abs(a - b) < tol for a, b in zip(xs, ys))
def test_page_trace_and_scaling():
assert close(orbit(1.5, 0.2, 6), [0.664023, 1.099139, 0.947260, 1.025241, 0.987149, 1.006362])
scaled = orbit(1.5, 0.2, 6)
assert close(orbit(1.5, 200.0, 6, K=1000.0), [1000 * u for u in scaled], tol=1e-9)
assert orbit(2.0, 0.5, 0) == []
with pytest.raises(ValueError):
orbit(2.0, 0.5, 3, K=0)
def test_stock_recruitment_forms():
rng = random.Random(61)
for _ in range(200):
r, K = rng.uniform(0.1, 3), rng.uniform(1, 1000)
a, b = to_stock_recruitment(r, K)
s = rng.uniform(0, 3 * K)
assert abs(a * s * math.exp(-b * s) - step(r, s, K)) < 1e-9 * max(1, s)
r2, K2 = from_stock_recruitment(a, b)
assert abs(r2 - r) < 1e-12 and abs(K2 - K) < 1e-9 * K
with pytest.raises(ValueError):
from_stock_recruitment(0.5, 1)
def test_fixed_point_and_bound():
for r in (0.5, 1.0, 1.5, 1.9):
assert abs(multiplier(r, [1.0]) - (1 - r)) < 1e-12
assert close(attractor(r), [1.0], tol=1e-8)
rng = random.Random(62)
for _ in range(2000):
r = rng.uniform(0.1, 4)
u = rng.uniform(1e-6, 10)
v = step(r, u)
assert 0 < v <= math.exp(r - 1) / r * (1 + 1e-12)
def test_two_cycle():
assert two_cycle(2.0) is None
for r in (2.1, 2.3, 2.5):
p, q = two_cycle(r)
assert abs(p + q - 2) < 1e-12
assert abs(step(r, p) - q) < 1e-9 and abs(step(r, q) - p) < 1e-9
d = q - 1
assert abs(multiplier(r, [p, q]) - ((1 - r) ** 2 - r * r * d * d)) < 1e-9
assert close(attractor(r), [p, q], tol=1e-7)
assert close(two_cycle(2.3), [0.407845, 1.592155])
assert abs(multiplier(2.3, two_cycle(2.3)) + 0.164925) < 1e-6
r_c = two_cycle_limit()
assert abs(r_c - 2.526468) < 1e-6
p, q = two_cycle(r_c)
assert abs((q - 1) ** 2 - ((r_c - 1) ** 2 + 1) / r_c ** 2) < 1e-9
def test_four_cycle_chaos_and_lyapunov():
assert close(attractor(2.6), [0.181222, 0.390846, 1.523166, 1.904765])
assert attractor(3.0) is None
assert abs(lyapunov(1.5) - math.log(0.5)) < 1e-9
assert lyapunov(3.0) > 0.3
assert lyapunov(2.6) < 0
def test_boom_and_bust_at_r_three():
peak = math.exp(2) / 3
assert abs(peak - 2.463019) < 1e-6
assert abs(step(3.0, peak) - 0.030572) < 1e-6
xs = orbit(3.0, 0.3, 20000)[1000:]
assert max(xs) <= peak + 1e-12 and min(xs) >= step(3.0, peak) - 1e-12
def test_fit_recovers_parameters():
rng = random.Random(63)
a, b = math.exp(1.2), 0.004
stock = [rng.uniform(10, 800) for _ in range(400)]
recruits = [a * s * math.exp(-b * s) * math.exp(rng.gauss(0, 0.2)) for s in stock]
a_hat, b_hat = fit_stock_recruitment(stock, recruits)
assert abs(math.log(a_hat) - 1.2) < 0.06 and abs(b_hat - b) < 0.0002
exact = fit_stock_recruitment([1, 2, 3], [a * s * math.exp(-b * s) for s in (1, 2, 3)])
assert abs(exact[0] - a) < 1e-9 and abs(exact[1] - b) < 1e-12
References
- W. E. Ricker, Stock and recruitment, Journal of the Fisheries Research Board of Canada 11(5), 1954, pages 559 to 623 (the stock-recruitment curve with a maximum, and its use for fish stocks)
- Robert M. May, Simple mathematical models with very complicated dynamics, Nature 261(5560), 1976, pages 459 to 467 (the exponential form x exp(r (1 - x)) beside the logistic form)
- J. D. Murray, Mathematical Biology I, An Introduction, 3rd ed., Springer, 2002, Chapter 2 (discrete population models for a single species)
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 ZBTGC3 Edit this page History Report an error Markdown