Getting started with EpiStochModels¶
EpistochModels can be installed from source (if you clone our Github repository).
But first you must have a D compiler installed. If your operating system does not have a pre-built package:
$ curl https://dlang.org/install.sh | bash -s
Then you can build and install the Python package.
$ python setup.py build
$ python setup.py install
We will publish the package on PyPI soon so that you will be able to simply pip install it.
Using the Built-in Models¶
The models can be used from Python (see notebooks) or straight from D code:
1/+dub.json:
2{
3 "name": "sir_example",
4 "authors": [
5 "Flávio Codeço Coelho"
6 ],
7 "description": "Example usage of SIR model from epistochmodels",
8 "copyright": "Copyright © 2020, Flávio Codeço Coelho",
9 "license": "GPL-3",
10 "dependencies": {
11 "epistochmodels": "*"
12 }
13}
14+/
15import std.datetime.stopwatch;
16import std.stdio;
17import models;
18
19void main(){
20 double beta = 0.7;
21 double gam = 0.3;
22 int N = 1_000_000;
23 int I0 = 10;
24 double tf = 1000;
25 auto sw = StopWatch(AutoStart.no);
26 auto model = new SIR(N, beta, gam);
27 model.initialize(N-I0, I0, 0);
28 sw.start();
29 auto sim = model.run(0, tf);
30 sw.stop();
31 writeln("Time of the SIR run with N=1000000: ", sw.peek());
32 writefln("Number of steps: %s", sim[0].length);
33}
The D code above can be run with the following command:
$ dub run --build=release --single sir_example.d
Implementing your own CTMC models¶
If you are willing to write D code, EpiStochModels also provides a class to simulate any CTMC model you want, as long as you provide it with a transition matrix and propensity functions.
The code below is the implementation of a SIRD model.
int[][] tmat = [[-1,1,0,0],
[0,-1,1,0],
[0,-1,0,1]];
double[string] pars = [
"beta": 0.3,
"gam": 0.05,
"mu": 0.01
];
alias double delegate(int[], double[string]) dy;
dy[] props = [
(v,p)=> p["beta"]*v[0]*v[1]/sum(v[0..$-1]), // infection
(v,p)=> p["gam"]*v[1], // recovery
(v,p)=> p["mu"]*v[1] // death
];
CTMC model = new CTMC(tmat, props);
model.initialize([995,5,0,0],pars);
auto res = model.run(0, 1000);