ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 06 Nov 2020 18:35:31 +0100Issues with Cake Eating Problemhttps://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/A while back I asked about simulating the cake eating problem in sage and the algorithm seems like an excellent way to work with things. So far the code looks as follows for obtaining a soloution to the cake eating problem:
#Step1: Name variables
k,k1,v0,beta,vk,vk1=var('k,k1,v0,beta,vk,vk1')
#Step2: Initialize your values
beta=0.6
v0=log(k)
#Step 3: The Loop to Obtain Policy function
for n in range(5):
vk = log(k-k1) + beta*v0(k=k1)
FOC = vk.diff(k1)
k1star = solve(FOC==0, k1)
print(n, k1star)
v0 = (vk).subs(k1=k1star[0].rhs())
The results I get are as follows:
0 [k1 == 3/8*k]
1 [k1 == 24/49*k]
2 [k1 == 147/272*k]
3 [k1 == 816/1441*k]
4 [k1 == 4323/7448*k]
Initially I thought that it would be a matter of just setting `k` to some value however this breaks the loop.
If I wanted to obtain values for `k1` (and then plot them) how would I go about doing this?EconJohnFri, 06 Nov 2020 18:35:31 +0100https://ask.sagemath.org/question/54134/Smoother Derivations of Hicksian Demands in Sagehttps://ask.sagemath.org/question/52490/smoother-derivations-of-hicksian-demands-in-sage/The problem I'm solving is an economics one relating to solving for hicksian demands,[given my difficulty solving for them directly](https://ask.sagemath.org/question/52479/solve-not-working-for-expenditure-minimization-problem-in-sage/), Im trying to tackle this same problem indirectly and it works for the most part however I'd like it to run smoother.
Setting up the problem we have:
x1, x2, l, p1, p2, a, b, R= var('x1, x2, l, p1, p2, a, b, R')
U = x1^a*x2^b
m = p1*x1+p2*x2;
L = U+ l * (R-m);
dLdx = L.diff(x1);
dLdy = L.diff(x2);
dLdl = L.diff(l);
solve([dLdx == 0, dLdy == 0, dLdl == 0], x1, x2, l)
Out: [[x1 == R*a/((a + b)*p1), x2 == R*b/((a + b)*p2), l == (a + b)*(R*a/((a + b)*p1))^a*(R*b/((a + b)*p2))^b/R]]
I then manually plug in the values for $x_1$ and and $x_2$ into $U$ to get the indirect utility function and solve for the expenditure function:
expend=solve(U==(R*a/((a + b)*p1))^a*(R*b/((a + b)*p2)^b),R);expend
Out: [R == ((a + b)*p2)^b*x1^a*x2^b/(b*(R*a/((a + b)*p1))^a)]
I again need to plug in this exact functional form to get the hicksian demands using shephard's lemma:
expend1=((a + b)*p2)^b*x1^a*x2^b/(b*(R*a/((a + b)*p1))^a)
expend1.diff(p1) #the hicksian demand for x1
Out: ((a + b)*p2)^(b - 1)*(a + b)*x1^a*x2^b/(R*a/((a + b)*p1))^a
expend1.diff(p2) #the hicksian demand for x2
Out: ((a + b)*p2)^(b - 1)*(a + b)*x1^a*x2^b/(R*a/((a + b)*p1))^a
I'm interested in getting my code to run more parsimoniously as I find myself having to manually pull out solved values to move to next stages of the problem. This is frustrating because I'd have to do this each time if i change my functional form.
**Note:** I see that this method actually does not work as I didnt notice `R` on the Left hand side of this problem.EconJohnThu, 16 Jul 2020 07:04:52 +0200https://ask.sagemath.org/question/52490/Solve() not working for Expenditure Minimization Problem in Sagehttps://ask.sagemath.org/question/52479/solve-not-working-for-expenditure-minimization-problem-in-sage/Though I've figured out how to go through a standard utility maximization problem analytically in sage Im having difficulty solving its dual problem, expenditure minimization. The code I have is the following:
x1, x2, l, p1, p2, a, b, R= var('x1, x2, l, p1, p2, a, b, R')
U = x1^a*x2^b
m = p1*x1+p2*x2;
L = m+ l * (R-U);
dLdx = L.diff(x1);
dLdy = L.diff(x2);
dLdl = L.diff(l);
solve([dLdx == 0, dLdy == 0, dLdl == 0], x1, x2, l)
The output I get however is:
[-b*l*x1^a*x2^(b - 1) + p2 == 0, -a*l*x1^(a - 1)*x2^b + p1 == 0, -x1^a*x2^b + R == 0]
which are only the first order conditions and not solving for $x_1$ and $x_2$.
This code is exactly like my utility maximization code [here](https://ask.sagemath.org/question/52475/obtaining-analytical-solutions-to-utility-maximization-problems/) however I only moved where `m` and `U` are.
why is this the case?
Note: if I were to consider the following code:
x1, x2, l, p1, p2, a, b, R= var('x1, x2, l, p1, p2, R')
U = x1^1/3*x2^2/3
m = p1*x1+p2*x2;
L = m+ l * (R-U);
dLdx = L.diff(x1);
dLdy = L.diff(x2);
dLdl = L.diff(l);
solve([dLdx == 0, dLdy == 0, dLdl == 0], x1, x2, l)
I obtain the following analytical solution:
[[x1 == 1/2*18^(1/3)*R/(R*p1/p2)^(2/3), x2 == 18^(1/3)*(R*p1/p2)^(1/3), l==1/2*18^(1/3)*p1/(R*p1/p2)^(2/3)],
[x1 == 1/4*18^(1/3)*R*(-I*sqrt(3) - 1)/(R*p1/p2)^(2/3), x2 == -1/2*18^(1/3)*
(R*p1/p2)^(1/3)*(I*sqrt(3) + 1), l == 1/4*18^(1/3)*p1*(-I*sqrt(3) - 1)/(R*p1/p2)^(2/3)],
[x1 == -1/4*18^(1/3)*R*(-I*sqrt(3) + 1)/(R*p1/p2)^(2/3), x2 == 1/2*18^(1/3)*(R*p1/p2)^(1/3)*(I*sqrt(3) - 1), l == -1/4*18^(1/3)*p1*(- I*sqrt(3) + 1)/(R*p1/p2)^(2/3)]]
My guess is that there is a bug with regards to the rules of multiplying undefined exponents.EconJohnWed, 15 Jul 2020 22:17:52 +0200https://ask.sagemath.org/question/52479/Deriving a contract curve in sagehttps://ask.sagemath.org/question/52480/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.EconJohnWed, 15 Jul 2020 23:07:14 +0200https://ask.sagemath.org/question/52480/Obtaining analytical solutions to utility maximization problemshttps://ask.sagemath.org/question/52475/obtaining-analytical-solutions-to-utility-maximization-problems/A while back I posted asking about <a href="https://ask.sagemath.org/question/51241/solving-lagrangians-in-sage/">Solving Lagrangians in Sage</a>. The numeric solutions I found to be extremely useful for understanding where exactly a maximum/ minimum lies. This is given by the following code:
x, y, l = var('x, y, l')
U = x^7/10 * y^3/10; U
m = 2*x+2*y; m
solve(m == 4000, y)
L = U - l * (m - 4000); L
dLdx = L.diff(x); dLdx
dLdy = L.diff(y); dLdy
dLdl = L.diff(l); dLdl
solve([dLdx == 0, dLdy == 0, dLdl == 0], x, y, l)
Im wondering however if there is a way to get an analytical solution? In the context of a utility maximization problem it would be a set of demand equations as a function of prices and Income. On paper we can do this easily in this context however it seems pretty difficult for me to understand.
EconJohnWed, 15 Jul 2020 18:03:41 +0200https://ask.sagemath.org/question/52475/Micro-economics packagehttps://ask.sagemath.org/question/31096/micro-economics-package/I am looking for a package for solving simple college-level micro-economics problems. For example, I am looking for a software where I can:
* Define utility functions, e.g. $u_1(x,y) = x^2 y$, $u_2(x,y) = x y^2$;
* Define initial endowments $(x_1,y_1)$ and $(x_2,y_2)$;
* Find and plot all the Pareto-efficient allocations;
* Find and plot all the envy-free allocations;
* Find a competitive equilibrium.
Is there a SageMath package for this?Erel Segal-HaleviFri, 27 Nov 2015 07:26:31 +0100https://ask.sagemath.org/question/31096/