Equations Inwards Stock-Flow Consistent Models
I had some communications amongst a reader Adam K. who is doing some piece of work on stock-flow consistent (SFC) models. He had some questions almost the equations together with variables inwards the Python sfc_models framework -- as described inwards my latest book.
One of the things I noticed belatedly inwards the formatting phase of the majority is that I did non give a detailed explanation of the algorithms that generate the equations. This was non alone an oversight: I wanted the majority to live last updates to the code, together with the equation generation algorithms are a target for a major refactoring. This article explains the electrical current situation, together with how it developed. The involve for an easily extensible equation generation algorithm trumped the want for formality. The construction of SFC models makes extremely formal procedures fairly brittle.
In particular, at that spot are end-to-end tests that validate that sfc_models generates the right finally output for a number of (simple) benchmark models from Godley together with Lavoie's Monetary Economics. No affair how nosotros generate the equations, the key output variables should draw upwards against known results.
Therefore, nosotros tin sack rip apart the equation generation code, together with know whether the novel version is generating the right output at every measuring of the way.
(More realistically, I would a create a novel version of the code, together with so prepare it until it passes all of the existing tests.)
In the absence of these end-to-end tests, whatever changes to the equation generation code would effectively create an alone novel software package, together with so it would accept a lot of yell hoc tests to meet whether it remains backwards compatible. The evolution fourth dimension could live almost every bit long every bit the fourth dimension every bit it took to create the existing bundle inwards the kickoff place. (A lot of one-time schoolhouse software projects were built inwards this fashion, together with this explains the notorious inability of software to live delivered on time.)
Take the classic model SIM, which I take away keep written almost extensively, which is the simplest recognisable economical model. The commons sfc_models implementation currently has 32 variables, although that includes some purely decorative variables (two fourth dimension axis variables, financial balance, etc.).
However, if nosotros treated the organisation using criterion systems theory, amongst the input variable beingness the exogenous authorities consumption variable, the organisation collapses to a linear state-space organisation amongst a unmarried province variable (the previous catamenia coin residuum held yesteryear the identify sector). That is, given the inherited coin balance, together with the electrical current catamenia grade of authorities consumption, nosotros tin sack calculate every other variable (including the end-of-period coin balance, which goes into the side yesteryear side period's calculation).
Based on some analysis I take away keep seen, this leads some people amongst physical sciences or engineering into a trap. They practise what they e'er do: strip downwards the mathematical organisation to its simplest state-space form, together with stance that every bit "the model." This ignores a critical job amongst province infinite models: they are a completely non-robust agency of looking at organisation dynamics.
Any departure of whatever defining equation volition inject novel dynamics into the system, presumably adding novel province variables. The dimensionality of the province infinite changes, together with at that spot is no agency to compare the master dynamics to the novel one.
Defining the organisation every bit circuit elements together with plopping the organisation into a circuit CAD bundle is non much of a help, fifty-fifty if the CAD bundle tin sack practise frequency domain analysis. The organisation volition withal collapse to the same state-space model, together with almost all of the dynamics implied yesteryear the equations were thrown out.
In summary, nosotros are stuck amongst having to driblet dead along all of the equations if nosotros want to practise the analysis properly. The fact that the equations collapse to a province infinite representation amongst a unmarried province variable inwards the baseline model is real useful if yous insist on doing equations yesteryear hand, every bit Godley together with Lavoie did inwards the textbook. However, this hand-derived collapsed model may deport no resemblance to the model which results from the slightest tweak to the model. (Doing equations yesteryear mitt is an one-time schoolhouse academic thing across disciplines. The electrical engineering curriculum caught upwards to the existence of digital computers a couplet of decades ago, together with they instantly tend to only inflict that on undergraduates inwards their kickoff couplet of years. If all yous take away keep are analog computers together with slide rules -- a shape of analog figurer -- yous take away keep to practise equations yesteryear hand.)
For my purposes, I needed a framework where I could easily modify the model structure. One strategy would live to write downwards an insanely complex model, together with so aspect at what happens when nosotros brand simplifying assumptions. Since that is an extremely brittle strategy, I opted for a framework where nosotros tin sack switch around the sectors inside a country, together with where nosotros tin sack driblet inwards other countries if desired. Such a framework could easily live extended towards a "build a model" graphical user interface.
If I were stuck amongst the one-time schoolhouse figurer languages I learned when I was younger, such a framework would take away keep been implemented amongst a giant heap of spaghetti code -- "if this sector is inwards the model, practise this..., else ...". Such an implementation would piece of work for the simplest models, but the code complexity would explode every bit novel types of sector behavior would live added. The organisation would inexorably march towards uselessness.
Using object oriented techniques, nosotros tin sack isolate the implementation of each sector, greatly simplifying code structure. New sectors tin sack occupation existing sectors every bit initial templates, together with changes to ane are naturally isolated from others. This makes the projection feasible.
The side yesteryear side number is: where does the equation generation logic reside?
For example, the amount of financial savings inwards the catamenia -- a flow -- is equal to the modify of the stock of financial assets. We could infer the savings inwards each catamenia yesteryear only looking at the modify inwards the stock, together with driblet the savings from our province infinite representation. However, this would non piece of work if for some ground nosotros wanted to piece of work amongst the previous period's saving; nosotros would involve to create a novel variable that stores that value for the electrical current period's calculation. (I cannot intend of an obvious occupation for that information, but it is to a greater extent than obvious for some flow variables, such every bit income.)
Since nosotros practise non know inwards advance which flow variables nosotros involve to reclassify every bit province variables, nosotros practise non gain a lot yesteryear treating them differently. In fact, the framework makes no formal distinction betwixt stock together with flow variables, since that distinction does non take away keep whatever operative number on the solution to the model. (Users could add together a ornament to distinguish the variables, but that would only live useful for making a prettier output.) From a systems engineering perspective, my initial instinct would live that nosotros take away keep to strictly split upwards province variables from other variables, However, every bit I worked amongst these models, I came to the stance that nosotros involve to live flexible when working amongst the model. Once a especial model is frozen mathematically, nosotros tin sack live to a greater extent than formal, but most of the code has to piece of work amongst the model when the equation construction is withal largely arbitrary.
For example, a sector may involve to occupation its after-tax income every bit a variable inwards diverse equations (e.g., consumption function). However, it does non know the exact shape of the equation that determines its after-tax income until the model is finalised. For example, it could live inwards a model without income taxes, together with so at that spot is no taxation term inside the after-tax income equation. This incertitude is handled inwards the introduce framework yesteryear the sector only declaring the existence of an after-tax income variable together with so using that variable inside equations; it lets the framework create total inwards the missing equation.
In fact, nosotros cannot unremarkably distinguish betwixt "inputs" together with "state variables" inwards these models. We tin sack modify a parameter value from beingness fixed for all fourth dimension to having a daze applied at some fourth dimension point: nosotros take away keep promoted the variable from beingness a (constant) endogenous variable to beingness an exogenous input! Such a modify completely alters the province infinite representation of the model.
The only formal distinction betwixt variables (other than exogenous/endogenous) inside the framework is that a heuristic is used to prune "decorative" variables at each fourth dimension measuring earlier solving the organisation equation (but later the model construction is frozen). For example, at that spot are a lot variables that are of accounting involvement -- such every bit the financial deficit -- that may take away keep no number on behavior on a especial model implementation. Such variables are pruned. However, if nosotros want to model the number of dimwitted financial hawks, nosotros would take away keep behavior affected yesteryear the grade of the financial deficit, together with so nosotros cannot driblet the variable from the organisation earlier the solution step.
The financial interactions betwixt sectors -- mediated yesteryear the internet holdings of financial assets (F) -- is handled almost alone yesteryear the Market and TaxFlow sectors. (Interactions betwixt sectors is what generates monetary inflows together with outflows.) These equations largely comprehend most of the give-and-take of stock together with flow variables. We could intermission out these variables every bit beingness special, but that would live largely decorative, together with non assist the model solution.
The behavioural variables are the to a greater extent than interesting. In most cases, nosotros would create novel sectors yesteryear adding novel behavioural rules; the non-behavioural relationships stay the same. For example, nosotros plop inwards a novel consumption business office into the identify sector, together with meet what happens.
To live fair, this is pretty closed to the existing behaviour. All yous involve to practise to create a novel Sector is to sub-class it, together with so modify out the behavioural equations yous wish. The novel sector volition inherit all of the non-behavioural relationships from its raise implementation. As such, a formal re-definition mightiness live of mathematical interest, but would take away keep almost no number on workflow.
(c) Brian Romanchuk 2017
One of the things I noticed belatedly inwards the formatting phase of the majority is that I did non give a detailed explanation of the algorithms that generate the equations. This was non alone an oversight: I wanted the majority to live last updates to the code, together with the equation generation algorithms are a target for a major refactoring. This article explains the electrical current situation, together with how it developed. The involve for an easily extensible equation generation algorithm trumped the want for formality. The construction of SFC models makes extremely formal procedures fairly brittle.
Refactoring?
The fact that I am non peculiarly concerned almost the potential for a re-write of the equation generation code -- a kernel constituent of the library -- reflects the forcefulness of the tools used to write the framework. The entire code base of operations is wrapped inwards unit of measurement tests, which ensure that code e'er generated the same target results, regardless to changes elsewhere. (If nosotros want to modify the functioning of a especial function, nosotros involve to modify its associated tests. The thought is that this volition non take away keep a ripple number on other tests.)In particular, at that spot are end-to-end tests that validate that sfc_models generates the right finally output for a number of (simple) benchmark models from Godley together with Lavoie's Monetary Economics. No affair how nosotros generate the equations, the key output variables should draw upwards against known results.
Therefore, nosotros tin sack rip apart the equation generation code, together with know whether the novel version is generating the right output at every measuring of the way.
(More realistically, I would a create a novel version of the code, together with so prepare it until it passes all of the existing tests.)
In the absence of these end-to-end tests, whatever changes to the equation generation code would effectively create an alone novel software package, together with so it would accept a lot of yell hoc tests to meet whether it remains backwards compatible. The evolution fourth dimension could live almost every bit long every bit the fourth dimension every bit it took to create the existing bundle inwards the kickoff place. (A lot of one-time schoolhouse software projects were built inwards this fashion, together with this explains the notorious inability of software to live delivered on time.)
Equations/Variables inwards sfc_models
The job amongst SFC models is that they take away keep agency also many variables (and hence, equations defining them).Take the classic model SIM, which I take away keep written almost extensively, which is the simplest recognisable economical model. The commons sfc_models implementation currently has 32 variables, although that includes some purely decorative variables (two fourth dimension axis variables, financial balance, etc.).
However, if nosotros treated the organisation using criterion systems theory, amongst the input variable beingness the exogenous authorities consumption variable, the organisation collapses to a linear state-space organisation amongst a unmarried province variable (the previous catamenia coin residuum held yesteryear the identify sector). That is, given the inherited coin balance, together with the electrical current catamenia grade of authorities consumption, nosotros tin sack calculate every other variable (including the end-of-period coin balance, which goes into the side yesteryear side period's calculation).
Based on some analysis I take away keep seen, this leads some people amongst physical sciences or engineering into a trap. They practise what they e'er do: strip downwards the mathematical organisation to its simplest state-space form, together with stance that every bit "the model." This ignores a critical job amongst province infinite models: they are a completely non-robust agency of looking at organisation dynamics.
Any departure of whatever defining equation volition inject novel dynamics into the system, presumably adding novel province variables. The dimensionality of the province infinite changes, together with at that spot is no agency to compare the master dynamics to the novel one.
Defining the organisation every bit circuit elements together with plopping the organisation into a circuit CAD bundle is non much of a help, fifty-fifty if the CAD bundle tin sack practise frequency domain analysis. The organisation volition withal collapse to the same state-space model, together with almost all of the dynamics implied yesteryear the equations were thrown out.
In summary, nosotros are stuck amongst having to driblet dead along all of the equations if nosotros want to practise the analysis properly. The fact that the equations collapse to a province infinite representation amongst a unmarried province variable inwards the baseline model is real useful if yous insist on doing equations yesteryear hand, every bit Godley together with Lavoie did inwards the textbook. However, this hand-derived collapsed model may deport no resemblance to the model which results from the slightest tweak to the model. (Doing equations yesteryear mitt is an one-time schoolhouse academic thing across disciplines. The electrical engineering curriculum caught upwards to the existence of digital computers a couplet of decades ago, together with they instantly tend to only inflict that on undergraduates inwards their kickoff couplet of years. If all yous take away keep are analog computers together with slide rules -- a shape of analog figurer -- yous take away keep to practise equations yesteryear hand.)
How Does sfc_models Build Equations?
Once yous realise that yous take away keep to laid upwards all the equations, SFC models start to aspect ugly. The number of equations ends upwards beingness real large. Given my aversion to mitt calculations, I needed a organisation to generate the equations. Hence, sfc_models was born.For my purposes, I needed a framework where I could easily modify the model structure. One strategy would live to write downwards an insanely complex model, together with so aspect at what happens when nosotros brand simplifying assumptions. Since that is an extremely brittle strategy, I opted for a framework where nosotros tin sack switch around the sectors inside a country, together with where nosotros tin sack driblet inwards other countries if desired. Such a framework could easily live extended towards a "build a model" graphical user interface.
If I were stuck amongst the one-time schoolhouse figurer languages I learned when I was younger, such a framework would take away keep been implemented amongst a giant heap of spaghetti code -- "if this sector is inwards the model, practise this..., else ...". Such an implementation would piece of work for the simplest models, but the code complexity would explode every bit novel types of sector behavior would live added. The organisation would inexorably march towards uselessness.
Using object oriented techniques, nosotros tin sack isolate the implementation of each sector, greatly simplifying code structure. New sectors tin sack occupation existing sectors every bit initial templates, together with changes to ane are naturally isolated from others. This makes the projection feasible.
The side yesteryear side number is: where does the equation generation logic reside?
- One strategy is to centralise the equation generation structure. Each sector makes its contribution to the structure, but at that spot is ane block of code setting the rules.
- The side yesteryear side is to decentralise the equation generation. There is a fundamental container for all equations, but each sector does its contribution to the listing of equations without worrying almost rules.
Since I had a difficult fourth dimension discerning the formal principles for equation generation, I opted for the decentralised approach. This is why at that spot is no formal construction for equation generation; from a casual glance at the Model object code, the equation generation algotrithm looks similar a free-for-all.
However, such a description is slightly misleading: at that spot are some basic principles inwards play. Most recognisable economical sectors -- households, businesses, governmental entities -- define their ain behavior using variables that are internal to that sector. For example, a identify sector typically has a consumption business office that depends upon its ain income, together with its ain stock of financial assets. Occasionally, the sector has to aspect upwards variables from elsewhere: perchance it needs the charge per unit of measurement of involvement to decide its portfolio allotment decision.
Other "sectors" grip the interaction betwixt the economical sector. (They are defined every bit "Sector" objects inwards the code, together with at a high grade are indistinguishable from economical sectors, but they are non sectors of the economic scheme every bit recognised yesteryear economists.) The most of import of these are Market objects: they align render together with demand inwards each market, together with stitch together the cash together with commodity flows betwixt sectors. This includes the labour market; it matches upwards the render together with demand for labour (of a given type, a multi-sector labour marketplace tin sack live implemented), together with it ensures that businesses pay the wages, together with the identify sector gets the reward added to their pre-tax income. The other major object to grip interactions is the TaxFlow object, which manages the taxation rules laid yesteryear the government.
This creates full general principles of how sectors are supposed to generate equations. The sectors packaged amongst sfc_models obey these principles, together with deed every bit a template for user extensions. However, users are gratis to plop inwards equations every bit they want inwards guild to brand extensions; they manifestly practise so at their ain risk. This flexibility is useful: it is a lot easier to brand a quick examine of something yesteryear sneaking inwards a few arbitrary equations than it is to create novel sectors. There is no betoken spending a lot of fourth dimension edifice a novel sector model if the equations volition non work, so doing feasibility analysis is a useful kickoff step.
However, these are only software engineering principles, at that spot is no formal construction to the equations. Since I concord myself out every bit a real serious formal mathematician, what's upwards amongst that?
Classification of Variables
One obvious classification is that nosotros should split upwards variables into province variables versus other variables that tin sack live inferred from the province variables together with inputs. In my give-and-take amongst Adam K., he argued that if nosotros are looking at flows together with stocks, only stock variables should live province variables; nosotros tin sack infer flows from stocks. (Other variables, such every bit expectational variables together with prices, may acquire province variables.) Unfortunately, this mightiness live truthful for some models, but non all.
For example, the amount of financial savings inwards the catamenia -- a flow -- is equal to the modify of the stock of financial assets. We could infer the savings inwards each catamenia yesteryear only looking at the modify inwards the stock, together with driblet the savings from our province infinite representation. However, this would non piece of work if for some ground nosotros wanted to piece of work amongst the previous period's saving; nosotros would involve to create a novel variable that stores that value for the electrical current period's calculation. (I cannot intend of an obvious occupation for that information, but it is to a greater extent than obvious for some flow variables, such every bit income.)
Since nosotros practise non know inwards advance which flow variables nosotros involve to reclassify every bit province variables, nosotros practise non gain a lot yesteryear treating them differently. In fact, the framework makes no formal distinction betwixt stock together with flow variables, since that distinction does non take away keep whatever operative number on the solution to the model. (Users could add together a ornament to distinguish the variables, but that would only live useful for making a prettier output.) From a systems engineering perspective, my initial instinct would live that nosotros take away keep to strictly split upwards province variables from other variables, However, every bit I worked amongst these models, I came to the stance that nosotros involve to live flexible when working amongst the model. Once a especial model is frozen mathematically, nosotros tin sack live to a greater extent than formal, but most of the code has to piece of work amongst the model when the equation construction is withal largely arbitrary.
For example, a sector may involve to occupation its after-tax income every bit a variable inwards diverse equations (e.g., consumption function). However, it does non know the exact shape of the equation that determines its after-tax income until the model is finalised. For example, it could live inwards a model without income taxes, together with so at that spot is no taxation term inside the after-tax income equation. This incertitude is handled inwards the introduce framework yesteryear the sector only declaring the existence of an after-tax income variable together with so using that variable inside equations; it lets the framework create total inwards the missing equation.
In fact, nosotros cannot unremarkably distinguish betwixt "inputs" together with "state variables" inwards these models. We tin sack modify a parameter value from beingness fixed for all fourth dimension to having a daze applied at some fourth dimension point: nosotros take away keep promoted the variable from beingness a (constant) endogenous variable to beingness an exogenous input! Such a modify completely alters the province infinite representation of the model.
The only formal distinction betwixt variables (other than exogenous/endogenous) inside the framework is that a heuristic is used to prune "decorative" variables at each fourth dimension measuring earlier solving the organisation equation (but later the model construction is frozen). For example, at that spot are a lot variables that are of accounting involvement -- such every bit the financial deficit -- that may take away keep no number on behavior on a especial model implementation. Such variables are pruned. However, if nosotros want to model the number of dimwitted financial hawks, nosotros would take away keep behavior affected yesteryear the grade of the financial deficit, together with so nosotros cannot driblet the variable from the organisation earlier the solution step.
The financial interactions betwixt sectors -- mediated yesteryear the internet holdings of financial assets (F) -- is handled almost alone yesteryear the Market and TaxFlow sectors. (Interactions betwixt sectors is what generates monetary inflows together with outflows.) These equations largely comprehend most of the give-and-take of stock together with flow variables. We could intermission out these variables every bit beingness special, but that would live largely decorative, together with non assist the model solution.
Behavioural versus Non-Behavioural
If I were to laid on the formalisation of the structure, I would separate variables every bit either behavioural or non-behavioural. Non-behavioural equations would include the accounting identity together with stock-flow relationships that SFC modellers dear writing about, but also things similar production functions together with fifty-fifty the Definition of the fourth dimension axis.The behavioural variables are the to a greater extent than interesting. In most cases, nosotros would create novel sectors yesteryear adding novel behavioural rules; the non-behavioural relationships stay the same. For example, nosotros plop inwards a novel consumption business office into the identify sector, together with meet what happens.
To live fair, this is pretty closed to the existing behaviour. All yous involve to practise to create a novel Sector is to sub-class it, together with so modify out the behavioural equations yous wish. The novel sector volition inherit all of the non-behavioural relationships from its raise implementation. As such, a formal re-definition mightiness live of mathematical interest, but would take away keep almost no number on workflow.
Influenza A virus subtype H5N1 Note on the Solver
Following the proposition of Adam K., I may aspect into adding inwards the MINPACK solver from the scipy Python package. The user would take away keep the selection of switching over to its use, assuming that scipy is installed. I volition in all probability leave of absence inwards my clunky iterative solver inwards the bundle so that the user tin sack run basic examples without whatever external dependencies. (The reward of the electrical current setup for me is that I know how to occupation it to reveal badly-posed equations; I would involve to add together debugging hooks for the novel solver.)(c) Brian Romanchuk 2017
No comments