-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added examples related to OOP lecture
- Loading branch information
Showing
4 changed files
with
107 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters