Data keyword for new ODE integrator interface

Hi all,

I really like the new interface for integrating ODEs in Stan version 2.4 and 2.5, because we no longer have to mess around with reconstructing parameter and data structures from 1d arrays. However, in the old interface it was possible to distinguish between data and parameters which is important because the ODE integrator has to add sensitivity equations for the parameters, but not the data (right?).
So my question is: does the new ODE interface distinguish between arguments that are parameters and data, or can we tell it by using the data keyword? For instance as I show in the following ODE model

functions {
  // the ubiquitous SIR model
  vector ode_sys(real t, vector y, real beta, data real gamma) { // assume gamma is known
    vector[2] dy = [-beta * y[1] * y[2], beta * y[1] * y[2] - gamma * y[2]]';
    return dy;
data {
  // we are really confident about gamma
  real<lower=0> gamma;
  real t0;
  int<lower=1> N;
  real<lower=t0> ts[N];
  // observations...
parameters {
  // we want to estimate beta
  real<lower=0> beta;
model {
  vector[2] y0 = [0.99, 0.01]';
  vector[2] y[N] = ode_adams(ode_sys, y0, t0, ts, beta, gamma);
  // compute likelihood of observations...



It’s all automatic. You can use the data keyword and then you will have to pass data to that argument…but you can also omit the data keyword and still pass data to this argument. As a result you will get the same speed. If that is not the case, then you found a bug.

I am glad you like the new interface…it was a lot of work!

1 Like

In case anyone is interested… I compared the runtime of 5 different variants of the above model.

1: gamma is data and has the data qualifier in the signature of ode_sys.
2: gamma is removed from the signature of ode_sys and set to a literal 1 in the equation for dy[2].
3: gamma is data but we don’t use the data qualifier
4: gamma is a parameter
5: gamma is defined in transformed parameters and set to a constant 1.