Confusion over `a` and `b` attributes from scipy.stats.uniform

空扰寡人 提交于 2019-12-14 02:03:35

问题


Consider the following code:

import scipy
print(scipy.__version__)  # gives 0.19.1

# Scipy.stats.uniform
unif = scipy.stats.uniform(1, 2)
print(unif.a, unif.b, unif.args) # gives a=0, b=1, args=(1,2)

It seems, regardless of the value I provide for loc and scale, the uniform-function returns a=0,b=1.

Compare that to e.g. randint:

# Scipy.stats.randint
randi = scipy.stats.randint(1, 10)
print(randi.a, randi.b, randi.args) # gives a=1, b=9, args=(1,10)

...which returns what I would expect.

So my question becomes: is this a bug in scipy, or have I misunderstood something? The unif.args value is set correctly though.

Cheers!


回答1:


It is my understanding that the a and b are internal parameters, are not used in scipy.stats.uniform, because their normal functionality is basically duplicative of the loc and scale parameters.

As mentioned in the scipy.stats.uniform documentation "This distribution is constant between loc and loc + scale."

So I don't think this is a bug, because the values of a and b should be treated as an implementation detail rather than a user-facing feature.




回答2:


The relevant source on this is here, abridged a bit:

class uniform_gen(rv_continuous):
    """A uniform continuous random variable.
    This distribution is constant between `loc` and ``loc + scale``.
    # ...
    """
    def _rvs(self):
        return self._random_state.uniform(0.0, 1.0, self._size)

   # ....
uniform = uniform_gen(a=0.0, b=1.0, name='uniform')

So a and b will always be 0 and 1, respectively.

I'm guessing your confusion (mine too occascionally with this notation) is that most textbooks will define uniform distributions as lying between a and b. But in this case a and b are something a bit different and, as @jakevdp said,

This distribution is constant between loc and loc + scale.

So relating this back to the traditional definition, think of a as loc and b as loc + scale.

(The parent class rv_continuous in turn is defined here if you're interested.)



来源:https://stackoverflow.com/questions/46846752/confusion-over-a-and-b-attributes-from-scipy-stats-uniform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!