简体   繁体   中英

Create random numbers with left skewed probability distribution

I would like to pick a number randomly between 1-100 such that the probability of getting numbers 60-100 is higher than 1-59.

I would like to have the probability to be a left-skewed distribution for numbers 1-100. That is to say, it has a long tail and a peak.

Something along the lines:

pers = np.arange(1,101,1)
prob = <left-skewed distribution>
number = np.random.choice(pers, 1, p=prob)

I do not know how to generate a left-skewed discrete probability function. Any ideas? Thanks!

The p argument of np.random.choice is the probability associated with each element in the array in the first argument. So something like:

    np.random.choice(pers, 1, p=[0.01, 0.01, 0.01, 0.01, ..... , 0.02, 0.02])

Where 0.01 is the lower probability for 1-59 and 0.02 is the higher probability for 60-100.

The SciPy documentation has some useful examples.

http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html

EDIT: You might also try this link and look for a distribution (about half way down the page) that fits the model you are looking for.

http://docs.scipy.org/doc/scipy/reference/stats.html

Like you described, just make sure your skewed-distribution adds up to 1.0:

pers = np.arange(1,101,1)

# Make each of the last 41 elements 5x more likely
prob = [1.0]*(len(pers)-41) + [5.0]*41

# Normalising to 1.0
prob /= np.sum(prob)

number = np.random.choice(pers, 1, p=prob)

This is the answer you are looking for using the SciPy function 'skewnorm'. It can make any positive set of integers either left or rightward skewed.

from scipy.stats import skewnorm
import matplotlib.pyplot as plt

numValues = 10000
maxValue = 100
skewness = -5   #Negative values are left skewed, positive values are right skewed.

random = skewnorm.rvs(a = skewness,loc=maxValue, size=numValues)  #Skewnorm function

random = random - min(random)      #Shift the set so the minimum value is equal to zero.
random = random / max(random)      #Standadize all the vlues between 0 and 1. 
random = random * maxValue         #Multiply the standardized values by the maximum value.

#Plot histogram to check skewness
plt.hist(random,30,density=True, color = 'red', alpha=0.1)
plt.show()

Please reference the documentation here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.skewnorm.html

Histogram of left-skewed distribution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM