# 伽瑪分布 (Gamma distribution)

• 課本寫法：
(1)
\begin{align} f(x) = \frac{1}{\Gamma(\alpha) \beta^{\alpha}} x^{\alpha-1} e^{-x/\beta} \end{align}
• R 的寫法：
(2)
\begin{align} gamma(shape=a, rate=s) = \frac{1}{\Gamma(a) s^a } x^{a-1} e^{-x/s} \end{align}
• Gamma 函數的定義
(3)
\begin{align} \Gamma(k) = \int_{0}^{\infty} z^{k-1} e^{-z} dz \end{align}

# 特性

(4)
\begin{eqnarray} 1. && E(X) = \alpha \beta \\ 2. && Var(X) = \alpha \beta^2 \end{eqnarray}

# 動差生成函數

(5)
\begin{align} m_x(t) = (1-\beta t)^{-\alpha} \end{align}

# R 程式範例一

op=par(mfrow=c(2,2))
curve(dgamma(x, 1,1), 0, 10)
curve(dgamma(x, 1,5), 0, 10)
curve(dgamma(x, 5,1), 0, 10)
curve(dgamma(x, 5,5), 0, 10)


# R 程式範例二

-log(dgamma(1:4, shape=1))
p <- (1:9)/10
pgamma(qgamma(p,shape=2), shape=2)
1 - 1/exp(qgamma(p, shape=1))

# even for shape = 0.001 about half the mass is on numbers
# that cannot be represented accurately (and most of those as zero)
pgamma(.Machine$double.xmin, 0.001) pgamma(5e-324, 0.001) # on most machines 5e-324 is the smallest # representable non-zero number table(rgamma(1e4, 0.001) == 0)/1e4  執行結果： > -log(dgamma(1:4, shape=1)) [1] 1 2 3 4 > p <- (1:9)/10 > pgamma(qgamma(p,shape=2), shape=2) [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 > 1 - 1/exp(qgamma(p, shape=1)) [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 > > # even for shape = 0.001 about half the mass is on numbers > # that cannot be represented accurately (and most of those as zero) > pgamma(.Machine$double.xmin, 0.001)
[1] 0.4927171
> pgamma(5e-324, 0.001)  # on most machines 5e-324 is the smallest
[1] 0.4752741
>                        # representable non-zero number
> table(rgamma(1e4, 0.001) == 0)/1e4

FALSE   TRUE
0.5188 0.4812
>