Create random numbers with left skewed probability distribution

风流意气都作罢 提交于 2019-12-12 14:41:10

问题


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!


回答1:


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)



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/24854965/create-random-numbers-with-left-skewed-probability-distribution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!