Simulating stochastic epidemics

Similar documents
Numerical solution of stochastic epidemiological models

Stochastic Models. John M. Drake & Pejman Rohani

Seasonally forced epidemics Biennial outbreaks of Measles in England and Wales

adaptivetau: efficient stochastic simulations in R

Transmission in finite populations

Thursday. Threshold and Sensitivity Analysis

Estimation. John M. Drake & Pejman Rohani

Introduction to SEIR Models

Three Disguises of 1 x = e λx

Stochastic modelling of epidemic spread

Stochastic modelling of epidemic spread

Models of Infectious Disease Formal Demography Stanford Summer Short Course James Holland Jones, Instructor. August 15, 2005

LAW OF LARGE NUMBERS FOR THE SIRS EPIDEMIC

Downloaded from:

Any live cell with less than 2 live neighbours dies. Any live cell with 2 or 3 live neighbours lives on to the next step.

Epidemics in Networks Part 2 Compartmental Disease Models

MCMC 2: Lecture 2 Coding and output. Phil O Neill Theo Kypraios School of Mathematical Sciences University of Nottingham

MATHEMATICAL MODELS Vol. III - Mathematical Models in Epidemiology - M. G. Roberts, J. A. P. Heesterbeek

Mathematical Analysis of Epidemiological Models: Introduction

Modeling the Spread of Epidemic Cholera: an Age-Structured Model

AGE STRUCTURED MODELS. Contents

Markov-modulated interactions in SIR epidemics

A BINOMIAL MOMENT APPROXIMATION SCHEME FOR EPIDEMIC SPREADING IN NETWORKS

MCMC 2: Lecture 3 SIR models - more topics. Phil O Neill Theo Kypraios School of Mathematical Sciences University of Nottingham

Copyright is owned by the Author of the thesis. Permission is given for a copy to be downloaded by an individual for the purpose of research and

Approximation of epidemic models by diffusion processes and their statistical inferencedes

Gillespie s Algorithm and its Approximations. Des Higham Department of Mathematics and Statistics University of Strathclyde

GEMF: GENERALIZED EPIDEMIC MODELING FRAMEWORK SOFTWARE IN PYTHON

Mathematical Modeling and Analysis of Infectious Disease Dynamics

Endemic persistence or disease extinction: the effect of separation into subcommunities

Introduction to Stochastic SIR Model

COOPERATIVE EPIDEMIC SPREADING IN COMPLEX NETWORKS

Project 1 Modeling of Epidemics

Table of Contents [ntc]

MA 777: Topics in Mathematical Biology

Predator-Prey Population Dynamics

Biology 559R: Introduction to Phylogenetic Comparative Methods Topics for this week:

AN INTRODUCTION TO STOCHASTIC EPIDEMIC MODELS-PART I

CS224W: Analysis of Networks Jure Leskovec, Stanford University

Statistical Inference for Stochastic Epidemic Models

CS 798: Homework Assignment 3 (Queueing Theory)

The effect of emigration and immigration on the dynamics of a discrete-generation population

The death of an epidemic

M4A42 APPLIED STOCHASTIC PROCESSES

Section 8.1 Def. and Examp. Systems

Relations in epidemiology-- the need for models

Final Project Descriptions Introduction to Mathematical Biology Professor: Paul J. Atzberger. Project I: Predator-Prey Equations

Discrete-event simulations

The Spreading of Epidemics in Complex Networks

Lecture VI Introduction to complex networks. Santo Fortunato

Global Stability of a Computer Virus Model with Cure and Vertical Transmission

Computer Intensive Methods in Mathematical Statistics

An Introduction to Stochastic Epidemic Models

A Generic Multivariate Distribution for Counting Data

A Mathematical Analysis on the Transmission Dynamics of Neisseria gonorrhoeae. Yk j N k j

Modeling with Itô Stochastic Differential Equations

Introduction to Queueing Theory

Social Influence in Online Social Networks. Epidemiological Models. Epidemic Process

THE STOCHASTIC DYNAMICS OF EPIDEMIC MODELS

Mathematical Epidemiology Lecture 1. Matylda Jabłońska-Sabuka

Queueing Theory. VK Room: M Last updated: October 17, 2013.

Time varying networks and the weakness of strong ties

14 Random Variables and Simulation

PARAMETER ESTIMATION IN EPIDEMIC MODELS: SIMPLIFIED FORMULAS

The Effect of Stochastic Migration on an SIR Model for the Transmission of HIV. Jan P. Medlock

Stochastic Simulation.

Recap. Probability, stochastic processes, Markov chains. ELEC-C7210 Modeling and analysis of communication networks

Australian Journal of Basic and Applied Sciences

Session 1: Probability and Markov chains

Predicting AGI: What can we say when we know so little?

Epidemics in Complex Networks and Phase Transitions

A Simple Ecological Model

Inferring the origin of an epidemic with a dynamic message-passing algorithm

Session-Based Queueing Systems

Exponential and Logarithmic Functions

Diffusion of information and social contagion

Supplemental Information Population Dynamics of Epidemic and Endemic States of Drug-Resistance Emergence in Infectious Diseases

Exercises Stochastic Performance Modelling. Hamilton Institute, Summer 2010

Lecture 4 The stochastic ingredient

On a stochastic epidemic SEIHR model and its diffusion approximation

DISCRETE STOCHASTIC PROCESSES Draft of 2nd Edition

Modelling residual wind farm variability using HMMs

SIR Epidemic Model with total Population size

MAS1302 Computational Probability and Statistics

probability George Nicholson and Chris Holmes 31st October 2008

Complex networks: an introduction Alain Barrat

Approximation of epidemic models by diffusion processes and their statistical inferencedes

Efficient Leaping Methods for Stochastic Chemical Systems

Electronic appendices are refereed with the text. However, no attempt has been made to impose a uniform editorial style on the electronic appendices.

2DI90 Probability & Statistics. 2DI90 Chapter 4 of MR

Math 350: An exploration of HMMs through doodles.

7.1 Basis for Boltzmann machine. 7. Boltzmann machines

CONTENTS. Preface List of Symbols and Notation

Models of Infectious Disease Formal Demography Stanford Spring Workshop in Formal Demography May 2008

This is now an algebraic equation that can be solved simply:

Analysis and Control of Epidemics

Hidden Markov Models Part 1: Introduction

Modelling of the Hand-Foot-Mouth-Disease with the Carrier Population

Epidemics and information spreading

Stochastic Processes and Advanced Mathematical Finance. Stochastic Processes

Transcription:

Simulating stochastic epidemics John M. Drake & Pejman Rohani 1 Introduction This course will use the R language programming environment for computer modeling. The purpose of this exercise is to introduce R for stochastic simulation. This exercise addresses only those features of R that are required for this course. A more complete introduction and handy reference can be found on the Internet at http://cran.r-project.org/doc/manuals/r-intro.html. 2 What is R? R is both a high level computer language and a software platform for writing and executing programs in that language. The structure of R is well suited to scientific programming. It efficiently performs many, many numerical procedures, including many that are extremely useful for statistical analysis and visualization. In what follows, we assume that the student has had no prior experience with R. The goal of this session is to develop some facility with the tasks that will be needed later in this course. Naturally, we do not aim to provide a comprehensive overview of either scientific programming or of R. Rather, these exercises are intended to function as a starting point for further exploration and selfstudy with an emphasis on applications and illustrations from theoretical epidemiology. All that is required for completing the exercises is the base R program. RStudio, which may be downloaded from http://rstudio.org, is recommended for those seeking a more featured development environment. 3 Stochastic models Stochastic epidemic models are representations of a contagion process that contain some element of randomness expressed in terms of mathematical probability. Such probability is important when Populations are small The epidemic system is far from its equilibrium state (i.e., far from extinction or endemic equilibrium) including emerging pathogens where the infection is introduced to a large, but largely or wholly susceptible population There are intrinsic cycles that cause the number of infected individuals to become close to zero or go locally extinct, or The basic reproduction number R 0 is near 1 Licensed under the Creative Commons attribution-noncommercial license, http://creativecommons.org/licenses/bync/3.0/. Please share and remix noncommercially, mentioning its origin. 1

Stochastic models explain why situations with similar population sizes, infectious agents, age distribution, immune history etc. may nonetheless yield very different epidemic trajectories. For instance, even a pathogen which is expected to propagate may not cause an outbreak if the index case recovers or dies becfore causing a secondary infection. Stochastic models enable the probability of this outcome to be evaluated. Similarly, if a second generation of infections occurs, but each infected case in this generation fails to propagate the infection, then transmission is limited. Stochastic models also enable the probability of this outcome to be evaluated, and, in general, the chance that an introduced pathogen causes a large outbreak versus that it quickly dies out. In this exercise we introduce (multivariate) birthdeath processes as a kind of stochastic model and Gillespie s direct method for solving such processes. We show how numerical analysis of birth-death models may be used to provide insight into the course of epidemics. 4 Birth-death processes First, we aim to understand the theory of birth-death processes in general. These models are continuous in time but discrete in the state space. That is, we require that the number of susceptible, infected, and recovered individuals be integers (rather than proportions). Thus, changes of state occur as unique jumps among the possible combinations of state variables. Over the long run, these jumps occur according to some average rates or propensities, like the expressions for ds/dt or di/dt used in deterministic modeling. If the random variation reflects variation among individuals (some give rise to two secondary infections, some to one, some to zero, etc.), such as scenario is referred to as demographic stochasticity, which is a kind of intrinsic noise. Demographic stochasticity acknowledges that even though the epidemic trajectory might appear to be smooth (at least in a large population), it nevertheless must consist of discrete changes, as illustrated in the following figure. When populations are small, or far from equilibrium, or near a critical point these small fluctuations cannot be ignored. Cases 0 2000 4000 6000 8000 10000 0.0 0.2 0.4 0.6 0.8 1.0 Time 2

At this point, there are still a number of different directions we could go. We will make two assumptions that, together with the assumption of demographic stochasticity, uniquely determine a whole class of models. First, we assume that the epidemic is a Markov chain. A Markov chain is a stochastic process with the property that the future state of the system is dependent only on the present state of the system and conditionally independent of all past states. This is known as the memoryless property. Second, we assume that the changes in the state variables (increments and decrements) are independent. This implies that they occur one at a time. That is, two individuals cannot simultaneously undergo a transition (a change in state, i.e., birth, death, transition from susceptible to recovered, etc.). Any time an individual undergoes such a transition, we will call it an event. Accordingly, this kind of modeling is sometimes referred to as event-driven simulation. For historical reasons, the continuous time Markov chain with increments and decrements of one is known as a birth-death process. (In general, a Markov chain with integer-valued increments and decrements is known as a jump process.) [Note: An aside about birth-death processes is that notation varies considerably from author to author, even though the authors are referring to exactly the same stochastic process. Particularly, the simulational literature uses a notation borrowed from chemistry, e.g., S µs S 1, probably because the algorithms that are commonly used to simulate birth-death processes were developed in the context of chemical kinetics. Probabilists, by contrast, often use the generating function notation and scientists that come to birth-death processes from a background in statistical mechanics represent the process using the Forward Kolmogorov Equation or Fokker-Planck Equation (a partial differential equation). Textbooks in epidemiology differ, too. The point is that once you learn to read the different notations, they are all saying the same thing, i.e., that changes in the state variables occur according to such and such rates. It s these rates that are the basis of simulation modeling.] Simple stochastic SI epidemic To illustrate the approach, we ll start with the simple closed stochastic SI epidemic. Because the population is closed (no births, deaths, or migration) we represent total population size as a constant N. Because we are considering abundances, not proportions, we adopt the convention used in the lecture portion of respresenting susceptibles, infecteds, and recovereds by X, Y, and Z and denote the initial number of infected individuals by Y 0 accordingly. This gives the initial number of susceptible individuals X 0 = N Y 0. By analogy to our deterministic model, we want the average rate at which susceptibles individually become infectious (the force of infection) to be β Y N and the average rate at which the population as a whole converts from susceptible to infectious to be β Y N S. That is, at average rate β Y N X the value of X is decremented by one and the value of Y is incremented by one. But when do these increments and decrements occur? To answer this, we turn to our assumption that the epidemic process is Markovian. If we can determine what the sequence of inter-event times is, then we have fully specified the trajectory of the epidemic, for we know that at each of those times the number of susceptibles decreases by one and the number of infecteds increases by one. So, what are the inter-event times? The memoryless property of the continuous time Markov chain requires that the time between events is independent of the time between any other set of events, and, moreover, that if we were to investigate the process at any point in time between events the time to the next event would be independent of the time elapsed since the previous event. This defines the birth-death process as a kind of Poisson process. There is only one distribution for the inter-event times that has this property, the exponential distribution. Since we know how to simulate random variables (in this case we use the function rexp) we just simulate the sequence of event times and make our increments and decrements accordingly. This approach is known as Gillespie s direct method. All that remains, then, is to relate the rate at which our process is happening to the generation of exponential random numbers. An exponential distribution is defined by a single parameter, although 3

the formula may be written in different ways. The parameterization assumed by R conveniently assumes the distribution is expressed as a Poisson process, i.e. the argument is itself expected to be the rate. Thus, we can simulate the inter-event time and update the state variables using one function. > SI.onestep <- function (x, params) { #function for one step of the stochastic SI epidemic + X <- x[2] #the second element of x is number of susceptibles X + Y <- x[3] #the third element of x is number of infecteds Y + with( + as.list(params), + { + new.y <- Y+1 #whenever an event occurs we increase infecteds by 1... + new.x <- X-1 #and descrease susceptibles by 1 + tau <- rexp(n=1,rate=params$beta*x*y/(x+y)) #exponential random time to next event + c(tau=tau,x=new.x,y=new.y) #store result + ) Now, we write a loop to iterate this simulation routine. > SI.model <- function (x, params, nstep) { #function to iterate the stochastic SI for nstep events + output <- array(dim=c(nstep+1,3)) #set up an array to store all the results + colnames(output) <- c("time","x","y") #name the variables in the array + output[1,] <- x #the first record of the array is the initial condition + for (k in 1:nstep) { #iterate the model for nstep events + output[k+1,] <- x <- SI.onestep(x,params) #update x and store result + output #return output Using the same value of β as before (remember: there is no recovery here), we run some simulations and plot. Note that because time is continuous, we don t actually know how many events will occur in a specified interval of time. Instead, we save some pre-set number of events. Additionally, notice how the function cumsum is used to add up the inter-event times to give a time series. > set.seed(38499583) #set the random seed so results are repeatable > nsims <- 10 #number of simulations to run > pop.size <- 200 #total size of the population > Y0 <- 2 #initial number infected > nstep <- pop.size-y0 #how many steps to run? until everyone infected > xstart <- c(time=0,x=(pop.size-y0),y=y0) #initial conditions > params <- list(beta=0.3) #parameters > data <- vector(mode='list',length=nsims) #create a list called ``data'' to store all runs > for (k in 1:nsims) { #simulate k different runs + data[[k]] <- as.data.frame(si.model(xstart,params,nstep)) #main simulation step + data[[k]]$cum.time <- cumsum(data[[k]]$time) #calculates the running sum of inter-event intervals > max.y<-max(data[[1]]$cum.time) #find the maximum time any simulation ran (to set x axis) > plot(c(0,pop.size),c(0,pop.size),type='n',xlab='time',ylab='incidence',xlim=c(0,max.y)) #set up plot > for (k in 1:nsims) { #loop over each simulation... + lines(y~cum.time,data=data[[k]],col=k,type='o') #to plot 4

incidence 0 50 100 150 200 0 10 20 30 40 time Exercise 1. Simulate the stochastic SI model using Gillespie s direct method. Experiment with the initial number of infecteds (Y 0 ) and with the total population size (N). What effects do these have on the predictability of the epidemic? What effects do these have on the variability of the final outbreak size? Extending the SI model We can extend the simple SI model to an arbitrary number of compartments. For concreteness, we study a stochastic version of the SIR model from the first section. The main difference between the SI model and the SIR model is that in the SIR model there s more than one kind of event that can occur. (This is true of the SI model with births and deaths, too, but we didn t look at that). That means we need to account for two things: (1) Our determination of the next event time has to take into consideration the multiple processes that are occurring simultaneously, and (2) Once we determine what time the event occurs we have to determine what type of event it is. Since the transition processes are independent we can calculate a total rate as the sum of the individual propensities. That is, the rate 5

at which the whole system is evolving is the sum of the rates of the individual processes which are the absolute values of the different transition terms in the model. For example, the transitions associated with the susceptible class are transition to the infected class (at rate β Y N X), births (at rate µn), and deaths (at rate µx). Thus, the total rate for the susceptible class is β Y N X + µn + µx. Our total rate for the whole proceess will include the rates for the Y and Z classes as well. The next step in the multi-dimensional Gillespie simulation is to determine which event occurs. In the long run each event much occur at its specific rate. This means that we can just randomly choose which event occurs so long as we do it in a weighted way such that each transition is selected in proportion to its contribution to the total rate. First we define our one-step function. > SIR.onestep <- function (x, params) { #function to calculate one step of stochastic SIR + X <- x[2] #local variable for susceptibles + Y <- x[3] #local variable for infecteds + Z <- x[4] #local variable for recovereds + N <- X+Y+Z #total population size (subject to demographic change) + with( #use with as in deterministic model to simplify code + as.list(params), + { + rates <- c(mu*n, beta*x*y/n, mu*x, mu*y, gamma*y, mu*z) + changes <- matrix(c( 1, 0, 0, + -1, 1, 0, + -1, 0, 0, + 0,-1, 1, + 0,-1, 0, + 0, 0,-1), + ncol=3, byrow=true) + tau <- rexp(n=1,rate=sum(rates)) # exponential waiting time + U <- runif(1) #uniform random deviate + m <- min(which(cumsum(rates)>=u*sum(rates))) + x <- x[2:4] + changes[m,] + return(out <- c(tau, x)) + ) Next we write our function for simulating a whole epidemic. > SIR.model <- function (x, params, nstep) { #function to simulate stochastic SIR + output <- array(dim=c(nstep+1,4)) #set up array to store results + colnames(output) <- c("time","x","y","z") #name variables + output[1,] <- x #first record of output is initial condition + for (k in 1:nstep) { #iterate for nstep steps + output[k+1,] <- x <- SIR.onestep(x,params) + output #return output Finally, we set up and simulate 10 runs and plot. > set.seed(38499583) #set seed > nsims <- 10 #number of simulations 6

> pop.size <- 1000 #total population size > Y0 <- 8 #initial number infected > X0 <- round(0.98*pop.size) #initial number susceptible (~98% of population) > nstep <- 2000 #number of events to simulate > xstart <- c(time=0,x=x0,y=y0,z=pop.size-x0-y0) #initial conditions > params <- list(mu=0.00001,beta=60,gamma=365/13) #parameters > data <- vector(mode='list',length=nsims) #initialize list to store the output > for (k in 1:nsims) { #simulate nsims times + data[[k]] <- as.data.frame(sir.model(xstart,params,nstep)) + data[[k]]$cum.time <- cumsum(data[[k]]$time) > max.time<-data[[1]]$cum.time[max(which(data[[1]]$y>0))] #maximum time in first simulation > max.y<-1.8*max(data[[1]]$y) #find max infected in run 1 and increase by 80% for plot > plot(y~cum.time,data=data[[1]],xlab='time',ylab='incidence',col=1,xlim=c(0,max.time),ylim=c(0,max.y)) > for (k in 1:nsims) { #add multiple epidemics to plot + lines(y~cum.time,data=data[[k]],col=k,type='o') 7

600 500 400 300 0 100 200 Incidence 0.0 0.1 0.2 0.3 0.4 Time Exercise 2. Simulate the stochastic SIR model using Gillespie s direct method. As before, experiment with the initial number of infecteds (Y0 ) and with the total population size (N ). What effects do these have on the predictability of the epidemic? Additional issues The birth-death framework is a popular approach to stochastic epidemic modeling. It has some important limitations, however. Overcoming these limitations is an area of active research: For even moderately large systems, Gillespie s direct method is very slow. Approximations are available (e.g., the so-called tau-leap method ) to speed up the calculations, though none is as easy to program as the direct method. Some of these may be called directly using the R package GillespieSSA. The Markovian assumption entails that the inter-event times of the birth-death process are expo8

nentially distributed. This is a biologically unrealistic assumption that has considerable impact on the variance of the process. Non-Markovian (i.e., non-memoryless) processes are the solution, but these come at the cost of additional conceptual and computational complexity. 9