小编Bal*_*lou的帖子

python 中 beta 二项式分布的高效采样

对于随机模拟,我需要绘制大量贝塔二项式分布的随机数。

目前我是这样实现的(使用python):

import scipy as scp
from scipy.stats import rv_discrete

class beta_binomial(rv_discrete):
       """
       creating betabinomial distribution by defining its pmf
       """
       def _pmf(self, k, a, b, n):
          return scp.special.binom(n,k)*scp.special.beta(k+a,n-k+b)/scp.special.beta(a,b)
Run Code Online (Sandbox Code Playgroud)

因此可以通过以下方式对随机数 x 进行采样:

betabinomial = beta_binomial(name="betabinomial")
x = betabinomial.rvs(0.5,0.5,3) # with some parameter 
Run Code Online (Sandbox Code Playgroud)

问题是,对一个随机数进行采样大约需要 10 分钟。0.5ms,在我的例子中,它主导了整个模拟速度。限制因素是 beta 函数(或其中的 gamma 函数)的评估。

有谁知道如何加快采样速度?

python random distribution stochastic beta-distribution

5
推荐指数
1
解决办法
1451
查看次数