快速任意分布随机采样(逆变换采样) [英] Fast arbitrary distribution random sampling (inverse transform sampling)
问题描述
random
模块(http://docs.python.org/2/library/random.html) 有几个固定函数可以随机抽样.例如,random.gauss
将从具有给定均值和西格玛值的正态分布中采样随机点.
The random
module (http://docs.python.org/2/library/random.html) has several fixed functions to randomly sample from. For example random.gauss
will sample random point from a normal distribution with a given mean and sigma values.
我正在寻找一种方法,在 python<中使用我自己的分布尽可能快地 在给定间隔之间提取多个
N
随机样本代码>.这就是我的意思:
I'm looking for a way to extract a number N
of random samples between a given interval using my own distribution as fast as possible in python
. This is what I mean:
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
中有没有类似的东西?
where ran_func_sample
is what I'm after and a, b
are the limits from which to draw the samples. Is there anything of that sort in python
?
推荐答案
您需要使用逆变换采样方法来获得根据您想要的规律分布的随机值.使用这种方法,您只需应用反函数到区间[0,1]内具有标准均匀分布的随机数.
You need to use Inverse transform sampling method to get random values distributed according to a law you want. Using this method you can just apply inverted function to random numbers having standard uniform distribution in the interval [0,1].
找到倒置函数后,您可以通过这种明显的方式根据所需的分布得到 1000 个数字分布:
After you find the inverted function, you get 1000 numbers distributed according to the needed distribution this obvious way:
[inverted_function(random.random()) for x in range(1000)]
更多关于逆变换采样:
此外,StackOverflow 上有一个与该主题相关的好问题:
Also, there is a good question on StackOverflow related to the topic:
这篇关于快速任意分布随机采样(逆变换采样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!