Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JasonPekos opened this issue Jul 2, 2024 · 6 comments

Comments

@JasonPekos
Copy link

JasonPekos commented Jul 2, 2024

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 JasonPekos changed the title Update ODE models to Stan 2.24 Update ODE models to Stan 2.24's solvers, as current method is deprecated. Jul 2, 2024
@JasonPekos
Copy link
Author

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

@MansMeg
Copy link
Collaborator

MansMeg commented Jul 3, 2024

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
Copy link
Author

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" #224 issue.

@MansMeg
Copy link
Collaborator

MansMeg commented Jul 4, 2024

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
Copy link
Author

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
Copy link
Collaborator

MansMeg commented Jul 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants