Nick Elias
Nick Elias

Reputation: 61

Generating skewed float random variables distribution in the domain [0,1]

I want to generate random float variables in python with the following characteristics:

Clearly, it's definitely a skewed distribution. I've experimented with scaled skewed normal distributions in scipy without getting what I want.

Let's say I want to create a vector of these random variables by calling a function with one or more parameters that set the shape of the distribution. Is there a distribution that has the above characteristics? If it's already in numpy or scipy that would be a bonus.

NB: It would be nice if I could independently set the width of the skewed distribution, but if not that would be OK (e.g., if the width depended on the mean).

EDIT: I want to generate multiple ensembles of random variables. For each ensemble, I tried randomizing the skew parameter to try and get a different mean. It sort of worked, but I wasn't happy with the results after I tried scaling the distribution such that the random variables would be between [0,1].

Upvotes: 2

Views: 349

Answers (1)

Woody1193
Woody1193

Reputation: 7970

I'm not sure if this is exactly what you're looking for, but you could try using random.triangular. According to the documentation, the default arguments result in a symmetric distribution with values between 0 and 1. However, if we change mode, we can generate a skewed distribution:

def generate_random(m, length):
    return [random.triangular(mode = m) for _ in range(length)]

Here are some test results (your mileage may vary):

sum(generate_random(0.6, 10000)) / 10000  # 0.52 - 0.53
sum(generate_random(0.7, 10000)) / 10000  # 0.56 - 0.57
sum(generate_random(0.8, 10000)) / 10000  # 0.59 - 0.60
sum(generate_random(0.9, 10000)) / 10000  # 0.63 - 0.64
sum(generate_random(1.0, 10000)) / 10000  # 0.66 - 0.67

Upvotes: 1

Related Questions