Skip to main content

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_continuous_frozen):
    def __init__(self, dist, *args, **kwds):
        super(frozen,self).__init__(dist, *args, **kwds)

def buniform(a, b):  # b for better
    dist = stats.uniform
    dist.name = '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.suport()   # (0.0, 1.0)

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

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

That's it!

Cool, I want it!

pip install better-uniform

or

git clone https://github.com/j-faria/better_uniform.git
cd better_uniform
python setup.py install

and later, from Python

from better_uniform import buniform

or better yet

from better_uniform import buniform as uniform

Metadata

Release files for better-uniform 1.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for better-uniform 1.0.7
File Size Uploaded
better_uniform-1.0.7.tar.gz 4.5 kB Details

Release files / better_uniform-1.0.7.tar.gz

Download URL better_uniform-1.0.7.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
94d25312f7b62ad0749f9938076b9fefc4b262f01ae1e9ef6512ea0e87a14d82
BLAKE2b-256 checksum
How to use checksums
7ad4be89d02680a66c0977dc2cb3f4cea9535325cdcfd032e3ab1dcf513fbb71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.12

Release history Release notifications | RSS feed

This release

1.0.7 This release

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page