Confusion over `a` and `b` attributes from scipy.s

2019-07-10 05:23发布

问题:

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.)