Matching Market

This simple model consists of a buyer, a supplier, and a market.

The buyer represents a group of customers whose willingness to pay for a single unit of the good is captured by a vector of prices wta. You can initiate the buyer with a set_quantity function which randomly assigns the willingness to pay according to your specifications. You may ask for these willingness to pay quantities with a _getbid function.

The supplier is similiar, but instead the supplier is willing to be paid to sell a unit of technology. The supplier for instance may have non-zero variable costs that make them unwilling to produce the good unless they receive a specified price. Similarly the supplier has a get_ask function which returns a list of desired prices.

The willingness to pay or sell are set randomly using uniform random distributions. The resultant lists of bids are effectively a demand curve. Likewise the list of asks is effectively a supply curve. A more complex determination of bids and asks is possible, for instance using time of year to vary the quantities being demanded.

Microeconomic Foundations

The market assumes the presence of an auctioneer which will create a book, which seeks to match the bids and the asks as much as possible. If the auctioneer is neutral, then it is incentive compatible for the buyer and the supplier to truthfully announce their bids and asks. The auctioneer will find a single price which clears as much of the market as possible. Clearing the market means that as many willing swaps happens as possible. You may ask the market object at what price the market clears with the get_clearing_price function. You may also ask the market how many units were exchanged with the get_units_cleared function.

Agent-Based Objects

The following section presents three objects which can be used to make an agent-based model of an efficient, two-sided market.


In [75]:
import random as rnd

class Supplier():

    def __init__(self):
        self.wta = []
    
    # the supplier has n quantities that they can sell
    # they may be willing to sell this quantity anywhere from a lower price of l
    # to a higher price of u
    def set_quantity(self,n,l,u):
        for i in range(n):
            p = rnd.uniform(l,u)
            self.wta.append(p)
    
    # return the dictionary of willingness to ask
    def get_ask(self):
        return self.wta

class Buyer():
    
    def __init__(self):
        self.wtp = []
    
    # the supplier has n quantities that they can buy
    # they may be willing to sell this quantity anywhere from a lower price of l
    # to a higher price of u
    def set_quantity(self,n,l,u):
        for i in range(n):
            p = rnd.uniform(l,u)
            self.wtp.append(p)
    
    # return list of willingness to pay
    def get_bid(self):
        return self.wtp

class Market():
    count = 0
    last_price = ''
    b = []
    s = []
    
    def __init__(self,b,s):
        # buyer list sorted in descending order
        self.b = sorted(b, reverse=True)
        # seller list sorted in ascending order
        self.s = sorted(s, reverse=False)
    
    # return the price at which the market clears
    # assume equal numbers of sincere buyers and sellers
    def get_clearing_price(self):
        # buyer makes a bid, starting with the buyer which wants it most

        for i in range(len(self.b)):
            if (self.b[i] > self.s[i]):
                self.count +=1
                self.last_price = self.b[i]
                
        return self.last_price
    
    def get_units_cleared(self):
        return self.count

Example Market

In the following code example we use the buyer and supplier objects to create a market. At the market a single price is announced which causes as many units of goods to be swapped as possible. The buyers and sellers stop trading when it is no longer in their own interest to continue.


In [78]:
# make a supplier and get the asks
supplier = Supplier()
supplier.set_quantity(100,0,10)
ask = supplier.get_ask()

# make a buyer and get the bids
buyer = Buyer()
buyer.set_quantity(100,0,10)
bid = buyer.get_bid()

# make a market where the buyers and suppliers can meet
# the bids and asks are a list of prices
market = Market(bid,ask)
price = market.get_clearing_price()
quantity = market.get_units_cleared()

# output the results of the market 
print("Goods cleared for a price of ",price)
print("Units sold are ", quantity)


Goods cleared for a price of  5.65611330984964
Units sold are  52

Operations Research Formulation

The market can also be formulated as a very simple linear program or linear complementarity problem. It is clearer and easier to implement this market clearing mechanism with agents. One merit of the agent-based approach is that we don't need linear or linearizeable supply and demand function.

The auctioneer is effectively following a very simple linear program subject to constraints on units sold. The auctioneer is, in the primal model, maximizing the consumer utility received by customers, with respect to the price being paid, subject to a fixed supply curve. On the dual side the auctioneer is minimizing the cost of production for the supplier, with respect to quantity sold, subject to a fixed demand curve. It is the presumed neutrality of the auctioneer which justifies the honest statement of supply and demand.

An alternative formulation is a linear complementarity problem. Here the presence of an optimal space of trades ensures that there is a Pareto optimal front of possible trades. The perfect opposition of interests in dividing the consumer and producer surplus means that this is a zero sum game. Furthermore the solution to this zero-sum game maximizes societal welfare and is therefore the Hicks optimal solution.

Next Steps

A good elaboration of this model would be to have multiple buyers with differing willingness to pay and quantities to purchase. The market object would need to be expanded to keep track of the "book" -- which parties actually completed the transaction. Another possible addition would be to have a weekly varying demand of customers, for instance caused by the use of natural gas as a heating agent. This would require the bids and asks to be time varying, and for the market to be run over successive time periods. A third or fourth addition would be to create transport costs, or enable intermediate goods to be produced. This would need a more elaborate market operator.