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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /+dub.json:
{
"name": "sir_example",
"authors": [
"Flávio Codeço Coelho"
],
"description": "Example usage of SIR model from epistochmodels",
"copyright": "Copyright © 2020, Flávio Codeço Coelho",
"license": "GPL-3",
"dependencies": {
"epistochmodels": "*"
}
}
+/
import std.datetime.stopwatch;
import std.stdio;
import models;
void main(){
double beta = 0.7;
double gam = 0.3;
int N = 1_000_000;
int I0 = 10;
double tf = 1000;
auto sw = StopWatch(AutoStart.no);
auto model = new SIR(N, beta, gam);
model.initialize(N-I0, I0, 0);
sw.start();
auto sim = model.run(0, tf);
sw.stop();
writeln("Time of the SIR run with N=1000000: ", sw.peek());
writefln("Number of steps: %s", sim[0].length);
}
|
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);