[英]sampe vector of points from truncated exponential distribution with specific average
我创建了一个截断的指数分布:
from scipy.stats import truncexpon
truncexp = truncexpon(b = 8)
现在,我想从此分布中采样8个点,以使它们的均值大约为4。什么是最好的方法,而无需进行大循环来随机采样直到均值足够接近?
平均值是您分布的特征。 如果继续采样值,则经验平均值将越来越接近分析平均值。
Scipy可以告诉您截断指数的平均值:
b = 8
truncexp = truncexpon(b)
truncexp.mean() # 0.99731539839326999
您可以使用分布来采样和计算经验均值:
num_samples = 100000
np.mean(truncexp.rvs(num_samples)) # 0.99465816346645264
计算平均值的解析公式是(第二行):
b = np.linspace(0.1, 20, 100)
m = 1/ ((1 - np.exp(-b)) / ((1 - (b + 1)*np.exp(-b))))
如果对此进行绘制,则可以看到平均值在不同b值下的表现。
对于b-> inf,均值将趋向于1。找不到平均值为4的ab。
如果要从平均值为4的截断指数进行采样,则可以简单地缩放样本。 这不会给您原始分布的样本,但是再次,原始分布的样本将永远不会给您平均值4。
truncexp.rvs(num_samples) * 4 / truncexp.mean()
truncexpon
分布具有三个参数:形状b
,位置loc
和scale
。 分布的支持为[x1, x2]
,其中x1 = loc
和x2 = shape*scale + loc
。 求解shape
的后一个方程,得到shape = (x2 - x1)/scale
。 我们将选择scale
参数,以使分布的均值为4。为此,我们可以将scipy.optimize.fsolve
应用于在truncexpon.mean((x2 - x1)/scale, loc, scale)
为4。
这是一个简短的脚本来演示:
import numpy as np
from scipy.optimize import fsolve
from scipy.stats import truncexpon
def func(scale, desired_mean, x1, x2):
return truncexpon.mean((x2 - x1)/scale, loc=x1, scale=scale) - desired_mean
x1 = 1
x2 = 9
desired_mean = 4.0
# Numerically solve for the scale parameter of the truncexpon distribution
# with support [x1, x2] for which the expected mean is desired_mean.
scale_guess = 2.0
scale = fsolve(func, scale_guess, args=(desired_mean, x1, x2))[0]
# This is the shape parameter of the desired truncexpon distribution.
shape = (x2 - x1)/scale
print("Expected mean of the distribution is %6.3f" %
(truncexpon.mean(shape, loc=x1, scale=scale),))
print("Expected standard deviation of the distribution is %6.3f" %
(truncexpon.std(shape, loc=x1, scale=scale),))
# Generate a sample of size 8, and compute its mean.
sample = truncexpon.rvs(shape, loc=x1, scale=scale, size=8)
print("Mean of the sample of size %d is %6.3f" %
(len(sample), sample.mean(),))
bigsample = truncexpon.rvs(shape, loc=x1, scale=scale, size=100000)
print("Mean of the sample of size %d is %6.3f" %
(len(bigsample), bigsample.mean(),))
典型输出:
Expected mean of the distribution is 4.000
Expected standard deviation of the distribution is 2.178
Mean of the sample of size 8 is 4.694
Mean of the sample of size 100000 is 4.002
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.