使用相同的概率数组采样 scipy 分布

问题描述

我想使用一组用户生成的概率从不同的 Beta 分布生成变量。我知道 scipy.stats.beta.rvs(a,b) 使用内部生成的概率生成随机变量,但我想改用我自己的概率。我怀疑这一定是微不足道的,但我自己无法解决。提前致谢。

解决方法

经过一番摸索,我认为解决方案是使用 scipy.special.betainc(a,b,x)。这似乎与以下脚本显示的 CDF 相同:

import scipy.stats
import scipy.special
import matplotlib.pyplot as plt
import numpy as np

a = 3.0
b = 5.0

ntrial = 100000

p = np.linspace(0.0,1.0,1000)
r = np.random.rand(ntrial)
v = scipy.special.betainc(a,r)

# Plot the CDFs

plt.plot(np.arange(ntrial) / float(ntrial),np.sort(v),'.',label='scipy.special')
plt.plot(p,scipy.stats.beta.cdf(p,a,b),label='scipy.stats')
plt.ylabel("cumul. prob.")
plt.legend()

enter image description here