감마분포 확률밀도함수
감마분포의 확률밀도함수는 다음과 같습니다.
$f(x; \alpha, \beta) = \frac {1}{\beta^{\alpha}\Gamma(\alpha)}x^{\alpha - 1} e^{-\frac {x}{\beta}}$ ($x > 0$)
감마분포 그래프 그리기(함수 사용)
from numpy import linspace, exp
# numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
# numpy.exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'exp'>
# start = 0, stop = 50, num = 101)로 설정
x = linspace(0, 50, 101)
x
$x$의 범위는 0에서 50까지 100 등분하였습니다.
from scipy.special import gamma
# scipy.special.gamma(z) = <ufunc 'gamma'>
shape = 5
scale = 2
y = x ** (shape - 1) * exp(-x / scale) / (scale ** shape * gamma(shape))
y
감마 분포의 공식으로 y를 계산하겠습니다.
$\alpha = 5$, $\beta = 2$인 감마 분포입니다.
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(10, 8))
plt.plot(x, y)
plt.xlabel('X')
plt.ylabel('P(X)')
plt.title('Gamma Distribution(shape = 5, scale = 2, loc = 0)')
plt.grid()
plt.show()
그래프를 그리면 위와 같습니다.
감마분포 그래프 그리기(scipy 사용)
from scipy.stats import gamma
# scipy.stats.gamma = <scipy.stats._continuous_distns.gamma_gen object>
# loc : location parameter (default=1)
loc = 0
y1 = gamma(shape, loc, scale).pdf(x)
print(y1)
plt.figure(figsize=(10, 8))
plt.plot(x, y1, 'r')
plt.xlabel('X')
plt.ylabel('P(X)')
plt.grid()
plt.title('Gamma Distribution(shape = 5, scale = 2, loc = 0)')
plt.show()
scipy.stats를 이용해 감마 분포를 계산하고 그래프를 그렸습니다.
결과가 거의 똑같이 나왔습니다. 실제 수식으로 계산한 것과 차이를 느끼지 못합니다.
감마분포 변형
from numpy import arange
# numpy.arange(start, stop, step, dtype=None)
plt.figure(figsize=(10, 8))
plt.xlabel('X')
plt.ylabel('P(X)')
plt.title('Gamma Distribution(loc = 0, scale = 2)')
plt.grid()
for shape in arange(5, 25, 5):
plt.plot(x, gamma(shape, loc, scale).pdf(x), label='(shape=' + str(shape) + ')')
plt.legend()
plt.show()
$\beta$와 loc를 고정하고 $\alpha$만 바꿔서 그래프를 그려보았습니다.
$\alpha$가 커질수록 더 많이 흩어집니다.
shape = 10
plt.figure(figsize=(10, 8))
plt.xlabel('X')
plt.ylabel('P(X)')
plt.title('Gamma Distribution(shape = 10, loc = 0)')
plt.grid()
for scale in arange(1, 4):
plt.plot(x, gamma(shape, loc, scale).pdf(x), label='(scale=' + str(scale) + ')')
plt.legend()
plt.show()
$\alpha$와 loc를 고정하고 $\beta$만 바꿔서 그래프를 그려보았습니다.
$\beta$가 커질수록 더 많이 흩어집니다.
shape = 10
scale = 2
plt.figure(figsize=(10, 8))
plt.xlabel('X')
plt.ylabel('P(X)')
plt.title('Gamma Distribution(shape = 10, scale = 2)')
plt.grid()
for loc in arange(5) * 2 - 1:
plt.plot(x, gamma(shape, loc, scale).pdf(x), label='(loc=' + str(loc) + ')')
plt.legend()
plt.show()
$\alpha$와 $\beta$를 고정하고 loc만 바꿔서 그래프를 그려보았습니다.
loc는 location parameter (default=0)을 나타냅니다.
위치가 달라진 것을 보실 수 있습니다.
'Python > 통계' 카테고리의 다른 글
정규분포 with python (0) | 2019.10.15 |
---|---|
(2)정규분포의 누적분포함수를 python으로 구현해보자 (0) | 2019.08.22 |
(1)정규분포 밀도함수 python 함수로 만들어보기 (0) | 2019.08.20 |
댓글