ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 16 Jul 2020 19:45:43 -0500Smoother 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.Thu, 16 Jul 2020 00:04:52 -0500https://ask.sagemath.org/question/52490/smoother-derivations-of-hicksian-demands-in-sage/Answer by EconJohn for <p>The problem I'm solving is an economics one relating to solving for hicksian demands,<a href="https://ask.sagemath.org/question/52479/solve-not-working-for-expenditure-minimization-problem-in-sage/">given my difficulty solving for them directly</a>, Im trying to tackle this same problem indirectly and it works for the most part however I'd like it to run smoother.</p>
<p>Setting up the problem we have:</p>
<pre><code>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]]
</code></pre>
<p>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:</p>
<pre><code> 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)]
</code></pre>
<p>I again need to plug in this exact functional form to get the hicksian demands using shephard's lemma:</p>
<pre><code>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
</code></pre>
<p>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. </p>
<p><strong>Note:</strong> I see that this method actually does not work as I didnt notice <code>R</code> on the Left hand side of this problem.</p>
https://ask.sagemath.org/question/52490/smoother-derivations-of-hicksian-demands-in-sage/?answer=52522#post-id-52522Thanks to [Juanjo](https://ask.sagemath.org/users/25859/juanjo/)'s answer to my question [here](https://ask.sagemath.org/question/52516/unknown-exponents-causing-problem-to-not-be-solved/) I've managed to get a script which gives us an analytical solution for our hicksian demand equations. This is:
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);
step1=solve([dLdx == 0, dLdy == 0, dLdl == 0], p1, p2, R)
step2=solve((p1/p2).subs(step1)==p1/p2, x1)
good2step3 = solve((U/R).subs(step2).log().log_expand()==0, x2)
good1step4=solve(step2[0].subs(good2step3).canonicalize_radical(),x1)
where `good1step4` is our hicksiand demand for $x_1$ and `good2step3` is our hicksian demand for $x_2$.
Its interesting how you need a different procedure when your constraint is non-linear.Thu, 16 Jul 2020 19:45:43 -0500https://ask.sagemath.org/question/52490/smoother-derivations-of-hicksian-demands-in-sage/?answer=52522#post-id-52522Answer by Emmanuel Charpentier for <p>The problem I'm solving is an economics one relating to solving for hicksian demands,<a href="https://ask.sagemath.org/question/52479/solve-not-working-for-expenditure-minimization-problem-in-sage/">given my difficulty solving for them directly</a>, Im trying to tackle this same problem indirectly and it works for the most part however I'd like it to run smoother.</p>
<p>Setting up the problem we have:</p>
<pre><code>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]]
</code></pre>
<p>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:</p>
<pre><code> 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)]
</code></pre>
<p>I again need to plug in this exact functional form to get the hicksian demands using shephard's lemma:</p>
<pre><code>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
</code></pre>
<p>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. </p>
<p><strong>Note:</strong> I see that this method actually does not work as I didnt notice <code>R</code> on the Left hand side of this problem.</p>
https://ask.sagemath.org/question/52490/smoother-derivations-of-hicksian-demands-in-sage/?answer=52497#post-id-52497This [answer](https://ask.sagemath.org/question/52479/solve-not-working-for-expenditure-minimization-problem-in-sage/) may be relevant : the "manual" solution proposed can be programmed (although the relevant simplifications may vary). Sage will give you a mathematically sound solution, that you may have to simplify "manually" (easy with Sage's help...).Thu, 16 Jul 2020 05:37:41 -0500https://ask.sagemath.org/question/52490/smoother-derivations-of-hicksian-demands-in-sage/?answer=52497#post-id-52497