---
title: Stochastic Dynamics and Extinctions - Practice
output:
  html_document:
    theme: null
    highlight: null
    css: ../styles/handelshiny.css
    fig_caption: true
    mathjax: default 
    keep_md: false
    includes:
      #in_header: in_header.txt
      before_body: ../styles/before_shiny_body.txt
      after_body: ../styles/after_shiny_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-sympomatic, 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 back 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}

_Note: Some of the simulations might take a few seconds to run. While they run, you should see a 'Simulation running' message._

###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 number of simulations to 1.
    
* With parameters set to correspond to the scenario just described, run the simulation.
* If your run the simulation again 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 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 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, rate of immunity loss to 0.2, rate of symptomatic transmission to 0.005, no presymptomatic transmission. 
* Set the duration of the symptomatic period to 1 month, the duration of the presymptomatic period to half a month. 
* Simulation time about 5 years.
* No births and deaths.
* Set 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 simulation end 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}

* 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. Check the help file for the function for more information. 
* If you call the function directly, you need to process the results returned from this function and produce useful output (such as a plot) yourself. 
* You can also modify this R function for your own purposes. To find out how to do that, type `?DSAIDE` at the R console.
* For some further reading concerning stochastic dynamics of ID, see e.g. [@lloyd-smith05a, @black66, @keeling97].

### References

