有人可以向我解释如何在 Scipy 中将 location 参数与 gamma.fit 函数一起使用吗?
在我看来,位置参数 (μ) 将分布的支持从 x ≥ 0 更改为 y = ( x - μ ) ≥ 0。如果 μ 为正,那么我们不会丢失所有不满足的数据x - μ ≥ 0?
谢谢!
有人可以向我解释如何在 Scipy 中将 location 参数与 gamma.fit 函数一起使用吗?
在我看来,位置参数 (μ) 将分布的支持从 x ≥ 0 更改为 y = ( x - μ ) ≥ 0。如果 μ 为正,那么我们不会丢失所有不满足的数据x - μ ≥ 0?
谢谢!
该fit
函数在寻找拟合时会考虑所有数据。向数据中添加噪声会改变拟合参数,并可能给出不能很好地表示数据的分布。所以我们在使用fit
.
下面是一些使用 numpy生成数据的代码y1
。它还会在 0 到 10 范围内的数据中添加噪声以创建. 拟合产生出色的结果,但试图拟合嘈杂是有问题的。我们添加的噪音抹去了分布。但是,我们也可以在拟合数据时保持 1 个或多个参数不变。在这种情况下,我们传递给,这会在执行拟合时强制保持位置,从而返回更好的结果。loc=2
scale=1
y2
y1
y2
floc=2
fit
2
from scipy.stats import gamma
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,10,.1)
y1 = np.random.gamma(shape=1, scale=1, size=1000) + 2 # sets loc = 2
y2 = np.hstack((y1, 10*np.random.rand(100))) # add noise from 0 to 10
# fit the distributions, get the PDF distribution using the parameters
shape1, loc1, scale1 = gamma.fit(y1)
g1 = gamma.pdf(x=x, a=shape1, loc=loc1, scale=scale1)
shape2, loc2, scale2 = gamma.fit(y2)
g2 = gamma.pdf(x=x, a=shape2, loc=loc2, scale=scale2)
# again fit the distribution, but force loc=2
shape3, loc3, scale3 = gamma.fit(y2, floc=2)
g3 = gamma.pdf(x=x, a=shape3, loc=loc3, scale=scale3)
并制作一些情节......
# plot the distributions and fits. to lazy to do iteration today
fig, axes = plt.subplots(1, 3, figsize=(13,4))
ax = axes[0]
ax.hist(y1, bins=40, normed=True);
ax.plot(x, g1, 'r-', linewidth=6, alpha=.6)
ax.annotate(s='shape = %.3f\nloc = %.3f\nscale = %.3f' %(shape1, loc1, scale1), xy=(6,.2))
ax.set_title('gamma fit')
ax = axes[1]
ax.hist(y2, bins=40, normed=True);
ax.plot(x, g2, 'r-', linewidth=6, alpha=.6)
ax.annotate(s='shape = %.3f\nloc = %.3f\nscale = %.3f' %(shape2, loc2, scale2), xy=(6,.2))
ax.set_title('gamma fit with noise')
ax = axes[2]
ax.hist(y2, bins=40, normed=True);
ax.plot(x, g3, 'r-', linewidth=6, alpha=.6)
ax.annotate(s='shape = %.3f\nloc = %.3f\nscale = %.3f' %(shape3, loc3, scale3), xy=(6,.2))
ax.set_title('gamma fit w/ noise, location forced')