python 中的逆正态随机数生成?

Inverse normal random number generation in python?

我过去曾使用 random.normal() 来生成一个数字,如果多次调用该数字,则总体上会创建一个钟形曲线分布。我现在想做的是创建相反/相反的分布,其中分布偏向某个范围内的极端值? excel 中的内置函数似乎可以满足我的需求。 python 有办法做到吗?谢谢

您必须决定使用哪种概率分布。

如果你想使用纯 Python,没有外部依赖,检查随机模块中有哪些分布可用:https://docs.python.org/3/library/random.html

例如,您可以使用参数为 (0.5, 0.5) 的 Beta 分布:https://docs.python.org/3/library/random.html#random.betavariate

查看维基百科页面以了解 beta 分发以了解参数:https://en.wikipedia.org/wiki/Beta_distribution

对于高级使用,外部包 scipy 是访问 Python 内概率分布的常用方法:https://docs.scipy.org/doc/scipy/reference/stats.html

听起来你想要的是将分布移动到范围的边缘,然后环绕?

像这样的东西可以满足您的需求:

num = (random.normal() + 0.5) % 1

您似乎想要一个具有“倒挂钟形曲线”的分布 与正态分布相比。如果是这样,那么下面的方法 通过拒绝抽样和修改的 标准正态分布的版本。 'x0' 和 'x1' 是范围 要生成的数字。

def invertedNormal(x0, x1):
  # Get the ends of the PDF (the bounding
  # box will cover the PDF at the given range)
  x0pdf = 1-math.exp(-(x0*x0))
  x1pdf = 1-math.exp(-(x1*x1))
  ymax = max(x0pdf, x1pdf)
  while True:
    # Choose a random x-coordinate
    x=random.random()*(x1-x0)+x0
    # Choose a random y-coordinate
    y=random.random()*ymax
    # Return x if y falls within PDF
    if y < 1-math.exp(-(x*x)):
      return x