Abce Python Library
Abce Python Library
Abce Python Library
net/publication/317645628
CITATIONS READS
0 1,506
8 authors, including:
Adrián Carro
University of Oxford
15 PUBLICATIONS 148 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Davoud Taghawi-Nejad on 18 June 2017.
1 Introduction
An economy is a complex system where the interaction of heterogeneous agents
plays a crucial role. However, current economic models used by academics, the
2 Davoud Taghawi-Nejad et al.
central banks and policy makers fail to acknowledge its complexity and are
either based on empirical statistical models fitted to past data (econometrics)
or assume a perfect world (DSGE) and by their very nature rule out crisis [4].
However, agent-based models (ABM) can take into account of this complexity
since they are expressive computer simulations of heterogeneous agents which
interact across different spatial and temporal scales according to predefined rules.
When modeling an economy, agents represent firms, households, banks and other
important entities that define the systems dynamic.
In social sciences and in particular in economics where designing replicable
experiments is most of the times impossible, agent-based models present a unique
opportunity for policy testing. To give true insights on economic phenomena,
a large number of heterogeneous agents and often stock-flow consistency7 are
crucial. Here we introduce the Agent-Based Computational Economics library
(ABCE) a Python library with the computational efficiency and accounting im-
plementation needed for state of the art ABMs. ABCE has been continuously
been updated and extended for 6 years after its conceptual framework was un-
veiled in [13].
While [13] discusses the theory that agent-based modeling is a language to
express economic phenomena as dynamical processes, this paper gives a practical
overview over how and why to use ABCE. It also incorporates the many updates
that have been introduced in ABCE.
ABCE8 is a Python-based modeling platform for economic simulations and
part of the Economic Simulation Library9 . For simulations of trade, production
and consumption, ABCE comes with standard functions that implement these
kinds of interactions and actions. The modeler only implements the decision
logic of an agent, then ABCE takes care of all exchange of goods and production
and consumption. Furthermore it can also handle contracts and even generate
balance sheets.
One special feature of ABCE is that goods have the physical properties of
goods in reality. In other words if agent A gives a good to agent B, then -
unlike information - agent B receives the good and agent B does not have the
good anymore. That means that agents can trade, produce or consume a good.
The ownership and transformations (production or consumption) of goods are
automatically handled by the platform.
The audience of ABCE are economists, computer scientists, mathematicians,
scientists conducting interdisciplinary research, and people of similar areas that
want to create agent-based models involving trade, production or contracts. It
is especially geared towards simulations that are similar to standard economic
models such as general or partial equilibrium models10 but allow the assumptions
to be relaxed to the extent of removing equilibrium. Current development efforts
7
Meaning that goods and money are not created “out of thin air”. ABCE is stock
flow consistent, if the stock-flow consistency is not explicitly broken
8
https://abce.readthedocs.io
9
https://economicsl.github.io
10
Without the equilibrium, of course.
ABCE: A Python Library for Economic Agent-based Modeling 3
2 Design
ABCEs main design goal is that code can be rapidly written to enable a modeler
to quickly write down code and quickly explore different alternatives of a model.
In Python, variables do not have to be declared, garbage does not have to be
collected and classes have no boiler-plate code. Another advantage of Python
that facilitates rapid coding is its rich environment of standard libraries. For
example, Python standard library, turtle enables representation of agents in a
spatial world, which allow the modeler to build a spatial model. Installation of
packages is much simpler in Python than in Java, C, or C++.
Execution speed is a secondary concern to the goal of rapid development. Ex-
ecution speed is increased achieved by making use of multiple-cores/processors
and using C (via Cython) for backend tasks. For the user, the library is a pure
python library, with the C backend completely hidden from the user. Although
Python is slower than Java or C in terms of execution speed, this disadvan-
tage can be largely overcome by using various optimized packages for numerical
calculations, such as NumPy[15] and SciPy[5], which make use of backend im-
plementations in C. ABCE allows to parallelize the code and gain significant
speed advantage over single-threaded code.
Thirdly the design is giving full modelling liberty to the programmer. This
is done by not implementing any economic assumptions in the simulation en-
gine of the library. Depending on the use case, a user may decide to not use
the pre-defined agents in the library, and may instead define new agents from
scratch without restrictions. In other words, ABCE is a library that allows trade,
consumption and production of goods but does not force specific economic as-
sumptions.
It is important to note that there are predefined agents (Agent, Firm or
Household) which are Python classes that can be inherited from. This will allow
the modeler to concentrate on defining the behaviour of the agents and the
specification of goods, production/consumption functions. ABCE automatically
handles the communication, trade and consumption of goods.
ABCE allows the modeler to program agents as ordinary Python objects, and
to run the simulation on a multi-core/processor computer without further config-
uration or intervention. The parallel execution of agents is the only non-standard
feature of ABCE, which bypasses Pythons global interpreter lock (GIL). The
4 Davoud Taghawi-Nejad et al.
Physical goods are at the heart of almost every economic model. The core feature
and main difference to other ABM platforms is the implementation of physical
goods. In contrast to information or messages, sharing a good means having less
of it. If agent A transfers a good to agent B then agent A does not have this
good anymore. One of the major strength of ABCE is that this is automatically
handled.
In ABCE goods can be created, destroyed, traded, given or changed through
production and consumption. All these functions are implemented in ABCE
and can be inherited by an agent. These functions are automatically handled by
ABCE upon decision from the modeler.
Every agent in ABCE must inherit from the abce.Agent class. This gives the
agent a couple of stock methods: create, destroy, trade, and give. create and
destroy create or destroy a good immediately. Because trade and give involve
a form of interaction between the agents, they are run over several sub-rounds.
Selling of a good for example works as follow:
• partial acceptance the money is credited and the unsold part of the good
is re-credited
Thirdly, ABCE is just a scheduler that schedules the actions of the agents
and a Python class that enables the agent to produce, consume, trade and com-
municate. A model written in ABCE, is therefore standard Python code and
the modeler can make use of the complete Python language and the Python
language environment.
Fourthly, many frameworks such as FLAME, NetLogo, StarLogo, Ascape
and SugarScape and, in a more limited sense, Repast are designed with spatial
representation in mind. For ABCE, a spatial representation is possible, but not
a design goal. Since agents in ABCE are ordinary Python objects, they can use
Python modules such as turtle from the Python standard library and therefore
gain a spatial representation much like NetLogo. This does not mean that ABCE
could not be a good choice for a problem where the spatial position plays a
role. Particularly if the model has different transport costs or other properties
according to the geographical position of the agents, but the agents do not move
or the movement does not have to be represented graphically, ABCE could still
be a good choice.
5.1 MASON
5.2 NetLogo
agent-based modeling and is widely advertised for its ease of use and appeal to
scholars without prior programming experience. This, however, comes at a price:
As Netlogo is an interpreted language running on top of a Java implementation,
performance and speed of NetLogo simulations are impeded. Further, NetLogo
is heavily centered on visualization and spatial structure, even if the spatial
structure does not have a useful interpretation for the use case at hand.
In contrast to Netlogo, ABCE is not a special purpose language but a library
that can directly be included from programs written in the general purpose lan-
guage Python. It also places much fewer restrictions on the modelers modelling
decisions and will, for instance not enforce the use of spatial (or any other)
structures. That said, NetLogo-style spatial simulations can be implemented in
ABCE/Python by using Pythons turtle library.
5.3 Repast
Repast is a modeling environment for social science. It was originally conceived
as a Java rewrite of SWARM. [3] [8] Repast has API in several flavors: Java, .Net,
and a Python-like language. Repast has been superseded by Repast Simphony
which maintains all functionality, but is limited to Java. Repast Simphony has
a point and click interface for simple models. :raw-tex:citeNORTH2005a Repast
supports static and dynamic scheduling. [3] Repast is vast, which contains 210
classes in 9 packages :raw-tex‘citeCollier‘. ABCE, thanks to its limited scope and
Python, has only 8 classes visible to the modeler in a single package.
5.5 Mesa
Mesa is a modular ABM framework, with a goal to be Python 3-based alternative
to NetLogo, Repast, or MASON. Its specific strength is the spatial representation
of agent positions. Which is the same as in netlogo. ABCE is specifically built
for economic modelling, has core components written in Cython, and has been
optimized for parallel execution.
ABCE: A Python Library for Economic Agent-based Modeling 9
The first step to make an ABM is to define the agents. ABCE provides the pro-
grammer with a predetermined set of benchmark agents and functions. However
the main advantage of ABCE is that gives total flexibility for the modeler to
define its own agents. Therefore we explain how an agent class must be defined.
The agent classes must inherit the base agent (abce.Agent) and possibly the
firm (abce.Firm) or the household class (abce.Household). The base class gives
agents the ability to interact with other agents. That includes amongst others the
ability to send messages and trade. The firm and household classes give agents
the ability to produce and consume. For firm and household agents the modeler
has to specify the functional form of the production or consumption function in
the init method. Below we show an example where a firm is created with
a money budget of 10 and produces ”GOOD” with a Cobb-Douglas production
function.
import abce
# Agent 2
def b u y c o o k i e s ( s e l f ) :
o f fe r s = s e l f . get offers ( ’ cookies ’ )
f o r o f f e r in o f f e r s :
if offer . price < 0.5:
try :
s e l f . accept ( o f f e r )
10 Davoud Taghawi-Nejad et al.
except NotEnoughGoods :
s e l f . accept ( offer ,
( s e l f . p o s s e s s i o n ( ’ money ’ ) /
offer . price ))
Amongst others agent 1 sends an offer of cookies to agent 2. The agent
receives all offers of cookies. She can iterate over all offers and decide, which
offers to accept and which not. Note that the interesting thing here is that the
modeler only implements the decision logic, but the transactions are handled by
ABCE in the background.
Once the agents have been defined, the simulation must be run in a start
file. Here the simulation class is instantiated, which then builds the agents from
the agent classes already defined as explained above. It is in this part that the
simulation parameters and agent parameters are defined as shown in the code
snipet below. The order of execution of agents action has to be specified, as
well as goods and their properties, such as services or perishable, respectively.
Finally, we need to define which observables should be measured throughout the
simulation.
p a r a m e t e r s = { ’ name ’ : ’ 2 x2 ’ ,
’ random seed ’ : None ,
’ rounds ’ : 10}
@gui ( p a r a m e t e r s )
def main ( p a r a m e t e r s ) :
w = S i m u l a t i o n ( rounds=p a r a m e t e r s [ ’ rounds ’ ] )
w . d e c l a r e r o u n d e n d o w m e n t ( r e s o u r c e= ’ a d u l t ’ , u n i t s =1,
p r o d u c t= ’ l a b o r ’ )
w . d e c l a r e p e r i s h a b l e ( good= ’ l a b o r ’ )
w . p a n e l ( ’ h o u s e h o l d ’ , p o s s e s s i o n s =[ ’ money ’ , ’GOOD’ ] ,
v a r i a b l e s =[ ’ c u r r e n t u t i l i y ’ ] )
w . p a n e l ( ’ f i r m ’ , p o s s e s s i o n s =[ ’ money ’ , ’GOOD’ ] )
f i r m s = w . b u i l d a g e n t s ( Firm , ’ f i r m ’ , 1 )
h o u s e h o l d s = w . b u i l d a g e n t s ( Household , ’ h o u s e h o l d ’ , 1 )
f o r r in w . n e x t r o u n d ( ) :
h o u s e h o l d s . do ( ’ s e l l l a b o r ’ )
f i r m s . do ( ’ b u y l a b o r ’ )
f i r m s . do ( ’ p r o d u c t i o n ’ )
f i r m s . do ( ’ p a n e l ’ )
f i r m s . do ( ’ s e l l g o o d s ’ )
h o u s e h o l d s . do ( ’ buy goods ’ )
h o u s e h o l d s . do ( ’ p a n e l ’ )
h o u s e h o l d s . do ( ’ consumption ’ )
if name == ’ main ’:
ABCE: A Python Library for Economic Agent-based Modeling 11
main ( p a r a m e t e r s )
The fact that ABCE is designed to be a library means that the same start.py
file can use other Python libraries. For example we can use a calibration tool
and a statistical package that runs the simulation repeatedly with different pa-
rameters to achieve a certain fit, read from a relational database to calibrate the
agents, frame the agents to move around and gather resources in a 2D/3D mesh
or a graph.
Let us assume for the sake of the argument that we want to simulate actions
that happen in real life in parallel, but we can only run agents sequentially in
the simulation. Since these actions happen in parallel we know they (a) start
with the same information set and (b) cannot affect each other. This has two
implications: they must start with the same information set and the effects on
the other agents must happen after they have all finished, i.e. the information
set can only be updated after all agents actions have been executed.
Generally speaking, multithreading and parallel execution have to cope with
the problem that actions do not necessarily finish at the same time, which has
to be considered when updating the information set.
To address this issue, in ABCE time is discrete and actions happen in lock-
step. Temporal events consist of rounds and sub-rounds. Rounds correspond
to real-time intervals, e.g. days. In each subround all agents execute the same
action. So for example in overnight-loans:
ABCE has several ways of collecting data. For example, agents can log data
directly to the database or the simulation can monitor variables and possessions
of agents and log them collectively to the database. The database is used to
generate graphs and can be used for further processing.
All data that has been collected can be automatically displayed on an inter-
active webpage. What is more the whole simulation can be packaged into a web-
app. In this case, a menu can be displayed in the browser, from which simulation
parameters can be chosen. A user can set the parameters, run the simulation,
and observe the results directly in the browser. Past simulations remain accessi-
ble in the menu. Examples of simulations written in ABCE with interactive web
interface can be accessed in https://www.taghawi-nejad.de/portfolio.
ABCE: A Python Library for Economic Agent-based Modeling 13
9 Conclusion
References
1. Allan, R.: Survey of agent based modelling and simulation tools. Tech. Rep. Octo-
ber (2010), http://purl.org/net/epubs/work/50398
2. Caiani, A., Godin, A., Caverzasi, E., Gallegati, M., Kinsella, S., Stiglitz, J.E.:
Agent based-stock flow consistent macroeconomics: Towards a benchmark model.
Journal of Economic Dynamics and Control 69, 375 – 408 (2016), http://dx.doi.
org/10.1016/j.jedc.2016.06.001
3. Collier, N.: RePast : An Extensible Framework for Agent Simulation
4. Farmer, J.D., Foley, D.: The economy needs agent-based modelling. Nature
460(7256), 685–686 (2009), https://www.nature.com/nature/journal/v460/
n7256/full/460685a.html
5. Jones, E., Oliphant, T., Peterson, P., et al.: SciPy: Open source scientific tools for
Python (2001–), http://www.scipy.org/, accessed: 2017-06-10
6. Luke, S., Cioffi-Revilla, C., Panait, L., Sullivan, K., Balan, G.: Mason: A multiagent
simulation environment. Simulation 81(7), 517–527 (Jul 2005), http://dx.doi.
org/10.1177/0037549705058073
7. Nikolai, C., Madey, G.: Tools of the trade: A survey of various agent based
modeling platforms. Journal of Artificial Societies and Social . . . 12(2) (2009),
http://jasss.soc.surrey.ac.uk/12/2/2.html
8. NORTH, M., HOWE, T., COLLIER, N., VOS, J.: THE REPAST SIMPHONY
RUNTIME SYSTEM. In: Proceedings of the Agent 2005 Conference on Generative
Social Processes, Models, and Mechanisms. p. 151 (2005)
9. Phelps, S.: Applying dependency injection to agent-based modeling: the jabm
toolkit. Tech. rep., Technical Report WP056-12, Centre for Computational Fi-
nance and Economic Agents (CCFEA) (2012), http://www.bracil.net/ccfea/
WorkingPapers/2012/CCFEA-WP056-12-r2.pdf
10. Railsback, S.F., Lytinen, S.L., Jackson, S.K.: Agent-based Simulation Platforms:
Review and Development Recommendations. Simulation 82(9), 609–623 (sep 2006),
http://dx.doi.org/10.1177/0037549706073695
ABCE: A Python Library for Economic Agent-based Modeling 15
11. Rossum, G.v.: Glue it all together with python. In: Position Paper for OMG-
DARPA-MCC Workshop on Compositional Software Architecture (1998), https:
//www.python.org/doc/essays/omg-darpa-mcc-position/
12. Serenko, A., Detlor, B.: Agent toolkits: A general overview of the market and an
assessment of instructor satisfaction with utilizing toolkits in the classroom (2002),
http://hdl.handle.net/11375/5601
13. Taghawi-Nejad, D.: Modelling the economy as an agent-based process : ABCE , a
modelling platform and formal language for ACE . Journal of Artificial Societies
and Social Simulation (2013), http://jasss.soc.surrey.ac.uk/16/3/1.html
14. Tobias, R., Hofmann, C.: Evaluation of free java-libraries for social-scientific agent
based simulation. Journal of Artificial Societies and Social Simulation 7(1) (2004),
http://jasss.soc.surrey.ac.uk/7/1/6.html
15. Walt, S.v.d., Colbert, S.C., Varoquaux, G.: The numpy array: a structure for ef-
ficient numerical computation. Computing in Science & Engineering 13(2), 22–30
(2011)
16. Wikipedia: Comparison of agent-based modeling software — wikipedia, the free en-
cyclopedia (2017), https://en.wikipedia.org/w/index.php?title=Comparison_
of_agent-based_modeling_software&oldid=783176104, [Online; accessed 14-
June-2017]