{ "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.6-final" }, "orig_nbformat": 2, "kernelspec": { "name": "python3", "display_name": "Python 3", "language": "python" } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "source": [ "# A General CTMC solver\n", "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. So far this class is available for D programs, because of the impossibility of passing the propensity functions from Python into D. Nevertheless, We will demonstrate here how to use it:\n", "\n", "Let's use the SIRD model as an example. The corresponding ODE model is:\n", "\n", "\\begin{align}\n", "\\frac{dS}{dt} &= -\\beta S \\frac{I}{N}\\\\\n", "\\frac{dI}{dt} &= \\beta S \\frac{I}{N} -\\gamma I -\\mu I\\\\\n", "\\frac{dR}{dt} &= \\gamma I\\\\\n", "\\frac{dD}{dt} &= \\mu I\n", "\\end{align}\n", "\n", "For this model, the following events are possible:\n", "\n", "\n", "1. **Infection** propensity: $\\beta S \\frac{I}{N}$\n", "1. **Recovery** propensity: $\\gamma I$\n", "1. **Death** propensity: $\\mu I$\n", "\n", "The following transition matrix describe the changes in the state of the system when these events happen:\n", "\n", "TM = \\begin{bmatrix}\n", "-1 & 1 & 0 & 0\\\\\n", "0 & -1 & 1 & 0\\\\\n", "0 & -1 & 0 & 1\n", "\\end{bmatrix}.\n", "\n", "The D code required to implement this model is:\n", "\n", "```D\n", "int[][] tmat = [[-1,1,0,0],\n", " [0,-1,1,0], \n", " [0,-1,0,1]];\n", "double[string] pars = [\n", " \"beta\": 0.3,\n", " \"gam\": 0.05,\n", " \"mu\": 0.01\n", "];\n", "alias double delegate(int[], double[string]) dy;\n", "dy[] props = [\n", " (v,p)=> p[\"beta\"]*v[0]*v[1]/sum(v[0..$-1]), // infection\n", " (v,p)=> p[\"gam\"]*v[1], // recovery\n", " (v,p)=> p[\"mu\"]*v[1] // death\n", " ];\n", "CTMC model = new CTMC(tmat, props,[\"S\", \"I\", \"R\", \"D\"], [\"beta\", \"gamma\", \"mu\"]);\n", "model.initialize([995,5,0,0],pars);\n", "auto res = model.run(0, 1000);\n", "```" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Locally registered package pyd 0.13.1+commit.5.g93d8235 was not found. Please run 'dub remove-local \"/home/fccoelho/Downloads/pyd/\"'.\n", "Locally registered package epistochmodels ~master was not found. Please run 'dub remove-local \"/home/fccoelho/Documentos/Software_projects/EpiStochModels/epistochmodels/\"'.\n", "Performing \"release\" build using ldc2 for x86_64.\n", "mir-core 1.0.3: target for configuration \"library\" is up to date.\n", "mir-algorithm 3.5.7: target for configuration \"default\" is up to date.\n", "mir-random 2.2.15: target for configuration \"extended\" is up to date.\n", "pyd 0.14.0: target for configuration \"python38\" is up to date.\n", "epistochmodels 0.3.1: target for configuration \"library\" is up to date.\n", "CTMC_example ~master: building configuration \"application\"...\n", "Linking...\n", "To force a rebuild of up-to-date targets, run again with --force.\n", "Running ./CTMC_example \n", "Time of the SIR run with N=1000: 1 ms, 350 μs, and 5 hnsecs\n" ] } ], "source": [ "!dub run --build=release --compiler=ldc2 --single CTMC_example.d" ] }, { "source": [ "After running the line above, an executable called `CTCM_example` will be created alongside the source file that you can simply run:" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Time of the SIR run with N=1000: 1 ms, 261 μs, and 8 hnsecs\n" ] } ], "source": [ "!./CTMC_example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ] }