# Deriving a contract curve in sage

Im trying to solve for the set of pareto efficent allocations using sage. So far i'm on the right track with my code of a two by two economy with consumer A and consumer B with resource constraints given by R1 and R2 for the total amount of good 1 and good 2.

xa1, xa2, xb1, xb2, a, b, R1, R2 = var('xa1, xa2, xb1, xb2, a, b, R1, R2')
Ua = xa1^a * xa2^b;
Ub = xb1^a*xb2^b;
R1=xa1+xb1;
R2=xa2+xb2;
MUa1=Ua.diff(xa1)
MUa2=Ua.diff(xa2)
MUb1=Ub.diff(xb1)
MUb2=Ub.diff(xb2)
MRSA=MUa1/MUa2
MRSB=MUb1/MUb2
solve([MRSA==MRSB],xa1)


The solution this code gives us is:

[xa1 == xa2*xb1/xb2]


I want to reduce xa1 to be a function of R1,R2 and xb1.

on paper this could be found by using the definitions of R1 and R2.

I've tried this before by including these variables in the square brackets of the solution but with no luck. Any help is appreciated.

edit retag close merge delete

Sort by » oldest newest most voted

Managed to figure out this problem. I found that if you define your constraints in the context of the solve() function we get a clearer picture. The contract curve from consumer A's perspective is given by the following:

xa1, xa2, xb1, xb2, a, b, R1, R2 = var('xa1, xa2, xb1, xb2, a, b, R1, R2')
Ua = xa1^a * xa2^b;
Ub = xb1^a*xb2^b;
MUa1=Ua.diff(xa1)
MUa2=Ua.diff(xa2)
MUb1=Ub.diff(xb1)
MUb2=Ub.diff(xb2)
MRSA=MUa1/MUa2
MRSB=MUb1/MUb2
solve([MRSA==MRSB,R1==xa1+xb1,R2==xa2+xb2],xa1,xb1,xb2)

Out: [[xa1 == R1*xa2/R2, xb1 == (R1*R2 - R1*xa2)/R2, xb2 == R2 - xa2]]


If we were to conciser consumer B's perspective we have to only change the last line of code to:

solve([MRSA==MRSB,R1==xa1+xb1,R2==xa2+xb2],xa1,xb1,xa2)

Out:  [[xa1 == (R1*R2 - R1*xb2)/R2, xb1 == R1*xb2/R2, xa2 == R2 - xb2]]


The reason why we need to change this last line of code is due to the nature of the solver.

Pretty happy with this result.

more

You can't : your solution involves xa2, which doesn't appear in the definition of R1 and R2 ; xa2 is an independent quantity.

May I suggest proofing your problem ?

EDIT : After typo correction, it's clearer. And CAN be solved. Let's rewrite the problem a bit:

xa1, xa2, xb1, xb2, a, b, R1, R2 = var('xa1, xa2, xb1, xb2, a, b, R1, R2')
Ua = xa1^a * xa2^b
Ub = xb1^a*xb2^b
MUa1=Ua.diff(xa1)
MUa2=Ua.diff(xa2)
MUb1=Ub.diff(xb1)
MUb2=Ub.diff(xb2)
MRSA=MUa1/MUa2
MRSB=MUb1/MUb2
Sol=solve([MRSA==MRSB],xa1)


Let's define R1 and R2 by equations, not assignments :

E1=R1==xa1+xb1
E2=R2==xa2+xb2


sage: Sol
[xa1 == xa2*xb1/xb2]


We can solve E1 and E2 for xa1 and xa2 (thus getting tid of th,R2andxb2 :em) :

sage: solve([E1,E2],[xa1,xa2])
[xa1 == R1 - xb1, xa2 == R2 - xb2]


Let(' substitute that into your solution :

sage: Sol.subs(solve([E1,E2],[xa1,xa2]))
R1 - xb1 == (R2 - xb2)*xb1/xb2


which gives us xb1 as a function of R1, R2 and xb1 :

sage: Sol.subs(solve([E1,E2],[xa1,xa2])).solve(xb1)
[xb1 == R1*xb2/R2]


which if I understand you correctly,is the result you sought.

HTH,

more

The form on paper you can solve this by subbing in our resource constraints and getting xa1==(R1-xa1)*xb1/(R2-xb1) further simplifying should give us xa1==xb1*R1/R2. I just havent figured out how to do this in sage.

This :

R1=xa1+xb1;
R2=xb1+xb2;


is curiously asymetric. Could you check ?

You seem to describe two symmetric actors. I would expect either :

R1=xa1+xb1;
R2=xa2+xb2;


or

R1=xa1+xa2;
R2=xb1+xb2;
`

Oh I see the issue. It was right in front of me. Editing