为直方图缩放拟合 pdf
Scaling of fitted pdf for a histogram
我正在处理一些时间序列数据,我试图用伽玛分布来拟合数据。但问题是拟合的pdf的幅度远低于直方图的幅度。这是我的代码和情节。剧情有什么问题?
# the data
data = contents[0][1:]
# normalized histogram
weights = np.ones_like(data)/len(data)
plt.hist(data, bins = 20, color = 'w', edgecolor = 'black', alpha = 0.5, weights = weights)
# fit with gamma distribution and plot the pdf
dist = getattr(scipy.stats, 'gamma')
param = dist.fit(data)
x = np.linspace(min(data), max(data), 100)
pdf_fit = dist.pdf(x, *param[:-2], loc = param[-2], scale = param[-1])
plt.plot(x, pdf_fit/sum(pdf_fit), label = 'Gamma')
plt.legend(loc = 'upper right')
plt.show()
在调用 plt.hist()
时,不要使用 weights=np.ones_like(data)/len(data)
,而是使用参数 normed=True
:
plt.hist(data, bins = 20, color = 'w', edgecolor = 'black', alpha = 0.5, normed = True)
我正在处理一些时间序列数据,我试图用伽玛分布来拟合数据。但问题是拟合的pdf的幅度远低于直方图的幅度。这是我的代码和情节。剧情有什么问题?
# the data
data = contents[0][1:]
# normalized histogram
weights = np.ones_like(data)/len(data)
plt.hist(data, bins = 20, color = 'w', edgecolor = 'black', alpha = 0.5, weights = weights)
# fit with gamma distribution and plot the pdf
dist = getattr(scipy.stats, 'gamma')
param = dist.fit(data)
x = np.linspace(min(data), max(data), 100)
pdf_fit = dist.pdf(x, *param[:-2], loc = param[-2], scale = param[-1])
plt.plot(x, pdf_fit/sum(pdf_fit), label = 'Gamma')
plt.legend(loc = 'upper right')
plt.show()
在调用 plt.hist()
时,不要使用 weights=np.ones_like(data)/len(data)
,而是使用参数 normed=True
:
plt.hist(data, bins = 20, color = 'w', edgecolor = 'black', alpha = 0.5, normed = True)