Thursday, February 27, 2014

Measuring Supply and Demand in a Simulated Economy

Isn't Equilibrium Where Supply Equals Demand?

I was on vacation last week (which is why I haven't posted in a while) and spent some time thinking about where I want to go with the blog. I realized that it would probably be best to focus on basic principles before trying to get too creative with things like variations on the bargaining model. Besides, we can get a lot more mileage out of these key ideas.

So, what's all this talk about competitive equilibrium being the allocation where agents' marginal rates of substitution are equal to each other and their utility curves are tangent? Isn't it a lot easier to just say that equilibrium is where supply equals demand? Well, yes, it is, and actually, that is how we found the competitive equilibrium; we just approached it from another perspective. Recall that we did calculate the agents' demand curves along the way. As we'll see below, we actually found the supply curves as well, even if that isn't obvious right now.

Supply and Demand: Some Common Misconceptions

First of all, let's define what these terms mean. Demand is a function that relates the price of a good to how much of that good an agent wants to buy. It is important to distinguish between demand and quantity demanded. If movie tickets cost $20, how many times will you go to the movies this month? What if tickets cost $2? It's safe to say that you'll probably go to the movies a lot more often if tickets only cost $2. Let's say that you spend $20 per month on movie tickets. Then your demand function for movie tickets looks like this:
$$ D(p_t) = \frac{20}{p_t} $$
When economists use the word demand, they're talking about this function. The value returned by this function is what economists call quantity demanded. Outside of the economics classroom, people usually aren't thinking of mathematical models, and so they use the word demand to refer to what economists would call quantity demanded.

Why does the distinction between demand and quantity demanded matter? Well, the model (i.e., the demand function) gives us a framework for reasoning about the general problem; a single outcome (quantity demanded) is only useful in one particular setting. It's also important that we focus on the demand function because in our simulator's economic model, demand and supply are really the same thing.

Wait, what?

Supply and Demand in an Endowment Economy 

So far, we're still considering an endowment economy. That is, no goods are produced; the only goods that exist are what we give the agents--their endowments--at the beginning of the simulation, and they can trade them as they see fit.

In some cases, agents will trade some of good 2 to gain more of good 1. These agents are buying good 1. In other cases, agents will trade some of good 1 to gain more of good 2. These agents are selling good 1. So these agents will sometimes be buyers, in which case their actions are governed by their demand functions, and sometimes be sellers, in which case their actions are governed by their supply functions.

So how do we derive these supply functions? Well, we've already done it. Recall that to find the equilibrium outcome, we used the agents' demand functions (or more specifically, agent 1's demand function for good 1 in terms of good 2). But what if that equilibrium allocation of good 1 is less than what the agent already has? This agent would then be selling good 1, and so we can look at the amount of good 1 they are willing to give up at that price, and interpret that as the quantity that agent is willing to supply.

In our earlier approach, using only the agents' demand functions, our model was equivalent to one in which, for a given price, the agents sell all of their goods for cash, then buy back the best allocation they can get for that amount of wealth. Both agents are buyers. We're looking at the optimal allocations as purchases they both make when starting at an allocation of (0, 0). But we can also look at the optimal allocations as trades they make from their starting allocations.

If we subtract the optimal allocation from the initial allocation and find that the agent trades for more of good 1, that difference is the agent's quantity demanded at that price. If we subtract the optimal allocation from the initial allocation and find that the agent trades for less of good 1, that difference is the agent's quantity supplied at that price.

If we just subtract the initial allocation from the agent's demand function, then wherever the result is positive, we call that the agent's demand, and wherever that result is negative, we take the absolute value and call that the agent's supply.

This is very easy to compute. We already did it in the cobb_douglas_competitive_eqbm transaction function we computed before. Now we can just take that demand function out and add it to our Agent class as a new method:

def demand(self, price):
    alpha = self.pref1 / (self.pref1 + self.pref2)
    quantity1 = alpha * (price * self.good1 + self.good2) / price
    quantity2 = price * quantity1 * self.pref2 / self.pref1
    return (quantity1, quantity2)

And we can call that method from within our transaction function:

def cobb_douglas_competitive_eqbm(X, Y):
    # Useful constants for the following calculations
    alphaX = X.pref1 / (X.pref1 + X.pref2)
    betaX  = X.pref2 / (X.pref1 + X.pref2)
    alphaY = Y.pref1 / (Y.pref1 + Y.pref2)
    betaY  = Y.pref2 / (Y.pref1 + Y.pref2)
    total_1 = X.good1 + Y.good1
    total_2 = X.good2 + Y.good2
    # Equilibrium price of good 1 relative to good 2
    price = (X.good2 * alphaX + Y.good2 * alphaY) / (X.good1 * betaX + Y.good1 * betaY)

    allocation_x = X.demand(price)
    allocation_y = (total_1 - allocation_x[0], total_2 - allocation_x[1])

    return (allocation_x, allocation_y, price)

Aggregating Supply and Demand in the Market

So now we can calculate how two agents trade using one agent's demand function and the other agent's supply function, if we want. But that's actually more work than what we've done, since we only need to know one agent's demand and the equilibrium price to calculate everything else. Where knowledge of supply and demand really comes into play is in finding the equilibrium for the market as a whole.

Now that we know how to compute supply and demand for each individual agent, we can easily compute the aggregate supply and demand for the whole market, like this:

def aggregate_demand(agents, max_price=10, steps=100):
    prices = (np.arange(steps) + 1) * max_price / float(steps)
    quantities = [sum([a.demand(p)[0] - a.good1 for a in agents if a.demand(p)[0] > a.good1]) for p in prices]
    return dict(zip(prices, quantities))

And here's the supply function using a loop instead of a list comprehension for readers less familiar with Python:

def aggregate_supply(agents, max_price=10, steps=100):
    prices = (np.arange(steps) + 1) * max_price / float(steps)
    result = {}
    for price in prices:
        quantity = 0
        for a in agents:
            if a.demand(price)[0] < a.good1:
                quantity += a.good1 - a.demand(price)[0]
        result[price] = quantity
    return result

Now that we know these two relationships, we can find the market equilibrium price: it's the place where supply equals demand, or where the surplus (supply minus demand) equals zero.

def find_market_equilibrium(agents):
    supply = aggregate_supply(agents)
    demand = aggregate_demand(agents)
    surplus = dict(zip(supply.keys(),
                       [s-d for s, d in zip(supply.values(), demand.values())]))
    min_surplus = np.min(np.abs(surplus.values()))
    equilibrium_price = [p for p in surplus if abs(surplus[p]) == min_surplus][0]
    equilibrium_quantity = supply[equilibrium_price]
    return (equilibrium_price, equilibrium_quantity)

Note that this equilibrium is an approximation. When we aggregated supply and demand across the market, we could only do so across an array of prices, and there's no guarantee that the real equilibrium price will be in that array. Instead, we just find the price that has the minimum surplus supply (in absolute value).

So, what does this do for us?

First of all, now that we know how to find supply and demand in our market, we can check to see whether our simulated agents are able to find the market equilibrium through their own individual transactions. Remember how the utility levels flattened out after the simulation had run for a while? That was due to the agents getting closer and closer to the market equilibrium as they traded with each other. If we plot the prices at which each trade was executed, we can see that the agents did in fact converge to the market equilibrium:

Left: Supply (blue) and Demand (green) curves and the equilibrium price (red).
Right: Trade prices in our barter economy as they converge to the equilibrium.

Now, there was quite a bit of turbulence in the prices early on in the simulation. They will eventually converge to equilibrium, if all of the agents are able to trade with each other. directly or indirectly. That is, there can't be any clusters of agents that are isolated from the rest; if there were, we would want to consider the population as two separate markets.

Using supply and demand to study the outcomes is a quick way to find out where the market equilibrium will be, without having to run several thousand simulated trades to get the population to converge. However, if we are more interested in the behavior of individual agents, then we will probably want to focus on the results of each interaction and transaction.

But even in this case, finding the market equilibrium using supply and demand lets us consider lots of variations in our simulation.

  • Suppose we want to analyze a market that has already been established for a while, we should assume that it has converged, or nearly converged, to an equilibrium. If we calculate that equilibrium price, it should give us some idea of what parameters to use to set our agents' initial allocations. 
  • Maybe we're not as interested in understanding how agents come to an equilibrium as we are in looking at the results of some event that disrupts the equilibrium. Suppose trade relations are severed between two groups and the population is divided into two; how do the groups reach a new equilibrium? 
  • We can also play around with different transaction functions and different interaction rules to see what kinds of markets converge to equilibrium faster. We can run several simulations to find different ways to structure the rules that govern our markets.

What if we introduce a new technology into our simulated economy? Surely that would affect the price of the goods, if one of them is now easier to produce. We could calculate new supply and demand functions, and then explore how the market adjusts to the new equilibrium. Ah, but we haven't even begun discussing production yet. Well, I'd say it's time to do something about that.

1 comment:

  1. Well, it's been over a month since my last post. It might seem like I've given up or lost interest in the blog, but that's not quite true. I've actually spent quite a bit of time putting together notes for a new post about general equilibrium models with production, but I just did not like the direction it was taking me. It was starting to feel as though I'm writing an economics text book, and that is just not the direction I want to go with this project. I want to focus on building simulations that do cool things, and there are thousands of economics texts that can explain the theory perfectly well.

    It also doesn't help, schedule-wise, that I've recently started studying two Coursera classes at once. (I do this from time to time, figure out which one interests me more, and come back to the online archives for the other one later on if I'm still interested.

    So, my schedule is a little tight, but more importantly I'm re-imagining my plan for how I want to run this blog; but I do plan to come back to it once I figure out what I want to do. I don't want to set a firm timeline, but I'll probably start writing again in the next few weeks.