Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

A better scipy.stats.uniform

Project description

A better scipy.stats.uniform

The stats sub-package of scipy is quite cool.
In particular, it provides dozens of probability distributions implemented with a common interface.

But scipy.stats.uniform always bugged me.

>>> from scipy import stats
>>> help(stats.uniform)

A uniform continuous random variable.
This distribution is constant between `loc` and ``loc + scale``.

Why loc + scale? Why not scale?

So I wrote better_uniform: eight small lines of code that don't bug me so much.

from scipy import stats 

class frozen(stats._distn_infrastructure.rv_frozen):
    def __init__(self, dist, *args, **kwds):
        super(frozen,self).__init__(dist, *args, **kwds)

def buniform(a, b):  # b for better
    dist = stats.uniform = 'uniform'
    return frozen(dist, loc=a, scale=b-a)

Now it works as I expect it to work:

d = buniform(0, 1)
d.rvs()        # 0 < rv < 1
d.interval(1)  # (0.0, 1.0)

d = buniform(1, 2)
d.rvs()        # 1 < rv < 2
d.interval(1)  # (1.0, 2.0)

# note the difference
from scipy.stats import uniform
d = uniform(1, 2)
d.rvs()        # 1 < rv < 3
d.interval(1)  # (1.0, 3.0)

That's it!

Cool, I want it!

pip install better-uniform


git clone
cd better_uniform
python install

and later, from Python

from better_uniform import buniform

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
better_uniform-1.0.3.tar.gz (2.2 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page