Skip to main content

Simulation of population dynamics

Project description

popdyn

Library for simulation of population dynamics.

Allows to simulate the interaction between specific groups in a population, calculating the amount of members for each group along the time. The transitions between groups are defined using probabilistic rules. The results can be obtained through one of the deterministic or indeterministic solvers.

Installing

pip3 install popdyn

Using the library

Let's dive in a simple example to see how it works.

First step is define the model, for this example the SIR model, whose name comes from the initials S (susceptible), I (infected), R (recovered) and has been quite widespread and used as a basis for more complicated models. In this model, a susceptible can pass to the group of infected if he interacts with an infected and in turn the infected pass to the group of recovered spontaneously. In this model, it is usually taken into account that the probability of contact between a susceptible person and an infected person depends on the number of susceptibles and the proportion of the total population that is made up of infected people.

S(t), I(t), and R(t) represent the number of susceptible, infected, and recovered individuals at time t, respectively. beta, known in this case as the transmission rate, represents the number of individuals that go from being susceptible to being infected in a unit of time for each possible effective contact. The number of possible effective contacts is given by the product of S(t) and I(t)/N, which represents the contacts of susceptibles with infected, since the rest of the contacts do not cause a transition from S to I. Note that here it is assumed that contacts occur with equal probability regardless of which group an individual belongs to. In addition to the previous transition, the spontaneous transition from an infected to a recovered one is taken into account, which depends on the number of infected and on gamma, called the recovery rate. Finally, the recovery rate is made up of the amount of recoveries that are obtained precisely from the transition from I to R described previously.

The system of differential equations of a deterministic SIR as described above would be:

dS/dt = - beta * S(t) * I(t) / N

dI/dt = beta * S(t) * I(t) / N - gamma * I(t)

dR/dt = gamma * I(t)

For the example below we consider an initial population composed by 97 susceptible and 3 infected. The values of beta and gamma are 0.35 and 0.035 respectively.

from popdyn import Model, Transition

# storage in a dict the ID and the initial population for each group
groups = {
    'S': 97,
    'I': 3,
    'R': 0
}

# initialize the model
sir = Model(list(groups.keys()))

# add transitions between groups
sir['S', 'I'] = Transition(0.35, 'S', 'I', N=True)
sir['I', 'R'] = Transition(0.035, 'I')

If we print the model, we can see the transitions for each group (N is the total population):

>>> print(sir)
S -> {'I': 0.35 * S * I / N^1}
I -> {'R': 0.035 * I}
R -> {}

Once ready the model, we can simulate the behavior of the groups in a period of time using one of the available solvers: Gillespie, TauLeaping or ODE.

results = sir.solve(t=100, initial_pop=list(groups.values()), solver='Gillespie')

Getting in results a dictionary with a key 'time' and a key for each group with the population at every time point.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

popdyn-0.6.3.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

popdyn-0.6.3-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file popdyn-0.6.3.tar.gz.

File metadata

  • Download URL: popdyn-0.6.3.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for popdyn-0.6.3.tar.gz
Algorithm Hash digest
SHA256 dd33c5665e7311b2db42de9abe8e28e8c705787081dc81dad23269f964b4c20f
MD5 edf7b2bd1c6e85930e53e89368a0b3b1
BLAKE2b-256 183b5f3296588d90c205369fc97c9cc9c0b869cdf5364d460d6da6efed897866

See more details on using hashes here.

File details

Details for the file popdyn-0.6.3-py3-none-any.whl.

File metadata

  • Download URL: popdyn-0.6.3-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for popdyn-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4658449995ced5e6c781a7cf1bc8e1ba138d6e93b243103a858b4e47e3f71676
MD5 4cc3f2921ee7cc05c87f9c9e1e1a56b1
BLAKE2b-256 f51a9e7d90280b11d66606832e5e322f08f1ba6189dd1de09b4e0fe3b0e74805

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page