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

Add Safeguards to EulerCore to reduce undefined behavior for SDE models #1165

Open
2 tasks done
nijawa opened this issue Dec 17, 2024 · 5 comments
Open
2 tasks done
Assignees
Labels
class::feature A feature to be implemented for some part of the software class::performance Performance related issues loc::backend This issue concerns the C++ backend implementation. model::ode This issue concerns any kind of ODE-based model. model::sde This issue concerns any kind of stochastic differential equation-based model.

Comments

@nijawa
Copy link
Contributor

nijawa commented Dec 17, 2024

Motivation / Current Behaviour

As of now SDE models have to implement checks for whether flows/derivatives result in overflows in the compartments (negative compartments or compartments greater than population size). The current implementation can also result in undefined behavior by floating point operations (see Issue #1009)

Enhancement description

Adapting the EulerCore (potentially writing a EulerMaruyamaCore) one can use vectorized functions to add those safeguards and other modifications into the core. Potential modifications are:

  • Clipping compartments to the intervall [0, population_size]
  • Setting compartments below some threshhold to zero
  • Rescaling compartments in case clipping changed total population size

Additional context

No response

Checklist

  • Attached labels, especially loc:: or model:: labels.
  • Linked to project
@nijawa nijawa added class::feature A feature to be implemented for some part of the software loc::backend This issue concerns the C++ backend implementation. model::ode This issue concerns any kind of ODE-based model. class::performance Performance related issues model::sde This issue concerns any kind of stochastic differential equation-based model. labels Dec 17, 2024
@nijawa
Copy link
Contributor Author

nijawa commented Dec 17, 2024

I have proposed a possible "new" EulerCore in the linked branch, before opening a pull request I would like to discuss whether this should be moved into a new core

@mknaranja
Copy link
Member

@reneSchm any expert opinion how Nils could proceed?

@mknaranja mknaranja moved this from Product Backlog to Sprint Backlog in MEmilio: Equation-Based-Models Development Dec 17, 2024
@reneSchm
Copy link
Member

I assume you mean NaNs in simulation results by "undefined behaviour"? While they are not great, these NaNs are perfectly well defined behaviour in our case.

I am not convinced that the IntegratorCore is a suitable place for this. The main issues I see are:

  • Placing these safeguards in the IntegratorCore mixes responsibilities and creates more interdependence. The Integrator is perfectly fine with negative values, and will not produce any unless they come from the model (or more accurately, the integrand aka. DerivFunction).
  • I am not sure that it is desirable to rescale integration results. It seems to me like a band-aid fix to an issue with the Model/DerivFunction, that may cause issues when used with different models later.
  • Does your EulerCore work with flow models? The flows (used as yt) are initialised as 0, so if I read this correctly, the integration result will be const. 0.

An alternative may be to use a new DerivFunction to compute the stochastic component from each flow and add it to that flow, as well as applying the safeguards. This DerivFunction can be used by a new Simulation class, or we can discuss adding it in some form to the FlowSimulation.

I have a experimental branch here. It does not have your safeguards yet, but I think they can be added around the calculation of m_pop or around the compuation of the stochastic components. Can you maybe try to use this with a model that is currently causing issues, so we can try and discuss fixes? That is, can you implement the "get_flow_denoised" function, which just returns the flows without clamping or adding a stochastic component.

@nijawa
Copy link
Contributor Author

nijawa commented Dec 17, 2024

Hi René thank you for your insight. I will implement a model with 3 variants and 4 different immunity states as an example.

@nijawa
Copy link
Contributor Author

nijawa commented Dec 31, 2024

I have implemented two models sde_ui3 and sde_ui10, reflecting a SIS model with virus mutation (the first with 3 variants and 4 immunity states, the second with 10 variants and 11 immunity states). It seems to work quite well but I have yet to write tests.

One thing I have noticed is that the 10 variant model is very demanding during build with cmake. My setup was barely able to finish compilation with 32 GB RAM and 100 GB swap file so I excluded the model from the cmake files in the branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
class::feature A feature to be implemented for some part of the software class::performance Performance related issues loc::backend This issue concerns the C++ backend implementation. model::ode This issue concerns any kind of ODE-based model. model::sde This issue concerns any kind of stochastic differential equation-based model.
Projects
Status: Sprint Backlog
Development

When branches are created from issues, their pull requests are automatically linked.

4 participants