#################################### 0
For at least 3 days, I have fighted been fighting with SageMath to construct this code which works nicely.
def use_prime(expr):
"""
Return the expression with one-variable function derivatives "prime
"""
op = expr.operator()
if op:
args = expr.operands()
aargs = (use_prime(a) for a in args)
opp = op
if (isinstance(op, sage.symbolic.operators.FDerivativeOperator)and sage.symbolic.operators.FDerivativeOperator)
and len(args) == 1):
name = op.function().name()
primes = "’" * len(op.parameter_set())
opp = function(f"{name}{primes}")
return opp(*aargs)
else:
return expr
###################################### ### 1
var('x y p w_0 θ I')
assume(p>=0)
assume(p<=1)
assume(θ>=1)
assume(p >= 0)
assume(p <= 1)
assume(θ >= 1)
U = function('U')#déclare function('U') # déclare U comme le nom d'une fonction
EU(x,y,p) EU(x, y, p) = p * U(x) + (1-p) (1 - p) * U(y)
w_1 = function('w_1')
w_1(w_0, π) = (w_0-π)
(w_0 - π)
w_2 = function('w_2')
w_2(w_0, π, x, I, p) = (w_0 - π) - x + I
π = function('π')
π(p,I,θ)=θ*(1-p)*I
π(p, I, θ)=θ*(1-p)*I
U = function('U')
EU(x, y, p) = p*U(x) p * U(x) + (1 - p)*U(y)
p) * U(y)
x, y, p, w_0, θ, I, = SR.var('x y p w_0 θ I')
f = EU(w_1(w_0, π(p,I,θ)),w_2(w_0, π, x, I, π(p,I,θ)) π(p, I, θ)) )
h = diff(f, I)
hh = use_prime(h)
k = diff(f, I,2)
I, 2)
kk = use_prime(k)
show(LatexExpr(r'''\text{L'assuré show(LatexExpr(r'''\text{L’assuré cherche à résoudre le programme suivant :} '''))
show(LatexExpr(r'''I^\star= show(LatexExpr(r'''I^\star = \textrm{argmax}_{\{I\}}\left\{pU(w_1)+(1-p) U(w_2)| w_1=w_0 w_1'''
r'''=w_0 - \pi, w_2=w_0 w_2 = w_0 - \pi -x + I, \pi = \theta(1-p)I \right\} '''))
'''))
show(LatexExpr(r'''\text{ou encore : } I^\star = \textrm{argmax}_{\{I\}}\{'''), f,LatexExpr(r'\}')) \textrm{argmax}_{\{I\}}\{'''),
f, LatexExpr(r'\}'))
show(LatexExpr(r'''\text{La condition d'optimalité d’optimalité du premier ordre est } d\mathbb{E}U '''
r'''d\mathbb{E}U = 0 \text{, soit : } ''' ))
'''))
show(LatexExpr(r'\,\,\,\,\,\,\,\,\,\,\,\,\,\, d\mathbb{E}U = '),hh, ' = 0')
d2EU=diff(EU(w_1(w_0, π),w_2(w_0, d2EU = diff(EU(w_1(w_0, π), w_2(w_0, π, x, I, p),p),I,2)
p), p), I, 2)
show(LatexExpr(r'''\text{La condition d'optimalité d’optimalité du second ordre est } d\mathbb{E}U '''
r'''d\mathbb{E}U = 0 \text{, soit : } ''' ))
show(LatexExpr(r'\,\,\,\,\,\,\,\,\,\,\,\,\,\,'),kk, '''))
show(LatexExpr(r'\,\,\,\,\,\,\,\,\,\,\,\,\,\,'), kk, ' < 0')
######################################################## ### 2
show(LatexExpr(r'\text{Commençons par vérifier si la condition du '
r'du second ordre est vérifiée}'))
show(LatexExpr(r'\text{On va réécrire à la main la condition du second ordre de '
r'de manière à obtenir une équation algébrique en '
r'en substituant une variable positive}'))
show(LatexExpr(r'U2 > 0. \text{ Pour ce faire, il faut réussir à isoler } U^{\prime\prime}.'))
'
r'U^{\prime\prime}.'))
var('A')
U1=hh.expand().coefficient(θ).coefficient(p).subs(2==A).coefficient(A)
U2=hh.coefficient(θ).coefficient(p)
U1 = hh.expand().coefficient(θ).coefficient(p).subs(2 == A).coefficient(A)
U2 = hh.coefficient(θ).coefficient(p)
show(U1)
show(U2)
eqn=(U1/U2==(p - 1)*p*θ/ eqn = (U1/U2 == (p - 1) * p * θ / ((p - 1)*θ 1) * θ + 1)*(p 1) * (p - 1))
eqn1=((U1/U2)==(p - 1)*p*θ/ eqn1 = ((U1/U2) == (p - 1) * p * θ / ((p - 1)*θ 1) * θ + 1)*(p 1) * (p - 1))
show(eqn1)
subs(2==A)
is not self evident since a priori replace replacing a constant by an other another one
doesn't a priori come naturaly in mind.
to mind naturally. I have been oblige forced to take some contournment road detours
since I cannot ask what is he coefficient of say $(p-1)^2*\theta$. $(p-1)^2*\theta$.
My question is why ?why.
Secondly, I would like to know if there is a more automatic way to arrive to at the eqn
equation. equation.
That is is, if there is a solution to transform an equation eqn = (A-B==0)
in a second one eqn1 = (A==B)
(of course if A
and B
where only parameters or variables I could do it with solve(A==B, A)
but I use A
and B
to save typping.typing.
Thirdly, I would like to substitute x
to I
and 1
to \theta
and look what is the result. result.
But nothing I have tried works. Thanks for your helphelp.