% \documentclass[serif]{beamer} % Serif for Computer Modern math font.
\documentclass[serif, handout]{beamer} % Handout to ignore pause statements.

\hypersetup{colorlinks,linkcolor=,urlcolor=red}

\usefonttheme{serif} % Looks like Computer Modern for non-math text -- nice!
\setbeamertemplate{navigation symbols}{} % Suppress navigation symbols
% \usetheme{Berlin} % Displays sections on top
\usetheme{Frankfurt}  % Displays section titles on top: Fairly thin but still swallows some material at bottom of crowded slides
%\usetheme{Berkeley}

\usepackage[english]{babel}
\usepackage{amsmath} % for binom
\usepackage{amsfonts} % for \mathbb{R} The set of reals
% \usepackage{graphicx} % To include pdf files!
% \definecolor{links}{HTML}{2A1B81}
% \definecolor{links}{red}
\setbeamertemplate{footline}[frame number] 

\mode<presentation>

\title{Moment-generating Functions\footnote{ This slide show is an open-source document. See last slide for copyright information.} \\ (Section 3.4)}
\subtitle{STA 256: Fall 2019}
\date{} % To suppress date

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}
\frametitle{Overview}
\tableofcontents
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Moment-generating functions}
%\framesubtitle{} 
{\LARGE
\begin{displaymath}
    M_{_X}(t) =  E(e^{Xt})  \pause =
\left\{ \begin{array}{l} % ll means left left
   \int_{-\infty}^\infty e^{xt} \, f_{_X}(x) \, dx  \\ 
   \\
   \sum_x e^{xt} p_{_X}(x) 
   \end{array}  \right.   % Need that crazy invisible right period!
\end{displaymath}  \pause
} % End size
\begin{itemize}
    \item Moment-generating function may not exist for all $t$. \pause
    \item It may not exist for any $t$. \pause
    \item Existence in an interval containing $t=0$ is what matters. \pause
    \item Moment-generating functions exist for most of the common distributions.
\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Properties of moment-generating functions} \pause
%\framesubtitle{} 
\begin{itemize}
    \item Moment-generating functions can be used to generate moments.  \pause
A \emph{moment} is a quantity like $E(X)$, $E(X^2)$, etc. \pause
%    \item To get $E(Y^k)$, differentiate $M_{_Y}(t)$ with respect to $t$. \pause Differentiate $k$ times and set $t=0$. \pause
    \item[] 
    \item Moment-generating functions correspond uniquely to probability distributions. \pause
    \item It's sometimes easier to calculate the moment-generating function of $Y=g(X)$ and recognize it, than to obtain the distribution of $Y$ directly.
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Generating Moments}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Generating moments with the moment-generating function: Preparation} \pause
%\framesubtitle{Preparation} 
Theorem: A power series may be differentiated or integrated term by term, and the result is a power series with the same radius of convergence.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Generating moments with the moment-generating function}
\framesubtitle{Using $e^y = \sum_{k=0}^\infty \frac{y^k}{k!} = 1 + y + \frac{y^2}{2!} + \frac{y^3}{3!} + \cdots $}  \pause
{\small
\begin{eqnarray*}
     M_{_X}(t)  & = & E(e^{Xt}) \\ \pause
                & = & \int_{-\infty}^\infty e^{xt} \, f_{_X}(x) \, dx \\  \pause
                & = & \int_{-\infty}^\infty 
                \left( \sum_{k=0}^\infty \frac{(xt)^k}{k!} \right)
                 \, f_{_X}(x) \, dx \\  \pause
                & = & \sum_{k=0}^\infty  \int_{-\infty}^\infty 
               \frac{(xt)^k}{k!}  \, f_{_X}(x) \, dx \\  \pause
                & = & \sum_{k=0}^\infty  \left( \int_{-\infty}^\infty 
                x^k  \, f_{_X}(x) \, dx \right) \frac{t^k}{k!} \\  \pause
                & = & \sum_{k=0}^\infty E(X^k) \frac{t^k}{k!}
\end{eqnarray*} 
} % End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{Generating moments continued} \pause
%\framesubtitle{} 
{\small
\begin{eqnarray*}
     M_{_X}(t)    & = & \sum_{k=0}^\infty E(X^k) \frac{t^k}{k!} \\ \pause
                  & = &  1 + E(X) t + E(X^2) \frac{t^2}{2!} + 
                  E(X^3) \frac{t^3}{3!} + \cdots \\  \pause
M^\prime_{_X}(t)  \pause 
    & = & 0 \pause + E(X) \pause + E(X^2) \frac{2t}{2!} \pause + 
                  E(X^3) \frac{3t^2}{3!} + \cdots \\  \pause 
    & = & E(X) + E(X^2) t + E(X^3) \frac{t^2}{2!} + 
                  E(X^4) \frac{t^3}{3!} + \cdots \\  \pause 
M^\prime_{_X}(0)  & = &  E(X) \\  \pause
M^{\prime\prime}_{_X}(t)  \pause 
    & = & 0 \pause + E(X^2) \pause + E(X^3) t \pause + 
                  E(X^4) \frac{t^2}{2!} + \cdots \\  \pause 
M^{\prime\prime}_{_X}(0) & = & E(X^2) \\ 
\end{eqnarray*}  \pause
And so on. \pause To get $E(Y^k)$, differentiate $M_{_Y}(t)$, $k$ times  with respect to $t$, and set $t=0$.
} % End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Example: Poisson Distribution}
\framesubtitle{$p(x) = \frac{e^{-\lambda}\, \lambda^x}{x!}$ for $x = 0, 1, \ldots $}  \pause 
\begin{eqnarray*}
    M(t) & = & E(e^{Xt}) \\ \pause 
         & = & \sum_{x=0}^\infty e^{xt} \, \frac{e^{-\lambda}\, \lambda^x}{x!} \\ \pause 
         & = & e^{-\lambda}\sum_{x=0}^\infty \frac{\left(\lambda e^t \right)^x}{x!} \\ \pause 
         & = & e^{-\lambda} e^{\lambda e^t} \\ \pause 
         & = & e^{\lambda (e^t-1)}
\end{eqnarray*} 
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{Differentiate to get moments for Poisson}
\framesubtitle{$M(t)=e^{\lambda (e^t-1)}$}  \pause
\begin{eqnarray*}
    M^\prime(t) \pause & = & e^{\lambda (e^t-1)} \cdot \pause \lambda e^t \\ \pause
                & = & \lambda e^{\lambda (e^t-1)+t} \\
\end{eqnarray*}  \pause
Set $t=0$ \pause and get $E(X)=\lambda$. \pause
\begin{eqnarray*}
    M^{\prime\prime}(t) 
        & = & \lambda e^{\lambda (e^t-1)+t} \cdot \pause (\lambda e^t+1) \\ \pause
        & = &  e^{\lambda (e^t-1)+t} \cdot (\lambda^2 e^t+\lambda)\\
\end{eqnarray*}  \pause
Set $t=0$ \pause and get $E(X^2)=  \pause \lambda^2+\lambda$. \vspace{3mm} \pause

So $Var(X) = E(X^2)=[E(X)]^2 \pause = \lambda^2+\lambda - \lambda^2 \pause = \lambda$
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Useful properties of moment-generating functions}
\framesubtitle{Use these to find distributions of \emph{functions} of random variables} 
\begin{itemize}
    \item $M_{_{aX}}(t) = M_{_X}(at)$ \pause
    \item $M_{_{a+X}}(t) = e^{at}M_{_X}(t)$ \pause
    \item If $X$ and $Y$ are independent, $M_{_{X+Y}}(t) = M_{_X}(t) \, M_{_Y}(t)$ \pause
    \item[] Extending by induction, \pause
    \item If $X_1, \ldots, X_n$ are independent, $M_{_{(\sum_{i=1}^n X_i)}}(t) = \prod_{i=1}^n M_{_{X_i}}(t)$.
\end{itemize}
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Identifying Distributions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Identifying Distributions using Moment-generating Functions} \pause
%\framesubtitle{} 
\begin{itemize}
    \item Getting expected values with the MGF can be easier than direct calculation. \pause But not always.
    \item Moment-generating functions can also be used to identify distributions. \pause
    \item Calculate the moment-generating function of $Y=g(X)$, \pause and if you recognize the MGF, you have the distribution of $Y$. \pause
    \item Here's what's happening technically. \pause
    \item  $M_{_X}(t) =  \int_{-\infty}^\infty e^{xt} \, f_{_X}(x) \, dx $ \pause so $M_{_X}(t)$ is a function of $F_{_X}(x)$. \pause That is,  $M_{_X}(t) = g(F_{_X}(x))$. \pause
    \item Uniqueness says the function $g$ has an inverse, \pause so that $F_{_X}(x) = g^{-1}(M_{_X}(t))$.
\end{itemize}
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{The function $M(t)$ is like a fingerprint of the probability distribution.} \pause
%\framesubtitle{} 
\begin{itemize}
    \item[] $Y \sim N(\mu,\sigma^2)$ if and only if $M_{_Y}(t) =   e^{\mu t + \frac{1}{2}\sigma^2t^2}$. \pause
    \item[] 
    \item[] $Y \sim \chi^2(\nu)$ if and only if $M_{_Y}(t) = (1-2t)^{-\nu/2}$ for $t < \frac{1}{2}$. \pause
\end{itemize} \vspace{5mm}
Chi-squared is a special Gamma, with $\alpha=\nu/2$ and $\lambda = \frac{1}{2}$.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Normal: $M(t) = e^{\mu t + \frac{1}{2}\sigma^2t^2}$} \pause
%\framesubtitle{} 
\begin{center}
\includegraphics[width=3in]{NormalMGF}
\end{center}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Chi-squared: $M(t) = (1-2t)^{-\nu/2}$}
\framesubtitle{Chi-squared is a special Gamma, with $\alpha=\nu/2$ and $\lambda= \frac{1}{2}$}  \pause
\begin{center}
\includegraphics[width=3in]{ChisqMGF}
\end{center}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Example: Sum of Poissons is Poisson} \pause
%\framesubtitle{} 
Let $X_1, \ldots, X_n$ be independent Poisson($\lambda_i$). \pause Let $Y = \sum_{i=1}^n X_i$. \pause Find the probability distribution of $Y$. \pause Recall Poisson MGF is $e^{\lambda (e^t-1)}$.  \pause
\begin{eqnarray*}
    M_{_Y}(t) & = & M_{_{(\sum_{i=1}^n X_i)}}(t) \\ \pause
           & = & \prod_{i=1}^n M_{x_i}(t) \\ \pause
           & = & \prod_{i=1}^n e^{\lambda_i (e^t-1)} \\ \pause
           & = & e^{(\sum_{i=1}^n\lambda_i) (e^t-1)} \\ \pause
\end{eqnarray*} 
MGF of Poisson, with $\lambda^\prime = \sum_{i=1}^n\lambda_i$. \pause Therefore, $Y \sim$ Poisson($\sum_{i=1}^n\lambda_i$).
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Copyright Information}

This slide show was prepared by  \href{http://www.utstat.toronto.edu/~brunner}{Jerry Brunner},
Department of Statistical Sciences, University of Toronto. It is licensed under a 
\href{http://creativecommons.org/licenses/by-sa/3.0/deed.en_US}
     {Creative Commons Attribution - ShareAlike 3.0 Unported License}. Use any part of it as you like and share the result freely. The \LaTeX~source code is available from the course website:

\vspace{5mm}
\href{http://www.utstat.toronto.edu/~brunner/oldclass/256f19} {\small\texttt{http://www.utstat.toronto.edu/$^\sim$brunner/oldclass/256f19}}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}


and $Var(Y) = \sigma^2$

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{} \pause
%\framesubtitle{} 
\begin{itemize}
    \item  \pause
    \item  \pause
    \item 
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{center}
\includegraphics[width=2in]{BivariateNormal}
\end{center}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{}
%\framesubtitle{} 
{\LARGE
\begin{eqnarray*}
    m_1 & = & a + b \\
    m_2 & = & c + d 
\end{eqnarray*} 
} % End size
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{} \pause
%\framesubtitle{} 
\begin{itemize}
    \item  \pause
    \item  \pause
    \item 
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# R code for plots of normal MGFs
tt = seq(from=-1,to=1,by=0.05)
mu = 0; sigsq = 1
zero = exp(mu*tt + 0.5*sigsq*tt^2)
mu = 1; one = exp(mu*tt + 0.5*sigsq*tt^2)
mu = -1; minusone = exp(mu*tt + 0.5*sigsq*tt^2)
x = c(tt,tt,tt); y = c(zero,one,minusone)
plot(x,y,pch=' ',xlab='t',ylab = 'M(t)')
lines(tt,zero,lty=1)
lines(tt,one,lty=2)
lines(tt,minusone,lty=3)
title("Fingerprints of the normal distribution")
# Legend
x1 <- c(-0.4,0) ; y1 <- c(4,4) ; lines(x1,y1,lty=1)
text(0.25,4,expression(paste(mu," =  0, ",sigma^2," = 1")))
x2 <- c(-0.4,0) ; y2 <- c(3.75,3.75) ; lines(x2,y2,lty=2)
text(0.25,3.75,expression(paste(mu," =  1, ",sigma^2," = 1")))
x3 <- c(-0.4,0) ; y3 <- c(3.5,3.5) ; lines(x3,y3,lty=3)
text(0.25,3.5,expression(paste(mu," = -1, ",sigma^2," = 1")))


# R code for plots of chi-squared MGFs
tt = seq(from=-0.25,to=0.25,by=0.005)
nu = 1; one = (1-2*tt)^(-nu/2)
nu = 2; two = (1-2*tt)^(-nu/2)
nu = 3; three = (1-2*tt)^(-nu/2)
x = c(tt,tt,tt); y = c(one,two,three)
plot(x,y,pch=' ',xlab='t',ylab = 'M(t)')
lines(tt,one,lty=1)
lines(tt,two,lty=2)
lines(tt,three,lty=3)
title("Fingerprints of the chi-squared distribution")
# Legend
x1 <- c(-0.2,-0.1) ; y1 <- c(2.5,2.5) ; lines(x1,y1,lty=1)
text(-0.05,2.5,expression(paste(nu," =  1")))
x2 <- c(-0.2,-0.1) ; y2 <- c(2.3,2.3) ; lines(x2,y2,lty=2)
text(-0.05,2.3,expression(paste(nu," =  2")))
x3 <- c(-0.2,-0.1) ; y3 <- c(2.1,2.1) ; lines(x3,y3,lty=3)
text(-0.05,2.1,expression(paste(nu," =  3")))





