一日坊主

雰囲気でやっている

最尤推定によるガンマ分布のフィッティング

scipyを利用してデータのサンプル点をガンマ分布にフィッティングさせる.

ガンマ分布の確率密度関数は以下で表される*1

\displaystyle{
f(x, a) = \frac{x^{a-1}\exp(-x)}{\Gamma(a)}\\
\text{(}x \geq 0, a \gt 0, \Gamma(a)\text{:ガンマ関数)}
}
import matplotlib.pyplot as plt
from scipy.stats import gamma

# ガンマ分布に従う乱数を1,000個生成
a, b = 3, 2
seed = 1
data = gamma.rvs(a, size=1000, scale=b, random_state=seed)

# 最尤推定によるフィッティング
fit_parameter = gamma.fit(data)
frozen_gamma = gamma.freeze(*fit_parameter)

# 可視化
x = np.linspace(0, 30, 30)
fig = plt.figure(figsize=(8, 5))
ax = fig.add_subplot(1, 1, 1, title='Fitting Gamma Parameters by MLE')
ax.plot(x, frozen_gamma.pdf(x), label='fitted')
ax.hist(data, bins=30, alpha=0.7, density=True, label='sample')
ax.legend()
plt.show()

f:id:twakamori:20200702010440p:plain
gamma