# [SciPy-User] problem with computing moments of normal distribution

josef.pktd@gmai... josef.pktd@gmai...
Wed Oct 14 07:43:16 CDT 2009

```On Wed, Oct 14, 2009 at 6:12 AM, Mark Bakker <markbak@gmail.com> wrote:
> Hello List, I seem to have trouble computing the moments of a normal
> distribution when the 'loc' keyword (I know, that's the mean in this case)
> is specified.
> Any ideas? Here's my output:
>
> In [207]: f = scipy.stats.norm()
> In [208]: f.moment(1) # works
> Out[208]: 0.0
> In [209]: f = scipy.stats.norm(loc=1)
> In [210]: f.moment(1) # doesn't work
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
> ----> 1
>       2
>       3
>       4
>       5
> /Library/Frameworks/Python.framework/Versions/5.0.0/lib/python2.5/site-packages/scipy/stats/distributions.py
> in moment(self, n)
>     131         return self.dist.stats(*self.args,**kwds)
>     132     def moment(self,n):
> --> 133         return self.dist.moment(n,*self.args,**self.kwds)
>     134     def entropy(self):
>     135         return self.dist.entropy(*self.args,**self.kwds)
> TypeError: moment() got an unexpected keyword argument 'loc'
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>

The current moment method does not support loc and scale, only the
standard distribution.

def moment(self, n, *args):
"""
n'th order non-central moment of distribution

Parameters
----------
n: int, n>=1
order of moment

arg1, arg2, arg3,... : array-like
The shape parameter(s) for the distribution (see docstring of the

"""

You can get mean, variance, skew and kurtosis through norm.stats

>>> stats.norm.stats(1, loc=5, scale=2, moments = 'mvsk')
(array(5.0), array(4.0), array(0.0), array(0.0))

It is possible to recover the central and non-central first four
moments from mvsk ( I have some helper function for this somewhere).
If you are just interested in the first two moments, then the
translation is very short.

However, moments higher than 2, including skew and kurtosis, are not
fully bugfixed. I know some distributions have wrong higher moments
(ncf?), but I don't have yet a generic test function to verify the
values for all distributions, and it is slow to find a reference and
compare the wrong formulas. Some other problems with moments (and
stats) are for cases where the variance is infinite.

You could file an enhancement ticket for `moment`. Without looking up
some references, I don't know how loc and scale will affect the
non-central moments higher than the second. If you can work this out,
then it will be sooner that the enhancement of `moment` to handle loc
and scale will get into scipy.

I'm sorry for not having a more positive answer, there are still a few
gaps in stats.distributions.

Josef
```