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()
你想达到什么目的?Logistic distribution按定义具有无限范围。如果以任何方式截断结果,它们的分布将发生变化。如果你只想在范围内随机数,有^{} 。在
你的问题更像是一个统计问题,而不是一个蹩脚的问题。一般来说,您需要能够在您感兴趣的时间间隔内进行标准化,并分析性地计算此间隔的CDF,以创建一种有效的采样方法。编辑:结果证明这是可能的(不需要拒绝取样):
您可以将结果标准化为最大返回值:
这将保持“shape”不变,并且值介于
0
和1
之间。但是,如果你从一个分布重复绘制,一定要将所有绘制规范化为相同的值(所有绘制的最大值)。在但是,如果你做这种事情在你想要达到的目标范围内是有意义的(我没有足够的信息来评论…),你需要非常小心
相关问题 更多 >
编程相关推荐