13.5 Stiff ODEs
Stiffness is a numerical phenomena that causes some differential equation solvers difficulty, notably the Runge-Kutta RK45 solver used in the examples earlier. The phenomena is common in chemical reaction systems, which are often characterized by having multiple vastly different time-scales. The stiffness of a system can also vary between different parts of parameter space, and so a typically non-stiff system may exhibit stiffness occasionally. These sorts of difficulties can occur more frequently with loose priors or during warmup.
Stan provides a specialized solver for stiff ODEs
(Cohen and Hindmarsh 1996; Serban and Hindmarsh 2005). An ODE system is
specified exactly the same way with a function of exactly the same
signature. The only difference is in the call to the solver the
rk45
suffix is replaced with bdf
, as in
ode_bdf(sho, y0, t0, ts, theta);
Using the stiff (bdf
) solver on a system that is not stiff
may be much slower than using the non-stiff (rk45
) solver because
each step of the stiff solver takes more time to compute. On the other hand,
attempting to use the non-stiff solver for a stiff system will cause
the timestep to become very small, leading the non-stiff solver taking more
time overall even if each step is easier to compute than for the stiff solver.
If it is not known for sure that an ODE system is stiff, run the model with
both the rk45
and bdf
solvers and see which is faster. If the rk45
solver is faster, then the problem is probably non-stiff, and then it makes
sense to try the adams
solver as well. The adams
solver uses higher order
methods which can take larger timesteps than the rk45
solver, though similar
to the bdf
solver each of these steps is more expensive to compute.