ComposableTuringIDModels.jl
A toolkit for composable probabilistic infectious disease modelling in Julia.
Why ComposableTuringIDModels?
Composable models: Assemble a model from interchangeable infection and observation parts — each infection model owning its own latent process — instead of writing one monolithic model.
Swap a part to change an assumption: Change the latent process, infection process, or observation model on its own to test how each assumption shapes your conclusions.
One interface: Every part becomes a Turing / DynamicPPL model through the single
as_turing_modelconstructor, so parts nest freely as submodels. The full Turing inference toolbox (NUTS, prior simulation) applies.Simulate and infer: Generate synthetic data from any model, then run Bayesian inference on the same model with real data.
A library of parts: Random walks, AR/MA/ARIMA latent processes, renewal and exponential-growth infection models, ODE (SIR/SEIR) processes, Poisson and negative-binomial observations, reporting delays, ascertainment, and aggregation — all interchangeable.
Getting started
You assemble a model from parts, and ComposableTuringIDModels turns the assembly into a single Turing model you can simulate from and fit. Each part is itself a model, joined through the generic as_turing_model constructor.
Compose a model: an ARIMA-style latent process (a differenced AR) folded into a direct-infections process, observed with Poisson error.
using ComposableTuringIDModels, Distributions, Turing
model = IDModel(
DirectInfections(;
Z = DiffLatentModel(; model = AR(), init = [Normal(), Normal()]),
initialisation = Normal()),
PoissonError())IDModel
├─ infection: DirectInfections
│ └─ Z: DiffLatentModel
│ └─ model: AR
│ └─ ϵ_t: HierarchicalNormal
└─ observation: PoissonErrorBuild a Turing model; missing observations simulate from the prior.
n = 30
prior_model = as_turing_model(model, missing, n)DynamicPPL.Model{typeof(ComposableTuringIDModels._as_turing_model_idmodel), (:model, :y_t, :n), (), (:y_t,), Tuple{IDModel{DirectInfections{DiffLatentModel{AR{Distributions.Truncated{Distributions.Normal{Float64}, Distributions.Continuous, Float64, Float64, Float64}, Distributions.Normal{Float64}, Int64, HierarchicalNormal{Float64, Distributions.Truncated{Distributions.Normal{Float64}, Distributions.Continuous, Float64, Float64, Float64}, Bool}, typeof(identity)}, Vector{Distributions.Normal{Float64}}}, typeof(exp), Distributions.Normal{Float64}}, PoissonError}, Missing, Int64}, Tuple{}, DynamicPPL.DefaultContext, false}(ComposableTuringIDModels._as_turing_model_idmodel, (model = IDModel, y_t = missing, n = 30), NamedTuple(), DynamicPPL.DefaultContext())Sample from the prior and inspect the generated quantities.
draw = rand(prior_model)
(; generated_y_t, I_t, Z_t) = prior_model()(generated_y_t = Union{Missing, Int64}[2, 2, 2, 1, 0, 0, 0, 0, 1, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], expected_y_t = [3.3831271516466574, 3.044906224486632, 1.9888167594063646, 1.2906128911118944, 0.8375481080596769, 0.5462780912891562, 0.3569046385115774, 0.23350911640815994, 0.15262979280814262, 0.09976049334460445 … 0.0010256871941593398, 0.0006842237410396727, 0.00045598498378983556, 0.0003031939933640396, 0.00020241781969428755, 0.0001345503311412326, 8.934675774518192e-5, 5.9352088838894625e-5, 3.949981861573408e-5, 2.6280604020382287e-5], I_t = [3.3831271516466574, 3.044906224486632, 1.9888167594063646, 1.2906128911118944, 0.8375481080596769, 0.5462780912891562, 0.3569046385115774, 0.23350911640815994, 0.15262979280814262, 0.09976049334460445 … 0.0010256871941593398, 0.0006842237410396727, 0.00045598498378983556, 0.0003031939933640396, 0.00020241781969428755, 0.0001345503311412326, 8.934675774518192e-5, 5.9352088838894625e-5, 3.949981861573408e-5, 2.6280604020382287e-5], Z_t = [0.5579507401465119, 0.4526203697152922, 0.026690134216360684, -0.40573251948231415, -0.8381263088895851, -1.2654768426098513, -1.6911363863165563, -2.115383894618318, -2.540589678950194, -2.9658327667240143 … -7.543242192121835, -7.948075321462567, -8.353900413693781, -8.761987449613143, -9.166026316874836, -9.574421954108926, -9.983835338560691, -10.392872976680566, -10.800064212490703, -11.207529114995411])Condition on data and run inference.
using StatsBase
posterior_model = as_turing_model(model, generated_y_t, n)
chain = sample(posterior_model, NUTS(), 1_000)
summarystats(chain)╭─FlexiSummary (9 statistics) ─────────────────────────────────────────────────╮
│ iter collapsed │
│ chain collapsed │
│ ↓ stat = [mean, std, mcse, ess_bulk, ess_tail, rhat, q5, q50, q95] │
│ │
│ Parameters (34) ── AbstractPPL.VarName │
│ Float64 latent_init[1], latent_init[2], ar_init, damp_AR, ρ, std, ϵ_t[1], │
│ ϵ_t[2], ϵ_t[3], ϵ_t[4], ϵ_t[5], ϵ_t[6], ϵ_t[7], ϵ_t[8], ϵ_t[9], │
│ ϵ_t[10], ϵ_t[11], ϵ_t[12], ϵ_t[13], ϵ_t[14], ϵ_t[15], ϵ_t[16], │
│ ϵ_t[17], ϵ_t[18], ϵ_t[19], ϵ_t[20], ϵ_t[21], ϵ_t[22], ϵ_t[23], │
│ ϵ_t[24], ϵ_t[25], ϵ_t[26], ϵ_t[27], init_incidence │
│ │
│ Extras (14) │
│ Float64 n_steps, is_accept, acceptance_rate, log_density, │
│ hamiltonian_energy, hamiltonian_energy_error, │
│ max_hamiltonian_energy_error, tree_depth, numerical_error, │
│ step_size, nom_step_size, logprior, loglikelihood, logjoint │
│ │
│ Summary │
│ param mean std mcse ess_bulk ess_tail rhat … │
│ latent_init… 0.1981 0.6148 0.0265 540.5651 482.3981 1.0078 … │
│ latent_init… -0.1366 0.7323 0.0309 572.4194 685.6259 1.0035 … │
│ ar_init -0.5162 0.5771 0.0304 357.8898 428.7520 1.0100 … │
│ damp_AR 0.0401 0.0307 0.0011 653.3745 378.7234 1.0006 … │
│ ρ 0.0401 0.0307 0.0011 653.3745 378.7234 1.0006 … │
│ std 0.0724 0.0571 0.0019 607.2068 501.9178 0.9994 … │
│ ϵ_t[1] -0.0376 0.9910 0.0324 946.9637 638.8158 1.0016 … │
│ ϵ_t[2] -0.0238 1.0175 0.0359 797.3677 547.0402 1.0085 … │
│ ϵ_t[3] 0.0147 0.9430 0.0313 895.1912 495.7595 1.0024 … │
│ ϵ_t[4] 0.0416 0.9631 0.0326 879.6388 625.7317 1.0056 … │
│ ϵ_t[5] 0.0317 0.9655 0.0288 1123.8897 628.4613 1.0037 … │
│ ϵ_t[6] -0.0051 1.0061 0.0329 925.9755 458.8817 1.0028 … │
│ ϵ_t[7] -0.0781 1.0050 0.0370 725.2189 597.7744 0.9997 … │
│ ϵ_t[8] -0.0726 1.0101 0.0343 879.8080 587.3085 0.9995 … │
│ ϵ_t[9] -0.0721 0.9561 0.0341 782.1843 607.2139 1.0007 … │
│ ϵ_t[10] -0.0506 0.9726 0.0319 932.6342 440.3132 0.9994 … │
│ ϵ_t[11] -0.0147 0.9707 0.0374 664.0998 510.5680 1.0092 … │
│ ϵ_t[12] -0.0328 0.9676 0.0324 899.5028 551.2529 0.9997 … │
│ ϵ_t[13] -0.0626 1.0071 0.0389 644.0831 691.3653 0.9995 … │
│ ϵ_t[14] -0.0323 0.9928 0.0305 1052.1453 619.9025 1.0003 … │
│ ϵ_t[15] -0.0102 1.0467 0.0398 672.8207 325.2004 1.0024 … │
│ ϵ_t[16] 0.0142 1.0020 0.0317 971.2241 501.2019 1.0000 … │
│ ϵ_t[17] -0.0172 1.0572 0.0346 931.3005 513.9338 1.0000 … │
│ ϵ_t[18] 0.0351 1.0237 0.0324 1004.6929 455.5281 1.0014 … │
│ ϵ_t[19] -0.0289 1.0021 0.0291 1173.7915 541.4724 1.0121 … │
│ ϵ_t[20] 0.0259 0.9814 0.0390 644.2909 567.3051 1.0060 … │
│ ϵ_t[21] -0.0122 0.9580 0.0333 828.4335 619.6259 1.0017 … │
│ ϵ_t[22] 0.0134 1.0230 0.0345 893.6294 501.1910 1.0440 … │
│ ϵ_t[23] -0.0072 0.9361 0.0256 1215.0735 562.8922 1.0033 … │
│ ϵ_t[24] -0.0092 1.0376 0.0358 839.7836 594.7249 1.0058 … │
│ ϵ_t[25] -0.0017 0.8688 0.0268 1059.7727 696.2342 0.9990 … │
│ ϵ_t[26] -0.0030 1.0006 0.0338 866.6358 552.4304 1.0002 … │
│ ϵ_t[27] -0.0043 1.0085 0.0324 957.3763 443.5088 0.9996 … │
│ init_incide… 0.3355 0.7624 0.0343 505.6876 448.6898 1.0036 … │
╰──────────────────────────────────────────────────────────────────────────────╯Swap a part
Because every part is interchangeable, the same swap-in/swap-out pattern applies throughout. Replace PoissonError() with NegativeBinomialError(), wrap the observation in a LatentDelay, or change the latent process — without touching the rest of the model.
Same infection process, two observation assumptions.
latent = DiffLatentModel(; model = AR(), init = [Normal(), Normal()])
infections = DirectInfections(; Z = latent, initialisation = Normal())
poisson_model = IDModel(infections, PoissonError())
negbin_model = IDModel(infections, NegativeBinomialError())IDModel
├─ infection: DirectInfections
│ └─ Z: DiffLatentModel
│ └─ model: AR
│ └─ ϵ_t: HierarchicalNormal
└─ observation: NegativeBinomialErrorThat is the point: you compare modelling assumptions by swapping parts, not by rewriting models.
Related packages
ModifiedDistributions.jl modifies the behaviour of the underlying Distributions.jl distributions.
ReparameterisedDistributions.jl switches distributions between parameterisations, so a prior can be stated in whichever one is natural.
LoweredDistributions.jl lowers a distribution onto a backend-agnostic dynamical-systems representation, so a delay distribution and a compartmental model become two views of one thing.
Turing.jl is the probabilistic programming backend every model here compiles down to.
Where to learn more
New here? Start with the Overview and the Composable design page.
Want worked examples? See the tutorials, which fit complete models to real surveillance data.
Want the full interface? Browse the Public API.
Want to see the code or report a problem? Check the GitHub repository.
Adapted from
The modelling code in this package is ported and adapted from the open-source, Apache-2.0 licensed EpiAware package (CDCgov/Rt-without-renewal, ported from the fork seabbs/Rt-without-renewal). ComposableTuringIDModels is a modified, derived work: it has been renamed, re-architected around the generic as_turing_model constructor, and upgraded to build against the latest Turing.jl. See the NOTICE file for full attribution and a summary of the changes, and LICENSE for the Apache-2.0 terms.
Part of the EpiAware ecosystem
ComposableTuringIDModels 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 ComposableTuringIDModels 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 ComposableTuringIDModels project is released with a Contributor Code of Conduct. By contributing, you agree to abide by its terms.