# Hierarchical Predator Prey modelling The Lotka-Volterra predator-prey model is the archetypical model for dynamical systems, depicting the fluctuating population development of the dynamical system. It is simple enough to fit parameters and estimate their uncertainty in a single replicate. But what if there was some environmental fluctuation we wanted ```python import numpy as np import arviz as az import xarray as xr import matplotlib.pyplot as plt import preliz as pz import jax jax.config.update("jax_enable_x64", True) from pymob import Config from pymob.inference.scipy_backend import ScipyBackend from pymob.sim.parameters import Param, RandomVariable from pymob.sim.config import Modelparameters from pymob.solvers.diffrax import JaxSolver from pymob.inference.analysis import plot_pair ``` WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions. ```python # import case study and simulation config = Config("../scenarios/lotka_volterra_hierarchical_hyperpriors/settings.cfg") config.case_study.package = "../.." config.case_study.scenario = "lotka_volterra_hierarchical_vaying_y0" config.import_casestudy_modules(reset_path=True) from sim import HierarchicalSimulation sim = HierarchicalSimulation(config) sim.setup() ``` Inserted './../..' in PATH at index=0 Inserted './../../lotka_volterra_case_study' in PATH at index=0 MinMaxScaler(variable=rabbits, min=0.0, max=1329.0) MinMaxScaler(variable=wolves, min=0.0, max=1019.0) Results directory exists at '/home/flo-schu/projects/pymob/case_studies/lotka_volterra_case_study/results/lotka_volterra_hierarchical_vaying_y0'. Scenario directory exists at '/home/flo-schu/projects/pymob/case_studies/lotka_volterra_case_study/scenarios/lotka_volterra_hierarchical_vaying_y0'. ## Investigate the structure of $y_0$ For simulating our artificial data (`hierarchical_model.ipynb`), we assumed some initial values of $y_0$. The $y_0$ values were generated from a uniform distribution between 2 and 15 for wolves and a uniform distribution between 35 and 70. Then, after simulating the observations, a poisson noise model was added on top of the deterministic simulation. So far, we have assumed that the noisy observation at $t=0$ are the true initial values for the simulation. To demonstrate this effect. We look at two trajectories that have different starting values ```python # expand time coordinates and constrain index coordinates for demonstration purposes sim.coordinates["time"] = np.linspace(0,10,100) # sim.coordinates["id"] = np.arange(0, 3) sim.dispatch_constructor() # TODO: Only partially replace the y0 values (like in theta) e = sim.dispatch( theta={"alpha": 1, "beta": 0.02}, y0={"rabbits": [50], "wolves":np.arange(1,121)} ) e() fig, (ax1, ax2) = plt.subplots(2,1) for i in sim.coordinates["id"]: e.results.sel(id=i).wolves.plot(ax=ax1, label=f"id={i}") e.results.sel(id=i).rabbits.plot(ax=ax2, label=f"id={i}") # plt.legend() e.results ```
<xarray.Dataset>
Dimensions: (id: 120, time: 100)
Coordinates:
* id (id) int32 0 1 2 3 4 5 6 ... 114 115 116 117 118 119
* time (time) float64 0.0 0.101 0.202 ... 9.798 9.899 10.0
rabbit_species (id) object 'Cottontail' 'Cottontail' ... 'Jackrabbit'
experiment (id) object '2010' '2010' '2010' ... '2012' '2012'
rabbit_species_index (id) int64 0 0 0 0 0 0 0 0 0 0 ... 1 1 1 1 1 1 1 1 1 1
experiment_index (id) int64 0 0 0 0 0 0 0 0 0 0 ... 2 2 2 2 2 2 2 2 2 2
Data variables:
rabbits (id, time) float64 50.0 55.2 60.94 ... 27.61 29.72
wolves (id, time) float64 1.0 1.023 1.052 ... 13.67 13.65<xarray.DataArray 'beta' ()> array(0.01748016)
<xarray.Dataset>
Dimensions: (hdi: 2)
Coordinates:
* hdi (hdi) <U6 'lower' 'higher'
Data variables:
beta (hdi) float64 0.01709 0.01786