Ask Your Question
0

Minus sign lost in substitution

asked 2023-05-18 11:18:37 +0200

Cyrille gravatar image

updated 2023-05-19 18:28:06 +0200

slelievre gravatar image

This question is related to a previous question:

Is there a bug in substitution of a function in SageMath?

In asking the former question I saw an error in the result but I thought it might be my fault.

After analysing the code and the result I have discovered an error in the substitution.

I rewrite only the necessary part of the code:

def use_prime(expr):
    """
    Return the expression with one-variable function derivatives in prime notation.
    """
    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 len(args) == 1):
            name = op.function().name()
            primes = "’" * len(op.parameter_set())
            opp = function(f"{name}{primes}")
        return opp(*aargs)
    else:
        return expr

var('x y p w_0 θ I')
assume(p >= 0)
assume(p <= 1)
assume(θ >= 1)
U = function('U')  # déclare U comme le nom d'une fonction
EU(x, y, p) = p * U(x) + (1-p) * U(y)
w_1 = function('w_1')
w_1(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
U = function('U')
EU(x, y, p) = p * U(x) + (1 - 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, θ)))
h = diff(f, I)
hh = use_prime(h)
k = diff(f, I, 2)
kk = use_prime(k)
show(LatexExpr(r'''\text{L’assuré cherche à résoudre le programme suivant :} '''))
show(LatexExpr(r'''I^\star = \textrm{argmax}_{\{I\}}\left\{pU(w_1)+(1-p) U(w_2)| w_1'''
               r''' = w_0 - \pi, 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'\}'))     
show(LatexExpr(r'''\text{La condition d’optimalité du premier ordre est } '''
               r'''d\mathbb{E}U = 0 \text{, soit : }'''))
show(LatexExpr(r'\,\,\,\,\,\,\,\,\,\,\,\,\,\, d\mathbb{E}U = '), hh, ' = 0')

The result is

image description

Notice that $\pi = \theta (1-p)I$ which is rewritten directly by SageMath as $\pi = - \theta (p-1) I$ we must add the * of course. But if you look at the definition of $I^\star$ after the substitution, the negative sign has disappeared inside $U()$.

Why?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2023-05-18 20:48:59 +0200

Emmanuel Charpentier gravatar image

U never being defoined, your "minimal example" prints nothong useful...

If I follow you, you are complaining tha Sage formats its expression as it pleases itself. Note that :

sage: print(LatexExpr(r"(1-p)I\,=\,%s"%latex((1-p)*I)))
(1-p)I\,=\,-I {\left(p - 1\right)}

which typesets $$ (1-p)I\,=\,-I {\left(p - 1\right)} $$ which is mathematematically correct. The fact that it may be deemed unaesthetic is not something that can be controlled by a program. In other words "beautiful" as no algorithmically usable definition.

This may have been discussed a couple (or more) of times in this site...

Complain fiercely.

BTW, another pet peeve of mine :

sage: print(LatexExpr(r"\frac{1}{\sqrt{2}}\,=\,%s"%latex(1/sqrt(2))))
\frac{1}{\sqrt{2}}\,=\,\frac{1}{2} \, \sqrt{2}

which typesets $$ \frac{1}{\sqrt{2}}\,=\,\frac{1}{2} \, \sqrt{2} $$. Now, that's ugly... (but still correct !).

FWIW : rather than typing intricate LatexExpr definition, you'd probably gain much time, sweat, tears and frustration by using SageTeX to integrate your computation in a well (and clearly)--written Latex text.

HTH,

edit flag offensive delete link more

Comments

Emmanuel you miss the point. I am not complaining that for a CAS $(1-p)$ is $-(1-p)$ there is a long time since I have understood that this is like that (I used Mathematica since it's beginning). I complain that there is a substitution error. $U(w_1)!=U(I(p-1)\theta+....)$ but $U(w_1)!=U(-I(p-1)\theta+....)$. Finaly $U$ is defined as a function as $w_1$, $w_2$ and $\pi$. I was not aware that one must also define them as variables. For teh test I want to stay inside notebooks not to construct a pdf.

Cyrille gravatar imageCyrille ( 2023-05-20 16:32:14 +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: 2023-05-18 11:18:37 +0200

Seen: 215 times

Last updated: May 19 '23