ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 22 Sep 2012 23:09:54 +0200newbie question about symbolic integration in sagehttps://ask.sagemath.org/question/9340/newbie-question-about-symbolic-integration-in-sage/Hi,
I am new to sage and my question can be naive, I apologize in advance. I would like to use sage for some symbolic integration operations to avoid human mistake.
Here is my first test (which is going to important for my application).
Integration of Normal distribution from -infinity to +infinity. Obviously the result should be one and I am wondering why I don't get it:
--
x, sigma, mu = var('x','sigma','mu')
q = 1/((2*pi)^(1/2)*sigma)*exp(-(x - mu)^2/(2*sigma^2))
show(q)
import sympy
show((sympy.integrate(q, (x,-sympy.oo, sympy.oo))).simplify())
--
Here is the result:
---
Integral(2**(1/2)*exp(-mu**2/(2*sigma**2))*exp(-x**2/(2*sigma**2))*exp(mu*x/sigma**2)/(2*pi**(1/2)*sigma),(x,-oo,oo))
--
I am wondering why I don't get 1? Am I missing anything?
Thanks
Sat, 22 Sep 2012 15:45:33 +0200https://ask.sagemath.org/question/9340/newbie-question-about-symbolic-integration-in-sage/Answer by achrzesz for <p>Hi,</p>
<p>I am new to sage and my question can be naive, I apologize in advance. I would like to use sage for some symbolic integration operations to avoid human mistake.
Here is my first test (which is going to important for my application). </p>
<p>Integration of Normal distribution from -infinity to +infinity. Obviously the result should be one and I am wondering why I don't get it:</p>
<hr/>
<pre><code>x, sigma, mu = var('x','sigma','mu')
q = 1/((2*pi)^(1/2)*sigma)*exp(-(x - mu)^2/(2*sigma^2))
show(q)
import sympy
show((sympy.integrate(q, (x,-sympy.oo, sympy.oo))).simplify())
</code></pre>
<hr/>
<h2>Here is the result:</h2>
<pre><code>Integral(2**(1/2)*exp(-mu**2/(2*sigma**2))*exp(-x**2/(2*sigma**2))*exp(mu*x/sigma**2)/(2*pi**(1/2)*sigma),(x,-oo,oo))
</code></pre>
<hr/>
<p>I am wondering why I don't get 1? Am I missing anything?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/9340/newbie-question-about-symbolic-integration-in-sage/?answer=14058#post-id-14058 sage: x, sigma, mu = var('x','sigma','mu')
sage: q = 1/((2*pi)^(1/2)*sigma)*exp(-(x - mu)^2/(2*sigma^2))
sage: assume(sigma>0)
sage: integral(q,(x,-oo,oo))
1
Sat, 22 Sep 2012 16:36:35 +0200https://ask.sagemath.org/question/9340/newbie-question-about-symbolic-integration-in-sage/?answer=14058#post-id-14058Comment by kcrisman for <pre><code>sage: x, sigma, mu = var('x','sigma','mu')
sage: q = 1/((2*pi)^(1/2)*sigma)*exp(-(x - mu)^2/(2*sigma^2))
sage: assume(sigma>0)
sage: integral(q,(x,-oo,oo))
1
</code></pre>
https://ask.sagemath.org/question/9340/newbie-question-about-symbolic-integration-in-sage/?comment=19020#post-id-19020Notice that @achrzesz used Maxima's integration (Sage's default integral mode) and not Sympy. Apparently sympy can't do the integral in question, perhaps because of the lack of an assumption framework, who knows...Sat, 22 Sep 2012 23:09:54 +0200https://ask.sagemath.org/question/9340/newbie-question-about-symbolic-integration-in-sage/?comment=19020#post-id-19020Comment by kayhan for <pre><code>sage: x, sigma, mu = var('x','sigma','mu')
sage: q = 1/((2*pi)^(1/2)*sigma)*exp(-(x - mu)^2/(2*sigma^2))
sage: assume(sigma>0)
sage: integral(q,(x,-oo,oo))
1
</code></pre>
https://ask.sagemath.org/question/9340/newbie-question-about-symbolic-integration-in-sage/?comment=19024#post-id-19024Thank you :) It is true that sign of sigma matters and what you said works and it yields "1". But when I type: integral(-q*log(q),(x,-oo,oo)) to compute entropy. it is still confused about sign of mu ?! even after assuming mu>0 produces stack overflow!Sat, 22 Sep 2012 20:25:18 +0200https://ask.sagemath.org/question/9340/newbie-question-about-symbolic-integration-in-sage/?comment=19024#post-id-19024