[SciPy-Dev] [stats] discrete vs continuous distributions, arguments

josef.pktd@gmai... josef.pktd@gmai...
Fri Jun 14 19:27:16 CDT 2013

On Fri, Jun 14, 2013 at 8:11 PM, Evgeni Burovski
<evgeny.burovskiy@gmail.com> wrote:
> Dear experts,
>
> Could somebody comment on the way how continuous and discrete distributions
> handle their positional arguments --- especially where there are too many of
> them. For example:
>
>>>> from scipy.stats import poisson, norm
>>>> poisson.pmf(42, 41) # k=42, \mu=41
> 0.060697388909241624
>>>>
>>>> poisson.pmf(42, 41, -101)  # gets shifted by -101?
> 1.7221234070193835e-35
>>>> poisson.pmf(42+101, 41)  # indeed
> 1.7221234070193835e-35
>>>>
>>>> norm.pdf(39, 41, 2)  # N(41, 2) at x=39 ?
> 0.12098536225957168
>>>> np.exp(-1./2)/np.sqrt(2.*np.pi*2**2)  # indeed, it is
> 0.12098536225957168
>>>>
>>>> norm.pdf(39, 41, 2, -101)  # is it N(41+101, 2) at x=39? or at x=39+101?
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File
> "/home/br/.local/lib/python2.7/site-packages/scipy/stats/distributions.py",
> line 1212, in pdf
>     args, loc, scale = self._fix_loc_scale(args, loc, scale)
>   File
> "/home/br/.local/lib/python2.7/site-packages/scipy/stats/distributions.py",
> line 545, in _fix_loc_scale
>     raise TypeError("Too many input arguments.")
> TypeError: Too many input arguments.

I assume you have a recent scipy. this was recently fixed.
with scipy 0.9 it's possible to get weird results with too many parameters

>>> stats.norm.pdf(5, 4,3,2,1)
1.4867195147342979e-06

does this raise an error ?
>>> stats.poisson.pmf(5, 4, 3, 2)
0.1562934518505317

the explanation

each continuous distribution requires shape parameter and optional loc and scale
loc and scale are keyword parameters, but can be given as positional parameters

numargs shows the number of shape parameters

>>> stats.norm.numargs
0
>>> stats.poisson.numargs
1

so we need at least numargs distribution parameters and at most numargs + 2

For the discrete distribution there is no scale, so only loc is an
extra keyword, which means discrete distributions take either numargs
arguments with loc=0 as default, or numargs + 1 parameters when loc is
also given.

the normal distribution has numargs=0, so it can either take 0, 1 or 2
arguments, interpreted as loc and scale with defaults 0, 1.
3 arguments is too many and raises an exception.

Poisson should raise an exception if there are 3 or more parameters
given, and needs at least one for the shape parameter (loc=0 is
optional).

What I don't remember is whether the discrete distribution have been
fixed so they raise an exception if there are too many arguments.

Josef

>>>>
>
> I understand what happens in the code (the loc parameter is free for
> poisson and is fixed at mu for norm), but I'm at loss whether this
> disparity is by design --- is it a feature or a bug or just my
> misunderstanding?
>
> Zhenya
>
> _______________________________________________
> SciPy-Dev mailing list
> SciPy-Dev@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-dev
>