python中的逆正态随机数生成? [英] Inverse normal random number generation in python?
问题描述
我过去曾使用 random.normal() 生成单个数字,如果多次调用该数字,总体上会创建钟形曲线分布.我现在想要做的是创建相反的/反向的,其中分布偏向于一个范围内的极端?excel 中的内置函数似乎可以满足我的要求.有没有办法在python中做到这一点?谢谢
I've used random.normal() in the past to generate a single number who, if called multiple times, in aggregate would create a bell curve distribution. What I'm trying to do now is to create the opposite / inverse, where the distribution is biased towards the extremes within a range? There are built in functions in excel that seem to do what I want. Is there a way to do it in python? Thank you
推荐答案
看来您想要一个带有倒钟形曲线"的分布;与正态分布相比.如果是,那么下面的方法通过拒绝采样和修改后的方式实现这种分布标准正态分布的版本.'x0' 和 'x1' 是范围要生成的数字.
It appears you want a distribution with an "upside-down bell curve" compared to the normal distribution. If so, then the following method implements this distribution via rejection sampling and a modified version of the standard normal distribution. 'x0' and 'x1' are the ranges of numbers to generate.
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
这篇关于python中的逆正态随机数生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!