# Dimension error while replicating the Lotka Volterra model example

**URL:** <https://discourse.pymc.io/t/dimension-error-while-replicating-the-lotka-volterra-model-example/16224>\
**Category:** v5\
**Tags:** smc\
**Created:** [December 7, 2024, 2:58pm UTC](https://discourse.pymc.io/t/dimension-error-while-replicating-the-lotka-volterra-model-example/16224 "2024-12-07T14:58:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maerad](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@maerad](https://discourse.pymc.io/u/maerad)\
**Post date:** [December 7, 2024, 2:58pm UTC](https://discourse.pymc.io/t/dimension-error-while-replicating-the-lotka-volterra-model-example/16224/1 "2024-12-07T14:58:32Z")

</div>

I’m trying to replicate the Lotka Volterra example in the Approximate Bayesian Computation section, and find that I’m getting an dimension mismatch error.

Specifically, while running the sample() method, I get

```auto
RuntimeError: The array return by func must be one-dimensional, but got ndim=3.
Apply node that caused the error: Simulator_sim_rv{"(),()->()"}(simulator_rng, [100 2], Exp.0, Exp.0)
Toposort index: 8
Inputs types: [RandomGeneratorType, TensorType(int64, shape=(2,)), TensorType(float64, shape=(1, 1)), TensorType(float64, shape=(1, 1))]
Inputs shapes: ['No shapes', (2,), (1, 1), (1, 1)]
Inputs strides: ['No strides', (8,), (8, 8), (8, 8)]
Inputs values: [Generator(PCG64) at 0x159748200, array([100, 2]), array([[0.1087194]]), array([[0.87183442]])]
Outputs clients: [[output[1](Simulator_sim_rv{"(),()->()"}.0)], [Composite{sqr((0.1 * (i0 - i1)))}(sim{[[10.12490 ... 04511178]]}, Simulator_sim_rv{"(),()->()"}.out)]]

```

I believe it is because it expects the sum\_stat to return a one-dimensional output, but if the sum\_stat is the identity function, then it should be able to compute the error with the simulated and observed data, right?

This is the version of PyMC that I’m currently using 5.19.1.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [December 7, 2024, 9:52pm UTC](https://discourse.pymc.io/t/dimension-error-while-replicating-the-lotka-volterra-model-example/16224/2 "2024-12-07T21:52:47Z")

</div>

Can you share the full code?

---

<div class="post-metadata">

**Author:** ![maerad](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@maerad](https://discourse.pymc.io/u/maerad)\
**Post date:** [December 8, 2024, 4:21am UTC](https://discourse.pymc.io/t/dimension-error-while-replicating-the-lotka-volterra-model-example/16224/3 "2024-12-08T04:21:04Z")

</div>

Yes, it is exactly as in the example. I’m pasting it here for your reference.

```py
import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pymc as pm

print(f"Running on PyMC v{pm. __version__ }")

from scipy.integrate import odeint

# Definition of parameters
a = 1.0
b = 0.1
c = 1.5
d = 0.75

# initial population of rabbits and foxes
X0 = [10.0, 5.0]
# size of data
size = 100
# time lapse
time = 15
t = np.linspace(0, time, size)

# Lotka - Volterra equation
def dX_dt(X, t, a, b, c, d):
    """Return the growth rate of fox and rabbit populations."""

    return np.array([a * X[0] - b * X[0] * X[1], -c * X[1] + d * b * X[0] * X[1]])

# simulator function
def competition_model(rng, a, b, size=None):
    return odeint(dX_dt, y0=X0, t=t, rtol=0.01, args=(a, b, c, d))

# function for generating noisy data to be used as observed data.
def add_noise(a, b):
    noise = np.random.normal(size=(size, 2))
    simulated = competition_model(None, a, b) + noise
    return simulated

observed = add_noise(a, b)

with pm.Model() as model_lv:
    a = pm.HalfNormal("a", 1.0)
    b = pm.HalfNormal("b", 1.0)

    sim = pm.Simulator("sim", competition_model, params=(a, b), epsilon=10, observed=observed)

    idata_lv = pm.sample_smc()

```

The exact error message is as pasted below.

```py
Traceback (most recent call last):
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pytensor/compile/function/types.py", line 992, in __call__
    outputs = vm() if output_subset is None else vm(output_subset=output_subset)
              ^^^^
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pytensor/graph/op.py", line 531, in rval
    r = p(n, [x[0] for x in i], o)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pytensor/tensor/random/op.py", line 402, in perform
    self.rng_fn(rng, *args, None if size is None else tuple(size)),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pymc/distributions/simulator.py", line 53, in rng_fn
    return cls.fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/folders/b9/hmz_c9_95kb0_r42zp984m7m0000gq/T/ipykernel_27808/1881729530.py", line 27, in competition_model
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/scipy/integrate/_odepack_py.py", line 247, in odeint
    output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: The array return by func must be one-dimensional, but got ndim=3.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/concurrent/futures/process.py", line 264, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pymc/smc/sampling.py", line 306, in _sample_smc_int
    smc._initialize_kernel()
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pymc/smc/kernels.py", line 249, in _initialize_kernel
    likelihoods = [self.likelihood_logp_func(sample) for sample in self.tempered_posterior]
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pytensor/compile/function/types.py", line 1002, in __call__
    raise_with_op(
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pytensor/link/utils.py", line 526, in raise_with_op
    raise exc_value.with_traceback(exc_trace)
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pytensor/compile/function/types.py", line 992, in __call__
    outputs = vm() if output_subset is None else vm(output_subset=output_subset)
              ^^^^
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pytensor/graph/op.py", line 531, in rval
    r = p(n, [x[0] for x in i], o)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pytensor/tensor/random/op.py", line 402, in perform
    self.rng_fn(rng, *args, None if size is None else tuple(size)),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/pymc/distributions/simulator.py", line 53, in rng_fn
    return cls.fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/folders/b9/hmz_c9_95kb0_r42zp984m7m0000gq/T/ipykernel_27808/1881729530.py", line 27, in competition_model
  File "/Users/maerad/mamba/envs/pymc_env/lib/python3.12/site-packages/scipy/integrate/_odepack_py.py", line 247, in odeint
    output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: The array return by func must be one-dimensional, but got ndim=3.
Apply node that caused the error: Simulator_sim_rv{"(),()->()"}(simulator_rng, [100 2], Exp.0, Exp.0)
Toposort index: 8
Inputs types: [RandomGeneratorType, TensorType(int64, shape=(2,)), TensorType(float64, shape=(1, 1)), TensorType(float64, shape=(1, 1))]
Inputs shapes: ['No shapes', (2,), (1, 1), (1, 1)]
Inputs strides: ['No strides', (8,), (8, 8), (8, 8)]
Inputs values: [Generator(PCG64) at 0x162E0CBA0, array([100, 2]), array([[0.78711716]]), array([[0.64662201]])]
Outputs clients: [[output[1](Simulator_sim_rv{"(),()->()"}.0)], [Composite{sqr((0.1 * (i0 - i1)))}(sim{[[8.89815 ... 51544454]]}, Simulator_sim_rv{"(),()->()"}.out)]]

```

The scipy version I’m using is 1.14.1

---

<div class="post-metadata">

**Author:** ![bekahdwhite](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bekahdwhite/32/9087_2.png) [@bekahdwhite](https://discourse.pymc.io/u/bekahdwhite)\
**Post date:** [March 11, 2025, 10:22pm UTC](https://discourse.pymc.io/t/dimension-error-while-replicating-the-lotka-volterra-model-example/16224/4 "2025-03-11T22:22:05Z")

</div>

Did you determine the bug? I’m using PyMC v5.21.1 and having the same issue.

---

<div class="post-metadata">

**Author:** ![SirTwinkleberry](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/sirtwinkleberry/32/9656_2.png) [@SirTwinkleberry](https://discourse.pymc.io/u/SirTwinkleberry)\
**Post date:** [April 29, 2026, 12:58am UTC](https://discourse.pymc.io/t/dimension-error-while-replicating-the-lotka-volterra-model-example/16224/5 "2026-04-29T00:58:24Z")

</div>

Having had the same issue, I found a partial reason and a solution.

#### Reason:

`competition_model` takes parameters `a` and `b` assuming they’re scalar or scalar-like. Unfortunately, for reasons that I do not yet have, `pymc.Simulator` will send an array of shape `(1,1)` for `a` and `b`, e.g., `a = [[1.2345]]`. When `competition_model` calls `dX_dt`, the latter will broadcast `(1,1) x (1,)` for the prey and the predator elements and array-ify them into shape `(2,1,1)`, e.g., `[[[1.2345]], [[5.4321]]]` which it will then return.

Therefore, `odeint` in `competition_model` will receive a `(2,1,1)` output (3 dimensions) instead of a `(2,)` output (1 dimension).

#### Solution:

Modify `competition_model` as such:

```python
# simulator function
def competition_model(rng, a, b, size=None):
    a = np.squeeze(a)
    b = np.squeeze(b)
    return odeint(dX_dt, y0=X0, t=t, rtol=0.01, args=(a, b, c, d))

```

Note that this github issue is relevant as well: [Approximate Bayesian Computation is broken · Issue #787 · pymc-devs/pymc-examples · GitHub](https://github.com/pymc-devs/pymc-examples/issues/787)

Edit: changed flatten to squeeze and removed type checking as per followup of github issue.
