Agent-based Market Model
Excerpt source code available in my Github
This is my largest agent-based modeling for a realistic economy model I developed since 2018. At first this was only a part of my project for building a sustainable and realistic economy game. Later I find that the design of the macroeconomy simulation is already an interesting project. Since then I gradually added more and more components in the model. It aims at studying the interaction among consumers, firms, banks within and across countries.
Algorithm
The simulation includes five major components:
Firms (shops): They are the supplier and consumer of material inside the country. They can also sell their products in other countries based on the greedy algorithm.
Consumers: They are responsible for buying the end-tier products in the supply chain. Their income is determined by the number of working population. This makes sure the money (M1) is conserved in the game.
Fund: They add act a sink for the residue cash in the game. For company which has a surplus of cash, they put their extra cash into this fund as an index to the prosperity of the society.
Good/Service market: This is the central place where all non-currency transactions take place. It uses the single order-book system to keep track of all the pending orders. In this simulation, consumers always buy within the same countries but suppliers can choose to sell in any country they find competitive.
Currency market: This is another central place where all currency transactions take place. When firms sell their product in another countries, they use this market to exchange their profit to their local currencies.
Routine
In the simulation, one step corresponds to one business day (therefore 251 business days per year). The firms, banks and markets take turns for their preparation and operations. Some important ones are:
Morning
Firms decide their production cycle, purchase, and sales of their production
Firms send their order to the market
Consumers allocate their budget for purchase
Afternoon
Good market and currency market process all orders and deliver payments and goods.
Firms pay wages to workers (i.e. consumer)
Firms decide their investment and payment of tax
There are other routines by the bank, firms in the monthly and annual basis, such as:
Expansion/Default of the firm
Announcement of fiscal policy
Random Events
The majority of the simulation is based on deterministic rules. To allow diversity in the simulations, random events are triggered spontaneously to create "shocks" in the system, and we studied how the agents adopt to these events. They include:
Change in budget allocation (as a change of consumer taste to different types of product)
Change in fiscal policy (the flow velocity of cash from bank to the system)
Good Market
The dynamical evolution of demand and supply constantly changes the prices of products in the market. The price is output as ohlc (open-high-low-close) structure every 21 steps (days, i.e. a month). For simplicity I only plot the close price.
The difference of two types of product can be clearly seen. Some common service, like research, has a similar shape in its price, meaning that the price is closer to equilibrium, and differs due to the currency exchange rate. For some more advanced product, the demand and supply shows deviation among countries. This suggests that the price is more elastic due to the scarcity of supply.
Sudden changes of the price usually corresponds to a change of fiscal policy, where the consumers become richer/poorer for their consumable.
Forex Market
Another feature in this agent-based model is that agents can sell their product in one of the market of neighbouring cities. In this program, when the net price of goods in neighbouring cities is higher, shops will sell the goods in those cities. As a result, the shop earns foreign currency. Then the shops will exchange the earned foreign budget back to its local currency, so that it can buy new material for the next round.
For illustration purpose, I set the liquidity comparable to the goods to make the effect more visible. W can see there are short pleateau when there is no economy state change. But when it changes, it leads to rapid rise or drop (~20 - 40%) in the exchange rate.
Production
I also completed a GUI interface so that I can understand the current flow of product and currency in the system. This will assist me to get more insight about the complex interaction among all agents, so that I can improve the models to reproduce more realistic behaviour.
The design of my GUI attempts to be as simple as possible.