Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 39 ยท Projects and Practice

Project: Predator and Prey Model

Build a complete predator-prey project: simulate Lotka-Volterra, add logistic growth, analyze equilibria with the Jacobian, and fit parameters to noisy data with SciPy.

This project brings together the main skills of the course on one classic model: you will write the equations, simulate them, analyze the equilibria with linearization, see the effect of a more realistic assumption, and finally recover the model's parameters from noisy "field data" with a least-squares fit. Treat it as a template for any modeling task: model, simulate, analyze, calibrate, report.

Step 1: the model

Let x(t) be the prey (hares, in thousands) and y(t) the predators (lynx, in hundreds). The Lotka-Volterra model assumes:

  • prey grow exponentially at rate a when alone,
  • predators eat prey at a rate proportional to meetings x y (rate b),
  • each meal creates new predators with efficiency d, and predators die at rate m.
TEXT
x' = a x - b x y
y' = d x y - m y

The system has two equilibria: extinction (0, 0), and the coexistence point (m/d, a/b). As the earlier lesson showed, the linearization at coexistence is a center and the model has a conserved quantity, so populations cycle forever with amplitude set by the initial condition. Real populations do not do that: any tiny perturbation changes the amplitude, and the model is called structurally unstable. The logistic variant below repairs this.

Python Simulate the classic model

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The cycles are long (about 7.6 time units for these parameters, a bit longer than the small-oscillation estimate of 7.26 because the oscillation is large). Predators peak after the prey, as in real data.

Step 2: add logistic growth

Real prey cannot grow without limit. Replace a x by a x (1 - x/K) with carrying capacity K:

TEXT
x' = a x (1 - x/K) - b x y
y' = d x y - m y

The coexistence equilibrium is x* = m/d, y* = (a/b)(1 - x*/K) (it exists if K > m/d). The Jacobian there has a negative trace, so the center becomes a stable spiral: all nearby trajectories settle on the equilibrium. Computing the Jacobian numerically is a good habit when formulas get messy.

Python Equilibrium and eigenvalues, numerically

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The eigenvalues have a negative real part, and the printed trajectory spirals into (3, 1.4). A single parameter, K, changed the qualitative picture, which is exactly the kind of insight a phase-plane analysis gives. Increasing K makes the real part less negative, so the spiral is weaker: the paradox of enrichment says that adding more food to the prey eventually destabilizes the coexistence.

Step 3: calibrate with data

Suppose you only have noisy measurements of both populations at a handful of times, and want estimates of the four parameters a, b, d, m. The standard approach:

  1. Pick a parameter vector p.
  2. Solve the ODE with p and evaluate it at the measurement times.
  3. Compute the residuals (model minus data).
  4. Let an optimizer such as scipy.optimize.least_squares change p to minimize the sum of squared residuals.

To keep the demonstration honest we generate the data ourselves (so we know the truth) with a seeded random generator.

Python Fit parameters to noisy data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fitted values land close to the truth, and the remaining residual is about the 3% noise level. If the residual is much larger than the noise, either the model is wrong or the optimizer stuck in a poor local minimum; try another starting guess.

A caution on identifiability: the classic model has a scaling symmetry between b and d and the units of the populations. If you only observed the prey, several parameter sets would fit equally well. Always check how sensitive the fit is, for example by repeating it with different noise seeds.

A modeling workflow

Model Write rates from assumptions.
Simulate Check against intuition.
Analyze Equilibria and stability.
Calibrate Fit parameters, check residuals.

Repeat with a better model when the residuals disagree.

1 / 2

Extensions to try

  • Add harvesting of the prey (-h x) and find the harvest rate where the predators go extinct.
  • Replace the product x y by a saturating Holling type II response x y / (1 + c x) and look for a limit cycle (the Rosenzweig-MacArthur model).
  • Add a third species and look at the chaos that three-level food chains can display.
  • Estimate parameter uncertainty from fit.jac using the covariance (J^T J)^-1 ฯƒยฒ.

Common mistakes

  • Trusting a fit without looking at residuals and at the data on the same plot.
  • Letting the optimizer choose negative rates; keep parameters positive (bounds or a log-transform).
  • Fitting with too short a time span, so the cycles are not even seen.
  • Reading the classic model's center as a prediction that real populations cycle with fixed amplitude.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the logistic predator-prey model, the coexistence equilibrium requires:

    The predator-free prey level is K, and predators can survive only if the prey density m/d they need is below K.
  2. 2. Adding logistic growth to Lotka-Volterra changes the coexistence point from a center to:

    The Jacobian trace becomes negative, so the neutral oscillation turns into a decaying one.
  3. 3. What does least_squares minimize?

    It adjusts the parameters to make the model's output as close to the data as possible in the least-squares sense.
  4. 4. Why seed the random number generator when generating test data?

    A fixed seed gives the same noisy data every run, so results can be compared.

Summary

  • Model the interaction rates, then simulate, then analyze with the Jacobian.
  • The classic model cycles forever; logistic growth gives a stable spiral.
  • Parameter calibration is a least-squares problem wrapped around an ODE solver.
  • Use positive parameters, sensible starting guesses and a residual check.
  • Identifiability and noise level limit what the data can tell you.

Next: Project: Epidemic Modeling with SIR