好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

在Python中快速任意分布随机抽样

随机模块( http://docs.python.org/2/library/random.html)具有若干固定功能以随机采样.例如,random.gauss将使用给定的均值和西格玛值对正态分布中的随机点进行采样.

我正在寻找一种在python中尽可能快地使用我自己的分布在给定间隔之间提取N个随机样本的方法.这就是我的意思:

def my_dist(x):
    # Some distribution, assume c1,c2,c3 and c4 are known.
    f = c1*exp(-((x-c2)**c3)/c4)
    return f

# Draw N random samples from my distribution between given limits a,b.
N = 1000
N_rand_samples = ran_func_sample(my_dist, a, b, N)

其中ran_func_sample是我所追求的,a,b是绘制样本的限制.在python中有什么类似的东西吗?

您需要使用逆变换采样方法来获取根据您想要的法则分布的随机值.使用此方法您可以只应用反转函数 在区间[0,1]中具有标准均匀分布的随机数.

在找到反转函数后,您可以根据所需的分布以这种明显的方式分配1000个数字:

[inverted_function(random.random()) for x in range(1000)]

有关逆变换采样的更多信息:

> http://en.wikipedia.org/wiki/Inverse_transform_sampling

此外,StackOverflow与该主题相关的问题很好:

> Pythonic way to select list elements with different probability

查看更多关于在Python中快速任意分布随机抽样的详细内容...

  阅读:14次