A hybrid framework for compartmental models enabling simulation-based inference Authors: Domenic P J Germano, Alexander E Zarebski, Sophie Hautphenne, Robert Moss, Jennifer A Flegg, Mark B Flegg Published: 2026 ABSTRACT AbstractMulti-scale systems often exhibit a combination of stochastic and deterministic dynamics. In compartmental models, low occupancy compartments tend to exhibit stochastic dynamics while high occupancy compartments tend to follow deterministic dynamics. Representing both dynamics with existing methods is challenging. Failing to account for stochasticity in small populations can produce “atto-foxes”, for example in the Lotka-Volterra ordinary differential equation (ODE) model. This limitation becomes problematic when studying the extinction of species or the clearance of infection, but it can be overcome by using discrete stochastic models, such as continuous-time Markov chains (CTMCs). Unfortunately, simulating CTMCs is impractical for many realistic models, where discrete events have very high frequencies. In this work, we develop a mathematical framework to couple continuous ODEs and discrete CTMCs: “Jump-Switch-Flow” (JSF). In this framework, compartments can reach extinct states (“absorbing states”), thereby resolving atto-fox-type problems. JSF has the desired behaviours of exact CTMC simulation, but is substantially computationally faster than existing alternatives, by at least one order of magnitude, and can even obtain constant scaling, irrespective of compartment occupancy. We demonstrate JSF’s utility for simulation-based inference, particularly multi-scale problems, with several case-studies. In a simulation study, we demonstrate how JSF can enable a more nuanced analysis of the efficacy of public health interventions. We also carry out a novel analysis of longitudinal within-host data from SARS-CoV-2 infections to quantify the timing of viral clearance. In this work, we show how JSF offers a novel approach to compartmental model simulation. Supplementary InformationThe online version contains supplementary material available at 10.1007/s00285-026-02409-y.Keywords: Hybrid simulation, Stochastic modelling, Viral clearance, Extinction, Compartmental modelling Introduction Dynamical systems are a powerful method for describing the world, and have successfully been applied in many fields. However, modelling populations which change in size across multiple scales remains a challenge Fowler (2021). Population sizes in biological processes often change over orders of magnitude, e.g. the spread of infectious disease (Anderson and May 1991); the boom-and-bust of insect populations (Ludwig et al. 1978); and the immune response to infections, such as HIV (Perelson 2002), and influenza virus (Baccam et al. 2006). The dynamics of small populations can be heavily influenced by stochastic effects, while for larger populations these fluctuations often average out, justifying the use of continuum models. When the size of the population under consideration does not vary across many orders of magnitude3.5, there is usually a natural (and obvious) choice between a stochastic or deterministic model. However, for multi-scale models, this decision remains a challenge, and developing modelling methods that can bridge these scales has been a long-standing goal of applied mathematics (Cotter and Erban 2016; Flegg et al. 2014; Isaacson 2013). Compartmental models describe how quantities (e.g. the number of molecule, cells, or people) change in a dynamical system. Two popular ways to represent compartmental models are ordinary differential equations (ODEs) and continuous-time Markov chains (CTMCs). Many ODEs one encounters are actually ensemble averages of CTMCs, although the mathematical justification of this is not always straightforward (Kurtz 1970, 1971, 1972). In ODEs, the state of the system changes continuously, which can lead to “atto-fox problems” where populations shrink to infeasibly small sizes, i.e. where a real population would likely have gone extinct (Fowler 2021; Mollison 1991; Lobry and Sari 2015). In CTMCs, the discrete state changes stochastically and there may be absorbing states, e.g. extinction of a population. As a result, CTMCs offer a more natural description of small populations sizes (Bretó et al. 2009), however simulating large populations can be computationally challenging. Moreover, a range of powerful techniques can be brought to bear on ODEs, enabling more thorough analysis. This raises a natural question about what to do when compartment occupancy changes across orders of magnitude. Exact and approximate algorithms to simulate CTMCs exist Gillespie (1976); Simoni et al. (2019). Exact methods (e.g. Doob-Gillespie, first/next reaction, and rejection based methods) are computationally expensive when state transitions occur at a high rate (Sanft and Othmer 2015). Approximate methods (e.g. Tau-leaping (Gillespie 2001; Cao et al. 2006) and the use of chemical Langevin equations (Rao and Arkin 2003; Gillespie 2000; Gibson and Bruck 2000)) scale to high transition rates but can have unacceptable approximation errors. To overcome the limitations of classical approximate methods, over the past two decades, there has been a concerted effort to develop hybrid stochastic-deterministic approaches (Simoni et al. 2019; Kreger et al. 2021; Bressloff and Newby 2014; Rebuli et al. 2017). Some hybrid modelling approaches involve partitioning the transitions in a CTMC into fast and slow transitions. Despite having significant implementation bookkeeping and overheads, these approaches can be sampled individually and subsequently synchronised in an efficient manner Simoni et al. (2019). Jump-diffusion differential equations partition the model compartments into fluid and discrete compartments, to construct hybrid switching jump diffusion processes Buckwar and Riedler (2011), Angius et al. (2015). Other hybrid simulation techniques have been developed in the context of within-host viral infection Kreger et al. (2021). However, this implementation is not generalised, but tailored to a single system, and does not allow switching between stochastic and deterministic regimes. In the context of determining the hybridisation between stochastic and deterministic couplings, path-integrals have been utilised, Bressloff and Newby (2014), which permit analysis of hybrid processes. Recently, Kynaston et al. (2023) introduced the regime conversion method (RCM) considering extended systems that represent each compartment with both a continuous and a discrete version, allowing for the conversion between them Kynaston et al. (2023). In the RCM, switching between the discrete and continuous representation requires a conversion reaction that is based on the species occupancy Kynaston et al. (2023). In the work we present here, switching between the discrete and continuous regimes only occurs when the species occupancy crosses a predefined threshold.3.6 The hybrid processes we introduce here belong to the class of piecewise deterministic Markov processes (PDMPs), originally defined by Davis (Davis 1984). PDMPs have been studied as a general class of stochastic processes (Riedler 2013; Azaïs et al. 2014; Saporta et al. 2016). In this work we consider their use in the context of compartmental models and their role in “plug and play” inference (Bretó et al. 2009).2.1 Essential for the real-world utility of many mathematical models is their ability to be calibrated to data. For deterministic compartment models, standard parameter inference methods, such as maximum likelihood and posterior sampling via Markov chain Monte Carlo (MCMC), can readily be applied. For stochastic models these inference problems are usually much harder. Simulation-based inference methods have emerged as a way to handle this challenge, e.g. the particle filter (Arulampalam et al. 2002; Kitagawa 1996) and approximate Bayesian computation (Alahmadi et al. 2020) in the Bayesian setting, and iterated filtering (Ionides et al. 2006; Bretó et al. 2009) in the frequentist setting.1.1 Calibrated multi-scale simulations of ODE (compartment) models is an important milestone towards accurate predictive models that address stochastic behaviour. In a recent paper, Trindade and Zygalakis develop a hybrid scheme for chemical kinetics over different scales with the goal of demonstrating the utility for parameter estimation (Trindade and Zygalakis 2024). In this paper, a CTMC is used to model reaction events on small populations (below a threshold I1>0) and for better efficiency Tau-leaping is used for reactions that only impact large populations (above a threshold I2>I1). Between these two thresholds, a “blending function” is used to create a linear transition between an accurate CTMC and efficient Tau-leaping treatment of the reactions. The hybrid adoption of CTMC and Tau-leaping regimes has the property of maintaining appropriate levels of noise at large populations but struggles as populations grow much larger where a Chemical Langevin or deterministic ODE description may be more computationally appropriate. In this paper, we present a simple and efficient hybrid simulation method which we call Jump-Switch-Flow (JSF). This method enables adaptive transitions between stochastic and deterministic regimes across compartments, while ensuring conservation principles are maintained. In this approach, compartments can individually switch regimes, allowing for the compartments to be split into stochastic and deterministic subsets, while remaining coupled via a jump clock that utilises the fact that the deterministic compartments result in the jump times forming an inhomogeneous Poisson process3.11. This regime switching presents unique challenges for matching at interfaces: ensuring that the deterministic regime represents the expected state after transition between regimes and that mass is conserved. Conservation is not always essential when it pertains to small population sizes, however in many cases sustained small populations have a significant impact on larger populations (e.g. enzymes in chemical processes and viruses within hosts). We demonstrate the utility and properties of the JSF method through simulation studies and a case study in which we reanalyse existing longitudinal SARS-CoV-2 viral load datasets. Two simulation studies are presented to explore the properties of JSF and demonstrate its use in an inference setting. In the first simulation study, we compare the computational efficiency and accuracy of JSF with the exact Doob-Gillespie method and the Tau-leaping method. In the second simulation study, we demonstrate the types of insight available when using an approach that supports absorbing (extinction) states. In the case study, we demonstrate the significance of being able to perform inference with models with absorbing states (i.e. models in which one of the compartments can go extinct.) We reanalyse longitudinal SARS-CoV-2 viral load data (Ke et al. 2022) using JSF to simulate a TEIRV (Target cells – Eclipsed cell – Infectious cells – Refractory cell – Virions) model and infer the state of host and virus across the infection. Understanding how a viral infection is cleared has important ramifications for both treatment and prevention. For example, the initial exposure may fail to initiate a systematic infection, and understanding the conditions under which this happens is important for infection prevention Pearson et al. (2011). Moreover, the infection may reach low levels, potentially escaping detection, but the virus may rebound and cause disease. Understanding when the virus has been cleared is important for optimising treatment duration. Inferring viral clearance can be computationally challenging (Yan et al. 2016), and this case study demonstrates how JSF enables the tractable estimation of viral clearance. Methods Jump-Switch-Flow mathematical framework Consider a compartmental model with n compartments, V→=v1,...,vn, where the state variable, vi:=vi(t), represents the value of the ith compartment at time t. For example, vi could be the number of people infected with a pathogen, or the quantity3.8 of a molecule in a cell. The state variables vi may take values from different domains, depending on the resolution needed, for example in an ODE, vi will have real values, and in a CTMC, vi might have integer values. When the variables represent quantities, typically, discrete values are used to represent small populations, while larger populations are represented with a continuum. We define the switching threshold parameter, Ωi∈Z≥0, as the value where the ith compartment transitions between discrete and continuous dynamics. If a compartment vi∈{0,1,…,Ωi}, we call it discrete (or jumping), and if vi∈(Ωi,∞), we call it continuous (or flowing). To accommodate both scales, let the domain of vi be the set Ei={0,1,…,Ωi}∪(Ωi,∞) (with the σ-algebra generated by the singletons of {0,1,…,Ωi} and the open intervals of (Ωi,∞)).2.1 The state space for our process is E=E1×…×En, with the product σ-algebra.2.1 While the switching threshold can be compartment specific, for ease of exposition, we will only consider a single threshold shared between all compartments, i.e. Ωi=Ω. At each time t, the components of V→ can be partitioned into V→F and V→J, where V→F contains the flowing variables vi>Ω, and V→J contains the jumping variables vi≤Ω. Therefore, at any moment in time dim(V→F) of the n compartments are flowing, and dim(V→J) of the n compartments are jumping, with dim(V→F)+dim(V→J)=n. The dynamics of each compartment vi are described by a set of m reactions R=rkk=1m. Each reaction rk is defined by two properties: the rate (per unit time) at which it occurs, λk, which (usually) is a function of the state V→; and the reaction’s effect on the state, i.e. the change ηik to the size of compartment vi when reaction rk occurs. As a vector, λ→∈Rm is referred to as the propensity vector. As a matrix, η∈Zn,m is referred to as the stoichiometric matrix. For ODE models that only contain flowing variables, these reactions occur continuously and are written in the form: For CTMC models, reactions in the system R occur as discrete events. In the latter case, each reaction rk has a separate propensity described by λk(t,V→). This propensity remains constant between reactions, but when a reaction rk occurs, there is a change in V→ (as specified by the elements of η·k), and therefore in λ→(t,V→). As an example, consider the SIR model of epidemics, which, in ordinary differential equations, has the form dS/dt=-βSI/(S+I+R), dI/dt=βSI/(S+I+R)-γI, and dR/dt=γI. The state of this model is V→=(S,I,R)⊺, and there are two “reactions”: infections (r1) and recoveries (r2). For infections, the rate of reaction may be modelled by λ1=βSI/(S+I+R), and the entries of the associated column of the stoichiometric matrix are η1,1=-1, η2,1=1 and η3,1=0. For recoveries, the rate of reaction may be modelled by λ2=γI, and the entries of the associated column of the stoichiometric matrix are, η1,2=0, η2,2=-1, and η3,2=1. Therefore, written in product form, the system is:3.9 We define RJ⊆R as the subset of reactions treated as stochastic jump events. The set RJ can be defined more precisely in two ways. While we use only the second in this manuscript, both are described for clarity. The first way to define RJ is as: RJ=rk:∃is.t.vi∈V→Jandηik≠0. In this definition, a reaction rk is included in RJ if and only if the reaction has some material effect on a jumping (discrete) compartment vi∈V→J. This is a minimum requirement; a reaction should not be permitted to evoke a continuous change in a discrete compartment. However, where it makes sense to do so, it is possible to allow reactions to make discrete changes to flowing (continuous) compartments. The second way to define RJ, which we use throughout this manuscript, captures a larger set of reactions: In this definition, a reaction is included in RJ if either (1) it causes a material change in jumping (discrete) compartments or (2) it is influenced/caused by a discrete compartment (e.g. as catalyst of some reaction). Reactions in RJ are simulated using stochastically sampled times, similar to CTMC models (and described below in the section on ‘Jump events’). It is important to note that unlike time homogeneous CTMC models, the propensities are not constant because the state V→ (and therefore λ→) are continuously varying. When any reaction rk∈RJ occurs, we say the system has jumped and an instantaneous change of ηik for each compartment vi occurs (irrespective of whether vi∈V→J or vi∈V→F, to ensure the conservation of mass). We therefore refer to reactions in RJ as jumps. The reactions in RF=R\RJ represent the continuous change of value of the relevant compartments, all of which are deterministic (see section on ‘Flow events’.) We therefore refer to reactions in RF as flows. At any moment in time |RF| is the number of reactions which are flowing, and |RJ| is the number of reactions which are jumping, such that |RF|+|RJ|=m. Finally, the hybrid model that we propose is capable of switching. Switching events occur when the size of a compartment vi crosses the threshold Ω and therefore changes membership between the flowing V→F and jumping V→J sets (see sections on ‘Jump clock updates’ and ‘Switching events’). Importantly, switching events may also impact which reactions are jumps, RJ. As such, switches are paradigm-defining events which ought to occur infrequently in comparison to jumps (which may occur frequently) and flows (which occur continuously). Due to the way that R is partitioned, it is possible to order the rows and columns of η into the upper-triangular block form: where ηFF∈Zdim(V→F),|RF|, ηJJ∈Zdim(V→J),|RJ| and ηFJ∈Zdim(V→F),|RJ| refer to stoichiometric coefficients for changes in flowing compartments under flows, jumping compartments under jumps, and flowing compartments under jumps, respectively. Note that, the lower left corner is zeros because, by definition, flowing reactions cannot influence the state of jumping variables. Written as a system of equations analogous to (1), the hybrid JSF framework we propose formally takes the following form. For any time interval ti0 and t+Δt when Jk<0. The time of the jump is found by interpolating for t+Δτ using Equation (17), where Δτ<Δt. Once the jump has occurred and the stoichiometric updates have been applied, Jk is reinitialised by sampling Jk=-log(uk) where uk∼Unif(0,1). The clock is then decremented with successive time steps Δt until the next jump k is realised. D A switching event of type 1 occurs when a discrete compartment jumps above the switching threshold, Ω. This is shown here after the jump time step Δτ2. E A switching event of type 2 occurs when a flowing compartment reaches the switching threshold, Ω. Here, the time at which the switch occurs is found using continuation. F A switching event of type 3 occurs when a flowing compartment is caused to go below the switching threshold, Ω, due to a jump in another compartment. Here the jump has caused the flowing compartment to end at the dotted circle (which we denote as v^i). This compartment is then re-labelled as jumping. Since jumping compartments require integer states, the state is re-initialised to ⌈v^i⌉ with probability vi-⌊v^i⌋ and otherwise to ⌊v^i⌋. This ensures the expected value of the state is maintained at the dotted circle (in the example presented here, the variable was rounded down). For all switching events, membership of VF and VJ is recomputed after switching Jump events The reactions in RJ produce discontinuous jumps in the state vector V→. In Equations (4) and (5), each element in the vector Λ→J corresponds to a reaction in RJ. Consider, for example, rk∈RJ. The kth element of Λ→J is: where δ(·) is the Dirac delta function and ti(k) is the time at which the reaction rk occurs for the ith time. In this way, the term Λ→J in Equations (4) and (5) manifests as discrete jumps in both V→F and V→J at each jump. For the sake of the simulation, the computation of the jump times ti(k) for each instance i of each reaction rk∈RJ is all that is required. The stoichiometric matrix describes the change to each compartment when that jump event occurs. The rate of reaction rk, λk(t,V→), may be state-dependent so can change due to the continuous change caused by flows. Figures 2A and 2B depict an example of flowing and jumping compartment (respectively) experiencing a jump event. In this illustration, we note that if λk depends on v2, the event time ti(k) is not sampled from an exponential distribution which requires a constant propensity/rate between events. Formally, the jump times form an inhomogeneous Poisson process. There are multiple ways to sample the jump times ti(k), see Klein and Roberts (1984) for a detailed discussion. We use a variant of the Next Reaction Method Gibson and Bruck (2000) (which is an optimised variant of the Doob-Gillespie method Gillespie (1976)) to sample jump times. We first note that the propensity for a jump depends only on the instantaneous state V→, and therefore at time t0 if there has been i-1 jumps associated with reaction rk, it has no bearing on the distribution of the time ti(k). Therefore, we shall simply denote ti(k)=tk as the next jump time for reaction rk. The cumulative probability function from which tk is sampled depends on the current time, t0, and the evolution of the state variables in time, V→(t). The cumulative distribution for the time of the next event associated with rk is: To sample tk, inverse transform sampling is used Klein and Roberts (1984), i.e. sample uk∼Unif(0,1) and then solve CDF(tk;k)=uk for tk. To account for the varying state, we define a new function, Jk(t) the jump clock for reaction rk, using Equation (11) and solving CDF(tk;k)=uk (inverse transform sampling). The jump clock acts as a timer, identifying the time tk for when rk next occurs, when Jk(tk)=0. The function Jk is defined by: noting that uk and 1-uk have the same distribution. In general, we cannot solve directly for tk, so we solve for it numerically by tracking the value of Jk(t) as V→ evolves through flows, jumps and switches. For each reaction rk, at some initial time, for example ti-1(k), being the continuous time of the (i-1)th jump associated with reaction rk, we sample uk and initialise t0=ti-1(k). The initial value of Jk(t) is therefore equal to the positive number log(uk-1) (since uk∼Unif(0,1)). As time progresses, Jk(t) decreases according to (12) since λk≥0. The value of Jk(t) decreases to zero over time and when Jk(t)=0, a jump associated with rk is triggered (hence the name “jump clock”). Once a jump clock reaches 0 and a jump is triggered, the clock is reset by sampling a new random number, uk∼Unif(0,1). See Fig. 2C for a schematic illustration of how the jump clock is updated. To update the jump clock, we require numerical integration of λk(t,V→(t)) forward in time. Fortunately, we also have piece-wise polynomial approximations for V→F(t) as a result of our numerical treatment of the continuous flows (see Subsection 2.3 ‘Flow events’), combined with piece-wise constant values for V→J(t) which only change when jumps occur. We discuss the numerical integration of the jump clock below. Jump clock updates For a given jump reaction rk, a jump clock is initialised at time t0 with uk∼Unif(0,1) giving Jk=log(uk-1). From time t to t+Δt, with t>t0, Equation (12) tells us the clock “ticks down” from Jk to Jk-ΔJk, where ΔJk is In general this integral is intractable, but we can approximate it as follows: consider the Taylor expansion of λk(t+s,V→(t+s)) about s=0, which gives where we write α=λk(t,V→), and β=∂λk∂V→dV→⊺dt. Since (i) our numerical evaluation of V→ is piece-wise linear across the time interval, by virtue of the fact that we use a forward Euler approximation to solving Equation (7), and (ii) Δt is small, ΔJk is approximated to the precision of our algorithm by taking noting that since V→ is piece-wise linear across the time interval, the convergence of the integration of the Jump-clock also behaves as Δt. In principle, higher order approximations to the flow events may be utilised, rather than first order forward Euler. However, in these instances, care is required when calculating the gradients for these higher-order schemes, to ensure that no stochastic events occur over the time-step. For this reason, we have limited our current implementation to forward Euler.3.3 Importantly, we know that between jumps dV→Fdt is given by Equation (7) whilst dV→Jdt=0. Thus, β=∂λk∂V→FηFFλ→F(t,V→)⊺ (evaluated at t). To calculate the updated jump clock, we compute Jk(t+Δt)=Jk(t)-ΔJk as the provisional value. We then have two distinct cases: (i) Jk(t)-ΔJk>0, then no jump occurred during the interval (t,t+Δt) and we have the jump clock Jk(t+Δt):=Jk(t)-ΔJk: (ii) Jk(t)-ΔJk<0, then a jump occurred (i.e. a rk reaction) during the interval (t,t+Δt), that we need to account for in the updated jump clock. In case (ii) where a jump occurs within the interval (t,t+Δt), let t+Δτ, where 0<Δτ<Δt, denote the time at which this jump occurs. We can find Δτ by interpolation and solving the following for Δτ: where ΔJk is the residual of the jump clock from t to t+Δτ. Δτ is then given by If there is a jump, rather than using Δt to forward compute the flowing compartments in Equation (8) we instead use Δτ to take part of a flow step and then implement the jump after the flow to get the state at time t+Δτ. After this, we reinitialise the jump clock Jk as described above. This procedure is illustrated in Fig. 2C. Switching events The way in which we handle switching is one of the main contributions of this manuscript. Switching events occur when variables transition between V→J and V→F, i.e. between discrete or continuous values. As a consequence, the contents of RJ and RF may change. There are three types of switching events. The first involves a compartment moving from V→J to V→F. This transition is straightforward as a new equation is added to Equation (7), and the state V→F is initialised at the switching time, with the new flowing compartment at Ω. Figure 2D depicts an example of a type 1 switching event. The second type involves a flowing compartment moving from V→F to V→J. Here, the flowing compartment reaches the switching threshold, Ω, and becomes discrete and no longer follows the flowing dynamics (of Equation (7)). The time at which this occurs is found by continuation, and computation is resumed. Figure 2E depicts an example of a type 2 switching event. The third type of switching event involves a flowing compartment being changed by a jump event which, unless corrected, would result in the compartment taking a non-integer value below the switching threshold Ω, and hence a transition from V→F to V→J. In this case, a flowing compartment jumps below the switching threshold Ω, and becomes discrete.3.4 To highlight how the third type of switching event is necessary, we propose considering a simple model where two species, X and Y, interact to produce a third species, Z, with X also undergoing a death-dominated birth-death process. Suppose that the switching threshold is set as Ω=1000, and that X(0)=1001, Y(0)=10 and Z(0)=0. Due to the death reaction of X, suppose that within the first (Flow) step, X experiences some decay ε<1. Therefore, X(t1)=1001-ε>Ω=1000 is in a flowing (continuous) regime. Now let’s suppose that in the next instance, X and Y combine to produce one Z. At this instance, X must experience some integer loss (which we can assume to be 1, but in general may be any positive value). In this case, producing Z will result in X(t2)<Ω but non-integer. Therefore, to account for type 3 switch events, let vi be the compartment switching from V→F to V→J due to a jump. In general, these types of jumps result in vi being non-integer, i.e. vi∉{0,1,…,Ω}∪(Ω,∞). To ensure the values of vi stay in {0,1,…,Ω}∪(Ω,∞), we add another constraint to the process, initially proposed by Rebuli et al. (2017). The idea is to randomly round the compartment value up or down to an integer in a way that conserves the average behaviour of the process. Let v^i be the value of the flowing compartment vi after jumping down across the threshold Ω but before being rounded into {0,1,…,Ω}∪(Ω,∞). We apply the following rule to round vi after the switch. We take vi=⌈v^i⌉ with probability v^i-⌊v^i⌋, and otherwise we round down by setting vi=⌊v^i⌋. This ensures the expected value of the variable after the switch is v^i as described under the flowing paradigm from which this compartment has come and that the variable remains in the domain {0,1,…,Ω}∪(Ω,∞). Figure 2F depicts an example of a type 3 switching event. Therefore, type 3 switching events will only maintain mass over an ensemble of instances, while type 2 switching events will always maintain mass.3.4 Riedler proved a convergence theorem for a general class of algorithms that includes the numerical scheme described above Riedler (2013). This convergence theorem establishes that if ηFFλ→F and ∑kλk are time-homogeneous, bounded, Lipschitz continuous and continuously differentiable, and that ∑kλk is bounded away from zero, then the numerical approximations converges almost surely to the exact samples in the sense that, for the jumps and switches, both the timing of the events and the values of the state converge with order one as Δt→0. The requirement that the event rate is bounded away from zero is to ensure there can be times at which to consider the convergence, otherwise a purely ODE-based approach to convergence would suffice. The requirement that the system is time-homogeneous can be removed by augmenting the system to include a variable tracking time.2.3 Results Effects of interventions to reduce transmission Simulation-based inference (e.g. the particle filter) relies on being able to efficiently simulate from the generative process. To demonstrate how JSF can be used in this setting, we first present a simulation study of forecasting the elimination of a infectious pathogen. SIRS model with demography The SIRS model with demography is an extension of the classic susceptible-infectious-removed (SIR) model. In the SIR model susceptible individuals may be infected by contact with infectious individuals; infected individuals eventually cease to be infectious and transition to the removed compartment. The SIRS model with demography extends the SIR model by allowing removed individuals to transition back to being susceptible to infection, by allowing individuals to give birth to new (susceptible) individuals, and by allowing individuals to die (at equal rates). The SIRS model with demography can be written as the following ODE system, with susceptible (S), infectious (I), and recovered (R) individuals: where N(t)=S(t)+I(t)+R(t) is the total population size, β the infection rate, γ the recovery rate, ω the immunity waning rate, κ the birth rate, and μ the death rate. Figure 3A shows a compartmental diagram of the SIRS model with demography. JSF enables us to forecast when a pathogen will be eliminated from a population, i.e. when we can claim that an epidemic has “ended”, and quantify the likely impact of varying levels of intervention. A A compartmental model representation of the SIRS with demography. B A simulated time series of noisy measurements of prevalence along with inferred prevalence trajectories and forecasts of future prevalence with associated uncertainty bands. The dashed vertical line indicates the date up until which we have data. The solid blue line indicates an estimate of the probability that the pathogen has been eliminated from the population (as opposed to persisting at very low levels). C The true parameters used in the simulation along with their posterior estimates and the associated priors used. Each of the marginal posterior distributions peaks tightly about the true value. D The probability of pathogen elimination increases with the strength of the intervention against transmission. Near certainty of elimination is achieved with a 30% reduction of transmission Figure 3B shows our simulated (via the Doob-Gillespie algorithm) time series of daily noisy measurements of the prevalence of infection. The true parameters used in the simulation are shown in Fig. 3C; these values are broadly consistent with existing estimates for influenza or SARS-CoV-2. We assume that these measurements are drawn from a negative binomial distribution where the expected value is equal to the true prevalence of infection and there is a constant (known) dispersion parameter, k=100. Estimating elimination probabilities Combining the SIRS model with a particle filter (Moss 2024; Kitagawa 1996) we used the first 100 days of the time series to forecast the remaining 150 days. Figure 3B shows the estimated true prevalence across the first 100 days and forecasts the prevalence for the subsequent 150 days. Included in the forecast is a daily estimate of the probability that the pathogen has been eliminated (in the simulation the pathogen was eliminated on day 250). It is important to note that there is an endemic equilibrium in this model, so it is reasonable for the probability to plateau as it does. To demonstrate the utility of the posterior distribution (i.e. the capacity of the particle filter to forecast the epidemic after the 100 days of observed data), we considered the impact of a possible intervention. This intervention, introduced on day 100, reduces the force of infection (and hence the effective reproduction number) by a factor of α. Figure 3D shows how the probability of eliminating the pathogen increases substantially as we decrease α from 1 (no intervention) to 0.7 (a 30% reduction in transmission). The fade-out probability is calculated as the proportion of sampled (hybrid stochastic-deterministic) trajectories that go extinct (via I=0) of the total number of sampled trajectories. The full details of the configuration of the particle filter, and the marginal posterior distributions are given in SI Section 2E. Computational properties To further investigate the computational properties of JSF and to compare it with exact (Doob-Gillespie) simulation and (approximate) tau-leaping simulation we used repeated simulations from the SIRS model with demography. We partitioned the simulations into three classes: early stochastic extinction, fade-out after a single epidemic, and sustained transmission (SI Section 2). As seen in Table SI 3, JSF and exact simulation generate very similar proportions of samples in each of these classes. Two aspects of this system of practical importance are the timing and magnitude of peak infections, and the total number of infections. As can be seen in Fig. SI 8 the distribution of peak timing and total number of infections are in good agreement between JSF and exact simulation. For the magnitude of peak infections, there is substantially less variability in the JSF samples than the exact samples, but the proportional difference is small. With regards to computational efficiency, Fig. SI 11B shows the average time required per simulation using JSF, exact simulation (Doob-Gillespie), tau-leaping, and the efficient Tau-hybrid method Matthew et al. (2023) for the SIRS model for populations of different sizes. For populations of size above about 105 (i.e. approximately the size of a small city), JSF is substantially computationally faster than either exact simulation or tau-leaping. Specifically, we observe an order of magnitude reduction in the computational time required to simulate the equivalent physical time horizon, when comparing JSF to the tau-leaping implementation. Moreover, when an appropriate fixed switching threshold is specified, the computational cost remains approximately constant as population size increases, irrespective of compartment occupancy.2.4 To simplify the interpretation of the results in Fig. SI 11B, each of the methods were implemented in the same interpreted language so that the observed differences can be attributed to the algorithm rather than the platform on which it is implemented. We have also included a comprehensive simulation case study of the simple birth-death process (see Section SI 2 of the supplementary information), which exhibits JSF2.5 computational efficiency for both birth-dominated and death-dominated regimes, with comparable results to the exact stochastic process. We also demonstrate JSF’s accuracy for particularly difficult stochastic simulations which exhibit multiple time scales (see Section SI 3 of the supplementary information), where we see that JSF produces summary statistics and trajectories comparable to the exact stochastic process, out performing current hybrid methods. SARS-CoV-2 virus clearance informed by longitudinal data Understanding viral clearance is important when deciding how long treatment must be maintained. This case study demonstrates how JSF enables inference of virus clearance, which has been computationally challenging to date Yan et al. (2016), Farrukee et al. (2018). In particular, we use a mathematical model to study viral clearance using longitudinal data from 6 individuals infected with SARS-CoV-2 Ke et al. (2022). The remainder of this section contains: (i) a description of the TEIRV mathematical model used, (ii) the observation model used to link the cycle number data to viral load, and (iii) key results obtained using JSF to fit the TEIRV mathematical model to the obtained viral load data, using a particle filter.3.12 TEIRV model of within-host viral dynamics The TEIRV model is an extension of the classic target-infected-virus (TIV) model Perelson (2002). In the TIV model, target cells may be infected by the virus; before dying, infected cells produce virus; and the virus can degrade or infect the remaining target cells. The TEIRV extends the TIV model through the inclusion of an eclipsed compartment, to model the delay between infection of a target cell and the subsequent production of virus, and a refractory compartment, to model heightened antiviral defences of target cells, e.g. through the effects of interferons Ke et al. (2022), Blanco-Melo et al. (2020). To simplify the use of this model, a quasi-steady-state approximation for interferon production is employed. This approximation allows us to avoid the need to explicitly model the amount of interferon present. Figure 4A shows a compartmental diagram of the TEIRV model. Target cells becoming infected by virions at rate β, which then enter the eclipsed phase. These eclipsed cells then become infectious at rate k. Infectious cells are cleared from the population at rate δ, and produce virions at rate π. Virions are themselves cleared at rate c. We note that V is the total number of virion particles, and therefore this model does not distinguish between intra- and extra-cellular virions. As a result, following the infection of a target cell (T), no virion particles are lost in this model. The infectious cells recruit interferon, which causes2.6 the target cells to become refractory (and hence protected against infection) at rate Φ. The refractory cells return to a naive state as target cells at rate ρ. These assumptions are represented with the following ODE system: The stoichiometric matrix corresponding to these assumptions is shown in Fig. 4B. Figure 4C shows a trajectory sampled from the TEIRV model when represented with JSF; the classic boom-bust dynamics of the viral populations can be seen in the exponential growth and decline of the V compartment. Unlike solutions of the ODE model in (19), we see that by time t=14 the populations of eclipsed cells, infected cells, and virus have gone extinct, indicating a definitive end to the infection. The TEIRV model describes the within-host dynamics of host cells and virus during infection. The model accounts for delays in virus production post cellular infection and the capability of target cells to enter a refractory state as a defence against infection. A A compartmental diagram of the TEIRV model showing the interactions between: Target cells (T), Refractory cells (R), cells in the Eclipsed phase of infection (E) and Infectious cells (I); and the (Total) Virions (V). Total Virion particles are utilised as to not distinguish between intra- and extra-cellular Virions. Solid arrows represent the exchange of mass (cells or virions) through compartments, while dashed lines represent the influence of a compartment on a reaction’s rate of occurrence. B The representation of this model as a reaction network with a stoichiometric matrix. This formalises the dependency on different variables indicated by the dashed arrows in the compartmental diagram. C An example trajectory showing the exponential growth and decline in the amount of free virus across the duration of the infection. This process terminates when the compartments representing the virus, and eclipsed2.6 and infected cells all reach zero (shortly before time 12). Compartments can hit zero in this hybrid continuous/discrete stochastic model but this does not occur in the solutions of the pure ODE representation The basic reproduction number, R0, is a fundamental quantity of epidemiological models. For the TEIRV model, R0 can be calculated from the next-generation matrix Diekmann et al. (2010): Observation model linking virus to time series The longitudinal data contains cycle number (CN) values from nasal samples. The CN values are inversely proportional to the number of virions present. In our analysis we used an existing (empirical) model to link the cycle numbers to the (logarithmic) viral genome load Ke et al. (2022): log10V=11.35-0.25CN. We assume the observed values are drawn from a normal distribution with mean log10V and unit standard deviation, and that the values are truncated at the detection limit of -0.65. Estimating virus reproduction and clearance Recall that the particle filter is capable of combining a mechanistic model of the within-host dynamics, such as the TEIRV, and an observation model, such as the viral load measurements, and will return a (Bayesian) posterior sample of both the parameters of the process and the trajectory of virus and cell populations through time. We assume that the infection begins with a single exposed target cell (in a population of 8×107 target cells) Ke et al. (2022). This gives us an initial condition for the process: T(0)=8×107, E(0)=1, I(0)=0, R(0)=0. We leave the initial viral load, V(0), as a parameter to be fit to the data. We selected six patient time series Ke et al. (2022), choosing ones that contained a full 14 data points, for both consistency and simplicity. Two of the model parameters were fixed as in previous analysis Ke et al. (2022): c=10, k=4. See SI Section 3 for full details of the particle filter and JSF configuration. Figure 5A shows our model fits to the first 10 days of viral load data for the six selected patients. After day 10, using the estimated parameters, we generate and predict the distribution of subsequent viral load until day 20. The estimated viral peak coincides with the data for each of the patients and the predicted viral trajectories closely match subsequent observations. JSF enables the inference of the viral load through time and the probability that the virus has been cleared from longitudinal data of SARS-CoV-2 viral load measurements. A Model fits to viral load data based on nasal swabs of six patients from Ke et al. (2022) using a refractory cell within-host model, using our Jump-Switch-Flow method. Nasal viral load data is shown as solid dots, and the limit of detection is shown as a dotted line. We show the switching threshold, Ω=102 by a dashed line. The red and purple bands show mean (0%), 25%, 50%, 75% and 95% credible intervals (from dark to light) for the inference and prediction, respectively. We also estimate the probability of viral clearance at a given point in time, based on the previous data to that point, in blue (light). B The posterior distribution of viral R0 for each patient, along with the median (white circle). These patient specific estimates are contrasted with the single point estimate from previous analysis Ke et al. (2022) We estimate the probability of viral clearance over time (right blue axis of in the panels of Fig. 5A). Viral clearance is defined by the point at which the virion, eclipsed, and infected cell populations all reach extinction. We note that this is a conservative measure if one were only interested in when a patient’s infectiousness becomes negligible. However, for proof of concept of inferring such quantities, we have chosen to estimate the probability of complete viral clearance. There is clear heterogeneity in the amount of time required to clear the virus (Fig. 5A). We estimate that with high certainty patients 423192, 443108 and 445602 clear the virus within 20 days. In contrast, for patients 444332, 444391 and 451152 we infer that they have not cleared the virus within that time frame. Within those who did clear the virus, we observe that different patients require different amounts of time to clear the virus. Specifically, patient 432192 (5A) is inferred to have cleared the virus first. Patients 443108 and 445602 both require an estimated 16 days to obtain viral clearance. The posterior distributions of the parameters (Fig. SI 12) and priors (Table SI 5), are given in SI Section 5. Because of potential identifiability issues with the rate parameters, we instead compare the estimated reproduction number, R0, to results from previous analysis (Ke et al. 2022). Figure 5B shows the posterior distributions of R0, as well as the point estimates from Ke et al. (2022). Since the previous analysis, (Ke et al. 2022), reports identical estimates for π, β and δ across these patients, we only include a single point estimate for their work. Our estimate is consistent with previous results (Ke et al. 2022) for patients 432192, 445602 and 451152. However, patients 443108, 444332 and 444391 all have higher R0 estimates in our results. Our R0 estimates are consistent with similar within-host viral infection analyses of other respiratory pathogens (Baccam et al. 2006; Hernandez-Vargas and Velasco-Hernandez 2020; Gubbins 2024). Discussion We have presented a simple hybrid simulation method for compartmental models: Jump-Switch-Flow (JSF). This method facilitates efficient simulation of multi-scale models. Through simulation studies and an analysis of longitudinal data from SARS-CoV-2 infections, we demonstrated the desirable computational properties of this method and the types of novel analyses it enables. JSF allows compartments to dynamically change between stochastic and deterministic behaviour. Combining JSF with a simulation-based inference method, e.g. a particle filter, enables inference for multi-scale models, and in situations where absorbing states (such as a population going extinct) are important. Simulations show JSF produces trajectories largely consistent with gold standard exact simulation techniques, e.g. Doob-Gillespie algorithm (see for example Figs. SI 2–SI 5 and SI 7 – SI 10). We demonstrate how JSF is highly accurate at producing summary statistics comparable to gold standard exact simulation techniques (see Figs. SI 2 – SI 5), with a significant improvement in accuracy for problems with multiple time scales when compared to currently available hybrid methods (see Fig. SI 4 and SI 5). However, JSF is also much faster for realistic population sizes (as can be seen in the comparison of computational speed in Fig. SI 11B). We demonstrate how our approach can be incorporated into a particle filter (Moss 2024) to perform parameter and state estimation, and prediction (see Fig. 3). A strength of JSF is the capacity to infer when a process has reached an absorbing state, for example, an epidemic fading-out, or a virus being cleared. The ability to infer when an absorbing state has been reached is important for calibrating intervention measures (Parag et al. 2020) and understanding immune response (Yan et al. 2016). ODE-based models rarely have absorbing states that can be reached in finite time, which fundamentally limits their capacity to describe extinction and clearance processes. We analysed viral load for SARS-CoV-2 infections using the JSF within a particle filter with a TEIRV, refractory cell model (Ke et al. 2022). In the subset of the data considered, we find consistent parameter values between patients (see Section SI 5). In a novel analysis, we estimated the probability of viral clearance through time, finding substantial heterogeneity in the time until viral clearance (see Fig. 5A). This is important as an accurate quantification of when the virus has been cleared is crucial for determining appropriate treatment regimes. A key advantage of JSF is the ability to combine the stochasticity of discrete small populations with convenient deterministic models for large populations. The point at which a compartment will transition between these descriptions is specified by the threshold parameters Ωi (one for each compartment). Setting these parameters to low values speeds up computation, while setting them higher captures more of the stochasticity in the process. As demonstrated in the simulation studies, an appropriate value can be determined through some preliminary simulations (see SI 4 for an example of preliminary simulation analysis for the SIRS model example). An important consideration in selecting this parameter is ensuring that the absorbing states can still be reached (e.g. compartment extinction), and that stable and steady states can be perturbed by random fluctuations. Potential considerations when specifying the values Ωi may also include preliminary simulations, increasing Ωi and observing how the computational complexity scales (see Fig. SI 11). Furthermore, developing theory to better understand when a CTMC is well approximated by an ODE is at the core to determine how the switching threshold should be chosen. We have laid the groundwork for better understanding these processes via numerical simulation-based studies of both the death- and birth-dominant birth-death processes (see Sections SI 2 and SI 3), which demonstrate how the switching threshold Ω can be fine-tuned to accurately capture the metrics of interest. We also provide an example of how such a numerical simulation-based study may be conducted for an SIRS with demography model (see Section SI 4). In this example, we demonstrate how JSF can be calibrated to exactly (but inefficiently) reproduce the desired metrics of the exact SSA (see Section SI 4A). In Fig. SI 10 we demonstrate how increasing the switching threshold Ω results in more accurate simulation, and in Fig. SI 11 we demonstrate how decreasing Ω reduces CPU runtime.3.1 The JSF process is a special case of piecewise deterministic Markov processes (PDMP) (Davis 1984). In this work, we have attempted to provide a practical way to instantiate this process for the simulation of compartmental models, in particular by modelling the jumping and switching behaviour appropriately and expressing this framework in a way that is more accessible to computational scientists. While recent work in Riedler (2013), Azaïs et al. (2014), Saporta et al. (2016) has considered methods for simulating PDMPs, it primarily focuses on the general PDMP setting rather than on compartmental modelling.2.1 Further understanding of the numerical properties of the Jump-Switch-Flow process and the convergence behaviour of the method for arbitrary choices of flow event solvers will also prove to be an exciting area of further research. However, extending the existing analysis of Riedler (2013) lies outside the scope of this work.2.1 Extensions include the incorporation of an intermittent Stochastic Differential Equation approximation between the CTMC and ODE regimes, and extension to spatial processes which exhibit both stochastic and deterministic behaviours. The modelling framework presented in this paper has the potential to change the way compartmental models are developed and calibrated, moving us towards more accurate and more efficient hybrid methods. These models will help to form the basis of informed decision making, based on realistic and accurate descriptions of the system. This has broad applicability, from ecological models, chemical systems and single cell models, to infectious diseases and within-host models. Supplementary Information Below is the link to the electronic supplementary material. Supplementary file 1 (pdf 13513 KB) Acknowledgements We thank Ada Yan for helpful discussions regarding within-host modelling. J.A.F.’s research is supported by the Australian Research Council (FT210100034, CE230100001) and the National Health and Medical Research Council (APP2019093). S.H.’s research is supported by the Australian Research Council (DP200101281). Author Contributions J.A.F and M.B.F conceptualised research; D.P.J.G, A.E.Z, S.H., R.M, J.A.F and M.B.F designed research; D.P.J.G and A.E.Z performed research; D.P.J.G, A.E.Z and R.M contributed software tools; D.P.J.G and A.E.Z analyzed data; J.A.F and M.B.F supervised research; D.P.J.G and A.E.Z wrote the paper; D.P.J.G, A.E.Z, S.H., R.M, J.A.F and M.B.F reviewed the paper; J.A.F acquired funding. Funding Open Access funding enabled and organized by CAUL and its Member Institutions Data Availability The manuscript has associated data in a data repository available at https://github.com/DGermano8/JSFGermano2024. Original data related to SARS-CoV-2 viral load measurements can also be found at Ke et al. (2022). Code Availability All code to reproduce our results is available at https://github.com/DGermano8/JSFGermano2024. A Python package implementing the Jump-Switch-Flow method is available at https://DGermano8.github.io/JSF. Footnotes Publisher's Note Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. D. P. J. Germano and A. E. Zarebski: These authors contributed equally to this work. References Associated Data Supplementary Materials Supplementary file 1 (pdf 13513 KB) Data Availability Statement The manuscript has associated data in a data repository available at https://github.com/DGermano8/JSFGermano2024. Original data related to SARS-CoV-2 viral load measurements can also be found at Ke et al. (2022). All code to reproduce our results is available at https://github.com/DGermano8/JSFGermano2024. A Python package implementing the Jump-Switch-Flow method is available at https://DGermano8.github.io/JSF.