问题描述
我正在使用自定义函数 f(x)
通过 copy
的 rv_continuous
类定义自定义分布.我的代码是
class my_pdf_gen(rv_continuous):def _pdf(自我,x,整数):返回f(x)/积分
其中 integral
可确保规范化.我可以使用它创建一个实例
my_pdf = my_pdf_gen(my_int,a = a,b = b,名称='my pdf')
使用 a,b
值范围的上限和下限,以及 my_int = scipy.integrate.quad(f,a,b)[0]
.我还可以使用 my_pdf.rvs(my_int,size = 5)
创建随机数据样本,但这非常慢.(当 size = 9
时最长为6秒).
我读到一个人也应该覆盖该类中的其他一些方法(例如 _ppf
),但是从示例中我发现尚不清楚如何在我的情况下实现它./p>
非常感谢!
我通过更改方法并使用Monte Carlo的拒绝采样器方法解决了该问题
def reject_sampler(p,xbounds,pmax):而True:x = np.random.rand(1)*(xbounds [1] -xbounds [0])+ xbounds [0]y = np.random.rand(1)* pmax如果y
其中 p
是概率密度函数, xbounds
是一个包含pdf上下限的元组,而 pmax
是pdf在域上的最大值.
此处建议使用蒙特卡罗的拒绝抽样器: python:从中随机抽样自定义概率函数
I am using a custom function f(x)
to define a custom distribution using copy
's rv_continuous
class. My code is
class my_pdf_gen(rv_continuous):
def _pdf(self, x, integral):
return f(x)/integral
where integral
ensure the normalisation. I am able to create an instance of it with
my_pdf = my_pdf_gen(my_int,a = a, b = b, name = 'my pdf')
with a,b
the upper and lower limit of the value's range, and my_int= scipy.integrate.quad(f, a, b)[0]
.I am also able to create a random sample of data using my_pdf.rvs(my_int, size = 5)
, but this is very slow. (Up to 6 seconds when size=9
).
I read that one should also overwrite some other methods in the class (like _ppf
), but from the examples I found it isn't clear to me how to achieve it in my case.
Thanks a lot!
I solved the problem by changing approach and using Monte Carlo's rejection sampler method
def rejection_sampler(p,xbounds,pmax):
while True:
x = np.random.rand(1)*(xbounds[1]-xbounds[0])+xbounds[0]
y = np.random.rand(1)*pmax
if y<=p(x):
return x
where p
is the probability density function, xbounds
is a tuple containing the upper and lower limits of of the pdf and pmax
is the maximum value of the pdf on the domain.
Monte Carlo's rejection sampler was suggested here: python: random sampling from self-defined probability function
这篇关于scipy rv_continuous非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!