gpt4 book ai didi

python - 截断 SciPy 随机分布

转载 作者:太空狗 更新时间:2023-10-30 01:03:05 26 4
gpt4 key购买 nike

有人对有效截断 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com