Skip to content

ComposableRecurrences ​

Composable and differentiable recurrences and convolutions in Julia, for renewal and delay models.

Why ComposableRecurrences? ​

  • Many models can be expressed as discrete-time recurrences, where each value depends on earlier values, and convolutions, where a delay spreads one series into another.

  • For example, a renewal process for infectious disease transmission is a recurrence, and the delay from infection to report is a convolution.

  • Instead of writing these by hand, we implement them as a set of composable operators.

  • These allow easy extension to age groups, regions, a finite population or a delay that changes over time.

  • We focus heavily on efficiency, both for forward computation and on all modern automatic differentiation backends, with the aim of being on par with or beating hand-written gradients.

  • We also aim to enable efficient computation on both CPU and GPU in a range of contexts.

Getting started ​

The example is a renewal model of an outbreak in three towns. Infections in each town arise from earlier infections in that town and, through travel, in the other two. Each town has a finite population. A share of infections is reported after a delay. The reproduction number falls in every town between days 50 and 56. The model is

where is the number of infections in town on day , the number of reports and the reproduction number. The value is the number of new infections before depletion, and the susceptible pool starts at the population . The generation interval has weight on lag over lags, the reporting delay has weight over lags, and a share   of infections is reported. The gravity coupling comes from the populations and the distances between towns, with  . The term is 1 when   and 0 otherwise. Town A has 10 infections on each of the six days before day 1, and the other towns none. The renewal and the delay both read these as their history, so the reports from day 1 include them. In the code, PerStratum(pop) gives each town its own population, and the run covers   days.

julia
using ComposableRecurrences
using ComposableRecurrences: Depletion

gi = [0.05, 0.2, 0.3, 0.25, 0.12, 0.08]
T = 75
towns = ["A", "B", "C"]
pop = [60_000.0, 25_000.0, 10_000.0]
dist = [0.0 20.0 45.0; 20.0 0.0 30.0; 45.0 30.0 0.0]
gravity = [a == b ? 0.0 : pop[b] / dist[a, b]^2 for a in 1:3, b in 1:3]
K = 0.998 * [a == b for a in 1:3, b in 1:3] + 0.002 * gravity ./ sum(gravity; dims = 2)
seed = [fill(10.0, 1, 6); zeros(2, 6)]
R = [1.8 - clamp((t - 50) / 6, 0, 1) for _ in towns, t in 1:T]

renewal = Recurrence(gi; coupling = K, modifiers = (Depletion(PerStratum(pop)),))
delay = Convolution([0.0, 0.1, 0.25, 0.3, 0.2, 0.1, 0.05])
infections = renewal(R; history = seed)
reports = delay(infections; gain = 0.4, history = seed)

Infections, reports and the remaining susceptible share in each town, with the intervention from day 50 to 56 shaded

Three towns, step by step builds this model one part at a time.

Where to learn more ​

Part of the EpiAware ecosystem ​

ComposableRecurrences is part of EpiAware, a set of composable tools for infectious disease modelling. See the other packages in the ecosystem.

Contributing ​

We welcome contributions and new contributors! Please open an issue or pull request on GitHub. This package follows ColPrac and is formatted with Runic.

How to cite ​

If you use ComposableRecurrences in your work, please cite it. Citation metadata lives in CITATION.cff, which GitHub renders as a "Cite this repository" button on the repository page.

Code of conduct ​

Please note that the ComposableRecurrences project is released with a Contributor Code of Conduct. By contributing, you agree to abide by its terms.