---
# System prepended metadata

title: Moment Generating Function

---

# Moment Generating Function
待修正
$X \sim Uni(a,b)$

$$
\begin{eqnarray}
    M_X(t)&=&E(e^{Xt})\\
    ~&=&\int_a^b\frac{e^{xt}}{b-a}dx\\
    ~&=&\frac{e^{bt}-e^{at}}{t(b-a)}
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X)&=&\frac{d}{dt}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d}{dt}\frac{e^{bt}-e^{at}}{t(b-a)}\big|_{t=0}\\
    ~&=&\frac{(be^{bt}-ae^{at})t(b-a)-(e^{bt}-e^{at})(b-a)}{[t(b-a)]^2}\big|_{t=0}\\
    ~&=&\frac{e^{xt}}{b-a}
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X^2)&=&\frac{d^2}{dt^2}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d^2}{dt^2}q+pe^t\big|_{t=0}\\
    ~&=&pe^t\big|_{t=0}\\
    ~&=&p
\end{eqnarray}
$$

$$
\begin{eqnarray}
    Var(X)&=&E(X^2)-(E(X))^2\\
    ~&=&p-p^2\\
    ~&=&pq
\end{eqnarray}
$$

---
$X \sim Bernoulli(p)$

$$
\begin{eqnarray}
    M_X(t)&=&E(e^{Xt})\\
    ~&=&qe^{0t}+pe^{1t}\\
    ~&=&q+pe^t
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X)&=&\frac{d}{dt}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d}{dt}q+pe^t\big|_{t=0}\\
    ~&=&pe^t\big|_{t=0}\\
    ~&=&p
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X^2)&=&\frac{d^2}{dt^2}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d^2}{dt^2}q+pe^t\big|_{t=0}\\
    ~&=&pe^t\big|_{t=0}\\
    ~&=&p
\end{eqnarray}
$$

$$
\begin{eqnarray}
    Var(X)&=&E(X^2)-(E(X))^2\\
    ~&=&p-p^2\\
    ~&=&pq
\end{eqnarray}
$$

---

$X \sim Bino(n, p)$

$$
\begin{eqnarray}
    M_X(t)&=&E(e^{Xt})\\
    ~&=&\sum_{x=0}^{n}e^{xt}{n\choose x}p^xq^{n-x}\\
    ~&=&\sum_{x=0}^{n}{n\choose x}(e^tp)^xq^{n-x}\\
    ~&=&(e^tp+q)^n\\
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X)&=&\frac{d}{dt}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d}{dt}(e^tp+q)^n\big|_{t=0}\\
    ~&=&n(e^tp+q)^{n-1}e^tp\big|_{t=0}\\
    ~&=&np
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X^2)&=&\frac{d^2}{dt^2}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d^2}{dt^2}(e^tp+q)^n\big|_{t=0}\\
    ~&=&npe^t(e^tp+q)^{n-1}+n(n-1)e^{2t}p^2(e^tp+q)^{n-2}\big|_{t=0}\\
    ~&=&n^2p^2-np^2+np
\end{eqnarray}
$$

$$
\begin{eqnarray}
    Var(X)&=&E(X^2)-(E(X))^2\\
    ~&=&n^2p^2-np^2+np-n^2p^2\\
    ~&=&npq
\end{eqnarray}
$$

---

$X \sim Poi(\lambda)$

$$
\begin{eqnarray}
    M_X(t)&=&E(e^{Xt})\\
    ~&=&\sum_{x=0}^{\infty}e^{xt}\frac{e^{-\lambda}\lambda^x}{x!}\\
    ~&=&\sum_{x=0}^{\infty}\frac{e^{-\lambda}(e^t\lambda)^x}{x!}\\
    ~&=&e^{-\lambda}e^{e^t\lambda}\sum_{x=0}^{\infty}\frac{e^{-e^t\lambda}(e^t\lambda)^x}{x!}\\
    ~&=&e^{-\lambda}e^{e^t\lambda}\\
    ~&=&e^{\lambda (e^t-1)}
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X)&=&\frac{d}{dt}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d}{dt}e^{\lambda^(e^t-1)}\big|_{t=0}\\
    ~&=&\lambda e^{\lambda(e^t-1)}e^t\big|_{t=0}\\
    ~&=&\lambda 
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X^2)&=&\frac{d^2}{dt^2}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d^2}{dt^2}e^{\lambda^(e^t-1)}\big|_{t=0}\\
    ~&=&\lambda e^t(1+\lambda e^t)e^{\lambda(e^t-1)}\big|_{t=0}\\
    ~&=&\lambda(1+\lambda)\\
    ~&=&\lambda^2+\lambda
\end{eqnarray}
$$

$$
\begin{eqnarray}
    Var(X)&=&E(X^2)-(E(X))^2\\
    ~&=&\lambda^2+\lambda-\lambda^2\\
    ~&=&\lambda
\end{eqnarray}
$$

---

$X\sim Hyper()$

---

$X \sim Exp(\lambda)$

$$
\begin{eqnarray}
    M_X(t)&=&E(e^{Xt})\\
    ~&=&\int_{0}^{\infty}e^{xt}\lambda e^{-\lambda x}dx\\
    ~&=&\int_{0}^{\infty}\lambda e^{-x(\lambda-t)}dx\\
    ~&=&\frac{\lambda}{\lambda-t}\int_{0}^{\infty}(\lambda-t)e^{x(\lambda-t)}dx\\
    ~&=&\frac{\lambda}{\lambda-t}
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X)&=&\frac{d}{dt}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d}{dt}\frac{\lambda}{\lambda-t}\big|_{t=0}\\
    ~&=&\frac{\lambda}{(\lambda-t)^2}\big|_{t=0}\\
    ~&=&\frac{1}{\lambda} 
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X^2)&=&\frac{d^2}{dt^2}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d^2}{dt^2}\frac{\lambda}{\lambda-t}\big|_{t=0}\\
    ~&=&\frac{2\lambda(\lambda-t)}{(\lambda-t)^4}\big|_{t=0}\\
    ~&=&\frac{2}{\lambda^2} 
\end{eqnarray}
$$

$$
\begin{eqnarray}
    Var(X)&=&E(X^2)-(E(X))^2\\
    ~&=&\frac{2}{\lambda^2}-\frac{1}{\lambda^2}\\
    ~&=&\frac{1}{\lambda^2}
\end{eqnarray}
$$

---

$X \sim Geo(p)$

$$
\begin{eqnarray}
    M_X(t)&=&E(e^{xt})\\
    ~&=&\sum_{x=1}^{\infty}e^{xt}q^{x-1}p\\
    ~&=&\frac{p}{q}\sum_{x=1}^{\infty}(qe^t)^x\text{ , only when } e^tq\lt1\\
    ~&=&\frac{p}{q}\frac{qe^t}{1-qe^t}\\
    ~&=&\frac{pe^t}{1-qe^t}
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X)&=&\frac{d}{dt}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d}{dt}\frac{pe^t}{1-qe^t}\big|_{t=0}\\
    ~&=&\frac{pe^t}{(1-qe^t)^2}\big|_{t=0}\\
    ~&=&\frac{1}{p}
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X^2)&=&\frac{d^2}{dt^2}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d^2}{dt^2}\frac{pe^t}{1-qe^t}\big|_{t=0}\\
    ~&=&\frac{pe^t(1-q^2e^{2t})}{(1-qe^t)^4}\big|_{t=0}\\
    ~&=&\frac{p(1-q^2)}{(1-q)^4}\\
    ~&=&\frac{p^2(1+q)}{p^4}\\
    ~&=&\frac{1+q}{p^2}
\end{eqnarray}
$$

$$
\begin{eqnarray}
    Var(X)&=&E(X^2)-(E(X))^2\\
    ~&=&\frac{1+q}{p^2}-\frac{1}{p^2}\\
    ~&=&\frac{q}{p^2}
\end{eqnarray}
$$

---

$X \sim Normal(\mu, \sigma)$

$$
\begin{eqnarray}
    M_X(t)&=&E(e^{Xt})\\
    ~&=&\int_{-\infty}^{\infty}e^{xt}\frac{e^{-\frac{1}{2}(\frac{x-\mu}{\sigma})^2}}{\sqrt{2\pi}\sigma}dx\\
    ~&=&\frac{1}{\sqrt{2\pi}\sigma}\int_{-\infty}^{\infty}e^{xt-\frac{(x-\mu)^2}{2\sigma^2}}dx\\
    ~&=&\frac{1}{\sqrt{2\pi}\sigma}\int_{-\infty}^{\infty}e^{\frac{-(x^2-2\mu x-2\sigma^2xt+\mu^2)}{2\sigma^2}}dx\\
    ~&=&\frac{1}{\sqrt{2\pi}\sigma}\int_{-\infty}^{\infty}e^{\frac{-(x^2-2x(\mu+\sigma^2t)+\mu^2)}{2\sigma^2}}dx\\
    ~&=&\frac{1}{\sqrt{2\pi}\sigma}\int_{-\infty}^{\infty}e^{\frac{-((x-(\mu+\sigma^2t))^2+\mu^2-(\mu+\sigma^2t)^2)}{2\sigma^2}}dx\text{, by completing the square}\\
    ~&=&\frac{1}{\sqrt{2\pi}\sigma}\int_{-\infty}^{\infty}e^{\frac{-(x-(\mu+\sigma^2t))^2-\sigma^4t^2-2\mu\sigma^2t}{2\sigma^2}}dx\\
    ~&=&\frac{e^{\frac{\sigma^2t^2+2\mu t}{2}}}{\sqrt{2\pi}\sigma}\int_{-\infty}^{\infty}e^{\frac{-(x-(\mu+\sigma^2t))^2}{2\sigma^2}}dx\\
    ~&=&e^{\frac{\sigma^2t^2}{2}+\mu t}\int_{-\infty}^{\infty}\underbrace{\frac{e^{\frac{-(x-(\mu+\sigma^2t))^2}{2\sigma^2}}}{\sqrt{2\pi}\sigma}}_{\text{PDF of } Normal(\mu+\sigma^2t,\sigma)}dx\\
    ~&=&e^{\frac{\sigma^2t^2}{2}+\mu t}\\
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X)&=&\frac{d}{dt}M_X(t)\big|_{t=0}\\
    ~&=&e^{\frac{\sigma^2t^2}{2}+\mu t}\big|_{t=0}\\
    ~&=&(\sigma^2t+\mu)e^{\frac{\sigma^2t^2}{2}+\mu t}\big|_{t=0}\\
    ~&=&\mu
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X^2)&=&\frac{d^2}{dt^2}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d^2}{dt^2}e^{\frac{\sigma^2t^2}{2}+\mu t}\big|_{t=0}\\
    ~&=&(\sigma^2t+\mu)^2e^{\frac{\sigma^2t^2}{2}+\mu t}+\sigma^2e^{\frac{\sigma^2t^2}{2}}\big|_{t=0}\\
    ~&=&\mu^2+\sigma^2
\end{eqnarray}
$$

$$
\begin{eqnarray}
    Var(X)&=&E(X^2)-(E(X))^2\\
    ~&=&\mu^2+\sigma^2-\mu^2\\
    ~&=&\sigma^2
\end{eqnarray}
$$

---

$X\sim Gamma(\alpha, \beta)$

$$
\begin{eqnarray}
    M_X(t)&=&E(e^{Xt})\\
    ~&=&\int_{0}^{\infty}e^{xt}\frac{1}{\Gamma(\alpha)\beta^\alpha}x^{\alpha-1}e^{-\frac{x}{\beta}}dx\\
    ~&=&\int_{0}^{\infty}\frac{1}{\Gamma(\alpha)\beta^\alpha}x^{\alpha-1}e^{-x\frac{1-\beta t}{\beta}}dx\\
    ~&=&\frac{1}{(1-\beta t)^\alpha}\int_{0}^{\infty}\frac{1}{\Gamma(\alpha)(\frac{\beta}{1-\beta t})^\alpha}x^{\alpha-1}e^{-x\frac{1-\beta t}{\beta}}dx\\
    ~&=&(1-\beta t)^{-\alpha}
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X)&=&\frac{d}{dt}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d}{dt}(1-\beta t)^{-a}\big|_{t=0}\\
    ~&=&\alpha\beta(1-\beta t)^{-a-1}\big|_{t=0}\\
    ~&=&\alpha\beta
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X^2)&=&\frac{d^2}{dt^2}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d^2}{dt^2}(1-\beta t)^{-a}\big|_{t=0}\\
    ~&=&\alpha(\alpha+1)\beta^2(1-\beta t)^{-a-2}\big|_{t=0}\\
    ~&=&\alpha^2\beta^2+\alpha\beta^2
\end{eqnarray}
$$

$$
\begin{eqnarray}
    Var(X)&=&E(X^2)-(E(X))^2\\
    ~&=&\alpha^2\beta^2+\alpha\beta^2-\alpha^2\beta^2\\
    ~&=&\alpha\beta^2
\end{eqnarray}
$$

---

$X\sim \chi^2(r)$

$$
\begin{eqnarray}
    M_X(t)&=&E(e^{Xt})\\
    ~&=&\int_{0}^{\infty}e^{xt}\frac{1}{\Gamma(\frac{r}{2})2^{\frac{r}{2}}}x^{\frac{r}{2}-1}e^{-x/2}dx\\
    ~&=&\int_{0}^{\infty}\frac{1}{\Gamma(\frac{r}{2})2^{\frac{r}{2}}}x^{\frac{r}{2}-1}e^{-\frac{x(1-2t)}{2}}dx\\
    ~&=&\frac{1}{(1-2 t)^\frac{r}{2}}\int_{0}^{\infty}\underbrace{\frac{1}{\Gamma(\frac{r}{2})(\frac{2}{1-2t})^\frac{r}{2}}x^{\frac{r}{2}-1}e^{-x\frac{1-2 t}{2}}}_{\text{PDF of }Gamma(\frac{r}{2},\frac{1-2t}{2})}dx\\
    ~&=&(1-2 t)^{-\frac{r}{2}}
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X)&=&\frac{d}{dt}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d}{dt}(1-2 t)^{-\frac{r}{2}}\big|_{t=0}\\
    ~&=&-2\frac{-r}{2}(1-2 t)^{-\frac{r}{2}-1}\big|_{t=0}\\
    ~&=&r(1-2 t)^{-\frac{r}{2}-1}\big|_{t=0}\\
    ~&=&r
\end{eqnarray}
$$

$$
\begin{eqnarray}
    E(X^2)&=&\frac{d^2}{dt^2}M_X(t)\big|_{t=0}\\
    ~&=&\frac{d^2}{dt^2}(1-2 t)^{-\frac{r}{2}}\big|_{t=0}\\
    ~&=&-2r(-\frac{r}{2}-1)(1-2 t)^{-\frac{r}{2}-1}\big|_{t=0}\\
    ~&=&(r^2+2r)(1-2 t)^{-\frac{r}{2}-1}\big|_{t=0}\\
    ~&=&r^2+2r
\end{eqnarray}
$$

$$
\begin{eqnarray}
    Var(X)&=&E(X^2)-(E(X))^2\\
    ~&=&r^2 + 2r -r^2\\
    ~&=&2r
\end{eqnarray}
$$