---
title: Stochastic Dynamics and Extinctions - Practice
output:
  html_document:
    theme: null
    highlight: null
    css: ../styles/dsaide.css
    fig_caption: true
    mathjax: default 
    keep_md: false
    includes:
      #in_header: in_header.txt
      before_body: ../styles/dsaide_before_body.txt
      after_body: ../styles/dsaide_after_body.txt 
bibliography: ../media/references.bib
---

##Overview {#shinytab1}
For this module, we will explore an SEIR model. This model is implemented in a _stochastic_ framework. Read about the model in the "Model" tab. Then do the tasks described in the "What to do" tab.

##The Model {#shinytab2}

###Model Overview

This model tracks susceptibles, exposed/pre-symptomatic, infected/symptomatic and recovered hosts. 
The following compartments are included:  

* **S** - uninfected and susceptible individuals 
* **E** - individuals who are infected and do not yet show symptoms. Those individuals can be infectious.
* **I** - individuals who are infected and show symptoms. Those individuals are likely infectious, but the model allows to adjust this, including no infectiousness.
* **R** - recovered/removed individuals. Those individuals have recovered and are immune. They can lose their immunity.


The included processes/mechanisms are the following:

* Susceptible individuals (S) can become infected by pre-symptomatic/exposed (E) or symptomatic (I) hosts at rates _b~E~_ and _b~I~_. 
* All infected hosts first enter the exposed/presymptomatic stage and leave the stage at rate _g~E~_, the inverse of which is the average time spent in this stage. 
* All infected hosts go from the presymptomatic to the symptomatic stage and leave the stage at rate _g~I~_. 
* Recovered individuals are initially immune to reinfection. They can lose their immunity at rate _w_ and return to the susceptible compartment. 
* New susceptible hosts enter the system (are born) at rate _e_. Natural death occurs at rate _n_.



###Model Implementation
The flow diagram for the model implemented in this app is:

![Flow diagram for this model.](../media/stochasticmodel.png)


Note that this model is _not_ an ordinary differential equation model. It is instead its stochastic equivalent.
We can specify the model by writing down every possible transition/event/reaction that can occur and their propensities (the propensity multiplied with the
time step gives the probability that a given event/transition occurs). For our model these are the following:

Event type              |  Transitions        |   Propensity      |
----------              |  -----------        |   ----------      |
Infection               |  S => S-1, E => E+1 |   S(b~E~E+b~I~I)  |
Progression to Symptoms |  E => E-1, I => I+1 |   g~E~E           |
Recovery                |  I => I-1, R => R+1 |   g~I~I           |
Waning of Immunity      |  R => R-1, S => S+1 |   wR          |
Births                  |  S => S+1           |   eS          |
Death of susceptible    |  S => S-1           |   nS          | 
Death of exposed        |  E => E-1           |   nE          |
Death of symptomatic    |  I => I-1           |   nI          |
Death of recovered      |  R => R-1           |   nR          |




##What to do {#shinytab3}

**The tasks below are described in a way that assumes everything is in units of MONTHS (rate parameters, therefore, have units of inverse months). If any quantity is not given in those units, you need to convert it first (e.g. if it says a year, you need to convert it to 12 months).**

_Some of the simulations might take a few seconds to run. Be patient._

###Task 1: 
* Set the model parameters such that it corresponds to the following setting: 
    * 1000 susceptible and 1 initially symptomatic host.
    * Simulation duration approximately 5 years.
    * Assume that only symptomatic individuals transmit, at rate 0.002. 
    * Assume that the duration of the symptomatic period is 1 month long, the duration of the presymptomatic period is half a month long. 
    * Assume immunity does not wane and that there are no births and deaths.
    * Set the number of simulations to 1.
    
* With parameters set to correspond to the scenario just described, run the simulation.
* If you rerun the simulation without changing anything, what do you expect to see? Test your expectations by running the simulation several times.
* To speed things up, set the number of simulations to 20, run all 20.

###Task 2: 
* In the previous task, you should have seen that occasionally, no outbreak occurs (because the infected person recovers/dies before they can infect others). Explore how the initial number of infected influences the chance of (not) getting outbreaks. Set the initial number of infected to 5, everything as above. 
* Run 20 outbreaks at a time, repeat a few times. Observe the number of times you do (not) get an outbreak.
* Repeat with the initial number of infected at 2,3, and 10.


###Task 3: 
* Set everything as in task 1, with 1 initial infected. Now explore how the rate of transmission (and with it the reproductive number) affects outbreaks.
* Try values for the transmission rate of 0.001, 0.0015 and 0.004. Compare the results.


###Task 4: 
* Set susceptibles to 400, infected to 100, the rate of immunity loss to 0.2, the rate of symptomatic transmission to 0.005, and no presymptomatic transmission. 
* Set the duration of the symptomatic period to 1 month and the duration of the presymptomatic period to half a month. 
* Simulation time about 5 years.
* No births and deaths.
* Set the number of simulations to 1. Run the model several times.
* You should see the stochastic version of a steady state, namely S-E-I-R values that fluctuate around some level.
    
###Task 5: 
* Record the fraction of susceptibles at the end of the simulation for the setting you just had. They will change.
* Now change the number of simulations to 5, 10, 20 (be patient). See how the average number of susceptibles at the end of the simulation settles down to some number. It will still fluctuate a bit, but as you take the average of more simulations, results become more stable (deterministic). 

###Task 6: 
* Set simulation number to 5, rest as before.
* Run the model a few times for around 50 years (be patient). Observe if/when you get extinctions.
* Now change the rate of transmission to 0.003, again run multiple simulations, then repeat with transmission at 0.0025.
* Contemplate why you see what you see.
  
  
###Task 7: 
* Keep exploring. Turn on births and deaths. Play around with anything else you can think of.  
  
  
##Answers 
##Answer 1: 

###Answer 2: 

###Answer 3: 


##Further Information {#shinytab4}
* This app (and all others) are structured such that the Shiny part (the graphical interface you are currently using and the server-side function that goes with it) calls an underlying R script which runs the simulation for the model of interest and returns the results.
* For this app, the underlying function running the simulation is called `simulate_stochastic.R`. You can call this function directly, without going through the shiny app. Type `?simulate_stochastic` into the R console for more information (you need to exit the graphical interface first or start a new R session). If you go that route, you need to use the results returned from this function and produce useful output (such as a plot) yourself. 
* You could also modify this R function for your own purposes - though that requires some more R coding knowledge. 
* To get a quick start explaining how to interact with the simulator functions directly, read the vignette for the package (type `vignette('DSAIDE')` into the R console).
* For some further reading concerning stochastic dynamics of ID, see e.g. [@lloyd-smith05a, @black66, @keeling97].

### References

