创建具有左偏概率分布的随机数

Roh*_*hit 9 python statistics

我想在 1-100 之间随机选择一个数字,这样获得数字 60-100 的概率高于 1-59。

我希望有可能成为数字 1-100 的左偏分布。也就是说,它有一个很长的尾巴和一个峰值。

沿线的东西:

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

我不知道如何生成左偏离散概率函数。有任何想法吗?谢谢!

nic*_*las 5

就像您所描述的那样,只需确保您的偏态分布总计为 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)
Run Code Online (Sandbox Code Playgroud)


Aar*_*itz 5

这是您使用 SciPy 函数“skewnorm”寻找的答案。它可以使任何正整数集向左或向右倾斜。

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()
Run Code Online (Sandbox Code Playgroud)

请参考此处的文档:https : //docs.scipy.org/doc/scipy/reference/generated/scipy.stats.skewnorm.html

左偏分布的直方图

该代码生成以下图。

在此处输入图片说明