# 補充:moment generating function (mgf)
## 背景
在有了 moment 的概念後,我們知道:
一個 random variable $X$ 的 nth moment around origin,就定義成 $X^n$ 的 expectation $E[X^n]$;如果並非 around origin 而是圍繞某個值 $b$,那麼 $X$ 的 nth moment around $b$ 就是 $E[(X-b)^n]$。
> 關於 moment 的一點介紹可參考 $A.2.5$ Expectation。
常用的如:
- first moment $E[X^1] = E[X]$ 也就是 $X$ 的 mean
- second moment $E[(X-\mu)^2]$ 也就是 $X$ 的 variance。
除此之外,其他的各個 moments 也都會從不同的面向去幫助我們了解一個 random variable 的 distribution 的 shape。
因此,我們希望能有一個 function 來幫助我們產生一個 distribution 的 moments,所以我們就引入了 <font color = "snake">moment generating function</font>。
## 定義

在我們討論 moment 時,moment 的對象是一個 random variable $X$,但是在計算 moment,也就是 $E[X^n]$ 時,因為要算 expectation,所以根據 random variable 是 discrete 或 continuous,我們會去定義 probability mass function (pmf) 或 probability density function (pdf)
> 忘記可回顧: $A.2.1$ Probability Distribution and Density Functions
### discrete case
若 $X$ 是一個 discrete 的 random variable(對應的也就是一個 pmf $f(x)$),它的 sample space 為 $S$,如果存在某個 $h>0$ 使得下方的 function 存在且為 finite:
\begin{equation}
E(e^{tX}) = \sum_{x \in S}e^{tx}f(x)
\end{equation}
> 這個式子說的是,對於 sample space 中的每個 outcome $x$,moment generating function 就會有剛好對應到的一項 $e^{tx}$,且它的係數即為這個 outcome 發生的機率。
那麼我們就將 $E(e^{tX})$ 定義為 $M(t)$,也就是 moment genrating function (mgf) of $X$。
### continuous case
如果 $X$ 是一個 continuous 的 random variable,那麼他的 mgf 就定義為:

## 意義
在 discrete case 裡,如果我們把 mgf 展開,會得到下方的式子,並且如前面所提到的,每個係數都是對應 outcome 的機率:

## 特性
如果將 $t$ 設為 $0$,則我們會得到 $M(0)=1$
> 原因如下:
>
> 
### mgf uniquely determines the distribution
mgf 的一個特性是,如果 distribution 在一個 bounded interval 中,那麼所有 moments 的 collection(也就是這個 collection 涵蓋所有 order,從 $0$ 到 $\infty$),就會 ++uniquely determine 這個 distribution++。
因此,如果有兩個 random variables(或是兩個 probability 的 distribution)有相同的 mgf,那他們的 probability 的 distribution 必定相同。
也就是說,假如我們有兩個 random variables,它們的 pmf 分別為 $f(x)$ 和 $g(y)$,而且他們的 sample space $S$ 相同,都是 $\{b_1,b_2,...\}$,並且下方式子成立:

那麼他們的 mgf 的各項係數,也就是 sample space 裡各個 outcome 發生的機率就會相同。
因此,我們可以得到以下的結論:
:::warning
一個 discrete random variable 的 mgf ++uniquely determines the distribution of that random variable++。
也就是說:
如果存在 mgf,則會有一個(且只會有一個) probability 的 distribution associated with 這個 mgf。
$\rightarrow$ 這件事對 continuous random variable 一樣成立!
:::
> mgf 不一定存在!
### Laplace Transforms
根據 Laplace transforms:
如果 mgf $M(t)$ 在 $-h<t<h$ 存在,那麼在 $t=0$,$M(t)$ 所有 order 的 derivatives 存在。
並且因為 series converges uniformly,所以我們可以交換微分和 summation 如下:

> 簡單來說就是對 $M(t)$ 微幾次,$\sum$ 後面就乘上 $x$ 的幾次方。
如果我們將 $t$ 設為 $0$,那麼代入上方式子會得到:

同理,推廣到 general case:

也就是說,如果 mgf 存在,那像我們要計算 mean 或 variance,就只需去計算對 mgf 於 $t=0$ 時微分一次和兩次:

結論:
\begin{equation}
M^{(r)}(0) = E(X^r)
\end{equation}
這個式子告訴我們的是:
:::warning
如果我們要去計算一個 random variable 的各個 moments,我們只要去對 $M(t)$ 微分即可。
:::
> 因此步驟就是首先我們先找出 $M(t)$ 的 closed form,接著就去對它微分來計算所求的 moment。
例子可參考下方例 2.3-7
## 例子
### 2.3-5

> 從 random variable $X$ 的 mgf 我們可以看出它的 sample space 的組成,以及各個 outcome 發生的機率。
### 2.3-6

> 透過一些離散的技巧展開 mgf,才能看出各 outcome 的 probability。
### 2.3-7
2.3-7 的 distribution 是 2.2-6 的結果,順便算一下:

接著算 2.3-7:

# 參考資料
- Hogg,Tanis,Zimmerman_Probability and Statistical Inference, 9th ed(2015), p.54-55, 59-63, 90
> 即 Section 2.2, 2.3 special mathematical expectations(mostly)