-
Introduction 4
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
-
Production Possibilities Frontier 4
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
-
Trade 3
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
-
Demand 4
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
Lecture4.4
-
-
Supply 2
-
Lecture5.1
-
Lecture5.2
-
-
Equilibrium 4
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
-
-
Curve Movements 4
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
Lecture7.4
-
-
Elasticity and Revenue 5
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
Lecture8.4
-
Lecture8.5
-
-
Taxes 7
-
Lecture9.1
-
Lecture9.2
-
Lecture9.3
-
Lecture9.4
-
Lecture9.5
-
Lecture9.6
-
Lecture9.7
-
-
Consumer and Producer Surplus 8
-
Lecture10.1
-
Lecture10.2
-
Lecture10.3
-
Lecture10.4
-
Lecture10.5
-
Lecture10.6
-
Lecture10.7
-
Lecture10.8
-
-
Imports and Exports 4
-
Lecture11.1
-
Lecture11.2
-
Lecture11.3
-
Lecture11.4
-
-
Tariffs 2
-
Lecture12.1
-
Lecture12.2
-
Plotting the Tax Model
This function will leverage off the previous ones we created. The new code added to our function is the below code, plus a little bit of changes to the plotting code.
IMPORTANT:
Don’t try plotting the new code snippets unless it is the full function. The small lines of code will not work outside of the function, I am just adding them in so that you can see what I’m adding each time to the function without needing to go on a scavenger hunt to tell the differences.
equilibriumQ = eqSolve(demandEquation,supplyEquation,tax)
equilibriumP1 = sympy.solve(demandEquation-equilibriumQ)[0]
equilibriumP2 = sympy.solve(supplyEquation-equilibriumQ)[0]
The first line gets the quantity we are going to use from the function we made. The next two lines solve for both prices by taking the quantity we found and moving it to the right side of the equation (hence the – equilibriumQ on both lines). Our new plotting function is:
import sympy
import matplotlib.pyplot as plt
p = sympy.Symbol("p")
q = sympy.Symbol("q")
cTax = sympy.Symbol("cTax")
pTax = sympy.Symbol("pTax")
def EquilibriumTax(demandEquation,supplyEquation,priceStart,priceEnd,tax):
prices = []
demand = []
supply = []
for price in range(priceStart,priceEnd+1):
prices += [price]
demand += [demandEquation.subs(p,price)]
supply += [supplyEquation.subs(p,price)]
equilibriumQ = eqSolve(demandEquation,supplyEquation,tax)
equilibriumP1 = sympy.solve(demandEquation-equilibriumQ)[0]
equilibriumP2 = sympy.solve(supplyEquation-equilibriumQ)[0]
plt.plot(demand,prices)
plt.plot(supply,prices)
plt.legend(["Demand","Supply"])
plt.plot(equilibriumQ,equilibriumP1, 'ro')
plt.plot(equilibriumQ,equilibriumP2, 'ro')
plt.xlabel("Supply and Demand Quantity")
plt.ylabel("Price")
plt.show()
print("The equilibrium prices are "+str(equilibriumP1)+" and "+str(equilibriumP2)+" and equilibrium quantity is "+str(equilibriumQ)+".")
Let’s try our new function with the easy equations demand = 10-p and supply = p.
EquilibriumTax(10-p,p,0,10,4)
Next up, we are going to add code to create triangles representing our deadweight loss. Between the quantity with taxes and without taxes, we have area that has been lost because of the taxes. We will plot this similarly to how we plotted the revenue squares, with patches. We will use the polygon function for patches.
triangle1 = patches.Polygon([[nonTaxQ,nonTaxPrice],[equilibriumQ,nonTaxPrice],[equilibriumQ,equilibriumP1]],True,color="green")
triangle2 = patches.Polygon([[nonTaxQ,nonTaxPrice],[equilibriumQ,nonTaxPrice],[equilibriumQ,equilibriumP2]],True)
currentAxis = plt.gca()
currentAxis.add_patch(triangle1)
currentAxis.add_patch(triangle2)
The array that we give polygon represents the x,y coordinates of all our polygon points. The argument True gives us a polygon that is filled in, and the color argument sets our polygon color. The full function:
import sympy
import matplotlib.pyplot as plt
import matplotlib.patches as patches
p = sympy.Symbol("p")
q = sympy.Symbol("q")
cTax = sympy.Symbol("cTax")
pTax = sympy.Symbol("pTax")
def EquilibriumTax(demandEquation,supplyEquation,priceStart,priceEnd,tax):
prices = []
demand = []
supply = []
for price in range(priceStart,priceEnd+1):
prices += [price]
demand += [demandEquation.subs(p,price)]
supply += [supplyEquation.subs(p,price)]
nonTaxPrice = sympy.solve(demandEquation-supplyEquation)[0]
nonTaxQ = demandEquation.subs(p,nonTaxPrice)
equilibriumQ = eqSolve(demandEquation,supplyEquation,tax)
equilibriumP1 = sympy.solve(demandEquation-equilibriumQ)[0]
equilibriumP2 = sympy.solve(supplyEquation-equilibriumQ)[0]
triangle1 = patches.Polygon([[nonTaxQ,nonTaxPrice],[equilibriumQ,nonTaxPrice],[equilibriumQ,equilibriumP1]],True,color="green")
triangle2 = patches.Polygon([[nonTaxQ,nonTaxPrice],[equilibriumQ,nonTaxPrice],[equilibriumQ,equilibriumP2]],True)
currentAxis = plt.gca()
currentAxis.add_patch(triangle1)
currentAxis.add_patch(triangle2)
plt.plot(demand,prices)
plt.plot(supply,prices)
plt.legend(["Demand","Supply"])
plt.plot(equilibriumQ,equilibriumP1, 'ro')
plt.plot(equilibriumQ,equilibriumP2, 'ro')
plt.xlabel("Supply and Demand Quantity")
plt.ylabel("Price")
plt.show()
print("The equilibrium prices are "+str(equilibriumP1)+" and "+str(equilibriumP2)+" and equilibrium quantity is "+str(equilibriumQ)+".")
EquilibriumTax(10-p,p,0,10,4)