{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "
\n", " \n", " \"QuantEcon\"\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Modeling Shocks in COVID 19 with Stochastic Differential Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contents\n", "\n", "- [Modeling Shocks in COVID 19 with Stochastic Differential Equations](#Modeling-Shocks-in-COVID-19-with-Stochastic-Differential-Equations) \n", " - [Overview](#Overview) \n", " - [The Basic SIR/SIRD Model](#The-Basic-SIR/SIRD-Model) \n", " - [Introduction to SDEs](#Introduction-to-SDEs) \n", " - [Ending Lockdown](#Ending-Lockdown) \n", " - [Reinfection](#Reinfection) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "\n", "Coauthored with Chris Rackauckas\n", "\n", "This lecture continues the analyzing of the COVID-19 pandemic established in [this lecture](seir_model.html).\n", "\n", "As before, 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", "Here we extend the model to include policy-relevant aggregate shocks." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Continuous-Time Stochastic Processes\n", "\n", "In continuous-time, there is an important distinction between randomness that leads to continuous paths vs. those which have ([almost surely right-continuous](https://en.wikipedia.org/wiki/C%C3%A0dl%C3%A0g)) jumps in their paths. The most tractable of these includes the theory of [Levy Processes](https://en.wikipedia.org/wiki/L%C3%A9vy_process).\n", "\n", "Among the appealing features of Levy Processes is that they fit well into the sorts of Markov modeling techniques that economists tend to use in discrete time, and usually fulfill the measurability required for calculating expected present discounted values.\n", "\n", "Unlike in discrete-time, where a modeller has license to be creative, the rules of continuous-time stochastic processes are much stricter. In practice, there are only two types of Levy Processes that can be used without careful measure theory.\n", "\n", "1. [Weiner Processes](https://en.wikipedia.org/wiki/Wiener_process) (as known as Brownian Motion) which leads to a diffusion equations, and is the only continuous-time Levy process with continuous paths \n", "1. [Poisson Processes](https://en.wikipedia.org/wiki/Poisson_point_process) with an arrival rate of jumps in the variable. \n", "\n", "\n", "Every other Levy Process can be represented by these building blocks (e.g. a [Diffusion Process](https://en.wikipedia.org/wiki/Diffusion_process) such as Geometric Brownian Motion is a transformation of a Weiner process, a [jump diffusion](https://en.wikipedia.org/wiki/Jump_diffusion#In_economics_and_finance) is a diffusion process with a Poisson arrival of jumps, and a continuous-time markov chain (CMTC) is a Poisson process jumping between a finite number of states).\n", "\n", "In this lecture, we will examine shocks driven by transformations of Brownian motion, as the prototypical Stochastic Differential Equation (SDE)." ] }, { "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 packages within the [SciML ecosystem](https://github.com/SciML/) and\n", "others covered in previous lectures" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "hide-output": false }, "outputs": [], "source": [ "using OrdinaryDiffEq, StochasticDiffEq\n", "using Parameters, Plots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Basic SIR/SIRD Model\n", "\n", "To demonstrate another common [compartmentalized model](https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology#Elaborations_on_the_basic_SIR_model) we will change the [previous SEIR](seir_model.html) model to remove the exposed state, and more carefully manage the death state, D.\n", "\n", "The states are are now: susceptible (S), infected (I), resistant (R), or dead (D).\n", "\n", "Comments:\n", "\n", "- Unlike the previous SEIR model, the R state is only for those recovered, alive, and currently resistant. \n", "- As before, we start by assuming those have recovered have acquired immunity. \n", "- Later, we could consider transitions from R to S if resistance is not permanent due to virus mutation, etc. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Transition Rates\n", "\n", "See the [previous lecture](seir_model.html), for a more detailed development of the model.\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", "- $ \\gamma $ is called the *resolution rate* (the rate at which infected people recover or die) \n", "- $ \\delta(t) \\in [0, 1] $ is the *death probability* \n", "- As before, we re-parameterize as $ R_0(t) := \\beta(t) / \\gamma $, where $ R_0 $ has previous interpretation \n", "\n", "\n", "Jumping directly to the equations in $ s, i, r, d $ already normalized by $ N $,\n", "\n", "\n", "\n", "$$\n", "\\begin{aligned}\n", " d s & = - \\gamma \\, R_0 \\, s \\, i \\, dt\n", " \\\\\n", " d i & = \\left(\\gamma \\, R_0 \\, s \\, i - \\gamma \\, i \\right) dt\n", " \\\\\n", " d r & = (1-\\delta) \\gamma \\, i \\, dt\n", " \\\\\n", " d d & = \\delta \\, \\gamma \\, i \\, dt\n", " \\\\\n", "\\end{aligned} \\tag{1}\n", "$$\n", "\n", "Note that the notation has changed to heuristically put the $ dt $ on the right hand side, which will be used when adding the stochastic shocks." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction to SDEs\n", "\n", "We start by extending our model to include randomness in $ R_0(t) $ and then the mortality rate $ \\delta(t) $.\n", "\n", "The result is a system of Stochastic Differential Equations (SDEs)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Shocks to Transmission Rates\n", "\n", "As before, we assume that the basic reproduction number, $ R_0(t) $, 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", "Beyond changes in policy, randomness in $ R_0(t) $ may come from shocks to the $ \\beta(t) $ process. For example,\n", "\n", "- Misinformation on Facebook spreading non-uniformly. \n", "- Large political rallies, elections, or protests. \n", "- Deviations in the implementation and timing of lockdown policy between demographics, locations, or businesses within the system. \n", "- Aggregate shocks in opening/closing industries. \n", "\n", "\n", "To implement these sorts of randomness, we will add on a diffusion term with an instantaneous volatility of $ \\sigma \\sqrt{R_0} $.\n", "\n", "- This equation is used in the [Cox-Ingersoll-Ross](https://en.wikipedia.org/wiki/Cox%E2%80%93Ingersoll%E2%80%93Ross_model) and [Heston](https://en.wikipedia.org/wiki/Heston_model) models of interest rates and stochastic volatility. \n", "- The scaling by the $ \\sqrt{R_0} $ ensure that the process stays weakly positive. The heuristic explanation is that the variance of the shocks converges to zero as R₀ goes to zero, enabling the upwards drift to dominate. \n", "- See [here](https://en.wikipedia.org/wiki/Cox%E2%80%93Ingersoll%E2%80%93Ross_model#Distribution) for a heuristic description of when the process is weakly and strictly positive. \n", "\n", "\n", "The notation for this [SDE](https://en.wikipedia.org/wiki/Stochastic_differential_equation#Use_in_probability_and_mathematical_finance) is then\n", "\n", "\n", "\n", "$$\n", "\\begin{aligned}\n", "d R_{0t} &= \\eta (\\bar{R}_{0t} - R_{0t}) dt + \\sigma \\sqrt{R_{0t}} dW_t\\\\\n", "\\end{aligned} \\tag{2}\n", "$$\n", "\n", "where $ W $ is standard Brownian motion (i.e a [Weiner Process](https://en.wikipedia.org/wiki/Wiener_process).\n", "\n", "Heuristically, if $ \\sigma = 0 $, divide this equation by $ dt $ and it nests the original ODE used in the previous lecture.\n", "\n", "While we do not consider any calibration for the $ \\sigma $ parameter, empirical studies such as [Estimating and Simulating a SIRD Model of COVID-19 for Many Countries, States, and Cities](https://www.nber.org/papers/w27128) (Figure 6) show highly volatile $ R_0(t) $ estimates over time.\n", "\n", "Even after lockdowns are first implemented, we see variation between 0.5 and 1.5. Since countries are made of interconnecting cities with such variable contact rates, a high $ \\sigma $ seems reasonable both intuitively and empirically." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mortality Rates\n", "\n", "Unlike the previous lecture, we will build up towards mortality rates which change over time.\n", "\n", "Imperfect mixing of different demographic groups could lead to aggregate shocks in mortality (e.g. if a retirement home is afflicted vs. an elementary school). These sorts of relatively small changes might be best modeled as a continuous path process.\n", "\n", "Let $ \\delta(t) $ be the mortality rate and in addition,\n", "\n", "- Assume that the base mortality rate is $ \\bar{\\delta} $, which acts as the mean of the process, reverting at rate $ \\theta $. In more elaborate models, this could be time-varying. \n", "- The diffusion term has a volatility $ \\xi\\sqrt{\\delta (1 - \\delta)} $. \n", "- As the process gets closer to either $ \\delta = 1 $ or $ \\delta = 0 $, the volatility goes to 0, which acts as a force to allow the mean reversion to keep the process within the bounds \n", "- Unlike the well-studied Cox-Ingersoll-Ross model, we make no claims on the long-run behavior of this process, but will be examining the behavior on a small timescale so this is not an issue. \n", "\n", "\n", "Given this, the stochastic process for the mortality rate is,\n", "\n", "\n", "\n", "$$\n", "\\begin{aligned}\n", "d \\delta_t & = \\theta (\\bar{\\delta} - \\delta_t) dt + \\xi \\sqrt{(\\delta_t (1-\\delta_t)} d W_t\\\\\n", "\\end{aligned} \\tag{3}\n", "$$\n", "\n", "Where the $ W_t $ Brownian motion is independent from the previous process." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### System of SDEs\n", "\n", "The system [(1)](#equation-sird) can be written in vector form $ x := [s, i, r, d, R₀, \\delta] $ with parameter tuple parameter tuple $ p := (\\gamma, \\eta, \\sigma, \\theta, \\xi, \\bar{R}_0(\\cdot), \\bar{ \\delta}) $\n", "\n", "The general form of the SDE is.\n", "\n", "$$\n", "\\begin{aligned}\n", "d x_t &= F(x_t,t;p)dt + G(x_t,t;p) dW\n", "\\end{aligned}\n", "$$\n", "\n", "With the drift,\n", "\n", "\n", "\n", "$$\n", "F(x,t;p) := \\begin{bmatrix}\n", " -\\gamma \\, R_0 \\, s \\, i\n", " \\\\\n", " \\gamma \\, R_0 \\, s \\, i - \\gamma i\n", " \\\\\n", " (1-\\delta) \\gamma i\n", " \\\\\n", " \\delta \\gamma i\n", " \\\\\n", " \\eta (\\bar{R}_0(t) - R_0)\n", " \\\\\n", " \\theta (\\bar{\\delta} - \\delta)\n", " \\\\\n", "\\end{bmatrix} \\tag{4}\n", "$$\n", "\n", "Here, it is convenient but not necessary for $ d W $ to have the same dimension as $ x $. If so, then we can use a square matrix $ G(x,t;p) $ to associate the shocks with the appropriate $ x $ (e.g. diagonal noise, or using a covariance matrix).\n", "\n", "As the two independent sources of Brownian motion only affect the $ d R_0 $ and $ d \\delta $ terms (i.e. the 5th and 6th equations), define the covariance matrix as\n", "\n", "\n", "\n", "$$\n", "\\begin{aligned}\n", "G(x, t) &:= diag\\left(\\begin{bmatrix} 0 & 0 & 0 & 0 & \\sigma \\sqrt{R_0} & \\xi \\sqrt{\\delta (1-\\delta)} \\end{bmatrix}\\right)\n", "\\end{aligned} \\tag{5}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Implementation\n", "\n", "First, construct our $ F $ from [(4)](#equation-dfcvsde) and $ G $ from [(5)](#equation-dg)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "G (generic function with 1 method)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function F(x, p, t)\n", " s, i, r, d, R₀, δ = x\n", " @unpack γ, R̄₀, η, σ, ξ, θ, δ_bar = p\n", "\n", " return [-γ*R₀*s*i; # ds/dt\n", " γ*R₀*s*i - γ*i; # di/dt\n", " (1-δ)*γ*i; # dr/dt\n", " δ*γ*i; # dd/dt\n", " η*(R̄₀(t, p) - R₀);# dR₀/dt\n", " θ*(δ_bar - δ); # dδ/dt\n", " ]\n", "end\n", "\n", "function G(x, p, t)\n", " s, i, r, d, R₀, δ = x\n", " @unpack γ, R̄₀, η, σ, ξ, θ, δ_bar = p\n", "\n", " return [0; 0; 0; 0; σ*sqrt(R₀); ξ*sqrt(δ * (1-δ))]\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next create a settings generator, and then define a [SDEProblem](https://docs.sciml.ai/stable/tutorials/sde_example/#Example-2:-Systems-of-SDEs-with-Diagonal-Noise-1) with Diagonal Noise." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mSDEProblem\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.9999242424242424, 7.575757575757576e-5, 0.0, 0.0, 0.5, 0.01]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_gen = @with_kw (T = 550.0, γ = 1.0 / 18, η = 1.0 / 20,\n", " R₀_n = 1.6, R̄₀ = (t, p) -> p.R₀_n, δ_bar = 0.01,\n", " σ = 0.03, ξ = 0.004, θ = 0.2, N = 3.3E8)\n", "p = p_gen() # use all defaults\n", "i_0 = 25000 / p.N\n", "r_0 = 0.0\n", "d_0 = 0.0\n", "s_0 = 1.0 - i_0 - r_0 - d_0\n", "R̄₀_0 = 0.5 # starting in lockdown\n", "δ_0 = p.δ_bar\n", "x_0 = [s_0, i_0, r_0, d_0, R̄₀_0, δ_0]\n", "\n", "prob = SDEProblem(F, G, x_0, (0, p.T), p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We solve the problem with the [SOSRI](https://docs.sciml.ai/stable/solvers/sde_solve/#Full-List-of-Methods-1) algorithm (Adaptive strong order 1.5 methods for diagonal noise Ito and Stratonovich SDEs)." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "length(sol_1.t) = 593\n" ] } ], "source": [ "sol_1 = solve(prob, SOSRI());\n", "@show length(sol_1.t);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As in the deterministic case of the previous lecture, we are using an adaptive time-stepping method. However, since this is an SDE, (1) you will tend to see more timesteps required due to the greater curvature; and (2) the number of timesteps will change with different shock realizations.\n", "\n", "With stochastic differential equations, a “solution” is akin to a simulation for a particular realization of the noise process.\n", "\n", "If we take two solutions and plot the number of infections, we will see differences 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" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol_2 = solve(prob, SOSRI())\n", "plot(sol_1, vars=[2], title = \"Number of Infections\", label = \"Trajectory 1\",\n", " lm = 2, xlabel = \"t\", ylabel = \"i(t)\")\n", "plot!(sol_2, vars=[2], label = \"Trajectory 2\", lm = 2, ylabel = \"i(t)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The same holds for other variables such as the cumulative deaths, mortality, and $ R_0 $:" ] }, { "cell_type": "code", "execution_count": 8, "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", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\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": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_1 = plot(sol_1, vars=[4], title = \"Cumulative Death Proportion\", label = \"Trajectory 1\",\n", " lw = 2, xlabel = \"t\", ylabel = \"d(t)\", legend = :topleft)\n", "plot!(plot_1, sol_2, vars=[4], label = \"Trajectory 2\", lw = 2)\n", "plot_2 = plot(sol_1, vars=[3], title = \"Cumulative Recovered Proportion\", label = \"Trajectory 1\",\n", " lw = 2, xlabel = \"t\", ylabel = \"d(t)\", legend = :topleft)\n", "plot!(plot_2, sol_2, vars=[3], label = \"Trajectory 2\", lw = 2)\n", "plot_3 = plot(sol_1, vars=[5], title = \"R_0 transition from lockdown\", label = \"Trajectory 1\",\n", " lw = 2, xlabel = \"t\", ylabel = \"R_0(t)\")\n", "plot!(plot_3, sol_2, vars=[5], label = \"Trajectory 2\", lw = 2)\n", "plot_4 = plot(sol_1, vars=[6], title = \"Mortality Rate\", label = \"Trajectory 1\",\n", " lw = 2, xlabel = \"t\", ylabel = \"delta(t)\", ylim = (0.006, 0.014))\n", "plot!(plot_4, sol_2, vars=[6], label = \"Trajectory 2\", lw = 2)\n", "plot(plot_1, plot_2, plot_3, plot_4, size = (900, 600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See [here](https://diffeq.sciml.ai/stable/solvers/sde_solve/#Recommended-Methods-1) for comments on finding the appropriate SDE algorithm given the structure of $ F(x, t) $ and $ G(x, t) $\n", "\n", "- If $ G $ has diagonal noise (i.e. $ G(x, t) $ is a diagonal, and possibly a function of the state), then `SOSRI` is the typical choice. \n", "- If $ G $ has additive and diagonal noise (i.e. $ G(t) $ is a diagonal and independent from the state), then `SOSRA` is usually the best algorithm for even mildly stiff $ F $. \n", "- If adaptivity is not required, then `EM` (i.e. Euler-Maruyama method typically used by economists) is flexible in its ability to handle different noise processes. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ensembles\n", "\n", "While individual simulations are useful, you often want to look at an ensemble of trajectories of the SDE in order to get an accurate picture of how the system evolves.\n", "\n", "To do this, use the `EnsembleProblem` in order to have the solution compute multiple trajectories at once. The returned `EnsembleSolution` acts like an array of solutions but is imbued to plot recipes to showcase aggregate quantities.\n", "\n", "For example:" ] }, { "cell_type": "code", "execution_count": 9, "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": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ensembleprob = EnsembleProblem(prob)\n", "sol = solve(ensembleprob, SOSRI(), EnsembleSerial(), trajectories = 10)\n", "plot(sol, vars = [2], title = \"Infection Simulations\", ylabel = \"i(t)\", xlabel = \"t\", lm = 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or, more frequently, you may want to run many trajectories and plot quantiles, which can be automatically run in [parallel](https://docs.sciml.ai/stable/features/ensemble/) using multiple threads, processes, or GPUs. Here we showcase `EnsembleSummary` which calculates summary information from an ensemble and plots the mean of the solution along with calculated quantiles of the simulation:" ] }, { "cell_type": "code", "execution_count": 10, "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" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trajectories = 100 # choose larger for smoother quantiles\n", "sol = solve(ensembleprob, SOSRI(), EnsembleThreads(), trajectories = trajectories)\n", "summ = EnsembleSummary(sol) # defaults to saving 0.05, 0.95 quantiles\n", "plot(summ, idxs = (2,), title = \"Quantiles of Infections Ensemble\", ylabel = \"i(t)\",\n", " xlabel = \"t\", labels = \"Middle 95% Quantile\", legend = :topright)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition, you can calculate more quantiles and stack graphs" ] }, { "cell_type": "code", "execution_count": 11, "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", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\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": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol = solve(ensembleprob, SOSRI(), EnsembleThreads(), trajectories = trajectories)\n", "summ = EnsembleSummary(sol) # defaults to saving 0.05, 0.95 quantiles\n", "summ2 = EnsembleSummary(sol, quantiles = (0.25, 0.75))\n", "\n", "plot(summ, idxs = (2,4,5,6),\n", " title = [\"Proportion Infected\" \"Proportion Dead\" \"R_0\" \"delta\"],\n", " ylabel = [\"i(t)\" \"d(t)\" \"R_0(t)\" \"delta(t)\"], xlabel = \"t\",\n", " legend = [:topleft :topleft :bottomright :bottomright],\n", " labels = \"Middle 95% Quantile\", layout = (2, 2), size = (900, 600))\n", "plot!(summ2, idxs = (2,4,5,6),\n", " labels = \"Middle 50% Quantile\", legend = [:topleft :topleft :bottomright :bottomright])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some additional features of the ensemble and SDE infrastructure are\n", "\n", "- [Plotting](https://diffeq.sciml.ai/stable/basics/plot/) \n", "- [Noise Processes](https://diffeq.sciml.ai/stable/features/noise_process/), [Non-diagonal noise](https://diffeq.sciml.ai/stable/tutorials/sde_example/#Example-4:-Systems-of-SDEs-with-Non-Diagonal-Noise-1), and [Correlated Noise](https://diffeq.sciml.ai/stable/tutorials/sde_example/#Example:-Spatially-Colored-Noise-in-the-Heston-Model-1) \n", "- [Parallel Ensemble features](https://diffeq.sciml.ai/stable/features/ensemble/) \n", "- Transforming the ensemble calculations with an [output_func or reduction](https://diffeq.sciml.ai/stable/features/ensemble/#Building-a-Problem-1) \n", "- Auto-GPU accelerated by using `EnsembleGPUArray()` from [DiffEqGPU](https://github.com/SciML/DiffEqGPU.jl/) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Changing Mitigation\n", "\n", "Consider a policy maker who wants to consider the impact of relaxing lockdown at various speeds.\n", "\n", "We will shut down the shocks to the mortality rate (i.e. $ \\xi = 0 $) to focus on the variation caused by the volatility in $ R_0(t) $.\n", "\n", "Consider $ \\eta = 1/50 $ and $ \\eta = 1/20 $, where we start at the same initial condition of $ R_0(0) = 0.5 $." ] }, { "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", "\n", "\n", "\n", "\n" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function generate_η_experiment(η; p_gen = p_gen, trajectories = 100,\n", " saveat = 1.0, x_0 = x_0, T = 120.0)\n", " p = p_gen(η = η, ξ = 0.0)\n", " ensembleprob = EnsembleProblem(SDEProblem(F, G, x_0, (0, T), p))\n", " sol = solve(ensembleprob, SOSRI(), EnsembleThreads(),\n", " trajectories = trajectories, saveat = saveat)\n", " return EnsembleSummary(sol)\n", "end\n", "\n", "# Evaluate two different lockdown scenarios\n", "η_1 = 1/50\n", "η_2 = 1/20\n", "summ_1 = generate_η_experiment(η_1)\n", "summ_2 = generate_η_experiment(η_2)\n", "plot(summ_1, idxs = (4,5),\n", " title = [\"Proportion Dead\" \"R_0\"],\n", " ylabel = [\"d(t)\" \"R_0(t)\"], xlabel = \"t\",\n", " legend = [:topleft :bottomright],\n", " labels = \"Middle 95% Quantile, eta = $η_1\",\n", " layout = (2, 1), size = (900, 900), fillalpha = 0.5)\n", "plot!(summ_2, idxs = (4,5),\n", " legend = [:topleft :bottomright],\n", " labels = \"Middle 95% Quantile, eta = $η_2\", size = (900, 900), fillalpha = 0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While the the mean of the $ d(t) $ increases, unsurprisingly, we see that the 95% quantile for later time periods is also much larger - even after the $ R_0 $ has converged.\n", "\n", "That is, volatile contact rates (and hence $ R_0 $) can interact to make catastrophic worst-case scenarios due to the nonlinear dynamics of the system." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ending Lockdown\n", "\n", "As in the deterministic lecture, we can consider two mitigation scenarios\n", "\n", "1. choose $ \\bar{R}_0(t) $ to target $ R_0 = 0.5 $ for 30 days and then $ R_0 = 2 $ for the remaining 17 months. This corresponds to lifting lockdown in 30 days. \n", "1. target $ R_0 = 0.5 $ for 120 days and then $ R_0 = 2 $ for the remaining 14 months. This corresponds to lifting lockdown in 4 months. \n", "\n", "\n", "Since empirical estimates of $ R_0(t) $ discussed in [[FVJ20]](../zreferences.html#nberw27128) and other papers show it to have wide variation, we will maintain a fairly larger $ \\sigma $.\n", "\n", "We start the model with 100,000 active infections." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mSDEProblem\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.9996969696969698, 0.00030303030303030303, 0.0, 0.0, 0.5, 0.01]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R₀_L = 0.5 # lockdown\n", "η_experiment = 1.0/10\n", "σ_experiment = 0.04\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", "\n", "p_early = p_gen(R̄₀ = R̄₀_lift_early, η = η_experiment, σ = σ_experiment)\n", "p_late = p_gen(R̄₀ = R̄₀_lift_late, η = η_experiment, σ = σ_experiment)\n", "\n", "\n", "# initial conditions\n", "i_0 = 100000 / p_early.N\n", "r_0 = 0.0\n", "d_0 = 0.0\n", "s_0 = 1.0 - i_0 - r_0 - d_0\n", "δ_0 = p_early.δ_bar\n", "\n", "x_0 = [s_0, i_0, r_0, d_0, R₀_L, δ_0] # start in lockdown\n", "prob_early = SDEProblem(F, G, x_0, (0, p_early.T), p_early)\n", "prob_late = SDEProblem(F, G, x_0, (0, p_late.T), p_late)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simulating for a single realization of the shocks, we see the results are qualitatively similar to what we had before" ] }, { "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", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\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": [ "sol_early = solve(prob_early, SOSRI())\n", "sol_late = solve(prob_late, SOSRI())\n", "plot(sol_early, vars = [5, 1,2,4],\n", " title = [\"R_0\" \"Susceptible\" \"Infected\" \"Dead\"],\n", " layout = (2, 2), size = (900, 600),\n", " ylabel = [\"R_0(t)\" \"s(t)\" \"i(t)\" \"d(t)\"], xlabel = \"t\",\n", " legend = [:bottomright :topright :topright :topleft],\n", " label = [\"Early\" \"Early\" \"Early\" \"Early\"])\n", "plot!(sol_late, vars = [5, 1,2,4],\n", " legend = [:bottomright :topright :topright :topleft],\n", " label = [\"Late\" \"Late\" \"Late\" \"Late\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, note that this masks highly volatile values induced by the in $ R_0 $ variation, as seen in the ensemble" ] }, { "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", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\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": [ "trajectories = 400\n", "saveat = 1.0\n", "ensemble_sol_early = solve(EnsembleProblem(prob_early), SOSRI(),\n", " EnsembleThreads(), trajectories = trajectories, saveat = saveat)\n", "ensemble_sol_late = solve(EnsembleProblem(prob_late), SOSRI(),\n", " EnsembleThreads(), trajectories = trajectories, saveat = saveat)\n", "summ_early = EnsembleSummary(ensemble_sol_early)\n", "summ_late = EnsembleSummary(ensemble_sol_late)\n", "\n", "plot(summ_early, idxs = (5, 1, 2, 4),\n", " title = [\"R_0\" \"Susceptible\" \"Infected\" \"Dead\"], layout = (2, 2), size = (900, 600),\n", " ylabel = [\"R_0(t)\" \"s(t)\" \"i(t)\" \"d(t)\"], xlabel = \"t\",\n", " legend = [:bottomright :topright :topright :topleft],\n", " label = [\"Early\" \"Early\" \"Early\" \"Early\"])\n", "plot!(summ_late, idxs = (5, 1,2,4),\n", " legend = [:bottomright :topright :topright :topleft],\n", " label = [\"Late\" \"Late\" \"Late\" \"Late\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, rather than looking at the ensemble summary, we can use data directly from the ensemble to do our own analysis.\n", "\n", "For example, evaluating at an intermediate (`t = 350`) and final time step." ] }, { "cell_type": "code", "execution_count": 16, "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", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\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": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N = p_early.N\n", "t_1 = 350\n", "t_2 = p_early.T # i.e. the last element\n", "bins_1 = range(0.000, 0.009, length = 30)\n", "bins_2 = 30 # number rather than grid.\n", "\n", "hist_1 = histogram([ensemble_sol_early.u[i](t_1)[4] for i in 1:trajectories],\n", " fillalpha = 0.5, normalize = :probability,\n", " legend = :topleft, bins = bins_1,\n", " label = \"Early\", title = \"Death Proportion at t = $t_1\")\n", "histogram!(hist_1, [ensemble_sol_late.u[i](t_1)[4] for i in 1:trajectories],\n", " label = \"Late\", fillalpha = 0.5, normalize = :probability, bins = bins_1)\n", "hist_2 = histogram([ensemble_sol_early.u[i][4, end] for i in 1:trajectories],\n", " fillalpha = 0.5, normalize = :probability, bins = bins_2,\n", " label = \"Early\", title = \"Death Proportion at t = $t_2\")\n", "histogram!(hist_2, [ensemble_sol_late.u[i][4, end] for i in 1:trajectories],\n", " label = \"Late\", fillalpha = 0.5, normalize = :probability, bins = bins_2)\n", "plot(hist_1, hist_2, size = (600,600), layout = (2, 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This shows that there are significant differences after a year, but by 550 days the graphs largely coincide.\n", "\n", "In the above code given the return from `solve` on an `EnsembleProblem` , e.g. `ensemble_sol = solve(...)`\n", "\n", "- You can access the i’th simulation as `ensemble_sol[i]`, which then has all of the standard [solution handling](https://diffeq.sciml.ai/stable/basics/solution/) features \n", "- You can evaluate at a real time period, `t`, with `ensemble_sol[i](t)`. Or access the 4th element with `ensemble_sol[i](t)[4]` \n", "- If the `t` was not exactly one of the `saveat` values (if specified) or the adaptive timesteps (if it was not), then it will use interpolation \n", "- Alternatively, to access the results of the ODE as a grid exactly at the timesteps, where `j` is timestep index, use `ensemble_sol[i][j]` or the 4th element with `ensemble_sol[i][4, j]` \n", "- Warning: unless you have chosen a `saveat` grid, the timesteps will not be aligned between simulations. That is, `ensemble_sol[i_1].t` wouldn’t match `ensemble_sol[i_2].t`. In that case, use interpolation with `ensemble_sol[i_1](t)` etc. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reinfection\n", "\n", "As a final experiment, consider a model where the immunity is only temporary, and individuals become susceptible again.\n", "\n", "In particular, assume that at rate $ \\nu $ immunity is lost. For illustration, we will examine the case if the average immunity lasts 12 months (i.e. $ 1/\\nu = 360 $)\n", "\n", "The transition modifies the differential equation [(1)](#equation-sird) to become\n", "\n", "\n", "\n", "$$\n", "\\begin{aligned}\n", " d s & = \\left(- \\gamma \\, R_0 \\, s \\, i + \\nu \\, r \\right) dt\n", " \\\\\n", " d i & = \\left(\\gamma \\, R_0 \\, s \\, i - \\gamma i \\right) dt\n", " \\\\\n", " d r & = \\left((1-\\delta) \\gamma i - \\nu \\, r\\right) dt\n", " \\\\\n", " d d & = \\delta \\gamma i \\, dt\n", " \\\\\n", "\\end{aligned} \\tag{6}\n", "$$\n", "\n", "This change modifies the underlying `F` function and adds a parameter, but otherwise the model remains the same.\n", "\n", "We will redo the “Ending Lockdown” simulation from above, where the only difference is the new transition." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "EnsembleSolution Solution of length 551 with uType:\n", "Float64" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function F_reinfect(x, p, t)\n", " s, i, r, d, R₀, δ = x\n", " @unpack γ, R̄₀, η, σ, ξ, θ, δ_bar, ν = p\n", "\n", " return [-γ*R₀*s*i + ν*r; # ds/dt\n", " γ*R₀*s*i - γ*i; # di/dt\n", " (1-δ)*γ*i - ν*r # dr/dt\n", " δ*γ*i; # dd/dt\n", " η*(R̄₀(t, p) - R₀);# dR₀/dt\n", " θ*(δ_bar - δ); # dδ/dt\n", " ]\n", "end\n", "\n", "p_re_gen = @with_kw ( T = 550.0, γ = 1.0 / 18, η = 1.0 / 20,\n", " R₀_n = 1.6, R̄₀ = (t, p) -> p.R₀_n,\n", " δ_bar = 0.01, σ = 0.03, ξ = 0.004, θ = 0.2, N = 3.3E8, ν = 1/360)\n", "\n", "p_re_early = p_re_gen(R̄₀ = R̄₀_lift_early, η = η_experiment, σ = σ_experiment)\n", "p_re_late = p_re_gen(R̄₀ = R̄₀_lift_late, η = η_experiment, σ = σ_experiment)\n", "\n", "trajectories = 400\n", "saveat = 1.0\n", "prob_re_early = SDEProblem(F_reinfect, G, x_0, (0, p_re_early.T), p_re_early)\n", "prob_re_late = SDEProblem(F_reinfect, G, x_0, (0, p_re_late.T), p_re_late)\n", "ensemble_sol_re_early = solve(EnsembleProblem(prob_re_early), SOSRI(), EnsembleThreads(),\n", " trajectories = trajectories, saveat = saveat)\n", "ensemble_sol_re_late = solve(EnsembleProblem(prob_re_late), SOSRI(), EnsembleThreads(),\n", " trajectories = trajectories, saveat = saveat)\n", "summ_re_early = EnsembleSummary(ensemble_sol_re_early)\n", "summ_re_late = EnsembleSummary(ensemble_sol_re_late)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ensemble simulations for the $ \\nu = 0 $ and $ \\nu > 0 $ can be compared to see the impact in the absence of medical innovations." ] }, { "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", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\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": [ "plot(summ_late, idxs = (1, 2, 3, 4),\n", " title = [\"Susceptible\" \"Infected\" \"Recovered\" \"Dead\"],\n", " layout = (2, 2), size = (900, 600),\n", " ylabel = [\"s(t)\" \"i(t)\" \"r(t)\" \"d(t)\"], xlabel = \"t\",\n", " legend = :topleft,\n", " label = [\"s(t)\" \"i(t)\" \"r(t)\" \"d(t)\"])\n", "plot!(summ_re_late, idxs = (1, 2, 3, 4),\n", " legend = :topleft,\n", " label = [\"s(t); nu > 0\" \"i(t); nu > 0\" \"r(t); nu > 0\" \"d(t); nu > 0\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we can examine the same early vs. late lockdown histogram" ] }, { "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", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\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": [ "bins_re_1 = range(0.003, 0.010, length = 50)\n", "bins_re_2 = range(0.0085, 0.0102, length = 50)\n", "hist_re_1 = histogram([ensemble_sol_re_early.u[i](t_1)[4] for i in 1:trajectories],\n", " fillalpha = 0.5, normalize = :probability,\n", " legend = :topleft, bins = bins_re_1,\n", " label = \"Early\", title = \"Death Proportion at t = $t_1\")\n", "histogram!(hist_re_1, [ensemble_sol_re_late.u[i](t_1)[4] for i in 1:trajectories],\n", " label = \"Late\", fillalpha = 0.5, normalize = :probability, bins = bins_re_1)\n", "hist_re_2 = histogram([ensemble_sol_re_early.u[i][4, end] for i in 1:trajectories],\n", " fillalpha = 0.5, normalize = :probability, bins = bins_re_2,\n", " label = \"Early\", title = \"Death Proportion at t = $t_2\")\n", "histogram!(hist_re_2, [ensemble_sol_re_late.u[i][4, end] for i in 1:trajectories],\n", " label = \"Late\", fillalpha = 0.5, normalize = :probability,\n", " bins = bins = bins_re_2)\n", "plot(hist_re_1, hist_re_2, size = (600,600), layout = (2, 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, there are significant differences between the early and late deaths and high variance.\n", "\n", "This bleak simulation has assumed that no individuals has long-term immunity and that there will be no medical advancements on that time horizon - both of which are unlikely to be true.\n", "\n", "Nevertheless, it suggest that the timing of lifting lockdown has a more profound impact after 18 months if we allow stochastic shocks imperfect immunity." ] } ], "metadata": { "date": 1595546525.6490545, "download_nb": 1, "download_nb_path": "https://julia.quantecon.org/", "filename": "covid_sde.rst", "filename_with_path": "continuous_time/covid_sde", "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": "../multi_agent_models/index", "title": "Multiple Agent Models" }, "prev_doc": { "link": "seir_model", "title": "Modeling COVID 19 with Differential Equations" }, "site_title": "Quantitative Economics with Julia", "title": "Modeling Shocks in COVID 19 with Stochastic Differential Equations" }, "nbformat": 4, "nbformat_minor": 2 }