This R code is an agent based stochastic model of the propagation of a virus in a population. The code file is: model_covid.R *Warning It is provided "as is" under a free CeCill License (http://cecill.info/). You can download the code and the sample files, and run the code. Commercial use is not permitted. Bear in mind that I am a data scientist for climate sciences. I am not an epidemiologist. This programme was devised to *illustrate* the impact of confinment on the spread of a virus. The programme was written by Pascal Yiou (LSCE, March 2020). More information (with results and discussion) is given on: https://cygnenoirblog.wordpress.com/2020/03/25/an-agent-based-model-of-covid-19/ The code comes with three sample R files (.Rdata), and three figures (.pdf), and a README.txt file. *The model We start with a population of N individuals, and only 1 is infected. The rule of the game is the following. Each individual can be in: stage 0: healthy. stage 1: carrier of the virus without symptom. In France, no systematic covid-19 test is made, so that the population in stage 1 is unknown. We start with 1 individual in stage 1. It turns out that managing stage 1 is crucial. stage 2: carrier of the virus with benign symptoms. Such symptoms are similar to those of seasonal flu. Most countries can count the population in stage 2. stage 3: serious symptoms appear and hospitalization with respiratory devices is necessary. Excess of this population can jeopardize a hospital system. stage 4: recovery from the virus after stages 1, 2 or 3. stage 5: death, after stage 3. Individuals in stage 1 are contagious. They can contaminate individuals in stage 0. The contamination rate is 2 on average, but can vary highly, depending on social factors. People who attend large scale meetings are more prone to contaminate others. I will assume that the contamination rate of each individual is drawn from a Poisson distribution with parameter 2. This contamination rate can be multiplied by a social factor (linked to public transportation, excessive social contacts, etc.), which is drawn from another Poisson distribution. This accounts for "super" spreaders. Heavy tail distributions could also be used, in a later version of the model. Individuals in stage 2 are no very contagious, because they are supposed to be confined. After a development phase (whose duration is drawn from a Poisson distribution), they either heal (stage 4), or symptoms worsen, and they have to go to hospital for intensive care (stage 3). Individuals in stage 3 stay there for a random (Poisson) number of days. Then they either heal (stage 4) or decease (stage 5), with a fixed uniform probability law, which can be adjusted from a prior medical history. Stage 4 is different from stage 0, so that I can count them explicitly, and because I leave the possibility that stage 4 individuals have become immune to the virus, which has to be medically verified. This stochastic model hence draws from several Poisson laws: number of contaminated stage 0 individuals from each stage 1 individual, duration of stage 1 for each individual, duration of stage 2 for each individual, duration of stage 3 for each individual. Uniform laws are used to determine the probabilities of shifting to recovery or a more serious stage. The parameter of the Poisson laws and uniform laws are infered from numbers that are gathered in the newspapers or websites. The model leaves the possibility of "declaring confinement" if the number of stage 2 indivuals exceeds a threshold. When confinement is declared, the contamination potential of stage 1 individuals drops to 0.8. Confinement time can go from t=N (no confinement at all) to t=1 (confinement as soon as one case is detected). The total number of individuals does not change, so that the population is "closed",  in the thermodynamical sense. This could be modified in a further version. The model is implemented in R. Since it is stochastic, Monte Carlo experiments can be performed in order to assess uncertainties. The model is rather computer greedy in terms of memory, contrary to most SEIR models, which are sets of ordinary differential equations. The model can be run with a linux command line (with arguments), so that it can be included in a shell script to make ensembles of simulations (aka Monte Carlo experiments). The goal of this model is to estimate the impact of confinement on the stage 3 population (which tests the whole hospital system) and death rate. Other parameters of the model can be changed to estimate the average delay between each stage. *Experimental set up I start with a population of N=100000 individuals. This corresponds to a large city in France. With this relatively low number, I can run the model on my laptop computer in less than 5 mn. Simulations last for 60 days. This is deemed to be enough to see the effects of confinement. The parameter values of the model are: The initial number of Stage 1 (S1) individuals is n0=1 (only one individual), The average contamination rate for S1 individuals is R=2, so that the mean contamination rate for each individual is: rpois(n=1,R) This average contamination rate is modulated by a factor  alpha=1 (constant), or alpha=max(rpois(n=N,lambda=R.alpha)-1, 0), where R.alpha = 10. The average incubation time (residence time in S1) is 5 days. The individual incubation time is: rpois(n=1, 5). The average residence time in Stage 2 (S2) is d.res.2 = 7 days. The individual residence time is: rpois(n=1, 7). The average residence time in Stage 3 (S3) is d.res.3 = 10 days. The individual residence time is: rpois(n=1, 10) The probability of going from S1 to S2 after the residence time is: p2=1 - 0.8 The probability of going from going from S2 to S3 after the residence time is: p3=1-0.8 The probability of decease in S3 after the residence time is: p4=0.06 The threshold of the S2 population that triggers confinement of S0 and S1 is: 1000 and N (no confinement). So, I perform three sets of simulations: -one "control" simulation (CRTL) without confinement (confinement threshold on S2 population is larger or equal than size of population) and the average propagation rate is constant (e.g. population of pedestrians, no super spreader). -one "confinement" simulation (CONFI), where confinement is triggered when S2 (population with symptoms) exceeds 1000 individuals. No super spreader. -one "superspreader" simulation (SUPSPR) (some individuals travel long distances or meet many people), with confinement that is triggered when S2 exceeds 1000 individuals. The output files (.Rdata and .pdf) for those three sets of experiments are provided.