截断SciPy随机分布

2024-04-20 03:23:15 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有人有什么建议可以有效地截断SciPy随机分布。例如,如果我生成这样的随机值:

import scipy.stats as stats
print stats.logistic.rvs(loc=0, scale=1, size=1000)

在不改变分布的原始参数和不改变样本大小的情况下,如何将输出值限制在0和1之间,同时尽量减少机器的工作量?在


Tags: import参数sizeasstats情况scipyloc
3条回答

你想达到什么目的?Logistic distribution按定义具有无限范围。如果以任何方式截断结果,它们的分布将发生变化。如果你只想在范围内随机数,有^{}。在

你的问题更像是一个统计问题,而不是一个蹩脚的问题。一般来说,您需要能够在您感兴趣的时间间隔内进行标准化,并分析性地计算此间隔的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,normed=True)

plt.show()

您可以将结果标准化为最大返回值:

>>> dist = stats.logistic.rvs(loc=0, scale=1, size=1000)
>>> norm_dist = dist / np.max(dist)

这将保持“shape”不变,并且值介于01之间。但是,如果你从一个分布重复绘制,一定要将所有绘制规范化为相同的值(所有绘制的最大值)。在

但是,如果你做这种事情在你想要达到的目标范围内是有意义的(我没有足够的信息来评论…),你需要非常小心

相关问题 更多 >