r-cas / ryacas

Ryacas: R Interface to the Yacas Computer Algebra System
GNU General Public License v2.0
38 stars 8 forks source link

Yacas: solving fractional-exponent (Cobb-Douglas) problem #65

Open bangecon opened 2 years ago

bangecon commented 2 years ago

More of a Yacas issue than with the R interface to it, but I'm cross-posting here in case someone who manages this space can also help.

I'm solving a basic economic constrained-optimization problem using a Cobb-Douglas objective function of the form,

$$y = x_1^\alpha x_2^{1-\alpha}$$ such that $$M - p_1x_1 - p_2x_2 \ge 0$$

Where the endogenous variables are x1 and x2, and the exogenous parameters are p1, p2, and alpha. I have no problem solving the first order conditions for x1, x2, and lambda, and combining the first two conditions I get something like this:

$$\alpha x_2^{1-\alpha} / p_1x_1^{1-\alpha} - (1-\alpha)x_1^\alpha / p_2x_2^\alpha = 0$$

For the first example I wrote up, I used the parameters, $\alpha = 0.25$, $p_1 = 1.5$, and $p_2 = 1$. No problems:

> L <- ysym('x1^(0.25) * x2^(0.75) + lambda*(100 - 1.5*x1 - x2)')
> dL <- c(deriv(L, 'x1'), deriv(L, 'x2'), deriv(L, 'lambda'))
> MUp1 <- solve(dL[[1]], 'lambda') |>
+     y_rmvars() |>
+     yac_str() |>
+     Simplify() |>
+     ysym()
> MUp2 <- solve(dL[[2]], 'lambda') |>
+     y_rmvars() |>
+     yac_str() |>
+     Simplify() |>
+     ysym()
> MUpcond <- MUp1 - MUp2
> (MUpsolve1 <- solve(MUpcond, 'x2'))
{x2==4.5*x1} 

This is the correct answer.

I changed the parameters to $\alpha = 0.4$, $p_1 = 1$, and $p_2 = 1.5$, and Yacas broke. The FOCs combine as:

$$0.4x_2^{0.4}/x_1^{0.4} - 0.4x_1^{0.6}/x_2^{0.6} = 0$$

The solution for x2 as a function of x1 couldn't be simpler: $x_2 = x_1$. And yet,

> L <- ysym('x1^(0.6) * x2^(0.4) + lambda*(100 - 1.5*x1 - x2)')
> dL <- c(deriv(L, 'x1'), deriv(L, 'x2'), deriv(L, 'lambda'))
> MUp1 <- solve(dL[[1]], 'lambda') |>
+     y_rmvars() |>
+     yac_str() |>
+     Simplify() |>
+     ysym()
> MUp2 <- solve(dL[[2]], 'lambda') |>
+     y_rmvars() |>
+     yac_str() |>
+     Simplify() |>
+     ysym()
> MUpcond <- MUp1 - MUp2
> (MUpsolve1 <- solve(MUpcond, 'x2'))
{} 

This is fundamentally the same problem to solve, is it not? Why doesn't solve() give an answer?

I replicated the code directly in Yacas, and it gives the same (non-) result. Why is a straightforward change in parameters in a relatively simple problem such a big deal?

P.S. I also tried log-linearizing the optimization problem, and that didn't help, either. P.P.S. Unrelated, but the Deriv:: version of Simplify() reduces expressions more and better than the version from Yacas; again, probably not an issue related to the R interface, and not directly related to the current problem.

mikldk commented 2 years ago

@grzegorzmazur do you have any idea?

@bangecon Meanwhile, you can try with caracas:

library(caracas)

Lsym <- as_sym('x1^a * x2^(1-a) + l*(100 - 1.5*x1 - x2)')

L <- subs(Lsym, 'a', '0.6')

dLx1 <- der(L, 'x1')
dLx2 <- der(L, 'x2')

MUp1 <- solve_sys(dLx1, 'l')
MUp2 <- solve_sys(dLx2, 'l')

MUpcond <- MUp1[[1]]$l - MUp2[[1]]$l
MUpsolve1 <- solve_sys(MUpcond, 'x2')
MUpsolve1
#> Solution 1:
#>   x2 =  x₁
grzegorzmazur commented 2 years ago

Not yet, I'll have a look at it on weekend.

bangecon commented 2 years ago

Caracas is a viable solution to the basic issue, but since I'm preparing these examples for teaching purposes it would be nice to find a solution that I can implement without asking students to download another program (i.e. Python).

On Fri, Jul 8, 2022, 1:36 AM Mikkel Meyer Andersen @.***> wrote:

@grzegorzmazur https://github.com/grzegorzmazur do you have any idea?

@bangecon https://github.com/bangecon Meanwhile, you can try with caracas https://r-cas.github.io/caracas/:

library(caracas)

Lsym <- as_sym('x1^a x2^(1-a) + l(100 - 1.5*x1 - x2)')

L <- subs(Lsym, 'a', '0.6')

dLx1 <- der(L, 'x1') dLx2 <- der(L, 'x2')

MUp1 <- solve_sys(dLx1, 'l') MUp2 <- solve_sys(dLx2, 'l')

MUpcond <- MUp1[[1]]$l - MUp2[[1]]$l MUpsolve1 <- solve_sys(MUpcond, 'x2') MUpsolve1

> Solution 1:

> x2 = x₁

— Reply to this email directly, view it on GitHub https://github.com/r-cas/ryacas/issues/65#issuecomment-1178610063, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEBYXWJJDUYFK3HDDISRNE3VS7D5RANCNFSM526RUMOQ . You are receiving this because you were mentioned.Message ID: @.***>