有人对有效截断 SciPy 随机分布有什么建议吗?例如,如果我像这样生成随机值:
import scipy.stats as stats
print stats.logistic.rvs(loc=0, scale=1, size=1000)
我如何在不改变分布的原始参数和样本量的情况下将输出值限制在 0 和 1 之间,同时最大限度地减少机器必须完成的工作量?
最佳答案
你的问题更像是一个统计问题而不是一个 scipy 问题。通常,您需要能够在您感兴趣的区间内进行归一化,并分析计算该区间的 CDF,以创建高效的采样方法。 编辑:事实证明这是可能的(不需要拒绝抽样):
import scipy.stats as stats
import matplotlib.pyplot as plt
import numpy as np
import numpy.random as rnd
#plot the original distribution
xrng=np.arange(-10,10,.1)
yrng=stats.logistic.pdf(xrng)
plt.plot(xrng,yrng)
#plot the truncated distribution
nrm=stats.logistic.cdf(1)-stats.logistic.cdf(0)
xrng=np.arange(0,1,.01)
yrng=stats.logistic.pdf(xrng)/nrm
plt.plot(xrng,yrng)
#sample using the inverse cdf
yr=rnd.rand(100000)*(nrm)+stats.logistic.cdf(0)
xr=stats.logistic.ppf(yr)
plt.hist(xr,density=True)
plt.show()
关于python - 截断 SciPy 随机分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11491032/