# 補充:Gamma Distribution
## random variable W
### 定義
在談 exponential distribution 時,我們知道如果有一個 (approximate) Poisson process,則定義為 waiting time until first occurence 的 random variable 具 exponential distribution。
> 關於 exponential distribution 的內容可參考筆記「補充:Exponential Distribution」。
那同樣如果我們考慮一個 Poisson process,並將 random variable $W$ 定為:
:::info
==$W$==:waiting time until <font color = "blue">$\alpha$-th occurence</font>
:::
> 從 exponential distribution 的討論第一個 occurence 改為討論第 $\alpha$ 個。
我們想知道 $W$ 的 distribution,所以如同討論 exponential distribution 的方式,我們先求 $W$ 的 cdf,再利用 cdf 去求 pdf。
### cdf, pdf
首先,一樣我們根據定義去求 $W$ 的 cdf $F(w)$:

接著微分求 pdf $f(w)$:

> 最後經歷的一串繁瑣的計算可以省略不看,反正就是前面每項都會相消,導致只剩最後一項。
$\rightarrow$ 有這樣形式的 pdf 的 random variable $W$ 具 gamma distribution,不過只是其中一種 gamma type。
## gamma function
為了要介紹 general 的 gamma distribution,我們要先來定義 gamma function:
:::info
<font color = "blue">gamma function</font>:
\begin{equation}
\Gamma(t) = \int_0^\infty y^{t-1}e^{-y}dy \qquad 0 < t
\end{equation}
:::
如果我們只考慮 $t>1$ 的部分,再利用分部積分求 $\Gamma(t)$:

最後會得到:
:::success
\begin{equation}
\Gamma(t) = (t-1)! \qquad t \in \mathbb{Z^+}
\end{equation}
:::
$\rightarrow$ 因此,gamma function 也被稱作 <font color = "green">generalized factorial</font>。
### Note
上面課本這樣的寫法預設了 $t>1$ 且為整數的情形,但其實:
:::warning
gamma function 只要 $t \in \mathbb{R^+}$ 就是 well-defined。
$\rightarrow$ 上方的定義也有寫到, $t$ 的唯一條件只有大於零。
:::
那麼如果 $t$ 只是一個++正數,但非正整數++,因為 gamma function 原始的定義是:
\begin{equation}
\Gamma(t) = \int_0^\infty y^{t-1}e^{-y}dy \qquad 0 < t
\end{equation}
所以對所有的正實數來說,這個積分都會收斂。
因此,即使 $t$ 取 $0.5$ 之類的數也是有意義的,只是沒有階乘的形式而已。
$\rightarrow$ 能夠寫成 $(t-1)!$ 只是 gamma function 的一個特例。
詳細可見下圖說明:
> 接下來的內容會用到筆記後面的內容,可看完下方 gamma distribution 再回過頭來看。

### 特性
:::success
\begin{equation}
\Gamma(n+1) = n\Gamma(n)
\end{equation}
:::
:::success
\begin{equation}
\int_0^\infty x^{n-1}e^{-ax}dx = \frac{\Gamma(n)}{a^n}
\end{equation}
:::
> 如果原本的 gamma function 式子在 $e$ 的次方多了 $a$,則等同於在分母多除 $a^n$。
#### memoryless property
在 $Case \ 1$ 的證明過程中,我們定義:
\begin{equation}
S_\alpha = X_1 + X_2 + ... +X_\alpha
\end{equation}
但是如果我們回想在談 exponential distribution 時,這些 $X_i$ 的意義是「從開始到第一個 event 發生所經過的時間」,但是我們這樣直接相加,直覺上似乎有點怪怪的,比如:
假設 random variables $X_1,...,X_\alpha$ 以及它們的 outcome 如下:
$X_1 = 1$
$X_2 = 2$
...
$X_\alpha = \alpha$
如果我們的開始時間令為 $t=0$,則 $t=1$ 時 event 第一次發生,$t=2$ 時 event 第二次發生⋯⋯,$t=\alpha$ 時 event 第 $\alpha$ 次發生。
這樣看起來第 $\alpha$-th event 發生所經過的 waiting time 應該要是 $\alpha$ 而非 $1+2+...+\alpha$。
這時就要提到 exponential distribution(以及其他某些 distribution 也有的)property,叫做 <font color = "snake">memoryless property</font>,定義如下:
:::info
一個 nonnegative random variable $X$ 是 <font color = "blue">memoryless</font> 若:
\begin{equation}
P(X>s+t \ |\ X>t) = P(X>s) \qquad \forall s,t \ge 0
\end{equation}
:::
> 舉個例子來描述這個式子的意思:
>
>> 假設我們現在有一個機器,在這個機器已經正常使用 $t$ 小時的情況下,它能維持正常使用直到至少 $s+t$ 小時的機率,會等同於它從一開始能正常使用到 $s$ 小時的機率。
>
> 上面的例子是我參考資料列的課本所舉,不過聽起來有點反直覺(畢竟一般認為一個機器用越久,因為耗損所以壞掉的機率會更高),所以我們再舉一個更日常的例子:
>
>> 假設公車平均來說十分鐘會來一班,也就是一個 exponential distribution with $\lambda = \frac{1}{10}$(unit interval 為一分鐘)如果 $A$ 已經等了十五分鐘但公車都還沒來,那麼$A$ 再等五分鐘公車才來的機率,和才剛到公車站的 $B$,要等五分鐘的機率相同。
>>
>> $\rightarrow$ $A$ 已經等了十五分鐘並不影響公車在五分鐘後來的機率。
>>
> - 當舉現實生活中的例子讓你感覺好像有哪裡反直覺或怪怪的,也有可能就只是這個現象的 distribution 根本就不具有 memoryless property 而已,這種時候就該去思考是否應該用別的 distribution 來 model 的可能性。
總之,雖然講是這樣講,但如果不好好確認、只講感覺,好像還是哪裡怪怪的,因此利用數學來簡單證明看看,確實會得到 exponential distributoin 具 memoryless property 這樣的結果:

## gamma distribution
### 定義
有了 gamma function 的定義以後,我們就能把上面的 pdf 轉換成正式定義(由 gamma function 組成)的形式:
:::info
我們說一個 random variable $X$ 具 <font color = "blue">gamma distribution</font>,若它的 pdf 滿足:
\begin{equation}
f(x) = \frac{1}{\Gamma(\alpha)\theta^\alpha}x^{\alpha -1}e^{-\frac{x}{\theta}} \qquad 0 \le x < \infty
\end{equation}
:::
兩者對比比較清楚:

因此,我們得到結論:
:::warning
$W$: 在一個 Poisson process 中的 ++waiting time until $\alpha$-th occurence++
$\rightarrow$ 具 ++gamma distribution++, with parameters $\alpha$ 和 $\theta = \frac{1}{\lambda}$
:::
#### 證明:f(x) 滿足 pdf properties

### 表示方式
#### Bayesian
在 Bayesian statistics 中,通常我們用 $\alpha, \beta \in \mathbb{R^+}$ 來表示我們的 parameters,其中:
- ==$\alpha$== 為 shape paramter
- ==$\beta = \frac{1}{\theta}$== 為 inverse scale paramter (rate parameter)
一個 random variable $X$ is gamma distributed with shape $\alpha$ and rate $\beta$ 表示為:
\begin{equation}
X \sim \Gamma(\alpha,\beta) \equiv \text{Gamma}(\alpha,\beta)
\end{equation}
對應的 pdf 表示為:
:::info
\begin{equation}
f(x;\alpha,\beta) = \frac{x^{\alpha-1}e^{-\beta x}\beta^\alpha}{\Gamma(\alpha)} \qquad x,\alpha,\beta > 0
\end{equation}
:::
#### applied fields (model waiting times)
如果是在一些應用的領域,例如說我們前面講到的用 gamma distribution 來 model waiting times,那常用的 parameter 符號為 $k, \theta$:
- 以 ==$k$== 來表示 $\alpha$
- $\beta = \frac{1}{\theta}$ 的 ==$\theta$==
我們說一個 random variable $X$ is gamma distributed with shape $k$ and scale $\theta$ 表示為:
\begin{equation}
X \sim \Gamma(k,\theta) \equiv \text{Gamma}(k,\theta)
\end{equation}
對應的 pdf 表示為:
:::info
\begin{equation}
f(x;k,\theta) = \frac{x^{k-1}e^{-\frac{x}{\theta}}}{\theta^k \Gamma(k)} \qquad x,k,\theta > 0
\end{equation}
:::
### 特性:mean, variance
先說結論,下方再補過程:
:::success
gamma distribution 的 mean, variance 分別為:
\begin{equation}
\mu = k\theta = \frac{\alpha}{\beta} \qquad \sigma^2 = k\theta^2 = \frac{\alpha}{\beta^2}
\end{equation}
:::
要求 gamma distribution 的 mean 和 variance,首先按以前的方法,我們先來求具 gamma distribution 的 random variable $X$ 的 mgf:

得到 mgf 以後,再微分一次、兩次,代入 $t=0$:

### 特例:exponential distribution
:::warning
exponential distribution 為 gamma distribution 的特例。
:::
其實由它們各自的 random variable 定義也可以看出,簡單解釋如下:

### 例子

這個例子能用另一種算法:

# 參考資料
- Hogg,Tanis,Zimmerman, Probability and Statistical Inference, 9th ed(2015), p.98-100
> 對應 $3.2$ 小節 the Exponential, Gamma, and Chi-Square Distributions 的中間部分。
- Sheldon Ross, A first course in Probability, 9th ed, p.214
- wiki: [Gamma distribution](https://en.wikipedia.org/wiki/Gamma_distribution)