-
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 Surplus
We are going to also draw the surplus on to the curves. As you will see, we need to draw the surplus differently based on whether or not we are exporting or importing.
The function which we are going to add to our class is this:
def drawSurplus1(self):
if self.export:
triangle1 = patches.Polygon([[self.domesticQ,self.worldPrice],[0,self.worldPrice],[0,self.priceEnd]],True,label="Consumer Surplus")
triangle2 = patches.Polygon([[0,0],[0,self.startingP],[self.startingQ,self.startingP]],True,label="Original Producer Surplus",color="red")
rect1 = patches.Polygon([[0,self.startingP],[0,self.worldPrice],[self.domesticQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Transferred Surplus",color="orange")
triangle3 = patches.Polygon([[self.domesticQ,self.worldPrice],[self.totalQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Surplus from International",color="yellow")
currentAxis = plt.gca()
currentAxis.add_patch(triangle1)
currentAxis.add_patch(triangle2)
currentAxis.add_patch(rect1)
currentAxis.add_patch(triangle3)
else:
triangle1 = patches.Polygon([[self.startingQ,self.startingP],[0,self.startingP],[0,self.priceEnd]],True,label="Original Consumer Surplus", color="red")
triangle2 = patches.Polygon([[0,0],[0,self.worldPrice],[self.domesticQ,self.worldPrice]],True,label="Producer Surplus")
rect1 = patches.Polygon([[0,self.startingP],[0,self.worldPrice],[self.domesticQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Transferred Surplus",color="orange")
triangle3 = patches.Polygon([[self.domesticQ,self.worldPrice],[self.totalQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Surplus from International",color="yellow")
currentAxis = plt.gca()
currentAxis.add_patch(triangle1)
currentAxis.add_patch(triangle2)
currentAxis.add_patch(rect1)
currentAxis.add_patch(triangle3)
Nothing about this code is new, we are just drawing the shapes which the lines create, after adding it, our class becomes the below.
import sympy
import matplotlib.pyplot as plt
import matplotlib.patches as patches
p = sympy.Symbol("p")
class supplyDemand:
def __init__(self,demandEquation,supplyEquation):
self.demandEquation = demandEquation
self.supplyEquation = supplyEquation
self.priceEnd = sympy.solve(demandEquation)[0]
self.prices = []
self.demandQ = []
self.supplyQ = []
for price in range(0,self.priceEnd+1):
self.prices += [price]
self.demandQ += [demandEquation.subs(p,price)]
self.supplyQ += [supplyEquation.subs(p,price)]
self.startingQ = sympy.solve(demandEquation-supplyEquation)[0]
self.startingP = demandEquation.subs(p,self.startingQ)
self.mode = "Normal"
def internationalTrade(self,worldPrice):
self.mode = "International"
self.worldPrice = worldPrice
if worldPrice>self.startingP:
self.export = True
self.domesticQ = self.demandEquation.subs(p,worldPrice)
self.internationalQ = self.supplyEquation.subs(p,worldPrice)-self.demandEquation.subs(p,worldPrice)
self.totalQ = self.domesticQ+self.internationalQ
else:
self.export = False
self.domesticQ = self.supplyEquation.subs(p,worldPrice)
self.internationalQ = self.demandEquation.subs(p,worldPrice)-self.domesticQ
self.totalQ = self.domesticQ+self.internationalQ
self.domesticQ
def drawSurplus1(self):
if self.export:
triangle1 = patches.Polygon([[self.domesticQ,self.worldPrice],[0,self.worldPrice],[0,self.priceEnd]],True,label="Consumer Surplus")
triangle2 = patches.Polygon([[0,0],[0,self.startingP],[self.startingQ,self.startingP]],True,label="Original Producer Surplus",color="red")
rect1 = patches.Polygon([[0,self.startingP],[0,self.worldPrice],[self.domesticQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Transferred Surplus",color="orange")
triangle3 = patches.Polygon([[self.domesticQ,self.worldPrice],[self.totalQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Surplus from International",color="yellow")
currentAxis = plt.gca()
currentAxis.add_patch(triangle1)
currentAxis.add_patch(triangle2)
currentAxis.add_patch(rect1)
currentAxis.add_patch(triangle3)
else:
triangle1 = patches.Polygon([[self.startingQ,self.startingP],[0,self.startingP],[0,self.priceEnd]],True,label="Original Consumer Surplus", color="red")
triangle2 = patches.Polygon([[0,0],[0,self.worldPrice],[self.domesticQ,self.worldPrice]],True,label="Producer Surplus")
rect1 = patches.Polygon([[0,self.startingP],[0,self.worldPrice],[self.domesticQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Transferred Surplus",color="orange")
triangle3 = patches.Polygon([[self.domesticQ,self.worldPrice],[self.totalQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Surplus from International",color="yellow")
currentAxis = plt.gca()
currentAxis.add_patch(triangle1)
currentAxis.add_patch(triangle2)
currentAxis.add_patch(rect1)
currentAxis.add_patch(triangle3)
def plot(self):
plt.plot(self.demandQ,self.prices,'k')
plt.plot(self.supplyQ,self.prices,'k')
plt.plot(self.startingQ,self.startingP, 'bo')
plt.xlabel("Supply and Demand Quantity")
plt.ylabel("Price")
if self.mode=="International":
plt.plot([0,self.priceEnd],[self.worldPrice,self.worldPrice],"k")
if self.startingP!=self.worldPrice:
plt.plot(self.totalQ,self.worldPrice, 'bo')
plt.plot(self.domesticQ,self.worldPrice,'bo')
plt.plot([0,self.domesticQ],[self.worldPrice,self.worldPrice],"m",label='Domestic Quantity')
plt.plot([self.domesticQ,self.totalQ],[self.worldPrice,self.worldPrice],"g", label='International Quantity')
if self.export:
print("The country exports during international trade, and the price moves from "+str(self.startingP)+" to "+str(self.worldPrice)+".")
print("The domestic producers go from producing "+str(self.startingQ)+" units to "+str(self.totalQ)+" units.")
print("The domestic consumers go from consuming "+str(self.startingQ)+" units to "+str(self.domesticQ)+" units.")
print("Total domestic producer revenue is "+str(self.totalQ*self.worldPrice))
else:
print("The country imports during international trade, and the price moves from "+str(self.startingP)+" to "+str(self.worldPrice)+".")
print("The domestic producers go from producing "+str(self.startingQ)+" units to "+str(self.domesticQ)+" units.")
print("The domestic consumers go from consuming "+str(self.startingQ)+" units to "+str(self.totalQ)+" units.")
print("Total domestic producer revenue is "+str(self.domesticQ*self.worldPrice))
plt.legend()
plt.show()
Let’s try it out!
economy1 = supplyDemand(10-p,p)
economy1.internationalTrade(4)
economy1.drawSurplus1()
economy1.plot()
Finally, let’s print out the changes in surplus. What you will see is that total surplus goes up when we allow international trade. Some surplus gets transferred from one side to the other, but also new surplus is created from the imports or exports
import sympy
import matplotlib.pyplot as plt
import matplotlib.patches as patches
p = sympy.Symbol("p")
class supplyDemand:
def __init__(self,demandEquation,supplyEquation):
self.demandEquation = demandEquation
self.supplyEquation = supplyEquation
self.priceEnd = sympy.solve(demandEquation)[0]
self.prices = []
self.demandQ = []
self.supplyQ = []
for price in range(0,self.priceEnd+1):
self.prices += [price]
self.demandQ += [demandEquation.subs(p,price)]
self.supplyQ += [supplyEquation.subs(p,price)]
self.startingQ = sympy.solve(demandEquation-supplyEquation)[0]
self.startingP = demandEquation.subs(p,self.startingQ)
self.mode = "Normal"
def internationalTrade(self,worldPrice):
self.mode = "International"
self.worldPrice = worldPrice
if worldPrice>self.startingP:
self.export = True
self.domesticQ = self.demandEquation.subs(p,worldPrice)
self.internationalQ = self.supplyEquation.subs(p,worldPrice)-self.demandEquation.subs(p,worldPrice)
self.totalQ = self.domesticQ+self.internationalQ
else:
self.export = False
self.domesticQ = self.supplyEquation.subs(p,worldPrice)
self.internationalQ = self.demandEquation.subs(p,worldPrice)-self.domesticQ
self.totalQ = self.domesticQ+self.internationalQ
self.domesticQ
def drawSurplus1(self):
if self.export:
triangle1 = patches.Polygon([[self.domesticQ,self.worldPrice],[0,self.worldPrice],[0,self.priceEnd]],True,label="Consumer Surplus")
triangle2 = patches.Polygon([[0,0],[0,self.startingP],[self.startingQ,self.startingP]],True,label="Original Producer Surplus",color="red")
rect1 = patches.Polygon([[0,self.startingP],[0,self.worldPrice],[self.domesticQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Transferred Surplus",color="orange")
triangle3 = patches.Polygon([[self.domesticQ,self.worldPrice],[self.totalQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Surplus from International",color="yellow")
currentAxis = plt.gca()
currentAxis.add_patch(triangle1)
currentAxis.add_patch(triangle2)
currentAxis.add_patch(rect1)
currentAxis.add_patch(triangle3)
print("The consumer surplus goes from "+str((self.priceEnd-self.startingP)*self.startingQ*.5)+" to "+str((self.priceEnd-self.worldPrice)*self.domesticQ*.5))
print("The producer surplus goes from "+str(self.startingP*self.startingQ*.5)+" to "+str(self.worldPrice*self.totalQ*.5))
else:
triangle1 = patches.Polygon([[self.startingQ,self.startingP],[0,self.startingP],[0,self.priceEnd]],True,label="Original Consumer Surplus", color="red")
triangle2 = patches.Polygon([[0,0],[0,self.worldPrice],[self.domesticQ,self.worldPrice]],True,label="Producer Surplus")
rect1 = patches.Polygon([[0,self.startingP],[0,self.worldPrice],[self.domesticQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Transferred Surplus",color="orange")
triangle3 = patches.Polygon([[self.domesticQ,self.worldPrice],[self.totalQ,self.worldPrice],[self.startingQ,self.startingP]],True,label="Surplus from International",color="yellow")
currentAxis = plt.gca()
currentAxis.add_patch(triangle1)
currentAxis.add_patch(triangle2)
currentAxis.add_patch(rect1)
currentAxis.add_patch(triangle3)
print("The producer surplus goes from "+str(self.startingP*self.startingQ*.5)+" to "+str(self.worldPrice*self.domesticQ*.5))
print("The consumer surplus goes from "+str((self.priceEnd-self.startingP)*self.startingQ*.5)+" to "+str((self.priceEnd-self.worldPrice)*self.domesticQ*.5))
def plot(self):
plt.plot(self.demandQ,self.prices,'k')
plt.plot(self.supplyQ,self.prices,'k')
plt.plot(self.startingQ,self.startingP, 'bo')
plt.xlabel("Supply and Demand Quantity")
plt.ylabel("Price")
if self.mode=="International":
plt.plot([0,self.priceEnd],[self.worldPrice,self.worldPrice],"k")
if self.startingP!=self.worldPrice:
plt.plot(self.totalQ,self.worldPrice, 'bo')
plt.plot(self.domesticQ,self.worldPrice,'bo')
plt.plot([0,self.domesticQ],[self.worldPrice,self.worldPrice],"m",label='Domestic Quantity')
plt.plot([self.domesticQ,self.totalQ],[self.worldPrice,self.worldPrice],"g", label='International Quantity')
if self.export:
print("The country exports during international trade, and the price moves from "+str(self.startingP)+" to "+str(self.worldPrice)+".")
print("The domestic producers go from producing "+str(self.startingQ)+" units to "+str(self.totalQ)+" units.")
print("The domestic consumers go from consuming "+str(self.startingQ)+" units to "+str(self.domesticQ)+" units.")
print("Total domestic producer revenue is "+str(self.totalQ*self.worldPrice))
else:
print("The country imports during international trade, and the price moves from "+str(self.startingP)+" to "+str(self.worldPrice)+".")
print("The domestic producers go from producing "+str(self.startingQ)+" units to "+str(self.domesticQ)+" units.")
print("The domestic consumers go from consuming "+str(self.startingQ)+" units to "+str(self.totalQ)+" units.")
print("Total domestic producer revenue is "+str(self.domesticQ*self.worldPrice))
plt.legend()
plt.show()
economy1 = supplyDemand(10-p,p)
economy1.internationalTrade(6)
economy1.drawSurplus1()
economy1.plot()
Source Code