stan-dev / posteriordb

Database with posteriors of interest for Bayesian inference
161 stars 26 forks source link

Update ODE models to Stan 2.24's solvers, as current method is deprecated. #263

Open JasonPekos opened 5 days ago

JasonPekos commented 5 days ago

Diffeqs should perhaps updated to use the new ode_rk45. I can take a look at this personally, if you want.

Sorry for the initial blank issue, I hit enter by accident while drafting this 😕 .

JasonPekos commented 5 days ago

New solvers are here. This would apply to the SIR, lotka_volterra, elim_abs, one_comp_mm_elim_abs, and soil_incubation models.

MansMeg commented 4 days ago

Thanks. This is good to update. The question is if we should update existing models or add new versions. I assume the log density might be slightly different with different solvers, right?

JasonPekos commented 4 days ago

Both the old and new solvers seem to use the same RK4(5) method (in this case, Dormand-Prince). Maybe the new solver is faster, or more numerically stable, but I'm not sure.

I think the question is whether or not Stan ever intends to drop support for the deprecated solvers. If they do, these models will eventually stop working, and updating them is at least a little important. If they don't, this basically falls into the "Make Models More Performant" https://github.com/stan-dev/posteriordb/issues/224 issue.

MansMeg commented 3 days ago

Ah. Ok. Do you know if the old and new solvers give the same log density? I assume they would (but I have not worked much with ODEs)?

This reminds me that we probably should decide on how to define ”identical” models. I have just assume identical models have proportional log densities. But here we also have an approximation from the solver, I guess.

JasonPekos commented 3 days ago

Do you know if the old and new solvers give the same log density?

Ok, so I found the release notes for the newer solvers.

From this, my assumption is that the actual solver code stays the same, and the new interface simply provides a more efficient way of setting up the problems. So, I think the log density should remain the same also. From the release:

The new ODE interface is intended to make it easier to specify the ODE RHS by avoiding packing and unpacking schemes required with the old interface. It is important to switch to the new interfaces because the old interfaces are significantly slower than they were previously.

Basically, what I think is going on, is that in the past ODEs need to be specified like this (pseudocode):

function f(t, y, theta){
    param_1  = theta[1]
    param_2 = theta[2]
    param_3 = theta[3]

    // calculate gradients
}

And with the new interface, you pass the parameters directly:

function f(t, y, param_1, param_2, param_3){
    // calculate gradients
}

and this is faster because it's costly to create intermediate variables (maybe for memory management reasons? but I'm not sure.)


Also, the Stan manual notes:

Any feature which changes semantic meaning (such as the upgraded ODE solver interface) will not be removed until a major version change (e.g., Stan 3.0).

So we have time before this breaks. I assumed it'd break in three versions from 2.4, but they specifically flag this as something that exists until 3.0.


So I guess my takeaway is that:

  1. The logdensities should be the same, as it's only the interface to the solver that's changing, which means we should only keep the updated models.
  2. The models should be updated to the new interface, mostly to make sure they don't break in an update
  3. But the update that breaks this won't be for a long time, so it's very not urgent.
MansMeg commented 2 days ago

Thanks. Yes. I agree. Lets leave this issue here. If you change the ODEs here feel free to do a PR.