截断SciPy随机分布

Tim*_*imY 5 python random statistics scipy

有没有人有建议有效截断SciPy随机分布.例如,如果我生成如下随机值:

import scipy.stats as stats
print stats.logistic.rvs(loc=0, scale=1, size=1000)
Run Code Online (Sandbox Code Playgroud)

如何在不改变分布的原始参数和不改变样本大小的情况下将输出值约束在0和1之间,同时最大限度地减少机器必须完成的工作量?

use*_*913 6

你的问题更像是一个统计问题,而不是一个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,normed=True)

plt.show()
Run Code Online (Sandbox Code Playgroud)