Recent Posts

Monday, October 17, 2016

SFC Models Library For Python

I have been "in the zone" for coding the last little while, and have an initial version of a Python library to analyse Stock-Flow Consistent models. I still need to work on making the package easier to install, so I will wait to give a fuller description when that is ready. Since I am supposed to be finishing a book, there are no guarantees when that will happen. (Although I expect to use this module within the following book, so the delay will not be too long.)

What I Hope the Module Will Accomplish

The site sfc-models.net has a code repository for solving SFC models. My impression is that the preferred technique in academia is to use Eviews to solve them. Although Eviews is designed to be used by economists, it has two drawbacks: it is not open source, and the Eviews programming language has features that would raise the eyebrows of most computer scientists.

The difficulty with the current methodology is that the researcher needs to develop highly complex models, with oodles of variables, by hand, and then transfer those equations to Eviews. Such a methodology can obviously be made more user-friendly. However, academics need access to the underlying equations so that it looks like they know what they are doing within journal articles.

I want to use a real programming language (Python, which is open source -- that is, can be downloaded at no charge) to take the drudgery out of this work. (I did a thesis in the early 1990s doing analytic closed-form mathematics; I swore never to do that again.)

There are two "killer apps" I hope to implement in this package.
  1. Machine interpretation of mathematical formulae written within formatted text blocks. That is, you can implement a mathematical model with almost no programming skills required.
  2. Machine-generated equations. The user sets up the sectors of an economy -- which may have multiple countries -- and the module generates the equations for you. (These equations can then be solved.) For example, I could set up the equivalent of the two country model from Chapter 5 of Monetary Economics without typing in 37 equations consisting of sub- and super-scripted variables.)
The first step was achieved (as noted below); whether I can achieve #2 remains to be seen. 

I would note that there is already a set of Python programs that implement the models from Godley & Lavoie's text: https://github.com/kennt/monetary-economics. I have spent almost no time looking at that project. Although interesting, it is a development direction that I do not want to follow.

One could try to achieve this another programming language. Matlab(tm) being another favourite within academia; however, Matlab is not free. Furthermore, it is straightforward to integrate Python modules into other development systems.

Machine Interpretation of Existing Equations


This is the code needed to generate the SIM model from Godley & Lavoie (Chapter 3):
(Available at GitHub if the above embedding does not work: https://gist.github.com/brianr747/a1bb4b613fbfd012da2497c26bd43910.)

 In case it is not obvious, the guts of the "code" is actually a documented string which is an almost direct replication of the mathematics within the text. (I had to clean up some of the equations, as noted.)

My package generates a new file ("SIM_model.py") which solves the system of equations.

(In case you are wondering, this package cannot solve any system of equations; it will only work on equations that are structured similar to that of model SIM. I hope to extend the solving package (or grab off-the-shelf solvers) to make the system more robust.


Machine-Generated Equations

It remains to be seen whether I can accomplish this, but it seems that I should be able to develop an algorithm to generate the equations for a SFC model based on a easily-understood high level description of a SFC model.

Once the algorithm is in place, one could imagine setting up a model economy by just dragging and dropping models of sectors of the economy into place, and setting up the interactions between sectors.

Code on GitHub

The code is available on GitHub: https://github.com/brianr747/SFC_models.

For an experienced Python programmer, that would be enough to get started. (Note: there is a setup.py script in the development branch.)

For those of you who are new to Python, I am working on creating an installation module. Once that installation module is completed, I will post installation instructions, and give a more formal introduction.

If you think you would be interested, I would first suggest installing Python (Version 3.3 or later), and trying some basic tutorials. As can be seen in my source code, you might be able to build some basic models solely by messing around with equations in the generating string (if you can install the package).

To get the plotting to work, you will need to install the matlibplot package, which can be tricky. The code that creates the plots are found in the examples directory: https://github.com/brianr747/SFC_models/blob/master/examples/plot_for_examples.py

Ugly Installation Instructions

For a cleaner installation method (before I have the install package), please see my article on Git and Github. However, I will give you the ugly method for impatient readers.
  1. On Github, switch to the "development" branch, instead of "master" - see the control at the upper left. (The "master" branch represents "production" versions of the code, "development" is the latest version.) Direct link to development: https://github.com/brianr747/SFC_models/tree/development
  2. Use the download option (on the upper right) to download as a zip file (tarball in linux?).
  3. Unzip the files somewhere useful.
  4. Run "setup.py" on the command line (double-clicking it in Windows might work): "python setup.py". (You may need to give the path to the Python executable, which varies.)
  5. The run_SIM_machine_generated.py script in the "examples" directory should now work (although you will need to install matlibplot to see plots).

Concluding Remarks

The code is in an extremely preliminary state. I just wanted to make this announcement so that any readers interested in economic modelling consider looking into using Python.

(c) Brian Romanchuk 2016

No comments:

Post a Comment

Note: Posts are manually moderated, with a varying delay. Some disappear.

The comment section here is largely dead. My Substack or Twitter are better places to have a conversation.

Given that this is largely a backup way to reach me, I am going to reject posts that annoy me. Please post lengthy essays elsewhere.