About the author: Mort, a data analysis enthusiast, is good at data visualization, pays more attention to machine learning, and hopes to learn more and communicate with friends in the industry.

At present, the whole world is still fighting against COVID-19. Since the outbreak of COVID-19, many people have proposed a variety of models to predict the development of the pneumonia epidemic, among which SIR model is a common one. This is because SIR is the most classic and commonly used model in the field of disease prevention and control. Today, the author uses graph theory to describe SIR model.

Susceptible to — Susceptible to — Infected — Recovered. Now, how Susceptible to — Susceptible to — Infected with a disease has become the Infected population. The Infected population had a beta probability of recovery, and Recovered. This process can be one or multiple, and many similar models have been derived, such as SIS, SIRS and SI. The following figure describes the SIR process.

Figure 1. Schematic diagram of SIR model

This time, the author uses graph theory, which is what we usually call network analysis to illustrate the SIR model. The general process is shown in the figure below.

Figure 2. Diagram of SIR network model

In fact, this process is very simple, that is, a few people infect a majority of people, and finally all the infected people recover. This model simplifies part of the problems we encounter in many practical situations, such as not considering the death of the infected people, not considering the re-infection of the infected people and so on. The graph theory library used this time is NetworkX, which is the most commonly used graph theory analysis library in Python. Let’s go straight to the code.

Start by importing various packages.

import matplotlib.pyplot as plt
import numpy.random as rdm
import networkx as nx
Copy the code

Next, define two variables, n and g. N is the total number of people, and Nx.Erdos_renyi_graph is a Random network graph. Erdos and Renyi are two Hungarian mathematicians, whose main contribution is to establish the famous E-R Random Graph Theory. This is acknowledged to have mathematically pioneered a systematic analysis of complex network topologies. This e-R diagram is also the basis of the application in this paper. The nx.erdos_renyi_graph(n, 0.01) means to connect 100 nodes with probability of 0.01. Here the two numbers of 100 and 0.01 are used by the author at random, you can set the relevant values according to your own research, here is mainly used for simple explanation. Then there are three string variables — susceptible, infected, and recovered — representing susceptible, infected, and recovered people respectively.

n = 100
g = nx.erdos_renyi_graph(n, 0.01)
susceptible = 'S'
infected = 'I'
recovered = 'R'
Copy the code

Now we’re going to define a couple of functions.

Def Onset (g): # initial onsetfor i in g.node.keys():
        g.node[i]['state'] = susceptible def infect_prop(g, PROPORTION)for i in g.node.keys():
        if(rdm.random() <= proportion):
            g.node[i]['state'] = infected def build_model(pinfected, pRecover):if g.node[i]['state'] == infected:
            for m in g.neighbors(i):
                if g.node[m]['state'] == susceptible:
                    if rdm.random() <= pInfect:
                        g.node[m]['state'] = infected
            if rdm.random() <= pRecover:
                g.node[i]['state'] = recovered
    returnModel def model_run(g, model): # Single model runforI in g.nodo.keys (): model(g, I) def model_iter(g, model, iter): #for i in range(iter):
        model_run(g, model)
Copy the code

The first function onset, that is, the state at the beginning. Here, we set the state of each node as “S”, which means “susceptible”. Here, we assume that everyone belongs to the susceptible group. The function is called “numpy.random. Random”, which returns a uniform distribution. The value is between 0 and 1, excluding 1. When RDM.random ()<= PROPORTION, we turn such crowd into infected crowd, so that the infected crowd is more uniform. The third function is build_model, which is a nested function with two parameters, plavigory and pRecover, respectively representing the infection probability and recovery probability. Here, G.eighbors (I) means every adjacent node of node I, and G is a graph generated by us. It is a class and will be explained later. Now, in these infected people, when their neighbors are susceptible to how susceptible they are, we have these nodes infected with probability paffected. And in these infected people, recovery occurs with pRecover probability. It’s a simple infection-recovery process. The following two functions, model_run and model_iter, run the model once and many times to simulate one and multiple loops, respectively.

The next step is to draw.

fig, ax= plt.subplots(figsize=(12.10))
ax.set_xticks([])
ax.set_yticks([])
pos = nx.spring_layout(g, k=0.2)
nx.draw_networkx_edges(g, pos, alpha=0.5, width = 1)
nx.draw_networkx_nodes(g, pos, node_size=80)
plt.show()
Copy the code

Nx.spring_layout is the method to set the position of the network diagram. K is the optimal distance between nodes, which can be set at will. The larger the value is, the more scattered the nodes will be. Draw_networkx_nodes (g, pos, node_size=80) draw_networkx_nodes(g, pos, node_size=80) The nx.draw_networkx_edges(g, pos, alpha=0.5, width = 1) are used to draw the edges, again passing in the position parameters and setting the transparency and width. The resulting graph is shown below.

Figure 3. Network diagram generated by the model

Networkx uses a random mapping algorithm, and the parameters we use are random, so this graph produces different results each time, but it’s basically similar, and you can see that there are already some nodes that are connected, and that’s where the disease starts to spread.

And then finally, the infection rate.

onset(g)
infect_prop(g, 0.05)
model = build_model(0.2.0.8)
model_iter(g, model, 10)
infected = [ v for (v, attr) in g.nodes(data = True) if attr['state'] == recovered ]
infection_rate = len(infected)/n
print(infection_rate)
Copy the code

Onset (g) of Onset (g) is the graph generated by g = nx.erdos_renyi_graph(n, 0.01), which is an example of a class. Onset (G, 0.05) of Infect_prop (G, 0.05) is the initial infection rate of the population set at 0.05. The infection probability and recovery probability in model = build_model(0.2, 0.8) are set to 0.2 and 0.8 respectively. As for why this is set, it is mainly based on the commonly used “80/20 theory”. This number can be set randomly according to the user’s simulation situation. In model_iter(G, Model, 10), we repeated this simulation process for 10 times, finally calculated the number of infections, and obtained the final stable infection rate infection_rate. Because many of these parameters are random, the results obtained are theoretically different every time. The results obtained by the author range from 0.03 to 0.12, which is of little significance and mainly focuses on understanding the methods of simulation.

Appreciate the author

Python Chinese community as a decentralized global technology community, to become the world’s 200000 Python tribe as the vision, the spirit of Chinese developers currently covered each big mainstream media and collaboration platform, and ali, tencent, baidu, Microsoft, amazon and open China, CSDN industry well-known companies and established wide-ranging connection of the technical community, Have come from more than 10 countries and regions tens of thousands of registered members, members from the ministry, tsinghua university, Peking University, Beijing university of posts and telecommunications, the People’s Bank of China, the Chinese Academy of Sciences, cicc, huawei, BAT, such as Google, Microsoft, government departments, scientific research institutions, financial institutions, and well-known companies at home and abroad, nearly 200000 developers to focus on the platform.

Scan code to pay attention to the following public number after the reply “forecast” to obtain source data

Click become:Community registered members ** like the article, click the **Looking at the