Skip to content

Commit

Permalink
added examples related to OOP lecture
Browse files Browse the repository at this point in the history
  • Loading branch information
jstac committed Mar 1, 2015
1 parent 5f4747c commit 8f826c7
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 16 deletions.
64 changes: 64 additions & 0 deletions examples/market.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Filename: market.py
Reference: http://quant-econ.net/py/python_oop.html
"""

from __future__ import division
from scipy.integrate import quad

class Market:

def __init__(self, ad, bd, az, bz, tax):
"""
Set up market parameters. All parameters are scalars. See
http://quant-econ.net/py/python_oop.html for interpretation.
"""
self.ad, self.bd, self.az, self.bz, self.tax = ad, bd, az, bz, tax
if ad < az:
raise ValueError('Insufficient demand.')

def price(self):
"Return equilibrium price"
return (self.ad - self.az + self.bz*self.tax)/(self.bd + self.bz)

def quantity(self):
"Compute equilibrium quantity"
return self.ad - self.bd * self.price()

def area1(self):
"Compute area under inverse demand function"
a, error = quad(lambda x: (self.ad/self.bd) - (1/self.bd)* x, 0, self.quantity())
return a

def consumer_surp(self):
"Compute consumer surplus"
return self.area1() - self.price() * self.quantity()

def area2(self):
"Compute area above the supply curve. Note that we exclude the tax."
a, error = quad(lambda x: -(self.az/self.bz) + (1/self.bz) * x, 0, self.quantity())
return a

def producer_surp(self):
"Compute producer surplus"
return (self.price() - self.tax) * self.quantity() - self.area2()

def taxrev(self):
"Compute tax revenue"
return self.tax * self.quantity()

def inverse_demand(self,x):
"Compute inverse demand"
return self.ad/self.bd - (1/self.bd)* x

def inverse_supply(self,x):
"Compute inverse supply curve"
return -(self.az/self.bz) + (1/self.bz) * x + self.tax

def inverse_supply_no_tax(self,x):
"Compute inverse supply curve without tax"
return -(self.az/self.bz) + (1/self.bz) * x



11 changes: 11 additions & 0 deletions examples/market_deadweight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

from market import Market

def deadw(m):
"Computes deadweight loss for market m."
# == Create analogous market with no tax == #
m_no_tax = Market(m.ad, m.bd, m.az, m.bz, 0)
# == Compare surplus, return difference == #
surp1 = m_no_tax.consumer_surp() + m_no_tax.producer_surp()
surp2 = m.consumer_surp() + m.producer_surp() + m.taxrev()
return surp1 - surp2
24 changes: 24 additions & 0 deletions examples/plot_market.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import matplotlib.pyplot as plt
import numpy as np
from market import Market

# Baseline ad, bd, az, bz, tax
baseline_params = 15, .5, -2, .5, 3
m = Market(*baseline_params)

q_max = m.quantity() * 2
q_grid = np.linspace(0.0, q_max, 100)
pd = m.inverse_demand(q_grid)
ps = m.inverse_supply(q_grid)
psno = m.inverse_supply_no_tax(q_grid)

fig, ax = plt.subplots()
ax.plot(q_grid, pd, lw=2, alpha=0.6, label='demand')
ax.plot(q_grid, ps, lw=2, alpha=0.6, label='supply')
ax.plot(q_grid, psno, '--k', lw=2, alpha=0.6, label='supply without tax')
ax.set_xlabel('quantity', fontsize=14)
ax.set_xlim(0, q_max)
ax.set_ylabel('price', fontsize=14)
ax.legend(loc='lower right', frameon=False, fontsize=14)
plt.show()

24 changes: 8 additions & 16 deletions examples/solow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
Filename: solow.py
Reference: http://quant-econ.net/py/python_oop.html
"""
from __future__ import division # Omit for Python 3.x
import numpy as np

Expand All @@ -11,22 +15,10 @@ class Solow:
"""

def __init__(self, n, s, d, alpha, z, k):
""" Solow growth model with Cobb Douglas production function.
Parameters
----------
n : scalar
population growth rate
s : scalar
savings rate
d : scalar
depreciation rate
alpha : scalar
capital's share of income
z : scalar
productivity parameter
k : scalar
current capital stock
"""
Solow growth model with Cobb Douglas production function. All
parameters are scalars. See http://quant-econ.net/py/python_oop.html
for interpretation.
"""
self.n, self.s, self.d, self.alpha, self.z = n, s, d, alpha, z
self.k = k
Expand Down

0 comments on commit 8f826c7

Please sign in to comment.