python - 截断 SciPy 随机分布

标签 python random statistics scipy

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

相关文章:

python - 如何生成随机值的N维数组?

python - 根据输入从二维数组中选择某些内容

python - 使用 Python 求解四分位数和十分位数

python - 按索引克隆列表中的项目

python - 将对象添加到 SQLAlchemy 关联对象时出现 KeyError

Python 列表到 MySQL 数据库

python - 将对数正态分布的拟合 PDF 缩放为 python 中的直方图

python - 如果安装了特定模块,如何在 python 中提供某些功能?

MySQL随机连接

r - 带 Kernlab 的内核 PCA 和结肠癌数据集的分类