Solving mathematical models of infectious disease epidemics in Python

**Caution! This article does not contain more information than I have tried. ** **

Infectious disease mathematical model

In response to the recent COVID 19 situation, I think that various mathematical models of infectious diseases have been talked about, but let's solve it with Python odeint and visualize it with matplotlib as it is. It sounds awkward, but you shouldn't express any feelings about COVID 19 from the result of properly implementing something in this article.

So, let's say that the easiest SIR model to solve is

\begin{align}
\frac{\mathrm{d} S}{\mathrm{d} t} & = - \beta I S\\
\frac{\mathrm{d} I}{\mathrm{d} t} & = \beta I S - \gamma I\\
\frac{\mathrm{d} R}{\mathrm{d} t} & = \gamma I
\end{align}

I will leave it as. $ \ Beta $ is the infection rate (/ (person x hours)), the number of people who have not yet been infected per unit time per sick person, and $ \ gamma $ is the cure rate ( / Hour), the reciprocal of the time it takes to heal. $ S $ is the number of people who have not been infected and have no infectivity, $ I $ is the number of people who are infected and have the ability to infect others, $ R $ is quarantine, cure, death, etc. The number of people who have been infected with or have lost the ability to infect others.

Python code Since it is troublesome, I put a solid code without any explanation.

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button

def solve(value, t, foi, rr):
	# fio = force of infection (beta)
	# rr = recovery rate
	S, I, R = value
	dSdt = - foi * I * S
	dIdt = foi * I * S - rr * I
	dRdt = rr * I
	return [dSdt, dIdt, dRdt]


span = 100
t = np.linspace(0, span, 256)
initial_value = [0.9, 0.1, 0] # initial values for S, I, R
solution = odeint(solve, initial_value, t, args = (0.5, 0.5))

fig, ax = plt.subplots(figsize=(12, 6))
plt.xlim(0, span)
plt.ylim(0, 1)
plt.subplots_adjust(bottom = 0.2)

data_S, = plt.plot(t, solution[:, 0], "blue", label = "Susceptible")
data_I, = plt.plot(t, solution[:, 1], "red", label = "Infectious")
data_R, = plt.plot(t, solution[:, 2], "orange", label = "Removed")

plt.legend(loc = "best")

rr_slider = Slider(plt.axes([0.2, 0.02, 0.65, 0.02]), "recovery rate", 0, 1, valinit = 0.5, valstep = 0.1)
foi_slider = Slider(plt.axes([0.2, 0.05, 0.65, 0.02]), "force of inf.", 0, 1, valinit = 0.5, valstep = 0.1)

def update(val):
	rr = rr_slider.val
	foi = foi_slider.val
	solution = odeint(solve, initial_value, t, args=(foi, rr))
	data_S.set_ydata(solution[:, 0])
	data_I.set_ydata(solution[:, 1])
	data_R.set_ydata(solution[:, 2])
	fig.canvas.draw_idle()

rr_slider.on_changed(update)
foi_slider.on_changed(update)

plt.show()

Execution result

スクリーンショット 2020-03-19 16.18.58.png I think this kind of thing will come out. By the way, I wasn't sure what the correct values for the infection rate and cure rate were, so I made some knobs so that I could adjust the values, so please play with them.

so

Well play it. If you want to solve the differential equation while undulating the parameters, please do not hesitate to contact us.

Recommended Posts

Solving mathematical models of infectious disease epidemics in Python
Mathematical model of infectious disease epidemics
Solving the equation of motion in Python (odeint)
Overview of generalized linear models and implementation in Python
Equivalence of objects in Python
Implementation of quicksort in Python
Pixel manipulation of images in Python
Division of timedelta in Python 2.7 series
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Implementation of life game in Python
Waveform display of audio in Python
Law of large numbers in python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
Implementation of particle filters in Python and application to state space models
Conversion of string <-> date (date, datetime) in Python
Check the behavior of destructor in Python
(Bad) practice of using this in Python
General Theory of Relativity in Python: Introduction
Output tree structure of files in Python
Display a list of alphabets in Python 3
Comparison of Japanese conversion module in Python3
Summary of various for statements in Python
Sum of variables in a mathematical model
The result of installing python in Anaconda
Gang of Four (GoF) Patterns in Python
The basics of running NoxPlayer in Python
Bulk replacement of strings in Python arrays
Project Euler # 16 "Sum of Powers" in Python
Traffic Safety-kun: Recognition of traffic signs in Python
Summary of built-in methods in Python list
Non-logical operator usage of or in python
Disease classification in Random Forest using Python
[Python] Saving learning results (models) in machine learning
In search of the fastest FizzBuzz in Python
Practical example of Hexagonal Architecture in Python
Project Euler # 17 "Number of Characters" in Python
Double pendulum equation of motion in python
Predict infectious disease epidemics with SIR model
Get rid of DICOM images in Python
[Python] Disease classification in random forest-with LDA-
Status of each Python processing system in 2020
Project Euler # 1 "Multiples of 3 and 5" in Python
Meaning of using DI framework in Python
Implement the mathematical model "SIR model" of infectious diseases in OpenModelica (see the effect of the vaccine)
Studying Mathematics in Python: Solving Simple Probability Problems
Output the number of CPU cores in Python
Draw a graph of a quadratic function in Python
[Python] Sort the list of pathlib.Path in natural sort
Receive websocket of kabu station ® API in Python
Summary of how to import files in Python 3
Project Euler # 10 "sum of prime numbers" in Python
Unattended operation of Google Spreadsheets (etc.) in Python
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Make a copy of the list in Python
Real-time visualization of thermography AMG8833 data in Python
Summary of how to use MNIST in Python
Rewriting elements in a loop of lists (Python)