-
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
-
Solving for Two Different Prices Part 2
Now let’s reverse the first step we did to find the modified quantity equation.
import sympy
p = sympy.Symbol("p")
q = sympy.Symbol("q")
cTax = sympy.Symbol("cTax")
pTax = sympy.Symbol("pTax")
def eqSolve(eq1,eq2,tax):
demandP = sympy.solve(eq1-q,p)[0]
supplyP = sympy.solve(eq2-q,p)[0]
demandP = demandP-cTax
supplyP = supplyP+pTax
demandQ = sympy.solve(demandP-p,q)[0]
supplyQ = sympy.solve(supplyP-p,q)[0]
print(demandQ)
print(supplyQ)
eqSolve(10-p,2*p,2)
The final step is solving the system of equations. We are going to feed sympy the equations in a tuple. Tuples are very similar to arrays, but we don’t use brackets, instead we use parentheses. We will give the program a tuple that looks like this (equation1, equation 2, equation 3). The three equations we are using as follows. 1) demandP = supplyP, these prices are the same because we added the correcting terms of tax. 2) demandP = supplyQ, you can’t sell more than people buy or vice versa. 3) tax = cTax+pTax, the two taxes need to add up. Everything needs to be moved to one side, so our tuple is: (demandP-supplyP, demandQ-supplyQ,tax-cTax-pTax). We are going to need to also tell sympy what variables we want to solve for after the tuple. We specify q,p,cTax,pTax so that it solves the system.
import sympy
p = sympy.Symbol("p")
q = sympy.Symbol("q")
cTax = sympy.Symbol("cTax")
pTax = sympy.Symbol("pTax")
def eqSolve(eq1,eq2,tax):
demandP = sympy.solve(eq1-q,p)[0]
supplyP = sympy.solve(eq2-q,p)[0]
demandP = demandP-cTax
supplyP = supplyP+pTax
demandQ = sympy.solve(demandP-p,q)[0]
supplyQ = sympy.solve(supplyP-p,q)[0]
return sympy.solve((demandP-supplyP, demandQ-supplyQ,tax-cTax-pTax), q,p,cTax,pTax)
eqSolve(10-p,2*p,2)
What we get back is something you haven’t seen in this course yet, a dictionary. I’m going to break away from the function for one second to explain a dictionary. Dictionaries have keys, and each key has a value associated with it. This value can be anything valid, like an integer, a string, or even an array.
To initiate a dictionary, you set a variable equal to something with this format: {key1: value1, key2:value2…keyN:valueN}. Let’s see an example.
music = {"Genre":"Rap","Artists":["Kanye West","Kendrick Lamar","J. Cole"],"Albums":10}
print(music)
To get the value for a key you do this:
print(music["Genre"])
And if we wanted to add one to the number of albums we could do this:
music["Albums"]+=1
print(music["Albums"])
Now that we know how dictionaries work, let’s get the value of quantity from our dictionary by using the key q.
import sympy
p = sympy.Symbol("p")
q = sympy.Symbol("q")
cTax = sympy.Symbol("cTax")
pTax = sympy.Symbol("pTax")
def eqSolve(eq1,eq2,tax):
demandP = sympy.solve(eq1-q,p)[0]
supplyP = sympy.solve(eq2-q,p)[0]
demandP = demandP-cTax
supplyP = supplyP+pTax
demandQ = sympy.solve(demandP-p,q)[0]
supplyQ = sympy.solve(supplyP-p,q)[0]
return sympy.solve((demandP-supplyP, demandQ-supplyQ,tax-cTax-pTax), q,p,cTax,pTax)[q]
eqSolve(10-p,2*p,2)
We are only going to need the quantity because we can find both prices for consumers and producers based on their respective curves in our model.