Ask Your Question
0

Unable to calculate market equilibrium

asked 2021-12-09 23:13:17 +0200

Cyrille gravatar image

Here is a huge notebook, but as the error arrives just at the end I am obliged to show that if Sage lacks of an assume() about a variable, I can't see which hypothesis is missing. The object of the notebook is to calculate the equilibrium price.

We begin by the demand side

var('a x y p_x p_y D Rev R l')
assume(a,'real')
assume(x,'real')
assume(y,'real')
assume(p_x,'real')
assume(p_y,'real')
assume(D,'real')
assume(Rev,'real')
assume(R,'real')
assume(l,'real')
assume(p_x,'real')
assume(p_y,'real')
assume(l,'real')
assume(l>0)
#assume(a>-1)
assume(a>0)
assume(p_x>0)
assume(p_y>0)
assume(R>0)
U =(1/(1-(1/a)))*x^(1-(1/a))+y
show(LatexExpr(r"""\text{La fonction d'utilité est }U(x,y) = """),U)
D= x*p_x + y*p_y
show(LatexExpr(r'\text{La Dépense } D = '),D)
Rev= R
show(LatexExpr(r'\text{Le Revenu } Rev = '),R)
L=U+l*(Rev-D)
show(LatexExpr(r'\text{Le lagrangien est } \mathcal{L}(x, y, λ) = '),L)
FOC = [diff(L,x),diff(L,y),diff(L,l)]
show(LatexExpr(r'\text{Les condition du premier ordre sont } \left\{\begin{array}{c}\mathcal{L}_x= 0\\\mathcal{L}_y= 0\\\mathcal{L}_λ= 0\end{array}\right. '))
show(LatexExpr(r'\text{soit }'))
show(LatexExpr(r'\mathcal{L}_x= 0 \Longleftrightarrow '),FOC[0]==0)    
show(LatexExpr(r'\mathcal{L}_y= 0 \Longleftrightarrow '),FOC[1]==0)
show(LatexExpr(r'\mathcal{L}_λ= 0 \Longleftrightarrow '),FOC[2]==0)
sol = solve([FOC[0]==0,FOC[1]==0,FOC[2]==0],x,y,l, algorithm="sympy")
xs=sol[0][x]
ys=sol[0][y]
ls=sol[0][l]
show(LatexExpr(r"""\text{À l'optimum, on a :}"""))
show(LatexExpr(r'  \,\,\,\,\,\,\,\,\,         x^d = '),xs)
show(LatexExpr(r'  \,\,\,\,\,\,\,\,\,         y^d = '),ys)
show(LatexExpr(r'  \,\,\,\,\,\,\,\,\,         \lambda^\star = '),ls)
show(LatexExpr(r'\text{Si le bien } y \text{ sert de numéraire, on  a : }p_y =1 \text{ et, ainsi :}'))
xsn=xs.substitute(p_y=1).factor()
ysn=ys.substitute(p_y=1).factor()
lsn=ls.substitute(p_y=1).factor()
show(LatexExpr(r'  \,\,\,\,\,\,\,\,\,         x^\star = '),xsn)
show(LatexExpr(r'  \,\,\,\,\,\,\,\,\,         y^\star = '),ysn)
show(LatexExpr(r'  \,\,\,\,\,\,\,\,\,         \lambda^\star = '),lsn)
show(LatexExpr(r'\text{On a :}'))
x_p=diff(xsn,p_x).simplify()
show(LatexExpr(r'x_p = '),x_p,LatexExpr(r'< 0.'))

Then follows the offer side

var('C c')
assume(c, 'real')
assume(c>0)
C= (1/2)*c*x^2
show(LatexExpr(r'\text{On suppose que la fonction de coût du producteur est  :  } C(x) ='), C)
pi=(p_x *x- C)
show(LatexExpr(r'\text{ Le profit est :  } \pi(x) = '), pi)
pip= diff(pi,x)
show(LatexExpr(r'\text{ Le profit marginal est :  } \pi^\prime(x) = '), pip, LatexExpr(r'( = 0\text{  à l}^\prime\text{optimum})'))
pipp= diff(pi,x,2)
show(LatexExpr(r"""\text{Il s'agit bien d'un maximum puisque :  } \pi^{\prime\prime}(x) = """),pipp,LatexExpr(r'<0'))
prod=solve(pip==0,x)[0].rhs()
show(LatexExpr(r"""\text{L'offre de produit est :  } x^o = """),prod)

And now the market equilibrium where Sage seems to lack an hypothesis :

show(LatexExpr(r'\text{L}^\prime\text{équilibre de concurrence pure et parfaite est caractérisé par le système d}^\prime\text{équations :  } '))
show(LatexExpr(r'x^d = x^o'))
show(LatexExpr(r'x^d = '),xsn)
show(LatexExpr(r'x^o = '),prod)
pem= solve(xsn==prod, p_x)[0].rhs()
show(pem)

Also I would like to have the inverse demand fonction directly that is from the notebook if

$x_s= (1/p_x)^a \Longrightarrow p_x = (1/x_s)^{1/a}$, I have tried

def symbolic_inverse(f, x):
     y = SR.var('y')
     g = (f - y).roots(x, multiplicities=False)
     return [expr.subs(y=x) for expr in g]

symbolic_inverse(SR(1/(x^(1/a))),x)

but even there it seem to lack an assumption.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2021-12-10 18:03:37 +0200

Emmanuel Charpentier gravatar image

Initial question :

sage: with assuming(a,"noninteger"):(xsn==prod).solve(p_x)
[p_x == c^(1/(a + 1))]

Second question :

sage: with assuming(x>0, a,"noninteger"):
....:     fp_x(x)=(x==(1/p_x)^a).solve(p_x)[0].rhs()
....:     
sage: fp_x
x |--> 1/(x^(1/a))

A generic solution to this second question raises a lot of questions :

  • Is f an expression or a callable expression ?

  • What is the relevant argument ?

  • What are the necessary assumptions ?

And there are probably others...

Generic notes :

  • You should lighten and densify your notations (e. g. var("x, y") ; assume (x, "real") ; assume(x>0) ; assume(y, "real") ; assume (y>0) can be lightened into var("x,y", domain="positive").

  • You should distinguish what you want to use as expressions and what you deem functions, distinguishing further arguments and parameters in the latter case...

Learn Python ; learn Sage (a sound time investment, in both cases...). But I repeat myself...

edit flag offensive delete link more

Comments

Emmanual, I have now stayed a very long time on Sage but docs are not always up to date and when you do not know what to search for it's often rude. For instance, in the doc named assumptions, (https://doc.sagemath.org/html/en/refe...) there is no motion of the option domain. And believe it or not I normaly use to search a lot before asking a question.

CyrilleP gravatar imageCyrilleP ( 2021-12-10 22:31:44 +0200 )edit

there is no motion of the option domain

var?

FWIW :

sage: assume(x,"xyzzy")

[ Snip.. ]

ValueError: xyzzy not a valid assumption, must be one of ['analytic', 'antisymmetric', 'commutative', 'complex', 'constant', 'decreasing', 'even', 'evenfun', 'imaginary', 'increasing', 'integer', 'integervalued', 'irrational', 'lassociative', 'noninteger', 'odd', 'oddfun', 'one_to_one', 'posfun', 'rassociative', 'rational', 'real', 'symmetric'].

See also :

sage: maxima_calculus.assume?
sage: maxima_calculus.declare?

and the Maxima documentation (current Sage assumption system is a wrapping around Maxima's...).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2021-12-11 18:41:52 +0200 )edit

So I must apologize but unfortunately I have not always the good way to search.

CyrilleP gravatar imageCyrilleP ( 2021-12-14 12:39:03 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-12-09 23:13:17 +0200

Seen: 66 times

Last updated: Dec 10 '21