Moments of a normal distribution

39 views
Skip to first unread message

Justin Vincent

unread,
Jul 15, 2014, 4:40:51 PM7/15/14
to sy...@googlegroups.com
Wanted to get the moments of a Normal distribution WRT mu and sigma, a la:

http://en.wikipedia.org/wiki/Normal_distribution#Moments

For starters, this outputs 1 for one for one crazy domains, and then leaves the integral unevaluated on the rest of the domain. But it gets one, so I'm cool with that.

from sympy.statistics import Normal
from sympy import symbols, integrate
f = Normal(mu, sigma).pdf(x)
x, mu, sigma = symbols('x mu sigma')
integrate(f, (x,-oo, oo))

Then when I get the first moment, it really starts to go crazy though, and leaves it all indefinite:
integrate(f * x, (x, -oo, oo))

What approach should I take here if I wanted to get those values cited in wikipedia? Haven't used Sympy much before. Thanks in advance.

Justin

Matthew Rocklin

unread,
Jul 15, 2014, 5:28:11 PM7/15/14
to sy...@googlegroups.com
Use sympy.stats, not sympy.statistics

In [1]: x, mu = symbols('x,mu', real=True)

In [2]: sigma = symbols('sigma', positive=True)

In [3]: from sympy.stats import *

In [4]: X = Normal('X', mu, sigma)

In [5]: moment?
Type:        function
String form: <function moment at 0x7fc3fbbe2b18>
File:        /home/mrocklin/workspace/sympy/sympy/stats/rv_interface.py
Definition:  moment(X, n, c=0, condition=None, **kwargs)
Docstring:
Return the nth moment of a random expression about c i.e. E((X-c)**n)
Default value of c is 0.

Examples
========

>>> from sympy.stats import Die, moment, E
>>> X = Die('X', 6)
>>> moment(X, 1, 6)
-5/2
>>> moment(X, 2)
91/6
>>> moment(X, 1) == E(X)
True

In [6]: moment(X, 1)
Out[6]: μ

In [7]: moment(X, 2)
Out[7]: 
 2    2
μ  + σ 

In [8]: moment(X, 3)
Out[8]: 
 3        2
μ  + 3⋅μ⋅σ 

In [9]: moment(X, 4)
Out[9]: 
 4      2  2      4
μ  + 6⋅μ ⋅σ  + 3⋅σ 



--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/2babc41b-0ab4-443c-a75b-d5910ce16c21%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

F. B.

unread,
Jul 16, 2014, 5:45:54 AM7/16/14
to sy...@googlegroups.com


On Tuesday, July 15, 2014 11:28:11 PM UTC+2, Matthew wrote:
Use sympy.stats, not sympy.statistics


sympy.statistics has been removed from the master branch. It will no longer exist as soon as the next SymPy version is released.

Justin Vincent

unread,
Jul 16, 2014, 4:13:42 PM7/16/14
to sy...@googlegroups.com
Thanks! This works great.

Justin Vincent

unread,
Jul 16, 2014, 8:04:59 PM7/16/14
to sy...@googlegroups.com

Matthew Rocklin

unread,
Jul 16, 2014, 9:01:45 PM7/16/14
to sy...@googlegroups.com
Cool.  It's nice to see SymPy used in this way.


Reply all
Reply to author
Forward
0 new messages