After some more cursory reading on this, I think making this one-way could make the problem a lot easier. I shared a guide from Wolfram earlier about stiffness detection: Stiffness Detection—Wolfram Language Documentation. It addresses some of what @yizhang mentioned earlier about LSODA dropping stiffness detection because of the inaccuracy of using the norm of the Jacobian to bound the dominant eigenvalue. The Wolfram page lists fewer methods (just one) for stiffness detection to switch an algorithm to stiff mode, compared to non-stiffness detection, for which more options are given. The Mathematica “automatic” non-stiffness detection methodology consists of the following control flow:
I definitely get where @yizhang is coming from with the large burden of verification and future maintenance that would come with implementing an ODE integrator natively in Stan math. And in many cases, the tried-and-true solvers from the established libraries are the way to go for sure. However, newer methods that are being validated over the past couple of decades aren’t being added to some of the older libraries, and some of the those methods could really benefit the simulation of emerging models with unknown stiff/non-stiff regimes. With the focus on differential equation systems in fields like system biology and the growing need of researchers in those fields to use PPL’s like Stan for robust model comparisons, perhaps ODE maintenance/validation could be factored in as part of Stan’s development upkeep?
@yizhang, are there newer C++ libraries that you trust that could be worth wrapping into Stan (here is one I found that seems more unvalidated/experimental)? And general question for everyone in this thread – does sufficient appetite exist for implementing some sort of switching solver or stiffness detection natively in Stan Math? I think that’s the critical question – if not, we should look at other C++ libraries that could be wrapped and most likely pivot away from discussing a switching algorithm for now. Otherwise, I’m definitely willing to at least learn more about stiffness and non-stiffness detection ahead of efforts to contribute on that front.
Looking in the long term, it would be a heavy investment, but expanded native differential equation functionality in Stan could really make it a selling point compared to TensorFlow Probability, PyMC4. As is, Turing.jl uses compatibility with DifferentialEquations.jl as one of the its main draws. Of course, a PPL would be hard-pressed to ever match a differential equations package in solvers with their different respective focuses, and neither should it because of time, manpower, and financial limitations; the prime development focus must always be on advancing the core HMC algorithm.
In the future, perhaps it would be more efficient resource usage to interface with a differential equations package with an FFI, since the addition of DDE, DAE, SDE, and PDE solvers that are not represented in existing C++/Fortran libraries are separate titanic issues. As an intermediate step for systems biologists, ecologists, climate modelers, astronomers, and the like though, a switching algorithm could be an intermediate step for easier plug-and-play ODE fitting; that would be easier than adding a different type of solver, but would still be useful.
If we feel that an ODE solver, much less other types of solvers, would be too troublesome to implement and maintain in Stan Math, the strategy should be oriented toward getting Stan to be able to call other differential equation packages. Labs across various fields are looking to perform Bayesian inference on an ever greater range of model types and will look elsewhere if they are unable to implement their models with the appropriate solvers in Stan, reducing potential Stan’s user-developer base. Folks can’t appreciate the XMC algorithm if they can’t write their model. At the end of the day, expanded FFI functionality and taking advantage of another package seems like it may very well take less effort than adding DDE, DAE, SDE, PDE, IDE, etc. solvers and would save on the maintenance; I’m not sure whether the most efficient route long-term is native implementation of solvers or addition of an FFI to Stan that allows calling of foreign packages (Sean Talts wrote a little bit about FFIs as one the possible areas of future focus in Stan in his “A Path Forward for Stan” writeup.
By the way, is the Adams-Moulton solver natively built in Stan math, or is it also from CVODE?