I could not find a built-in function in Python to generate a log uniform distribution given a min and max value (the R equivalent is here), something like: loguni[n, exp(min), exp(max), base] that returns n log uniformly distributed in the range exp(min) and exp(max).
The closest I found though was numpy.random.uniform
.
uniform() is a Uniform continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. size : [tuple of ints, optional] shape or random variates.
To generate random numbers from the Uniform distribution we will use random. uniform() method of random module. In uniform distribution samples are uniformly distributed over the half-open interval [low, high) it includes low but excludes high interval.
uniform() method in Python Random module uniform() is a method specified in the random library in Python 3. Parameters : x Specifies the lower limit of the random number required to generate. y Specifies the upper limit of the random number required to generate.
Use rand to generate 1000 random numbers from the uniform distribution on the interval (0,1). rng('default') % For reproducibility u = rand(1000,1); The inversion method relies on the principle that continuous cumulative distribution functions (cdfs) range uniformly over the open interval (0,1).
From http://ecolego.facilia.se/ecolego/show/Log-Uniform%20Distribution:
In a loguniform distribution, the logtransformed random variable is assumed to be uniformly distributed.
Thus
logU(a, b) ~ exp(U(log(a), log(b))
Thus, we could create a log-uniform distribution using numpy
:
def loguniform(low=0, high=1, size=None):
return np.exp(np.random.uniform(low, high, size))
If you want to choose a different base, we could define a new function as follows:
def lognuniform(low=0, high=1, size=None, base=np.e):
return np.power(base, np.random.uniform(low, high, size))
EDIT: @joaoFaria's answer is also correct.
def loguniform(low=0, high=1, size=None):
return scipy.stats.reciprocal(np.exp(low), np.exp(high)).rvs(size)
SciPy v1.4 includes a loguniform
random variable: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.loguniform.html
Here's how to use it:
from scipy.stats import loguniform
rvs = loguniform.rvs(1e-2, 1e0, size=1000)
This will create random variables evenly spaced between 0.01 and 1. That best shown by visualizing the log-scaled histogram:
This "log-scaling" works regardless of base; loguniform.rvs(2**-2, 2**0, size=1000)
also produces log-uniform random variables. More details are in loguniform
's documentation.
I believe the scipy.stats.reciprocal
is the distribution you want.
From the documentation:
The probability density function for reciprocal is:
f(x, a, b) = \frac{1}{x \log(b/a)}
for a <= x <= b and a, b > 0
reciprocal takes
a
andb
as shape parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With