Microeconomics - Demand and Supply Simulated
Ayush Goel
In Microeconomics, the concept of demand and supply representing the market was a bit confusing to me. I didn't really understand how all producers and consumers would end up coming to a single consensus, and form a stable equilibrium, rather than constantly changing the prices. So I thought of making a simulation to see what actually happens.
In this simulation, I made sure that every consumer and producer starts off with a completely random idea on what the right price should be. It turns out that all producers and consumers actually end up converging to the exact same point! Exactly what microeconomics predicts, so this theory really does work in practice. What's even more interesting is that no matter what consumer and producers initially think the price should be, they will update their beliefs and always end up at the same point!
Here, I want to simulate a few basic concepts related to demand and supply: market equilibrium, taxes, price floors and price ceils.
This is the static version of the simulation. All simulations have been pre-run by me and uploaded as gifs, so you can still see it run.
The interactive version is available on binder. It will take 5-7 minutes for the simulation to fire up (it has to download a fresh copy of a programming language which takes time), so please be patient.
The first task is to generate the supply and demand curves. In my simulation, the supply and demand curves will only represent the limit prices for consumers and producers. This means that if a consumer has a limit of 1.0, they would be happy purchasing it for a lower price, but will never cross $1.0. Similarly, if a producer has a limit of 0.5, he will be happy selling it for a higher price, but will never go lower than this.
For now, let's assume that the price of the good varies from 0 - 1.0 For the demand curve, we can simply generate random numers between 0 and 1 (with equal probability) and then sort them in descending order. This will give a downward sloping curve. For the supply curve, we do the same but sort it in ascending order (to make it upward sloping). This should give us a representative market along with appropriate curves.
The slider below controls the number of producers = number of consumers in the market. 100 means that there are 100 producers and 100 consumers. A larger market size for the simulation will better show what economics models predict.
Number of Producers and Consumers
Apart from this, consumers and producers also have expectations about what the market price should be (also called producer and consumer prices). This will be random value within their limit, but not necessarily at their limit. To take an example, if you as a consumer can afford to spend 100 on buying a chair (this is your limit), it doesn't mean that you think this is the fair value of the chair. You might bargain as much as possible and be rigid to buy it at any price above 50 (this is your expectation).
The collective expectations of consumers and producers is what determines the market price. We will represent the expectations with dots (they are color coded to match whether they are consumers or producers).
As you can see, no blue dot goes below the blue line (corresponding to supply curve) and no orange dot goes above the orange line (corresponding to the demand curve).
The dots seem randomly spread because they are. We haven't run the simulation yet, so the dots haven't gotten a chance to converge yet.
In this simulation, we will simulate the market over multiple trading days. Now let's lay down the rules for how each trading day in the market will work:
A random consumer and producer will be paired with each other. If the consumer's expectation (or price) is more than the producer's expectation (or price), then they will make a trade.
If such a trade happens, then the producer will increase his expectation (because he got a deal so easily, so can increase his price), whereas the consumer will reduce his expectation (he also got a deal so easily, so could probably get away paying a little less).
If the consumer's expectation is less than the producer's expectation, then we check if the consumer's limit is more than the producer's limit. If it is, then they do some bargaining and still make a trade (since they are still both better off making a trade).
If such a trade happens, then the producer will decrease his expectation (he had to bargain to get a deal, and bargaining is a risky affair). On the other hand, the consumer will increase his expectation (same reason, he doesn't want to risk not buying something, even though he can afford to).
Trading continues like this until no more trades can be made.
Now, any producers who have not made a trade will decrease their expectations a lot (they didn't get a sale so have no choice but to reduce prices). Any consumers who didn't make a trade will increase their expectations a lot (they could buy anything, so have no choice but to increase prices and attract producers).
Remember that throughout, the expectations will always be clamped to the limits (i.e. a producer's expectation will never be lower than his ability, and a producer's expectation will never be more than his ability).
Market Equilibrium
This is the first main concept learnt in microeconomics.
According to the theory, the equilibrium is where the demand curve intersects the supply curve (the orange line intersecting the blue line).
So let's see if that is what happens in our simulation. Press the start button to start the simulation. The number 0.1 means that it takes 0.1 seconds for 1 trading day to be executed. Keeping it at this number will allow you to best see the market progress. Going below 0.1 may make the graph lag and you may not see the graph update as it should.
Simulation Speed
After running it for a while, you should be able to see 2 clear trends:
All of the dots on the right of the intersection (between demand and supply curves) have their prices stuck to their limits.
The reason is that the consumers on the right simply can't afford to pay the market price. They adjust their expectations to be as high as possible (their limit), but still don't get a deal. That's why their dots are stuck to the curve. Similarly, the producers on the right simply can't sell a product at the market price, because their limit is higher than that. Thus, they reduce their expectations as much as possible, but can't beyond a point.
All of the dots on the left of the intersection form a straight line, with the height = to the height of the intersection.
This is exactly what equilibrium is. All producers and consumers can to the same consensus as to what the price should be. Notice how in the simulation, every producer and consumer was designed to act in a self-interested way. There was no collusion or any thought about society, yet they all figure out that the equilibrium price is the best possible (this is the principle of the Invisible Hand).
Also note the value of the surplus at the end, we will use it to compare the effect of different government policies later.
Taxes
Now let's examine the impact of a government policy on the free market. It is repeated time and again that any intervention in free markets (often) leads to inefficiencies in the market. Let's simulate this and see if the claim is right.
The first government policy we will examine is tax. We will simulate specific indirect tax, which is levied when the good is sold and is constant irrespective of the price. Also, here we will assume that the government expects the producers to pay the tax at the end of the day (the producers are well aware of this policy change).
The slider below will allow you to control the amount of tax levied. Now, a thing to note is that the limits of the producers (the supply curve) has changed. For example, let's assume that there is a tax of 0.1. Now, originally a producer who had a limit of 0.2 could have sold a product at 0.21 and still gone home with a profit. But now, if he sells the same product for 0.21, he has to pay the tax of 0.1, so goes home with only 0.11, which is less than his limit of 0.2. Thus, every producer's limit increases by the value of the supply curve. Why? Because once they make the trade at this price, they pay away the tax and end up at the original supply curve limit.
Let's denote this new limit (Supply curve + tax) with a green line.
Finally, let's run the simulation and see what happens:
Tax Amount
Simulation Speed
Now when we run the simulation, the consumer and producer prices form two separate lines! They are no longer the same line but have diverged. The reason is the tax. Consumers pay a price along their line, but producers pay the tax afterwards, and end up at the lower price.
Infact, if you look carefully, the lines are separated exactly by the value of the tax. One thing to note is that the producers don't pay for the entire tax. To verify this, look at the line for consumers. In the free market, it was close to around 0.5, but now (a higher tax amount will show this more clearly) the consumers pay a price significantly higher than 0.5. Thus, the tax is "shared" by consumers and producers.
Also compare the surpluses. If you see, the market surplus is now overall less than in the free market. This is what is meant by how government policies negatively impact the market. Producers and consumers are both unhappy with the tax.
Price Floors
Price Floors are another interesting form of government intervention. Here, the government essentially enforces a regulation where no transaction in the market can occur below the price floor. It is essentially the minimum price a transaction can go.
However, price floors are not as straightforward as taxes as we even have to consider what to do with the surplus.
When a price floor is placed above market price, more producers are able to produce and sell at that minimum price, but less consumers are able to buy. Thus, too much of the good is produced, and something has to be done. In the caase of product markets, the government often just buys all of this surplus, which is what we will simulate here.
The logic here is going to be simple. Trading will continue on as it was before, but will have one restriction: if the consumer price is not more than the price floor, then the trade will not occur. This is because if the consumer price is lower, then the trade becomes illegal (for the sake of simplicity, we won't consider any underground markets where such a transaction is possible, only a fully legal and regulated market).
Below is the slider for the price floor (which corresponds to the green line of the graph). You can change it and watch the simulation adapt. (the price floor will only matter if placed above the equilibrium price).
Price Floor
Simulation Speed
If you play with the simulation, you should notice 2 different scenarios:
If the price floor is below the market price, nothing happens and it is essentially the free market all over again.
This intuitively makes sense as consumers and producers are already making transactions at a price higher than the price floor, so it doesn't make a difference to them.
When the price floor is above market price, a large number of producers but a small number of consumers are participating in the market.
This also makes sense as now when the price floor is above the market price, more producers are able to produce and sell. Producers don't even need to decrease their expectations when a consumer doesn't buy from them since the government will have to do it at the price floor anyway.
Looking at the surplus, we see that the producer surplus has gone up dramatically, but the consumer surplus has gone down even more. That's why, in the end the market surplus is still less than the free market economy.
Price Ceiling
Price Ceiling are exactly the opposite as price floors. Instead of having a minimum transaction price, there is now a maximum transaction price.
In Price Floors, we realized that there was a surplus created since more producers are able to produce. In Price Ceils, there is a shortage created, as now many more consumers can afford to buy the good, but there are very few producers.
In the Price Floor, we saw that the government could step in and buy the surplus of the producers. But in the Price Ceil, the government can't easily fix the shortage. If they tried, they would have to find producers to buy from or hire them, but there are no more producers left (as they are all in the market)! However, there is still an opportunity to import the good.
For this reason, we will just assume that the government doesn't step in.
The slider below allows you to control the price ceiling (green line).
Price Ceiling
Simulation Speed
Once again, we notice 2 different cases in the simulation:
If the price ceil is above the market price, nothing changes.
This happens for the exact same reason as the price floor. Producers are anyways selling the product at a price lower than the price floor, so there is no issue.
If the price ceil is below the market price, a large number of consumers want to participate, but only a few producers are left.
This seems intuitive as well since now the product's price falls within the limits for more consumers. A thing to note here is that just because all of the consumers' expectations fall to the price ceil, it doesn't mean that all of them are able to buy the good, only some are. In fact, in the price floor, everyone had settled quickly since the government was buying the excess. Here, there is still some movement, so this is slightly more unstable.
While you would expect consumer surplus to increase drastically, it doesn't. This is again due to the fact that the government isn't filling in the shortage, so not all consumers get the benefit of the low price. Overall, the market surplus also goes down.
In all of the different scenarios we have simulated here, the end outcome has matched what microeconomics predicts. I think this shows how good a model demand and supply is, and gives a better understanding of the principle of the Invisible hand, and how everyone comes to the same consensus.
Limitations:
We have built a very simplified decision framework for consumers and producers. In reality, the thought process may be much more complicated. For example, in a real market, there is a chance that no bargaining will happen, so the producer will never reduce their prices.
In reality, people are also biased. This means that sometimes, people wouldn't update their beliefs in the right way, or some irrelevant information may factor into their prices. For example, the anchoring bias may completely hinder the expectations for consumers and producers, so they may find it hard to converge.
A more detailed simulation could be made, but I think this illustrates the model well enough, while being simple.