Computational Exercises: Solutions > Market Equilibrium (g08)

market.py

"""
market.py
Spring 2022 PJW

Solve for a market equilibrium given a large number of
heterogeneous households on the demand side and a single
market supply curve.
"""

import csv
import scipy.optimize as opt

#
#  Read the household information from a file
#

def read_households(filename):
    households = []
    fh = open(filename)
    reader = csv.DictReader(fh)
    for hh in reader:
        for v in ['inc','a','b']:
            hh[v] = float( hh[v] )
        households.append(hh)
    print( "Lines read:", len(households) )
    return households

#
#  Compute and return a list of individual demand quantities
#  given a buyer price and a list of attributes of the households
#

def ind_demand(prd,hhlist):
    qlist = []
    for hh in hhlist:
        q = (hh['a']*hh['inc']/prd) - hh['b']
        qlist.append(q)
    return qlist

#
#  Compute a market demand given a buyer price and a list of
#  attributes of the households. Calls ind_demand along the way.
#

def mkt_demand(prd,hhlist):
    qlist = ind_demand(prd,hhlist)
    qdm = sum(qlist)
    return qdm

#
#  Compute a market supply given a seller price.
#

def mkt_supply(prs):
    qs = 5000*prs
    return qs

#
#  Compute an excess demand given a buyer price, a tax, and a list
#  of household attributes.
#

def excess_d(prd,tax,hhlist):
    prs = prd - tax
    qd = mkt_demand(prd,hhlist)
    qs = mkt_supply(prs)
    excess = qd - qs
    return excess

#
#  Read in the actual household data
#

hhlist = read_households('households.csv')

#
#  Iterate over prices to find the initial equilibrium when the
#  tax is zero. Print out the results.
#

guess = 20
tax = 0
prd1 = opt.newton(excess_d,guess,maxiter=20,args=[tax,hhlist])
prs1 = prd1
qd1 = mkt_demand(prd1,hhlist)
qs1 = mkt_supply(prs1)
print('\nEquilibrium 1:')
print('   pd =',round(prd1,2),', qd =',round(qd1))
print('   ps =',round(prs1,2),', qs =',round(qs1))

#
#  Now solve for the equilibrium when the tax is raised to 5.
#

tax = 5
prd2 = opt.newton(excess_d,guess,maxiter=20,args=[tax,hhlist])
prs2 = prd2 - tax
qd2 = mkt_demand(prd2,hhlist)
qs2 = mkt_supply(prs2)
print('\nEquilibrium 2:')
print('   pd =',round(prd2,2),', qd =',round(qd2))
print('   ps =',round(prs2,2),', qs =',round(qs2))

#
#  Calculate the revenue raised by the tax.
#

print('\nRevenue:\n  ',round(tax*qd2))

#
#  Calculate the buyer and seller shares of the tax burden.
#

print("\nTax burdens:")
print('   buyer %:',round( 100*(prd2-prd1)/5))
print('   seller %:',round( 100*(prs1-prs2)/5))
Site Index | Zoom | Admin
URL: https://wilcoxen.maxwell.insightworks.com/pages/6175.html
Peter J Wilcoxen, The Maxwell School, Syracuse University
Revised 02/16/2022