-
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
-
Creating the International Functionality
Solution
import sympy
import matplotlib.pyplot as plt
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 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")
plt.show()
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
Let’s see how the quantities shift if the world price was 6 for a product, when the equilibrium before trade was 5.
economy1 = supplyDemand(10-p,p)
economy1.plot()
economy1.internationalTrade(6)
print(economy1.startingQ)
print(economy1.export)
print(economy1.domesticQ)
print(economy1.totalQ)
What happens when we introduce trade is that we export products. The domestic consumption goes from 5 to 4 units, and the domestic production goes from 5 to 6 units.
Let’s also update the plotting function. We will add a horizontal line, and two intersections on the supply and demand curves. These intersections will be at the domestic quantity, and the total quantity (which makes sense when you consider the way the math works)
import sympy
import matplotlib.pyplot as plt
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
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([self.totalQ,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],label='Domestic Quantity')
plt.plot([self.domesticQ,self.totalQ],[self.worldPrice,self.worldPrice], label='International Quantity')
plt.legend()
plt.show()
economy1 = supplyDemand(10-p,p)
economy1.internationalTrade(6)
economy1.plot()
When we create the horizontal lines, with plt.plot, so we feed the x coordinates, then the y coordinates, instead of giving it x,y coordinates like in patches. We draw three horizontal lines so that we can see where the domestic quantity is, and where the international quantity is. As well, by giving them labels and then calling plt.legend(), we are able to add a legend.
Finally, let’s add some print outs about what happens when we add international trade. This is the code that is going to be added with the international trade plotting.
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))
Adding this code, we get the following class.
import sympy
import matplotlib.pyplot as plt
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
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([self.totalQ,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],label='Domestic Quantity')
plt.plot([self.domesticQ,self.totalQ],[self.worldPrice,self.worldPrice], label='International Quantity')
plt.legend()
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.show()
economy1 = supplyDemand(10-p,p)
economy1.internationalTrade(6)
economy1.plot()