Simple Agent-based Macro Model in Python

Alex Linnen

 

Links to files can be found at the bottom of the post.

Summary:

The purpose of this project is to familiarise myself with the tools available in Python to create agent-based models. Python offers many advantages over other languages, due to it’s simplicity and vast library of data science packages. I reviewed several Python packages before starting this project, but eventually decided on Mesa. This is a package for creating agent-based models in Python, in a similar manner to the JAS package used in Java.

This model contains four modules:

  • model.py – contains the model class and initialises values, as well as some useful functions for retrieving and manipulating data.
  • agents.py – contains the agent classes for both households and firms.
  • server.py – constructs the user interface.
  • run.py – launches the model.

The project folder can be found on Github. Click here to view and download it.

August 2024:

I have completed a draft of porting the  Lengnick (2013) macro model into Python. This presented various challenges as a novice programmer, as the package used (Mesa) lacked some of the features of JAS such as the ability to run processes in a monthly schedule. I managed to recreate this using my own method.

The model runs and carries out the monthly processes as the original Java project, however it is a much simpler process for the end user. Instead of compiling code and loading it into a Java program, you can simply run the process and a browser window opens presenting the interface:

 

As you can see, there are line charts for variables that are updated with each step of the model being run, I have kept these the same as in the Java model, plotting Price, Wage, Open Positions, Employment and Production. I have also added Household and Firm liquidity, although these are not particularly relevant in the base model.

Another thing to note, although most of the variables follow a similar pattern to the Java model, I was unable to recreate the endogenous shocks that occur to production in the original model. This could be for a variety of reasons, I suspect potentially because the different ways that Java and Python handle random number generators. As this is just an example to show how Mesa can be used to create agent-based models, I do not consider it worth investigating at this time and would prefer to move on to more complex projects.

Files:

The project can be run simply by downloading a .exe file. This will open Terminal, followed by a browser window from which you can navigate the model observer. Once finished, make sure to close the Terminal window to fully exit the process.

You can also download the Python project files either from Github linked above, or the following .zip file:

Categories
Contact

For any further information, please contact scher@essex.ac.uk