Below is a complete implementation of an SEIR (Susceptible, Exposed, Infected, Recovered) model in Turing.jl, adapted to use the Reed-Frost formulation for transmission. The Reed-Frost model is inherently discrete-time and stochastic, so I've implemented it with daily time steps. As per your request:
- Observations:
- A prevalence measure: This is modeled as observations of the number of infected individuals (I_t) at each time step, observed via a Negative Binomial distribution to account for overdispersion.
- Deaths: These are generated daily based on a convolution of the number of new infections (new E_t) with a delay distribution. Specifically, the expected number of deaths on day t is computed by convolving the history of new infections with a discretized Gamma distribution (representing the delay from infection to death). The actual deaths are then observed via a Negative Binomial distribution.
Key Model Details:
- Reed-Frost Transmission: For each day t, the number of new exposures (new E_t) is drawn from a Binomial distribution based on the susceptible population (S_t) and the probability of infection, which depends on the number of infected individuals (I_t). The probability of a susceptible individual escaping infection is (1 - β)^I_t, where β is the infection probability per contact.
- SEIR Transitions:
- S to E: Stochastic, based on Reed-Frost.
- E to I: Modeled as a stochastic process where each exposed individual has a daily probability σ of transitioning to infected.
- I to R: Modeled as a stochastic process where each infected individual has a daily probability γ of recovering.
- Deaths:
- New infections (new E_t) are convolved with a delay distribution (e.g., a Gamma distribution discretized to daily steps).
- The expected deaths on day t are calculated as: expected_deaths_t = sum_k (new_E_{t-k} * g(k) * CFR), where g(k) is the PMF of the delay distribution, and CFR is the case fatality rate.
- Observed deaths_t ~ NegativeBinomial(expected_deaths_t, φ), where φ is the dispersion parameter.
- Prevalence Observations: Observed as the number of infected individuals I_t, with observations drawn from NegativeBinomial(I_t, φ2).
- Parameters:
- β: Infection probability (Reed-Frost parameter).
- σ: Daily probability of E transitioning to I.
- γ: Daily probability of I transitioning to R.
- CFR: Case fatality rate.
- Delay distribution: A Gamma distribution (shape α, rate β_delay) discretized to daily steps.
- φ and φ2: Dispersion parameters for Negative Binomial observations.
Requirements for Running This Code:
- Install Turing.jl, Distributions.jl, and other dependencies if not already installed.
- This code assumes you have data for observations (e.g., arrays for observed prevalence and deaths).
- Run this in a Julia environment.
using Turing
using Distributions
using StatsBase # For convolution and other utilities
@model function SEIR_ReedFrost(T::Int, initial_S::Float64, initial_E::Float64, initial_I::Float64, initial_R::Float64,
observed_prevalence::Vector{Float64}, observed_deaths::Vector{Float64},
β_prior::Distribution, σ_prior::Distribution, γ_prior::Distribution,
CFR_prior::Distribution, α_delay_prior::Distribution, β_delay_prior::Distribution,
φ_prior::Distribution, φ2_prior::Distribution)
# Priors for parameters
β ~ β_prior # Infection probability (Reed-Frost)
σ ~ σ_prior # Daily probability E to I
γ ~ γ_prior # Daily probability I to R
CFR ~ CFR_prior # Case fatality rate (between 0 and 1)
α_delay ~ α_delay_prior # Shape parameter for Gamma delay distribution
β_delay ~ β_delay_prior # Rate parameter for Gamma delay distribution
φ ~ φ_prior # Dispersion for deaths (Negative Binomial)
φ2 ~ φ2_prior # Dispersion for prevalence (Negative Binomial)
# Initialize state vectors
S = Vector{Float64}(undef, T)
E = Vector{Float64}(undef, T)
I = Vector{Float64}(undef, T)
R = Vector{Float64}(undef, T)
new_E = Vector{Float64}(undef, T) # New infections (new E_t)
new_I = Vector{Float64}(undef, T) # New transitions from E to I
recoveries = Vector{Float64}(undef, T) # New recoveries from I to R
S[1] = initial_S
E[1] = initial_E
I[1] = initial_I
R[1] = initial_R
# Precompute the delay distribution PMF (Gamma discretized to daily steps)
max_delay = 30 # Assume a maximum delay of 30 days for practicality
delay_dist = Gamma(α_delay, 1/β_delay) # Gamma(shape, scale) in Distributions.jl
delay_pmf = pdf.(delay_dist, 1:max_delay) # Discretize to daily steps
delay_pmf = delay_pmf / sum(delay_pmf) # Normalize to a PMF
expected_deaths = Vector{Float64}(undef, T) # For deaths convolution
for t in 2:T
# Step 1: Reed-Frost transmission (S to E)
infection_prob = 1 - (1 - β)^I[t-1] # Probability of infection
new_E[t] ~ Binomial(S[t-1], infection_prob) # Stochastic transition
# Step 2: E to I transition
new_I[t] ~ Binomial(E[t-1], σ) # Stochastic: Each E has probability σ to become I
# Step 3: I to R transition
recoveries[t] ~ Binomial(I[t-1], γ) # Stochastic: Each I has probability γ to recover
# Update states
S[t] = S[t-1] - new_E[t]
E[t] = E[t-1] + new_E[t] - new_I[t]
I[t] = I[t-1] + new_I[t] - recoveries[t]
R[t] = R[t-1] + recoveries[t]
# Step 4: Compute expected deaths for day t (convolution with delay distribution)
expected_deaths[t] = 0.0
for k in 1:min(t-1, max_delay) # Sum over possible delays
if t - k >= 1 # Ensure we don't go before t=1
expected_deaths[t] += new_E[t-k] * delay_pmf[k] * CFR
end
end
# Observe deaths for day t
observed_deaths[t] ~ NegativeBinomial(expected_deaths[t], φ) # NegativeBinomial(mean, dispersion)
end
# Observe prevalence (e.g., number of infected I_t)
for t in 1:T
observed_prevalence[t] ~ NegativeBinomial(I[t], φ2)
end
end
# Example usage
# Define priors (you can adjust these based on your knowledge)
β_prior = Uniform(0.01, 0.1) # Infection probability
σ_prior = Uniform(0.1, 0.5) # E to I rate
γ_prior = Uniform(0.05, 0.2) # I to R rate
CFR_prior = Uniform(0.01, 0.05) # Case fatality rate
α_delay_prior = Gamma(2, 1) # Shape for delay (Gamma prior)
β_delay_prior = Gamma(0.5, 1) # Rate for delay
φ_prior = Gamma(1, 1) # Dispersion for deaths
φ2_prior = Gamma(1, 1) # Dispersion for prevalence
# Simulate some dummy data for testing (in practice, use real data)
T = 50 # Number of days
initial_S = 999.0 # Initial susceptible
initial_E = 1.0 # Initial exposed
initial_I = 0.0 # Initial infected
initial_R = 0.0 # Initial recovered
observed_prevalence = rand(T) * 10 # Dummy data
observed_deaths = rand(T) * 5 # Dummy data
# Run the model (for inference, use MCMC or other samplers)
model = SEIR_ReedFrost(T, initial_S, initial_E, initial_I, initial_R,
observed_prevalence, observed_deaths,
β_prior, σ_prior, γ_prior, CFR_prior,
α_delay_prior, β_delay_prior, φ_prior, φ2_prior)
# Example: Sample from the posterior (this will take time)
chain = sample(model, NUTS(0.65), 1000) # 1000 samples, adjust as needed
Explanation of the Code:
-
Model Function:
- The
@model macro defines the Turing model.
- Priors are specified for all parameters.
- State vectors (S, E, I, R, etc.) are initialized and updated in a loop over time steps.
- For each day:
- Compute new exposures using Reed-Frost.
- Update compartments stochastically.
- Compute expected deaths by convolving new_E with the delay PMF and multiplying by CFR.
- Add likelihood for observed deaths and prevalence.
-
Delay Convolution:
- The Gamma distribution is discretized to get a PMF for the delay from infection to death.
- For day t, expected deaths are the weighted sum of past new infections, using the PMF.
-
Observations:
- Prevalence: Directly tied to I_t, observed via Negative Binomial.
- Deaths: Tied to the convolved new infections, observed via Negative Binomial.
-
Running the Model:
- Use
sample from Turing to perform MCMC sampling for inference.
- This model is set up for Bayesian inference, so you can estimate parameters from data.
Notes:
- Performance: This is a stochastic compartmental model with loops, which can be computationally intensive for large T. If T is very large, consider vectorizing where possible or using a more efficient sampler.
- Assumptions: I assumed simple stochastic transitions for E to I and I to R using Binomial distributions. If you need more complex delay distributions for these, you can extend the code.
- Validation: Before using real data, test with simulated data to ensure the model behaves as expected.
- Extensions: If you want to add more features (e.g., varying population size, interventions), let me know!
If you need modifications or have more details (e.g., specific priors or data formats), just ask!