[SciPy-user] **kwds in frozen distribution class

joep josef.pktd@gmail....
Fri Sep 12 14:42:39 CDT 2008


problems with **kwds in moment method in distributions
======================================================

Summary:
--------

* ``moment`` in frozen continuous distribution with loc and scale
keyword arguments raises exception
* ``moment`` in frozen discrete distribution accepts the presence of
loc and scale, but ignores them
* ``stats`` method works correctly, but only for first and second
moment

>>> scipy.version.version
'0.6.0'
>>> numpy.version.version
'1.1.0'

example continuous distribution
-------------------------------


>>> stats.gamma(4).stats()
(array(4.0), array(4.0))
>>> stats.gamma(4).moment(1)
4
>>> stats.gamma(4).moment(2)
4

moment in frozen distribution with loc and scale keyword arguments
raises exception

>>> stats.gamma(4,loc = 10, scale = 10).stats()
(array(50.0), array(400.0))
>>> stats.gamma(4,loc = 10, scale = 10).moment(1)
Traceback (most recent call last):
  File "<pyshell#166>", line 1, in ?
    stats.gamma(4,loc = 10, scale = 10).moment(1)
  File "C:\Programs\Python24\lib\site-packages\scipy\stats
\distributions.py", line 124, in moment
    return self.dist.moment(n,*self.args,**self.kwds)
TypeError: moment() got an unexpected keyword argument 'loc'


actually moment does not allow for loc and scale in the not-frozen
distribution
either:

>>> stats.gamma.moment(2,4)
4
>>> stats.gamma.stats(4,loc = 10, scale = 10)
(array(50.0), array(400.0))

>>> stats.gamma.moment(2,4,loc = 10, scale = 10)
Traceback (most recent call last):
  File "<pyshell#175>", line 1, in ?
    stats.gamma.moment(2,4,loc = 10, scale = 10)
TypeError: moment() got an unexpected keyword argument 'loc'


check:stats agrees with random sample:

>>> rvs=stats.gamma.rvs(4,loc = 10, scale = 10,size=10000)
>>> rvs.mean()
50.01717741991262
>>> rvs.var()
400.12936828905185


example: discrete distribution
------------------------------

stats works correctly with or without loc,scale parameters:

>>> stats.poisson.stats(4,loc = 10, scale = 10)
(array(14.0), array(4.0))
>>> stats.poisson(4,loc = 10, scale = 10).stats()
(array(14.0), array(4.0))
>>> rvs=stats.poisson.rvs(4,loc = 10, scale = 10, size=10000)
>>> rvs.mean()
13.9985
>>> rvs.var()
3.9602977499998575

>>> stats.poisson.stats(4)
(array(4.0), array(4.0))
>>> stats.poisson(4).stats()
(array(4.0), array(4.0))

moment in frozen distribution accepts the presence of loc and scale,
but ignores them

the following are not for the log,scale transformed random variable
>>> stats.poisson(4,loc = 10, scale = 10).moment(1)  #wrong result
4
>>> stats.poisson(4,loc = 10, scale = 10).moment(2)  #wrong result
4
>>> stats.poisson(4,loc = 10, scale = 10).moment(3)  #wrong result
4.0
>>> stats.poisson(4,loc = 10, scale = 10).moment(4)  #wrong result
52.0
>>> stats.poisson.moment(1,4,loc = 10, scale = 10)  #wrong result
4

instead the results are for the untransformed variable

>>> stats.poisson(4).moment(1)
4
>>> stats.poisson(4).moment(2)
4
>>> stats.poisson(4).moment(3)
4.0
>>> stats.poisson(4).moment(4)
52.0

just to check result
>>> rvs0=stats.poisson.rvs(4,size=10000000)
>>> ((rvs0-4)**4).mean()
51.991537899999997

adding loc, scale to moment of already frozen distribution raises
exception,
which at least is not misleading

>>> stats.poisson(4).moment(1,loc = 10, scale = 10)
Traceback (most recent call last):
  File "<pyshell#227>", line 1, in ?
    stats.poisson(4).moment(1,loc = 10, scale = 10)
TypeError: moment() got an unexpected keyword argument 'loc'


Josef


More information about the SciPy-user mailing list