Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate Log Uniform Distribution in Python?

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.

like image 514
jkrish Avatar asked May 15 '17 10:05

jkrish


People also ask

How do you find the uniform distribution in Python?

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.

How do you generate a random uniform distribution in Python?

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.

How do you get uniformly distributed random numbers in Python?

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.

How do you generate a random number from a uniform distribution?

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


3 Answers

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)
like image 191
Scott Gigante Avatar answered Oct 19 '22 04:10

Scott Gigante


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.

like image 28
Scott Avatar answered Oct 19 '22 04:10

Scott


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 and b as shape parameters.

like image 8
joaoFaria Avatar answered Oct 19 '22 04:10

joaoFaria