Algorithm Search⌘K

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 Nt+1=Nter(1−Nt/K)N_{t+1} = N_t e^{r(1 - N_t / K)} for a species with separate generations. Below the level KK the population grows, and above KK 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 R=aSe−bSR = a S e^{-bS}, the number of young fish RR that a stock of SS spawners produces, for fisheries. Like the logistic map, the model has a stable equilibrium for small rr, then cycles of period 2, 4, 8, and chaos for large rr. Unlike the logistic map, the population stays positive for every rr, so the model is a valid population model for all its parameters.

The machine

The state. One number Nt>0N_t > 0, the population, or the number of spawners, in generation tt. The scaled state is ut=Nt/Ku_t = N_t / K.

The parameters. The growth rate r>0r > 0, with no unit: ere^r is the factor of growth per generation when the population is very small. The equilibrium level K>0K > 0, in the units of NN (individuals, or tonnes of spawners).

The update rule. Nt+1=Ntexp⁡(r(1−Nt/K))N_{t+1} = N_t \exp(r(1 - N_t / K)), or ut+1=f(ut)u_{t+1} = f(u_t) with f(u)=uer(1−u)f(u) = u e^{r(1 - u)}. The slope is f′(u)=er(1−u)(1−ru)f'(u) = e^{r(1 - u)} (1 - r u). The stock-recruitment form R=aSe−bSR = a S e^{-bS} is the same rule with a=era = e^r and b=r/Kb = r / K, so r=ln⁡ar = \ln a and K=ln⁡a/bK = \ln a / b.

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, ere−rN/Ke^{r} e^{-rN/K}, 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 NtN_t, then the chance to survive is the zero term of a Poisson distribution, e−cNte^{-cN_t} (heuristic). The model is deterministic.

  1. RICKER(r, K, N_0, T)
  2. 1N ← N_0
  3. 2for t ← 1 to T do
  4. 3N ← N exp(r (1 - N / K))
  5. 4record N_t ← N
  6. 5return N_1, ..., N_T

Each step costs one exponential and a few arithmetic operations, so TT generations cost Θ(T)\Theta(T).

What it counts

  • The trajectory N1,…,NTN_1, \ldots, N_T, the population or the stock in each generation.
  • The long-run behaviour: the equilibrium KK, a cycle of period pp, or an orbit with no period.
  • The multiplier of a cycle, the product of f′(u)f'(u) over its points. The cycle is stable when the absolute value of the multiplier is less than 1.
  • The Lyapunov exponent, the mean of ln⁡∣f′(ut)∣\ln |f'(u_t)| along the orbit, which is positive for chaos.
  • In fisheries, the recruits RR from a stock SS, and the stock S=1/bS = 1/b 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 r>4r > 4. The Ricker map is positive for every rr, and its large values are followed by very small ones, not by negative ones.
  • Logistic growth model. Suppose that the population grows by dN/dt=r(1−Nt/K)NdN/dt = r(1 - N_t / K) N during one generation, with the rate per individual fixed by the density NtN_t at the start of the generation. Then dln⁡N/dtd \ln N / dt is constant during the generation, and the population at its end is exactly Nter(1−Nt/K)N_t e^{r(1 - N_t / K)}. So the Ricker model is logistic growth with a density effect that acts once per generation (theorem, by integration).
  • Beverton-Holt model. The model Nt+1=R0Nt/(1+Nt/M)N_{t+1} = R_0 N_t / (1 + N_t / M) 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 ff, and the rate of approach to KK is ∣1−r∣|1 - r| per generation.

Known results in the model

Positive and bounded (theorem). If u0>0u_0 > 0, then 0<ut≤er−1/r0 < u_t \le e^{r-1}/r for every t≥1t \ge 1.

Proof. f(u)>0f(u) > 0 for u>0u > 0. The slope er(1−u)(1−ru)e^{r(1-u)}(1 - ru) is zero only at u=1/ru = 1/r, positive below and negative above, so ff has its maximum f(1/r)=er−1/rf(1/r) = e^{r-1}/r there. □\square

The value after the peak is f(er−1/r)f(e^{r-1}/r). At r=3r = 3 the peak is e2/3=2.463019e^2/3 = 2.463019 times KK, and the next generation is 0.0305720.030572 times KK. Over 199000 generations at r=3r = 3 the orbit fills the range from 0.0305720.030572 to 2.4630192.463019 (measurement). A crash to 3 percent of KK is a real risk of extinction in a small population, which the deterministic model does not show (heuristic).

The equilibrium (theorem). The fixed point u=1u = 1, that is N=KN = K, has multiplier f′(1)=1−rf'(1) = 1 - r. It is stable for 0<r<20 < r < 2. For 0<r<10 < r < 1 the population approaches KK from one side, at r=1r = 1 the fixed point is superstable, and for 1<r<21 < r < 2 the population overshoots and oscillates with decreasing size. For example, r=1.5r = 1.5 from u0=0.2u_0 = 0.2 gives 0.664023,1.099139,0.947260,1.025241,0.987149,1.0063620.664023, 1.099139, 0.947260, 1.025241, 0.987149, 1.006362, and the error is roughly halved and changes sign in each step, since 1−r=−0.51 - r = -0.5. The Lyapunov exponent there is ln⁡0.5=−0.6931\ln 0.5 = -0.6931.

The 2-cycle (theorem). For r>2r > 2 the map has a 2-cycle 1−d,1+d1 - d, 1 + d, where d∈(0,1)d \in (0, 1) solves ln⁡((1+d)/(1−d))=rd\ln((1 + d)/(1 - d)) = r d. Its multiplier is (1−r)2−r2d2(1 - r)^2 - r^2 d^2.

Proof. Let u1,u2u_1, u_2 be a 2-cycle. Multiply u2=u1er(1−u1)u_2 = u_1 e^{r(1 - u_1)} and u1=u2er(1−u2)u_1 = u_2 e^{r(1 - u_2)} to get er(2−u1−u2)=1e^{r(2 - u_1 - u_2)} = 1, so u1+u2=2u_1 + u_2 = 2. Write u1=1−du_1 = 1 - d and u2=1+du_2 = 1 + d. Then u2/u1=erdu_2 / u_1 = e^{rd}, which is the equation for dd. The left side is 2d+2d3/3+…2d + 2d^3/3 + \ldots, convex in dd with slope 2 at 0 and infinite at 1, so it meets rdrd at some d∈(0,1)d \in (0, 1) exactly when r>2r > 2. The multiplier is f′(u1)f′(u2)=er(2−u1−u2)(1−ru1)(1−ru2)=(1−r+rd)(1−r−rd)=(1−r)2−r2d2f'(u_1) f'(u_2) = e^{r(2 - u_1 - u_2)} (1 - r u_1)(1 - r u_2) = (1 - r + rd)(1 - r - rd) = (1 - r)^2 - r^2 d^2. □\square

At r=2.3r = 2.3 the cycle is 0.407845,1.5921550.407845, 1.592155 with multiplier −0.164925-0.164925. The population alternates between 41 percent and 159 percent of KK. The cycle loses stability when the multiplier is −1-1, that is when d2=((r−1)2+1)/r2d^2 = ((r - 1)^2 + 1)/r^2. Together with the equation for dd this gives r=2.526468r = 2.526468 (numerical root).

The period-doubling cascade (measurement). A bisection on the period of the attractor gives the next doublings near r=2.6563r = 2.6563 (period 4 to 8), 2.68462.6846 (8 to 16) and 2.69072.6907 (16 to 32), and the onset of chaos near r=2.692r = 2.692. The ratios of the successive intervals are about 4.594.59 and 4.644.64. The map has one quadratic maximum, so by the known result of Feigenbaum they converge to 4.6692016094.669201609, the same constant as for the logistic map. At r=2.6r = 2.6 the stable 4-cycle is 0.181222,0.390846,1.523166,1.9047650.181222, 0.390846, 1.523166, 1.904765.

Chaos (measurement). At r=3r = 3 the orbit from u0=0.3u_0 = 0.3 has no period up to 64, and with 1000 steps of transient and 200000 steps of average its Lyapunov exponent is 0.38540.3854. At r=3.5r = 3.5 it is 0.53790.5379, and at r=2.6r = 2.6 it is −0.5751-0.5751. A search on a grid of step 0.0010.001 finds a stable 3-cycle from about r=3.103r = 3.103. Where a 3-cycle exists, the theorem of Li and Yorke gives points of every period.

Fitting the curve (theorem and practice). ln⁡(R/S)=ln⁡a−bS\ln(R/S) = \ln a - bS is linear in SS. So ordinary least squares of ln⁡(R/S)\ln(R/S) on SS estimates ln⁡a\ln a and bb from pairs of stock and recruits. If the noise is a factor eεe^{\varepsilon} with independent normal ε\varepsilon 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.

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

  1. 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)
  2. 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)
  3. 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.

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/ZBTGC3/text.yaml