{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "
\n", " \n", " \"QuantEcon\"\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Modeling COVID 19 with Differential Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contents\n", "\n", "- [Modeling COVID 19 with Differential Equations](#Modeling-COVID-19-with-Differential-Equations) \n", " - [Overview](#Overview) \n", " - [The SEIR Model](#The-SEIR-Model) \n", " - [Implementation](#Implementation) \n", " - [Experiments](#Experiments) \n", " - [Ending Lockdown](#Ending-Lockdown) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "\n", "Coauthored with Chris Rackauckas\n", "\n", "This is a Julia version of code for analyzing the COVID-19 pandemic.\n", "\n", "The purpose of these notes is to introduce economists to quantitative modeling of infectious disease dynamics, and to modeling with ordinary differential\n", "equations.\n", "\n", "In this lecture, dynamics are modeled using a standard SEIR (Susceptible-Exposed-Infected-Removed) model\n", "of disease spread, represented as a system of ordinary differential\n", "equations where the number of agents is large and there are no exogenous stochastic shocks.\n", "\n", "The first part of the model is inspired by\n", "* Notes from [Andrew Atkeson](https://sites.google.com/site/andyatkeson/) and [NBER Working Paper No. 26867](https://www.nber.org/papers/w26867)\n", "* [Estimating and Forecasting Disease Scenarios for COVID-19 with an SIR Model](https://www.nber.org/papers/w27335) by Andrew Atkeson, Karen Kopecky and Tao Zha\n", "* [Estimating and Simulating a SIRD Model of COVID-19 for Many Countries, States, and Cities](https://www.nber.org/papers/w27128) by Jesús Fernández-Villaverde and Charles I. Jones\n", "* Further variations on the classic SIR model in Julia [here](https://github.com/epirecipes/sir-julia).\n", "\n", "We then extend this deterministic model in [this lecture](covid_sde.html) which build on this model, adding in aggregate shocks and policy tradeoffs.\n", "\n", "The interest is primarily in\n", "\n", "- studying the impact of suppression through social distancing on the spread of the infection \n", "- the number of infections at a given time (which determines whether or not the health care system is overwhelmed); and \n", "- how long the caseload can be deferred (hopefully until a vaccine arrives) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "hide-output": true }, "outputs": [], "source": [ "using InstantiateFromURL\n", "# optionally add arguments to force installation: instantiate = true, precompile = true\n", "github_project(\"QuantEcon/quantecon-notebooks-julia\", version = \"0.8.0\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "hide-output": false }, "outputs": [], "source": [ "using LinearAlgebra, Statistics, Random, SparseArrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition, we will be exploring the [Ordinary Differential Equations](https://diffeq.sciml.ai/dev/tutorials/ode_example/) package within the [SciML ecosystem](https://github.com/SciML/)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "hide-output": false }, "outputs": [], "source": [ "using OrdinaryDiffEq\n", "using Parameters, Plots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The SEIR Model\n", "\n", "In the version of the SEIR model, all individuals in the population are assumed to be in a finite number of states.\n", "\n", "The states are: susceptible (S), exposed (E), infected (I) and removed (R).\n", "\n", "This type of [compartmentalized model](https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology#Elaborations_on_the_basic_SIR_model) has many extensions (e.g. SEIRS relaxes lifetime immunity and allow transitions from $ R \\to S $).\n", "\n", "Comments:\n", "\n", "- Those in state R have been infected and either recovered or died. Note that in some variations, R may refer only to recovered agents. \n", "- Those who have recovered, and live, are assumed to have acquired immunity. \n", "- Those in the exposed group are not yet infectious. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Changes in the Infected State\n", "\n", "Within the SEIR model, the flow across states follows the path $ S \\to E \\to I \\to R $.\n", "\n", "We will ignore birth and non-covid death during our time horizon, and assume a large, constant, number of individuals of size $ N $ throughout.\n", "\n", "With this, the symbols $ S, E, I, R $ are used for the total number of individuals in each state at each point in time, and $ S(t) + E(t) + I(t) + R(t) = N $ for all $ t $.\n", "\n", "Since we have assumed that $ N $ is large, we can use a continuum approximation for the number of individuals in each state.\n", "\n", "The transitions between those states are governed by the following rates\n", "\n", "- $ \\beta(t) $ is called the *transmission rate* or *effective contact rate* (the rate at which individuals bump into others and expose them to the virus). \n", "- $ \\sigma $ is called the *infection rate* (the rate at which those who are exposed become infected) \n", "- $ \\gamma $ is called the *recovery rate* (the rate at which infected people recover or die) \n", "\n", "\n", "The rate $ \\beta(t) $ is influenced by both the characteristics of the disease (e.g. the type and length of prolonged contact required for a transmission) and behavior of the individuals (e.g. social distancing, hygiene).\n", "\n", "The SEIR model can then be written as\n", "\n", "\n", "\n", "$$\n", "\\begin{aligned}\n", " \\frac{d S}{d t} & = - \\beta \\, S \\, \\frac{I}{N}\n", " \\\\\n", " \\frac{d E}{d t} & = \\beta \\, S \\, \\frac{I}{N} - \\sigma E\n", " \\\\\n", " \\frac{d I}{d t} & = \\sigma E - \\gamma I\n", " \\\\\n", " \\frac{d R}{d t} & = \\gamma I\n", "\\end{aligned} \\tag{1}\n", "$$\n", "\n", "Here, $ dy/dt $ represents the time derivative for the particular variable.\n", "\n", "The first term of [(1)](#equation-seir-system-big), $ -\\beta \\, S \\, \\frac{I}{N} $, is the flow of individuals moving from $ S \\to E $, and highlights the underlying dynamics of the epidemic\n", "\n", "- Individuals in the susceptible state (S) have a rate $ \\beta(t) $ of prolonged contacts with other individuals where transmission would occur if either was infected \n", "- Of these contacts, a fraction $ \\frac{I(t)}{N} $ will be with infected agents (since we assumed that exposed individuals are not yet infectious) \n", "- Finally, there are $ S(t) $ susceptible individuals. \n", "- The sign indicates that the product of those terms is the outflow from the $ S $ state, and an inflow to the $ E $ state. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Reproduction Number\n", "\n", "If $ \\beta $ was constant, then we could define $ R_0 := \\beta / \\gamma $. This is the famous *basic reproduction number* for the SEIR model. See [[HSW05]](../zreferences.html#heffernan2005perspectives) for more details.\n", "\n", "When the transmission rate is time-varying, we will follow notation in [[FVJ20]](../zreferences.html#nberw27128) and refer to $ R_0(t) $ as a time-varying version of the basic reproduction number.\n", "\n", "Analyzing the system in [(1)](#equation-seir-system-big) provides some intuition on the $ R_0(t) := \\beta(t) / \\gamma $ expression:\n", "\n", "- Individual transitions from the infected to removed state occur at a Poisson rate $ \\gamma $, the expected time in the infected state is $ 1/\\gamma $ \n", "- Prolonged interactions occur at rate $ \\beta $, so a new individual entering the infected state will potentially transmit the virus to an average of $ R_0 = \\beta \\times 1 / \\gamma $ others \n", "- In more complicated models, see [[HSW05]](../zreferences.html#heffernan2005perspectives) for a formal definition for arbitrary models, and an analysis on the role of $ R_0 < 1 $. \n", "\n", "\n", "Note that the notation $ R_0 $ is standard in the epidemiology literature - though confusing, since $ R_0 $ is unrelated to $ R $, the symbol that represents the removed state. For the remainder of the lecture, we will avoid using $ R $ for removed state.\n", "\n", "Prior to solving the model directly, we make a few changes to [(1)](#equation-seir-system-big)\n", "\n", "- Re-parameterize using $ \\beta(t) = \\gamma R_0(t) $ \n", "- Define the proportion of individuals in each state as $ s := S/N $ etc. \n", "- Divide each equation in [(1)](#equation-seir-system-big) by $ N $, and write the system of ODEs in terms of the proportions \n", "\n", "\n", "\n", "\n", "$$\n", "\\begin{aligned}\n", " \\frac{d s}{d t} & = - \\gamma \\, R_0 \\, s \\, i\n", " \\\\\n", " \\frac{d e}{d t} & = \\gamma \\, R_0 \\, s \\, i - \\sigma e\n", " \\\\\n", " \\frac{d i}{d t} & = \\sigma e - \\gamma i\n", " \\\\\n", " \\frac{d r}{d t} & = \\gamma i\n", "\\end{aligned} \\tag{2}\n", "$$\n", "\n", "Since the states form a partition, we could reconstruct the “removed” fraction of the population as $ r = 1 - s - e - i $. However, keeping it in the system will make plotting more convenient." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Implementation\n", "\n", "We begin by implementing a simple version of this model with a constant $ R_0 $ and some baseline parameter values (which we discuss later).\n", "\n", "First, define the system of equations" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "F_simple (generic function with 1 method)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function F_simple(x, p, t; γ = 1/18, R₀ = 3.0, σ = 1/5.2)\n", " s, e, i, r = x\n", "\n", " return [-γ*R₀*s*i; # ds/dt = -γR₀si\n", " γ*R₀*s*i - σ*e;# de/dt = γR₀si -σe\n", " σ*e - γ*i; # di/dt = σe -γi\n", " γ*i; # dr/dt = γi\n", " ]\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given this system, we choose an initial condition and a timespan, and create a `ODEProblem` encapsulating the system." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mODEProblem\u001b[0m with uType \u001b[36mArray{Float64,1}\u001b[0m and tType \u001b[36mFloat64\u001b[0m. In-place: \u001b[36mfalse\u001b[0m\n", "timespan: (0.0, 350.0)\n", "u0: [0.9999995, 4.0e-7, 1.0e-7, 0.0]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i_0 = 1E-7 # 33 = 1E-7 * 330 million population = initially infected\n", "e_0 = 4.0 * i_0 # 132 = 1E-7 *330 million = initially exposed\n", "s_0 = 1.0 - i_0 - e_0\n", "r_0 = 0.0\n", "x_0 = [s_0, e_0, i_0, r_0] # initial condition\n", "\n", "tspan = (0.0, 350.0) # ≈ 350 days\n", "prob = ODEProblem(F_simple, x_0, tspan)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With this, choose an ODE algorithm and solve the initial value problem. A good default algorithm for non-stiff ODEs of this sort might be `Tsit5()`, which is the Tsitouras 5/4 Runge-Kutta method)." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "hide-output": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol = solve(prob, Tsit5())\n", "plot(sol, labels = [\"s\" \"e\" \"i\" \"r\"], title = \"SEIR Dynamics\", lw = 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We did not provide either a set of time steps or a `dt` time step size to the `solve`. Most accurate and high-performance ODE solvers appropriate for this problem use adaptive time-stepping, changing the step size based the degree of curvature in the derivatives.\n", "\n", "Or, as an alternative visualization, the proportions in each state over time" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "hide-output": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "areaplot(sol.t, sol', labels = [\"s\" \"e\" \"i\" \"r\"], title = \"SEIR Proportions\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While maintaining the core system of ODEs in $ (s, e, i, r) $, we will extend the basic model to enable some policy experiments and calculations of aggregate values." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Extending the Model\n", "\n", "First, we can consider some additional calculations such as the cumulative caseload (i.e., all those who have or have had the infection) as $ c = i + r $. Differentiating that expression and substituting from the time-derivatives of $ i(t) $ and $ r(t) $ yields $ \\frac{d c}{d t} = \\sigma e $.\n", "\n", "We will assume that the transmission rate follows a process with a reversion to a value $ \\bar{R}_0(t) $ which could conceivably be influenced by policy. The intuition is that even if the targeted $ \\bar{R}_0(t) $ was changed through social distancing/etc., lags in behavior and implementation would smooth out the transition, where $ \\eta $ governs the speed of $ R_0(t) $ moves towards $ \\bar{R}_0(t) $.\n", "\n", "\n", "\n", "$$\n", "\\begin{aligned}\n", "\\frac{d R_0}{d t} &= \\eta (\\bar{R}_0 - R_0)\\\\\n", "\\end{aligned} \\tag{3}\n", "$$\n", "\n", "Finally, let $ \\delta $ be the mortality rate, which we will leave constant. The cumulative deaths can be integrated through the flow $ \\gamma i $ entering the “Removed” state.\n", "\n", "Define the cumulative number of deaths as $ D(t) $ with the proportion $ d(t) := D(t)/N $.\n", "\n", "\n", "\n", "$$\n", "\\begin{aligned}\\\\\n", "\\frac{d}{d t}d(t) &= \\delta \\gamma i\n", "\\end{aligned} \\tag{4}\n", "$$\n", "\n", "While we could integrate the deaths given the solution to the model ex-post, it is more convenient to use the integrator built into the ODE solver. That is, we add $ \\frac{d}{dt} d(t) $ rather than calculating $ d(t) = \\int_0^t \\delta \\gamma\\, i(\\tau) d \\tau $ ex-post.\n", "\n", "This is a common trick when solving systems of ODEs. While equivalent in principle to using the appropriate quadrature scheme, this becomes especially convenient when adaptive time-stepping algorithms are used to solve the ODEs (i.e. there is not a regular time grid). Note that when doing so, $ d(0) = \\int_0^0 \\delta \\gamma i(\\tau) d \\tau = 0 $ is the initial condition.\n", "\n", "The system [(2)](#equation-seir-system) and the supplemental equations can be written in vector form $ x := [s, e, i, r, R₀, c, d] $ with parameter tuple $ p := (\\sigma, \\gamma, \\eta, \\delta, \\bar{R}_0(\\cdot)) $\n", "\n", "Note that in those parameters, the targeted reproduction number, $ \\bar{R}_0(t) $, is an exogenous function.\n", "\n", "The model is then $ \\frac{d x}{d t} = F(x,t) $ where,\n", "\n", "\n", "\n", "$$\n", "F(x,t) := \\begin{bmatrix}\n", " -\\gamma \\, R_0 \\, s \\, i\n", " \\\\\n", " \\gamma \\, R_0 \\, s \\, i - \\sigma e\n", " \\\\\n", " \\sigma \\, e - \\gamma i\n", " \\\\\n", " \\gamma i\n", " \\\\\n", " \\eta (\\bar{R}_0(t) - R_0)\n", " \\\\\n", " \\sigma e\n", " \\\\\n", " \\delta \\, \\gamma \\, i\n", "\\end{bmatrix} \\tag{5}\n", "$$\n", "\n", "Note that if $ \\bar{R}_0(t) $ is time-invariant, then $ F(x, t) $ is time-invariant as well." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Parameters\n", "\n", "The parameters, $ \\sigma, \\delta, $ and $ \\gamma $ should be thought of as parameters determined from biology and medical technology, and independent of social interactions.\n", "\n", "As in Atkeson’s note, we set\n", "\n", "- $ \\sigma = 1/5.2 $ to reflect an average incubation period of 5.2 days. \n", "- $ \\gamma = 1/18 $ to match an average illness duration of 18 days. \n", "- $ \\bar{R}_0(t) = R_0(0) = 1.6 $ to match a **basic reproduction number** of 1.6, and initially time-invariant \n", "- $ \\delta = 0.01 $ for a one-percent mortality rate \n", "\n", "\n", "As we will initially consider the case where $ R_0(0) = \\bar{R}_0(0) $, the parameter $ \\eta $ will not influence the first experiment." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Implementation\n", "\n", "First, construct our $ F $ from [(5)](#equation-dfcv)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "hide-output": false }, "outputs": [], "source": [ "function F(x, p, t)\n", " s, e, i, r, R₀, c, d = x\n", " @unpack σ, γ, R̄₀, η, δ = p\n", "\n", " return [-γ*R₀*s*i; # ds/dt\n", " γ*R₀*s*i - σ*e; # de/dt\n", " σ*e - γ*i; # di/dt\n", " γ*i; # dr/dt\n", " η*(R̄₀(t, p) - R₀);# dR₀/dt\n", " σ*e; # dc/dt\n", " δ*γ*i; # dd/dt\n", " ]\n", "end;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function takes the vector `x` of states in the system and extracts the fixed parameters passed into the `p` object.\n", "\n", "The only confusing part of the notation is the `R̄₀(t, p)` which evaluates the `p.R̄₀` at this time (and also allows it to depend on the `p` parameter)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Parameters\n", "\n", "The baseline parameters are put into a named tuple generator (see previous lectures using [Parameters.jl](https://github.com/mauro3/Parameters.jl)) with default values discussed above." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "hide-output": false }, "outputs": [], "source": [ "p_gen = @with_kw ( T = 550.0, γ = 1.0 / 18, σ = 1 / 5.2, η = 1.0 / 20,\n", " R₀_n = 1.6, δ = 0.01, N = 3.3E8,\n", " R̄₀ = (t, p) -> p.R₀_n);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the default $ \\bar{R}_0(t) $ function always equals $ R_{0n} $ – a parameterizable natural level of $ R_0 $ used only by the `R̄₀` function\n", "\n", "Setting initial conditions, we choose a fixed $ s, i, e, r $, as well as $ R_0(0) = R_{0n} $ and $ m(0) = 0.01 $" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mODEProblem\u001b[0m with uType \u001b[36mArray{Float64,1}\u001b[0m and tType \u001b[36mFloat64\u001b[0m. In-place: \u001b[36mfalse\u001b[0m\n", "timespan: (0.0, 550.0)\n", "u0: [0.9999995, 4.0e-7, 1.0e-7, 0.0, 1.6, 0.0, 0.0]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = p_gen() # use all default parameters\n", "\n", "i_0 = 1E-7\n", "e_0 = 4.0 * i_0\n", "s_0 = 1.0 - i_0 - e_0\n", "\n", "x_0 = [s_0, e_0, i_0, 0.0, p.R₀_n, 0.0, 0.0]\n", "tspan = (0.0, p.T)\n", "prob = ODEProblem(F, x_0, tspan, p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `tspan` of `(0.0, p.T)` determines the $ t $ used by the solver. The time scale needs to be consistent with the arrival\n", "rate of the transition probabilities (i.e. the $ \\gamma, \\sigma $ were chosen based on daily data, so the unit of $ t $ is a day).\n", "\n", "The time period we investigate will be 550 days, or around 18 months:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Experiments\n", "\n", "Let’s run some experiments using this code." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "length(sol.t) = 45\n" ] } ], "source": [ "sol = solve(prob, Tsit5())\n", "@show length(sol.t);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that the adaptive time-stepping used approximately 45 time-steps to solve this problem to the desired accuracy. Evaluating the solver at points outside of those time-steps uses an interpolator consistent with the solution to the ODE.\n", "\n", "While it may seem that 45 time intervals is extremely small for that range, for much of the $ t $, the functions are very flat - and hence adaptive time-stepping algorithms can move quickly and interpolate accurately.\n", "\n", "The solution object has [built in](https://docs.sciml.ai/stable/basics/plot/) plotting support." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "hide-output": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot(sol, vars = [6, 7], label = [\"c(t)\" \"d(t)\"], lw = 2,\n", " title = [\"Cumulative Infected\" \"Death Proportion\"],\n", " layout = (1,2), size = (900, 300))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A few more comments:\n", "\n", "- If you want to ensure that there are specific points that the adaptive-time stepping must include (e.g. at known discontinuities) use [tstops](https://diffeq.sciml.ai/stable/basics/common_solver_opts/#Output-Control-1). \n", "- The built-in plots for the solutions provide all of the [attributes](https://docs.juliaplots.org/latest/tutorial/) in [Plots.jl](https://github.com/JuliaPlots/Plots.jl). \n", "- See [here](https://docs.sciml.ai/stable/basics/solution/) for details on analyzing the solution and extracting the output. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Experiment 1: Constant Reproduction Case\n", "\n", "Let’s start with the case where $ \\bar{R}_0(t) = R_{0n} $ is constant.\n", "\n", "We calculate the time path of infected people under different assumptions of $ R_{0n} $:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "hide-output": false }, "outputs": [], "source": [ "R₀_n_vals = range(1.6, 3.0, length = 6)\n", "sols = [solve(ODEProblem(F, x_0, tspan, p_gen(R₀_n = R₀_n)),\n", " Tsit5(), saveat=0.5) for R₀_n in R₀_n_vals];" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we chose `saveat=0.5` to get solutions that were evenly spaced every `0.5`.\n", "\n", "Changing the saved points is just a question of storage/interpolation, and does not change the adaptive time-stepping of the solvers.\n", "\n", "Let’s plot current cases as a fraction of the population." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "hide-output": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "labels = permutedims([\"R_0 = $r\" for r in R₀_n_vals])\n", "infecteds = [sol[3,:] for sol in sols]\n", "plot(infecteds, label=labels, legend=:topleft, lw = 2, xlabel = \"t\",\n", " ylabel = \"i(t)\", title = \"Current Cases\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As expected, lower effective transmission rates defer the peak of infections.\n", "\n", "They also lead to a lower peak in current cases.\n", "\n", "Here is cumulative cases, as a fraction of population:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "hide-output": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cumulative_infected = [sol[6,:] for sol in sols]\n", "plot(cumulative_infected, label=labels ,legend=:topleft, lw = 2, xlabel = \"t\",\n", " ylabel = \"c(t)\", title = \"Cumulative Cases\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Experiment 2: Changing Mitigation\n", "\n", "Let’s look at a scenario where mitigation (e.g., social distancing) is\n", "successively imposed, but the target (maintaining $ R_{0n} $) is fixed.\n", "\n", "To do this, we start with $ R_0(0) \\neq R_{0n} $ and examine the dynamics using the $ \\frac{d R_0}{d t} = \\eta (R_{0n} - R_0) $ ODE.\n", "\n", "In the simple case, where $ \\bar{R}_0(t) = R_{0n} $ is independent of the state, the solution to the ODE given an initial condition is $ R_0(t) = R_0(0) e^{-\\eta t} + R_{0n}(1 - e^{-\\eta t}) $\n", "\n", "We will examine the case where $ R_0(0) = 3 $ and then it falls to $ R_{0n} = 1.6 $ due to the progressive adoption of stricter mitigation measures.\n", "\n", "The parameter `η` controls the rate, or the speed at which restrictions are\n", "imposed.\n", "\n", "We consider several different rates:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "hide-output": false }, "outputs": [], "source": [ "η_vals = [1/5, 1/10, 1/20, 1/50, 1/100]\n", "labels = permutedims([\"eta = $η\" for η in η_vals]);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let’s calculate the time path of infected people, current cases, and mortality" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "hide-output": false }, "outputs": [], "source": [ "x_0 = [s_0, e_0, i_0, 0.0, 3.0, 0.0, 0.0]\n", "sols = [solve(ODEProblem(F, x_0, tspan, p_gen(η=η)), Tsit5(), saveat=0.5) for η in η_vals];" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, plot the $ R_0 $ over time:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "hide-output": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Rs = [sol[5,:] for sol in sols]\n", "plot(Rs, label=labels, legend=:topright, lw = 2, xlabel = \"t\",\n", " ylabel = \"R_0(t)\", title = \"Basic Reproduction Number\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let’s plot the number of infected persons and the cumulative number\n", "of infected persons:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "hide-output": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "infecteds = [sol[3,:] for sol in sols]\n", "plot(infecteds, label=labels, legend=:topleft, lw = 2, xlabel = \"t\",\n", " ylabel = \"i(t)\", title = \"Current Infected\")" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "hide-output": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cumulative_infected = [sol[6,:] for sol in sols]\n", "plot(cumulative_infected, label=labels ,legend=:topleft, lw = 2, xlabel = \"t\",\n", " ylabel = \"c(t)\", title = \"Cumulative Infected\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ending Lockdown\n", "\n", "The following is inspired by [additional results](https://drive.google.com/file/d/1uS7n-7zq5gfSgrL3S0HByExmpq4Bn3oh/view) by Andrew Atkeson on the timing of lifting lockdown.\n", "\n", "Consider these two mitigation scenarios:\n", "\n", "1. choose $ \\bar{R}_0(t) $ to target $ R_0(t) = 0.5 $ for 30 days and then $ R_0(t) = 2 $ for the remaining 17 months. This corresponds to lifting lockdown in 30 days. \n", "1. $ R_0(t) = 0.5 $ for 120 days and then $ R_0(t) = 2 $ for the remaining 14 months. This corresponds to lifting lockdown in 4 months. \n", "\n", "\n", "For both of these, we will choose a large $ \\eta $ to focus on the case where rapid changes in the lockdown policy remain feasible.\n", "\n", "The parameters considered here start the model with 25,000 active infections\n", "and 75,000 agents already exposed to the virus and thus soon to be contagious." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mODEProblem\u001b[0m with uType \u001b[36mArray{Float64,1}\u001b[0m and tType \u001b[36mFloat64\u001b[0m. In-place: \u001b[36mfalse\u001b[0m\n", "timespan: (0.0, 550.0)\n", "u0: [0.9996969696969696, 0.00022727272727272727, 7.575757575757576e-5, 0.0, 0.5, 0.0, 0.0]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R₀_L = 0.5 # lockdown\n", "R̄₀_lift_early(t, p) = t < 30.0 ? R₀_L : 2.0\n", "R̄₀_lift_late(t, p) = t < 120.0 ? R₀_L : 2.0\n", "p_early = p_gen(R̄₀= R̄₀_lift_early, η = 10.0)\n", "p_late = p_gen(R̄₀= R̄₀_lift_late, η = 10.0)\n", "\n", "\n", "# initial conditions\n", "i_0 = 25000 / p_early.N\n", "e_0 = 75000 / p_early.N\n", "s_0 = 1.0 - i_0 - e_0\n", "\n", "x_0 = [s_0, e_0, i_0, 0.0, R₀_L, 0.0, 0.0] # start in lockdown\n", "\n", "# create two problems, with rapid movement of R₀(t) towards R̄₀(t)\n", "prob_early = ODEProblem(F, x_0, tspan, p_early)\n", "prob_late = ODEProblem(F, x_0, tspan, p_late)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unlike the previous examples, the $ \\bar{R}_0(t) $ functions have discontinuities which might occur. We can improve the efficiency of the adaptive time-stepping methods by telling them to include a step exactly at those points by using `tstops`\n", "\n", "Let’s calculate the paths:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "hide-output": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol_early = solve(prob_early, Tsit5(), tstops = [30.0, 120.0])\n", "sol_late = solve(prob_late, Tsit5(), tstops = [30.0, 120.0])\n", "plot(sol_early, vars = [7], title = \"Total Mortality\", label = \"Lift Early\", legend = :topleft)\n", "plot!(sol_late, vars = [7], label = \"Lift Late\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we examine the daily deaths, $ \\frac{d D(t)}{dt} = N \\delta \\gamma i(t) $." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "hide-output": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "flow_deaths(sol, p) = p.N * p.δ * p.γ * sol[3,:]\n", "\n", "plot(sol_early.t, flow_deaths(sol_early, p_early), title = \"Flow Deaths\", label = \"Lift Early\")\n", "plot!(sol_late.t, flow_deaths(sol_late, p_late), label = \"Lift Late\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pushing the peak of curve further into the future may reduce cumulative deaths\n", "if a vaccine is found, or allow health authorities to better smooth the caseload." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Randomness\n", "\n", "Despite its richness, the model above is fully deterministic. The policy $ \\bar{R}_0(t) $ could change over time, but only in predictable ways.\n", "\n", "One way that randomness can lead to aggregate fluctuations is the granularity that comes through the discreteness of individuals. This topic, the connection between SDEs and the Langevin equations typically used in the approximation of chemical reactions in well-mixed media is explored in further lectures on continuous time Markov chains.\n", "\n", "Instead, in the [next lecture](covid_sde.html), we will concentrate on randomness that comes from aggregate changes in behavior or policy." ] } ], "metadata": { "date": 1595535545.3873255, "download_nb": 1, "download_nb_path": "https://julia.quantecon.org/", "filename": "seir_model.rst", "filename_with_path": "continuous_time/seir_model", "kernelspec": { "display_name": "Julia 1.4.2", "language": "julia", "name": "julia-1.4" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.4.2" }, "next_doc": { "link": "covid_sde", "title": "Modeling Shocks in COVID 19 with Stochastic Differential Equations" }, "prev_doc": { "link": "index", "title": "Modeling in Continuous Time" }, "site_title": "Quantitative Economics with Julia", "title": "Modeling COVID 19 with Differential Equations" }, "nbformat": 4, "nbformat_minor": 2 }