Hello,

I am using PyStan to attempt to fit a model to some time-series data, all of which I have attached below.

My model is comprised of 3 ODEs, and I am using integrate_ode_rk45 to numerically integrate them.

One peculiarity of my model is that I have a step change happening in two of its parameters, which occur at different times during an experiment. I have implemented these step changes using simple if statements in the function.

When I go to fit this model I regularly get error messages along the lines of “Exception: normal_lpdf: Location parameter is -nan”, which (by investigating with print statements) is indeed true.

By adding a lot of print statements to my code I have managed to figure out that issues are arising at (or just after) the discontinuity points at which the parameters change (i.e. those that undergo step changes). I assume that the cause for the errors is therefore that the gradient estimation in the ODE_RK45 algorithm is being thrown off by the step change.

Does anyone know how I might go about overcoming this issue? I expect forcing the ode solver to use a smaller time step might help, but I couldnt see an option for this in the Stan reference manual.

It may be of interest that I previously had a similar model (which included step changes in parameters) working fine; when I changed the structure of the ODEs this problem emerged.

ex.py (2.0 KB) model.stan (3.8 KB) out.csv (7.6 KB)

Thanks!