Draw any Edgeworth box
The purpose of this notebook
Test by yourself
U1
U2
#To read the text above and translate it as a lamda function
from sympy import Symbol
c = Symbol("c")
t = Symbol("t")
from sympy.parsing.sympy_parser import parse_expr
U1_e = parse_expr(U1)
U2_e = parse_expr(U2)
from sympy.utilities.lambdify import lambdify
U1_l = lambdify((c,t),U1_e)
U2_l = lambdify((c,t),U2_e)
#to realy draw the EdgeworthBox
draw_Ed_Bow(U1_l,U2_l,8,6,colors=["k","Orange", "lightblue","mistyrose"],Num_ind=5)
draw_Ed_Bow(U1_l,U2_l,8,6,Xlab="Courgettes",Ylab="Tomatoes",AlPoint=(3,3),Contract_draw=False)
plt.show()
The function
#The main function
def draw_Ed_Bow(U1,U2, Xmax, Ymax, Xmin=10**(-6), Ymin=10**(-6), Utility_Show = False, Num_ind = 10,Xlab ="X",Ylab="Y",e=200, Contract_draw=True,AlPoint = None,colors =["black","Orange", "blue","red"],Utility_draw = True):
"""
Input :
U1: Utility of the 1st agent (must depend on 2 variables)
U2: Utility of the 2nd agent (must depend on 2 variables)
Xmax : the limit of the box
Ymax: the limit of the box
Xmin=10**(-6): the limit of the box (default: set to ~0 to avoid problems with log expressions)
Ymin=10**(-6): the box limit (default: set to ~0 to avoid problems with log expressions)
Utility_Show = False: Show utility levels on the Edgeworth box
Num_ind = 10 : Number of indifference curves per agent
e = 200 : Number of steps to compute the utility levels and the contract curve
Contract_draw = True: Draw the contract curve
AlPoint = None : show an allocation point and his 2 indifference curves. Should be a tuple/list (x,y)
colors = ["black","Orange", "blue","red"] : To choose the color of : [ContractCurve, endowment point, indifference curves Agent1, indifference curves agent2]
Utility_draw = True : Draw the indefrence curves
Output:
None (but draws the Edgeworth box)
"""
delta = min((Xmax-Xmin)/e,(Ymax-Ymin)/e)
x = np.arange(Xmin, Xmax, delta) #Tomates
y = np.arange(Ymin, Ymax, delta) #courgettes
X, Y = np.meshgrid(x, y)
Z1 = lambda x,y : U1(x,y)
Z2 = lambda x,y : U2(Xmax-x,Ymax-y)
#the contract curve
Num_ind_1 = Num_ind
Num_ind_2 = Num_ind
if Contract_draw == True:
Z2grad = np.gradient(Z2(X,Y))
Z1grad = np.gradient(Z1(X,Y))
out = (Z2grad[0]*Z1grad[1]-Z2grad[1]*Z1grad[0])
Cont = plt.contour(X,Y,out,colors=colors[0],levels=[0])
fmt = {}
strs = ["Contract curve"]
for l, s in zip(Cont.levels, strs):
fmt[l] = s
plt.clabel(Cont, Cont.levels, inline = True,
fmt = fmt, fontsize = 10)
C_curv = abs(pd.DataFrame(out ,index=y, columns=x))
C_curv = C_curv.index @ (C_curv == C_curv.apply(min))
xC_curv = np.arange(Xmin,Xmax,(Xmax-Xmin)/(Num_ind+1))
C_curv = np.interp(xC_curv,C_curv.index,C_curv)
Num_ind_1 = pd.Series(Z1(xC_curv,C_curv)).sort_values(ascending=True)
Num_ind_2 = pd.Series(Z2(xC_curv,C_curv)).sort_values(ascending=True)
#Draw the dotation point and his curves
if AlPoint != None:
plt.scatter(AlPoint[0],AlPoint[1],s=200,marker=".",color = colors[1],label="Allocation point")
Num_ind_1 = [Z1(AlPoint[0],AlPoint[1])]
Num_ind_2 = [Z2(AlPoint[0],AlPoint[1])]
#draw the indifference curve
if Utility_draw == True:
C1 = plt.contour(X, Y, Z1(X,Y),colors = colors[2],levels=Num_ind_1)
C2 = plt.contour(X, Y, Z2(X,Y),colors = colors[3],levels=Num_ind_2)
if Utility_Show == True:
fmt = {}
strs = round(pd.Series(C1.levels[:]),1)
for l, s in zip(C1.levels, strs):
fmt[l] = s
plt.clabel(C1, C1.levels, inline = True,
fmt = fmt, fontsize = 10)
#Utility level2
fmt = {}
strs = round(pd.Series(C2.levels[:]),1)
for l, s in zip(C2.levels, strs):
fmt[l] = s
plt.clabel(C2, C2.levels, inline = True,
fmt = fmt, fontsize = 10)
plt.title("Edgeworth box")
plt.xlabel(Xlab)
plt.ylabel(Ylab)
Example of uses
# Utility of 1st agent (depend on X,Y):
U1 = lambda x, y: np.log10(x) + y ** (0.5) + 10
# Utility of 2nd agent (depend on X,Y):
U2 = lambda x, y: 0.6 * np.log10(x) + 2 * (y) ** 0.2
draw_Ed_Bow(U1, U2, 4, 8, e=300,Num_ind=8,Contract_draw=False)
plt.show()
# Utility of 1st agent (depend on X,Y):
U1 = lambda x, y: np.log10(x) + y ** (0.5) + 10
# Utility of 2nd agent (depend on X,Y):
U2 = lambda x, y: 0.6 * np.log10(x) + 2 * (y) ** 0.2
draw_Ed_Bow(U1, U2, 4, 8, e=300,Num_ind=3)
plt.show()
#Utility of 1st agent (depend on X,Y):
U1 = lambda b,f : 2*b + f
#Utility of 2nd agent (depend on X,Y):
U2 = lambda b,f : 12*b + (10-f)*f
draw_Ed_Bow(U1,U2,8,4,Num_ind=4,colors=["k","Orange", "lightblue","mistyrose"])
draw_Ed_Bow(U1,U2,8,4,Xlab="Book",Ylab="Food",AlPoint=(3,0.5),Contract_draw=False)
plt.show()
#Utility of 1st agent (depend on X,Y):
U1 = lambda c,t : c**0.5 * t**0.3
#Utility of 2nd agent (depend on X,Y):
U2 = lambda c,t : c**0.5 * t**0.5
draw_Ed_Bow(U1,U2,8,6,colors=["k","Orange", "lightblue","mistyrose"],Num_ind=5)
draw_Ed_Bow(U1,U2,8,6,Xlab="Courgettes",Ylab="Tomatoes",AlPoint=(3,3),Contract_draw=False)
plt.show()
#Utility of 1st agent (depend on X,Y):
U1 = lambda c,t : 2*c + t
#Utility of 2nd agent (depend on X,Y):
U2 = lambda c,t : 12*c +(10-t)*t
draw_Ed_Bow(U1,U2,8,4,colors=["k","Orange", "lightblue","mistyrose"],Num_ind=5)
draw_Ed_Bow(U1,U2,8,4,Xlab="Courgettes",Ylab="Tomatoes",AlPoint=(2,2),Contract_draw=True)
plt.show()
#Utility of 1st agent (depend on X,Y):
U1 = lambda x,y : x*np.exp(-(x**2 + y**2))
#Utility of 2nd agent (depend on X,Y):
U2 = lambda c,t : c**0.5 * t**0.5
maxval = 2.5
draw_Ed_Bow(U1,U2,maxval,maxval,Ymin=0,Xmin=0,Contract_draw=False,e=600,Num_ind=10)
draw_Ed_Bow(U1,U2,maxval,maxval,Ymin=0,Xmin=0,Contract_draw=True,e=600,Num_ind=10,Utility_draw = False)
plt.title("Not an Edgeworth Box !")
plt.show()
#Utility of 1st agent (depends on X,Y):
UM = lambda x,y : (-1 + (x+6)**2 + (x+6) * (y*4) )/((x+6) + (y*4))+100
#Utility of 2nd agent (depends on X,Y):
UH = lambda x,y : (-1 + x**2 + x * y )/(x + y)+100
draw_Ed_Bow(UH,UM,8,4,Xlab="Courgettes",Ylab="Tomatoes",Num_ind=5)
plt.text(-0.3,-0.2,"M")
plt.text(8.1,4.1,"H")
plt.show()
#Utility of 1st agent (depends on X,Y):
UM = lambda x,y : (-1 + (x+6)**2 + (x+6) * (y*4) )/((x+6) + (y*4))+100
#Utility of 2nd agent (depends on X,Y):
UH = lambda x,y : (-1 + x**2 + x * y )/(x + y) +100
draw_Ed_Bow(UH,UM,8,4,Xlab="Courgettes",Ylab="Tomatoes",Num_ind=5)
plt.text(-0.3,-0.2,"M")
plt.text(8.1,4.1,"H")
plt.show()