# COVID-19 - Model and ODE delay equations

**URL:** <https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632>\
**Category:** Modelling & Simulations\
**Created:** [March 28, 2020, 8:39am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632 "2020-03-28T08:39:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emmanuel-R8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmanuel-r8/32/11839_2.png) [@Emmanuel-R8](https://discourse.julialang.org/u/Emmanuel-R8)\
**Post date:** [March 28, 2020, 8:39am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/1 "2020-03-28T08:39:26Z")

</div>

I have a working model and parameter optimiser for the COVID-19 epidemy (Emmanuel-R8/COVID-19-Julia]([[GitHub - Emmanuel-R8/COVID-19-Julia: A re-implementation of the Neherlab.org model in Julia](https://github.com/Emmanuel-R8/COVID-19-Julia)). The model is inspired (but slighlty more detailed) by this impressive project [Neherlab COVID-19](https://neherlab.org/covid19/). My version is explained there [Forecasting COVID-19](https://emmanuel-r8.github.io/2020/03/25/2020-03-25-forecasting-covid-19.html).

However, those model rely on delay equations which are often ignored for simplicity but clearly distort the actual value of parameters. I have not been able to find good examples of delay equations.

Any pointers?

---

<div class="post-metadata">

**Author:** ![adhalanay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adhalanay/32/7371_2.png) [@adhalanay](https://discourse.julialang.org/u/adhalanay)\
**Post date:** [March 28, 2020, 9:33am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/2 "2020-03-28T09:33:38Z")

</div>

Hi,  
If you are looking for general references, there are a number of books:

[\* J.Hale, S.M. Verduyn Lunel, _Introduction to Functional Differential Equations_](https://doi.org/10.1007/978-1-4612-4342-7)  
[\* Diekman, O., van Gils, S. A., Verduyn Lunel, S. M., & Walther, H. _Delay-Equations: Functional, Complex and Nonlinear Analysis_](https://doi.org/10.1007/978-1-4612-4206-2)  
[\* Kolmanovskii, V.,Myshkis, A. _Introduction to the theory and applications of functional-differential equations._](https://doi.org/10.1007/978-94-017-1965-0)  
Odo Diekman, one of the authors of the second book is a leading expert in biological models and coauthored a book about this: _Mathematical tools for understanding infectious disease dynamics_.

The only book on numerical methods is _Numerical Methods for Delay Differential Equations_  
by Alfredo Bellen and Marino Zennaro.  
Hope that helps

---

<div class="post-metadata">

**Author:** ![Emmanuel-R8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmanuel-r8/32/11839_2.png) [@Emmanuel-R8](https://discourse.julialang.org/u/Emmanuel-R8)\
**Post date:** [March 28, 2020, 9:36am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/3 "2020-03-28T09:36:24Z")

</div>

Thanks for the references., but I should have been clearer. I am looking for guidance to implement them in Julia.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [March 28, 2020, 9:49am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/4 "2020-03-28T09:49:36Z")

</div>

Presumably you’ve seen [https://docs.sciml.ai/dev/tutorials/dde\_example/#Delay-Differential-Equations-1](https://docs.sciml.ai/dev/tutorials/dde_example/#Delay-Differential-Equations-1)?

---

<div class="post-metadata">

**Author:** ![adhalanay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adhalanay/32/7371_2.png) [@adhalanay](https://discourse.julialang.org/u/adhalanay)\
**Post date:** [March 28, 2020, 9:49am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/5 "2020-03-28T09:49:50Z")

</div>

Oh, you can start with [this](https://docs.sciml.ai/stable/tutorials/dde_example/) tutorial from the official documentation.

---

<div class="post-metadata">

**Author:** ![Emmanuel-R8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmanuel-r8/32/11839_2.png) [@Emmanuel-R8](https://discourse.julialang.org/u/Emmanuel-R8)\
**Post date:** [March 28, 2020, 10:29am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/6 "2020-03-28T10:29:28Z")

</div>

I saw those links. But I have several time lags that I would like to optimise for. I couldn’t readily see how this fits into the examples provided.

Seems like the conclusion is that resources are meager (given that I was looking for the lazy option of copy/paste someone else’s hard work…)

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 28, 2020, 2:20pm UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/7 "2020-03-28T14:20:53Z")

</div>

> [@Emmanuel-R8](#):
>
> But I have several time lags that I would like to optimise for.

[https://docs.sciml.ai/latest/analysis/parameter\_estimation/](https://docs.sciml.ai/latest/analysis/parameter_estimation/)  
[https://github.com/SciML/DiffEqFlux.jl/blob/master/test/layers\_dde.jl](https://github.com/SciML/DiffEqFlux.jl/blob/master/test/layers_dde.jl)

are both doing optimization of DDE parameters. You can basically copy paste from the second link.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 28, 2020, 3:49pm UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/8 "2020-03-28T15:49:47Z")

</div>

You may know about this already, but here is a common trick for converting delay differential equations to ODEs for the purposes of numerical approximation/simulation.

Suppose that some agents convert from state A to B after a delay of T. Introduce interim states I\_1, I\_2, \dots, I\_n, with Poisson transitions rates \lambda\_0 from A to I\_1, \lambda\_k from I\_k to I\_{k+1}, and finally \lambda\_n from I\_n to B.

Calibrate your parameters so that \sum 1/\lambda\_i = T, so the expected time it takes to get from A to B matches. The higher n is, the smaller the variance of the expected time will be (this is easy to check and is a good exercise).

In practice, for a lot of processes, you can get away with n = 2, \dots, 5 and get a good approximation. For a lot of things transitions are not deterministic anyway, so this may be a better model to start with. Eg for COVID-19, you can assume transmission rates increasing in k for the I\_k.

---

<div class="post-metadata">

**Author:** ![Emmanuel-R8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmanuel-r8/32/11839_2.png) [@Emmanuel-R8](https://discourse.julialang.org/u/Emmanuel-R8)\
**Post date:** [April 3, 2020, 4:31am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/9 "2020-04-03T04:31:23Z")

</div>

I missed your answer. Apologies.

I didn’t know that trick. Seems like an easy way to optimise for the value of the delay.

I have not started on implementing the delay as I have been debugging the parameter optimisation.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 3, 2020, 7:32am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/10 "2020-04-03T07:32:04Z")

</div>

How does this compare to classic Padé-approximation of the Laplace-transform of time delay? Essentially, y(t) = x(t-\tau) \Rightarrow^{\cal L} Y(s) = \exp(-s\tau) X(s), with Padé approximation \exp(-\tau s) = \exp(-\frac{\tau s}{2})/\exp(\frac{\tau s}{2}) where the numerator and denominator are approximation in power series,

\exp(-\frac{\tau s}{2}) \approx 1-\frac{\tau s}{2} + \frac{1}{2}\left(\frac{\tau s}{2}\right)^2 -\cdots \\ \exp(\frac{\tau s}{2}) \approx 1+\frac{\tau s}{2} + \frac{1}{2}\left(\frac{\tau s}{2}\right)^2 +\cdots 

which is then transformed back to ODEs?  
Thus, in the simplest case,

y(t) = x(t-\tau) \\ \Downarrow \\ \left(1+\frac{\tau s}{2}\right)Y(s) \approx \left(1-\frac{\tau s}{2}\right)X(s) \\ \Downarrow \\ y(t) + \frac{\tau}{2}\frac{dy}{dt} \approx x(t) - \frac{\tau}{2}\frac{dx}{dt} \\ \Downarrow \\ \frac{dy}{dt} \approx - \frac{2}{\tau}(y-x) - \frac{dx}{dt}

The “problem” here is \frac{dx}{dt}. If x is a signal, you need to differentiate it. If x is the state of another ODE, you just eliminate it by replacing it with the right hand side of that ODE.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [April 3, 2020, 7:50am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/11 "2020-04-03T07:50:40Z")

</div>

Interesting to compare those two. A compartment fed with poisson rates acts a bit like an damped integrator, do you typically get these additional derivative terms `dx/dt` (making x an integrator of that new coordinate as well)?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [April 3, 2020, 7:58am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/12 "2020-04-03T07:58:34Z")

</div>

the problem of differentiating wrt to the delay D in DDE is not well posed mathematically as it affects the state space. By scaling time, you can assume D=1 and multiply your vector field by D. It becomes then possible to differentiate.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 3, 2020, 8:02am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/13 "2020-04-03T08:02:04Z")

</div>

Note: the right hand side derivatives can, of course, be avoided by writing:

\exp(-\tau s) = \frac{1}{\exp(\tau s)} \approx \frac{1}{1+\frac{\tau s}{2}+\frac{1}{2}\left(\frac{\tau s}{2}\right)^2 +\cdots}

but as I recall, that leads to a poorer approximation.

---

<div class="post-metadata">

**Author:** ![sgjanssens](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@sgjanssens](https://discourse.julialang.org/u/sgjanssens)\
**Post date:** [April 3, 2020, 8:03am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/14 "2020-04-03T08:03:39Z")

</div>

Yes, indeed, but if there are multiple delays present, then differentiation of the right-hand side with respect to the remaining (after scaling) delay parameters is still - in general - not possible.

(This actually had me wondering a bit while following this topic. It may be that (perhaps implicitly) the problem is circumvented by using that the history segments gain one degree of smoothness after each time step equal to the maximum delay.)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 3, 2020, 8:18am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/15 "2020-04-03T08:18:22Z")

</div>

> [@BLI](#):
>
> How does this compare to classic Padé-approximation of the Laplace-transform of time delay?

I think that a finer grid of interim transitions (again, 2–5 as I suggested above) may lead to a better approximation than the one you suggested while remaining very tractable (but this is just intuition, I did not do the math).

Also, I find it intuitive to think about people being in “stages” of infection, between which they transition stochastically. I think that a fixed lag \tau for the population is a convenience assumption; I don’t think the process is deterministic so I would not work very hard to maintain this assumption.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [April 3, 2020, 8:25am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/16 "2020-04-03T08:25:10Z")

</div>

> [@sgjanssens](#):
>
> Yes, indeed, but if there are multiple delays present, then differentiation of the right-hand side with respect to the remaining (after scaling) delay parameters is still - in general - not possible.

My feeling is that you can do it. The state space is something like C([-D, 0], R) where D is the maximum delay. If the rescaled delays stay below 1, it should be fine for this part.

Then the question of mild/strong solution comes in. Regularity of C0-semigroups wrt to parameters, I have never thought much about this.

---

<div class="post-metadata">

**Author:** ![sgjanssens](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@sgjanssens](https://discourse.julialang.org/u/sgjanssens)\
**Post date:** [April 3, 2020, 8:35am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/17 "2020-04-03T08:35:04Z")

</div>

> [@rveltz](#):
>
> Regularity of C0-semigroups wrt to parameters, I have never thought much about this.

I had not thought about it much either, until relatively recently. There is a comment about it in [this article](https://epubs.siam.org/doi/abs/10.1137/19M1243993) in Remark 21, but the issue was well-known before that, see the references in that remark and also the work by Krisztin and Walther on state-dependent DDEs. There the problem becomes even more serious.

It is not my intention to take this too far off-topic, but if you would like to, we can continue the discussion elsewhere.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 3, 2020, 8:43am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/18 "2020-04-03T08:43:47Z")

</div>

Note: I don’t intend to suggest using first order approximation for the exponential functions; that’s just an example. Anyway, would your proposal be similar to setting (\tau\ge 0, \tau\_i \ge 0):

\exp(\tau s) = \prod\_{i=1}^n\exp(\tau\_i s)

where \sum\_{i=1}^n\tau\_i = \tau (e.g., n=4 or n=5), using only first order approximations of \exp(\tau\_i s), and then tune the \tau\_i to get the best approximation?

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [April 3, 2020, 11:30am UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/19 "2020-04-03T11:30:32Z")

</div>

[No, I have to calculate that more carefully].

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 3, 2020, 1:53pm UTC](https://discourse.julialang.org/t/covid-19-model-and-ode-delay-equations/36632/20 "2020-04-03T13:53:38Z")

</div>

> **[It’s about time: Analysing an alternative approach for reductionist modelling...](https://www.biorxiv.org/content/10.1101/781708v1)**
>
> Thoughtful use of simplifying assumptions is crucial to make systems biology models tractable while still representative of the underlying biology. A useful simplification can elucidate the core dynamics of a system. A poorly chosen assumption can,...

could be interesting to you as well
