A python library for doing System Dynamics that you should know about

We can unleash the power of complex models and data science using PySD package.

System dynamics is an approach to the study of complexity. Developed by Jay Forrester, system dynamics is a method to help managers and policymakers design and implement high impact policies based in insightful models. Usually, its representation is through causal loop diagrams and stock and flow maps in further stages of the methodology. A simple system dynamics model can be represented as following:

Stock and flow model for inventory problem.

This type of models are made using Vensim PLE software for educational propuses, which generates a model file .mdl for simulation. Now we can use PySD package for established a python environment for the simulation model. This can be made using the pip command in jupyter notebook:

PySD Package.

The next command lines import pysd package and load the inventory model file, if the inventory file is printed, it returns a translated .py model.

Load and import the inventory file.

Using the doc() method in inventory model, this will generate all model elements, that includes values, equations, units, names and even comments in the original model file.

Inventory model elements.

The way for running the simulation model with the default parameters stored in the original model is using the run() command. This returns a Pandas dataframe for each timestamp provide for the inventory model.

Running the inventory model.

The obtained dataframe can be explored based on the same commands using for Pandas dataframe, for example, the exploration plotting for some columns is similar using matplotlib:

Simple plotting using matplotlib for simulation model.
Inventory over time.

There are several useful aspects if it is necessary to access some components in the dataframe. Using the run() command, We can specify important components and return them in another dataframe using the return_columns argument, also We could use the return_timestamps argument if we want irregular timestamps in the model.

inventory.run(return_columns=[‘TIME’, ‘Adjustment time’, ‘Net production rate’,’Inventory’])

inventory.run(return_timestamps=[0, 1, 3, 4, 7, 11, 18, 29, 32, 45])

One of the most important features found in the PySD package is the access to parameters and modify them, just like when using a Vensim model, it allows to the model to know its behaviour under different circumstances, the goal is making better policies through the knowledge of the system. For doing this, the run() command gives us a method in the params argument, this argument expects a dictionary where keys are the components on the model, if We want to change the value of the adjustment time variable from 5 to 10 in the inventory model, We proceed this way:

inventory.run(params={‘Adjustment time’: 10})

Inventory over time with change in adjustment time.

If We need that the adjustment time variable changes over time to see the behaviour, We can give to the run() command a set of Pandas series values, PySD package will interpolate between the values.

Finally, We can set the initial conditions of the inventory model using the default value for the initial_condition keyword argument, which is ‘original’. This value runs the model from the initial conditions that were specified originally by the .mdl model file. We can alternately specify a tuple containing the start time and a dictionary of values for the adjustment time variable:

inventory.run(initial_condition=(0,{‘Adjustment time’:20}))

In conclusion, System Dynamics centers in show how organizational structure, results based in policies and time delays interact to influence the outputs of a system. The range of applications includes health economics, population dynamics, ecosystem among others. Using PySD Python package as complement of this methodology, become System dynamic models robusts and full of posibilities when interact with other packages like Pandas, Numpy and Matplotlib.

For further information about PySD package see: https://readthedocs.org/projects/pysd/downloads/pdf/latest/

For further information about System Dynamics methodology see: https://www.amazon.com/-/es/John-Sterman/dp/007238915X

Research Professor and data enthusiast