Error in compiling ODE model

While trying to compile the example from here with pystan.StanModel I get the error

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable "sho" does not exist.
 error in 'unknown file name' at line 23, column 39
  -------------------------------------------------
    21:
    22:     generated quantities {
    23:       vector[2] y_sim[T] = ode_rk45(sho, y0, t0, ts, theta);
                                              ^
    24:         for (t in 1:T) {
  -------------------------------------------------

The code I am running:

#!/usr/bin/env python3
import pystan

q_code = '''
    functions {
      vector sho(real t, vector y, real theta) {
        vector[2] dydt;
        dydt[1] = y[2];
        dydt[2] = -y[1] - theta * y[2];
        return dydt;
      }
    }

    data {
      int<lower=1> T;
      vector[2] y0;
      real t0;
      real ts[T];
      real theta;
    }

    model {
    }

    generated quantities {
      vector[2] y_sim[T] = ode_rk45(sho, y0, t0, ts, theta);
        for (t in 1:T) {
          y_sim[t, 1] += normal_rng(0, 0.1);
          y_sim[t, 2] += normal_rng(0, 0.1);
        }
    }
'''
sm = pystan.StanModel(model_code=q_code)
  • Python Version: 3.8.3
  • PyStan Version: 2.19.1.1

Thank you for your help!

That is an interesting error.

Did you install cvodes branch?

Thanks for the reply.

No, since this is only for stiff solvers and I am using ode_rk45.

1 Like

The variadic ode_rk45 is a new feature in Stan 2.24. PyStan is currently at 2.19 so it’s not available yet. The old version of the user guide page you linked shows different code that should work.
If you want the latest Stan you have to use CmdStanPy instead of PyStan.

Thank you!

Hi Niko. How can I see that CmdStanPy supports the latest version of Stan?

CmdStanPy uses CmdStan and by default install_cmdstan will install the latest version.

Thanks!