\documentclass[12pt]{article}

\usepackage{Math203}

\usepackage{amsmath,amssymb}
\usepackage{hyperref}
\usepackage{color}
\usepackage{xcolor}
\usepackage{enumitem}

\pagestyle{plain}

\renewcommand{\labelenumi}{\arabic{enumi}.}
\renewcommand{\labelenumii}{\arabic{enumi}.\arabic{enumii}}
\renewcommand{\labelenumiii}{\arabic{enumi}.\arabic{enumii}.\arabic{enumiii}}

\setlength{\topmargin}{-0.75 in} \setlength{\textheight}{9.5 in}

\parindent0in

\newenvironment{packed_enum}{
\begin{enumerate}
  \setlength{\itemsep}{12pt}
  \setlength{\parskip}{0pt}
  \setlength{\parsep}{0pt}
  \setlength{\topsep}{0pt}
}{\end{enumerate}}

\renewcommand{\labelitemii}{$\blacktriangleright$}

\def\ctitle{Math 203: Principles of Statistics 1}
\def\ttitle{Fall 2024}
\def\stitle{The Normal Distribution}

\makeatletter
\def\@maketitle{%
  \newpage
  \null
  \begin{center}%
    {\textsclarge{\ctitle} \par}%
    \medskip
    {\textsclarge{\ttitle} \par}%
    \medskip
    {\textsc{\stitle} \par}
  \end{center}%
  \par
  \vskip 1.5em}
\makeatother

\begin{document}

<<funcs,echo=FALSE>>=
myrnd <- function(x,d) {formatC(x, format="f", digits=d)}
library(shape)
library(diagram)
@


\maketitle

The Normal distribution is the most widely used continuous probability distribution.  The probability density function (pdf), $f(y)$, takes the form
\[
f(y) = \left( \frac{1}{2 \pi \sigma^2} \right)^{1/2} \exp \left\{ - \frac{1}{2 \sigma^2} (y - \mu)^2 \right\} \qquad -\infty < y < \infty
\]
where $\mu$ and $\sigma>0$ are parameters of the distribution. It can be shown that 
\[
\int_{-\infty}^\infty f(y) \ d y  = 1
\]
as is required of any pdf.  The Normal pdf has the symmetric, mound-shaped (or ``bell-shaped'') form:  here $\mu=10$ and $\sigma=5$:

<<chunk1,fig.height=5, fig.width=8>>=
mu<-10
sigma<-5
y<-seq(-20,40,by=0.01)
f.y<-dnorm(y,mu,sigma)
par(mar=c(4,4,2,2))
plot(y,f.y,type='l',ylab='f(y)')
@
For different choices of $\mu$ and $\sigma$, we get a different pdf:  here $\mu=-2$ and $\sigma=0.1$:
<<chunk2,fig.height=5, fig.width=8>>=
mu<--2
sigma<-0.1
y<-seq(-3,-1,by=0.001)
f.y<-dnorm(y,mu,sigma)
par(mar=c(4,4,2,2))
plot(y,f.y,type='l',ylab='f(y)')
@

Note the change in the vertical and horizontal scales:  we can see that the pdf is symmetric around $\mu$, and $\sigma$ controls the amount of spread of the distribution.  In fact, it can be shown that for this distribution, we have for the expectation and variance
\[
E(Y) = \mu \qquad Var(Y) = \sigma^2.
\]

\textbf{Note:}  We can see from the formula and the second plot that $f(y) > 0$; however, we also see that the pdf can exceed 1; the maximum of the pdf occurs at $y=\mu$, and takes the value
\[
\left( \frac{1}{2 \pi \sigma^2} \right)^{1/2}
\]
which varies as $\sigma$ varies; for $\sigma=\Sexpr{sigma}$, we have that the maximum value is $\Sexpr{myrnd(dnorm(0,0,sigma),4)}$.  This confirms the fact that the pdf does \textbf{not} return probabilities (i.e. $f(y)$ \textbf{does not define} $P(Y=y)$).


\pagebreak

To obtain \textbf{cumulative probabilities} from the pdf, we need to integrate:  we have that
\[
F(y) = P(Y \leq y) = \int_{-\infty}^y f(t) \ d t = \int_{-\infty}^y \left( \frac{1}{2 \pi \sigma^2} \right)^{1/2} \exp \left\{ - \frac{1}{2 \sigma^2} (t - \mu)^2 \right\} \ dt
\]
where $t$ is being used here as the dummy/integrating variable.  This probability $P(Y \leq y)$ is therefore the area under the pdf from $-\infty$ up to the numerical value $y$.  

\medskip
The probability cannot be computed analytically, but can be computed numerically: here we compute $P(Y \leq 15)$ by integrating the pdf up to 15 (the gray area under the curve):
<<chunk3,fig.height=5.25, fig.width=8>>=
mu<-10
sigma<-5
y<-seq(-20,40,by=0.01)
f.y<-dnorm(y,mu,sigma)
par(mar=c(4,4,2,2))
plot(y,f.y,type='l',ylab='f(y)')
id<-which.min(abs(y-15))
polygon(c(y[1:id],rev(y[1:id])),c(0*f.y[1:id],rev(f.y[1:id])),col='gray')
@
The function \texttt{pnorm} allows us to compute this value
<<chunk4,fig.height=5, fig.width=8>>=
prob<-pnorm(15,10,5)
prob
@
so the probability is $\Sexpr{myrnd(prob,4)}$.

\bigskip

We can plot the cumulative distribution function (cdf), $F(y)$, as $y$ varies:
<<chunk5,fig.height=5, fig.width=8>>=
F.y<-pnorm(y,mu,sigma)
par(mar=c(4,4,3,2))
plot(y,F.y,type='l',ylab='F(y)', main='CDF of Normal(10,25)')
@

An important result allows use to compute these cumulative probabilities for arbitrary $\mu$ and $\sigma$ by referring to the \textbf{standard} case, where $\mu=0$ and $\sigma=1$.  Here is the pdf in this case
<<chunk6,fig.height=5, fig.width=8>>=
mu<--0
sigma<-1
y<-seq(-3.5,3.5,by=0.001)
f.y<-dnorm(y,mu,sigma)
par(mar=c(4,4,2,2))
plot(y,f.y,type='l',ylab='f(y)',main='Standard Normal pdf')
F.y<-pnorm(y,mu,sigma)
par(mar=c(4,4,3,2))
plot(y,F.y,type='l',ylab='F(y)', main='CDF of Normal(0,1)')
@
We have that if $Y \sim Normal(\mu,\sigma^2)$, then the transformed random variable
\[
Z = \frac{Y - \mu}{\sigma} \sim Normal(0,1)
\]
and so for any numerical value $y$
\[
P(Y \leq y) = P\left(Z \leq \frac{y - \mu}{\sigma} \right).
\]
The cdf of $Z$ is often written $\Phi(\cdot)$, and so we may write
\[
F(y) = \Phi\left( \frac{y - \mu}{\sigma} \right).
\]
Therefore we only need to know the values of $\Phi(z)$ as $z$ varies to compute all cumulative probabilities for the Normal distribution.  Note also that the standard Normal pdf is \textbf{symmetric} about zero, so we have that for $z < 0$
\[
\Phi(z) = P(Z \leq z) = P(Z > -z) = 1-\Phi(-z).
\]

\bigskip


\textbf{Examples}:

\begin{enumerate}[label=(\alph*)]

<<chunk7a,fig.height=5, fig.width=8,echo=FALSE>>=
mu<-5
sigma<-2
y<-4.5
z<-(y-mu)/sigma
@

\item $\mu = 5, \sigma = 2$: if $y = 4.5$, we first compute
\[
z = \frac{y - \mu}{\sigma} = \frac{\Sexpr{y} - \Sexpr{mu}}{\Sexpr{sigma}} = \Sexpr{myrnd(z,2)}
\]
and then use $\texttt{pnorm}(z) = \texttt{pnorm(\Sexpr{z})}$ to compute that
\[
P(Y \leq \Sexpr{y}) = P(Z \leq \Sexpr{z}) = \texttt{pnorm(\Sexpr{z})} = \Sexpr{myrnd(pnorm(z),5)}.
\]

<<chunk7b,fig.height=5, fig.width=8,echo=FALSE>>=
mu<--2
sigma<-4
y<-1
z<-(y-mu)/sigma
@

\item $\mu = -2, \sigma = 4$: if $y = 1$, we first compute
\[
z = \frac{y - \mu}{\sigma} = \frac{\Sexpr{y} - (\Sexpr{mu})}{\Sexpr{sigma}} = \Sexpr{myrnd(z,5)}
\]
and then use $\texttt{pnorm}(z) = \texttt{pnorm(\Sexpr{z})}$ to compute that
\[
P(Y \leq \Sexpr{y}) = P(Z \leq \Sexpr{z}) = \texttt{pnorm(\Sexpr{z})} = \Sexpr{myrnd(pnorm(z),5)}.
\]

<<chunk7c,fig.height=5, fig.width=8,echo=FALSE>>=
mu<-20
sigma<-3
y<-25
z<-(y-mu)/sigma
@

\item $\mu = 20, \sigma = 3$: if $y = 25$, we first compute
\[
z = \frac{y - \mu}{\sigma} = \frac{\Sexpr{y} - \Sexpr{mu}}{\Sexpr{sigma}} = \Sexpr{myrnd(z,5)}
\]
and then use $\texttt{pnorm}(z) = \texttt{pnorm(\Sexpr{z})}$ to compute that
\[
P(Y \leq \Sexpr{y}) = P(Z \leq \Sexpr{z}) = \texttt{pnorm(\Sexpr{z})} = \Sexpr{myrnd(pnorm(z),5)}.
\]
\end{enumerate}

\pagebreak

\textbf{Normal tables:}  We can tabulate the standard Normal cdf using \texttt{R}.  
<<chunk8,fig.height=5, fig.width=8,echo=FALSE,size='small'>>=
Phi.mat<-round(pnorm(outer(seq(0.0,3.5,by=0.1),seq(0.0,0.09,by=0.01),'+')),4)
rownames(Phi.mat)<-myrnd(seq(0.0,3.5,by=0.1),1)
colnames(Phi.mat)<-myrnd(seq(0.0,0.09,by=0.01),2)
Phi.mat
@

This table gives the standard Normal cdf, 
\[
\Phi(z) = P(Z \leq z)
\]
for $z$ between 0 and 3.5, on a grid of values.    Due to the symmetry of the standard Normal pdf around zero, we only need to tabulate for $z > 0$, as for $z < 0$
\[
\Phi(z) = P(Z \leq z) = P(Z > -z) = 1 - P(Z \leq -z)  = 1 -\Phi(-z).
\]

\pagebreak

\textbf{Examples}:

\begin{enumerate}[label=(\alph*)]

<<chunk9a,fig.height=5, fig.width=8,echo=FALSE>>=
mu<-5
sigma<-2
y<-4.5
z<-(y-mu)/sigma
@

\item $\mu = 5, \sigma = 2$: if $y = 4.5$, we first compute
\[
z = \frac{y - \mu}{\sigma} = \frac{\Sexpr{y} - \Sexpr{mu}}{\Sexpr{sigma}} = \Sexpr{myrnd(z,2)}
\]
For $z < 0$
\[
\Phi(z) = P(Z \leq z) = P(Z > -z) = 1 - P(Z \leq -z).
\]
Here $z = \Sexpr{z}$, so we 
\begin{itemize}
  \item find the \textbf{row} corresponding to 0.2
  \item find the \textbf{column} corresponding to 0.05
\end{itemize}
and we find the entry 0.5987.  Thus
\[
P(Z \leq 0.25) = 0.5987
\]
and hence using the symmetry result, we have
\[
P(Y \leq 4.5) = P(Z \leq -0.25) = 1 - P(Z \leq 0.25)  = 1 - 0.5987 = \Sexpr{myrnd(1-0.5987,4)}.
\]


<<chunk9b,fig.height=5, fig.width=8,echo=FALSE>>=
mu<--2
sigma<-4
y<-1
z<-(y-mu)/sigma
@

\item $\mu = -2, \sigma = 4$: if $y = 1$, we first compute
\[
z = \frac{y - \mu}{\sigma} = \frac{\Sexpr{y} - (\Sexpr{mu})}{\Sexpr{sigma}} = \Sexpr{myrnd(z,5)}.
\]
Here $z = \Sexpr{z}$, so we 
\begin{itemize}
  \item find the \textbf{row} corresponding to 0.7
  \item find the \textbf{column} corresponding to 0.05
\end{itemize}
and we find the entry 0.7734.  Thus
\[
P(Y \leq 1) = P(Z \leq 0.75) = 0.7734
\]


<<chunk9c,fig.height=5, fig.width=8,echo=FALSE>>=
mu<-20
sigma<-3
y<-25
z<-(y-mu)/sigma
@

\item $\mu = 20, \sigma = 3$: if $y = 25$, we first compute
\[
z = \frac{y - \mu}{\sigma} = \frac{\Sexpr{y} - \Sexpr{mu}}{\Sexpr{sigma}} = \Sexpr{myrnd(z,5)}.
\]
Here $z = \Sexpr{myrnd(z,5)}$, so we must approximate and take $z = \Sexpr{myrnd(z,2)}$
\begin{itemize}
  \item find the \textbf{row} corresponding to 1.6
  \item find the \textbf{column} corresponding to 0.07, giving 
\end{itemize}
and we find the entry 0.9525.  Thus
\[
P(Y \leq 25) = P(Z \leq 1.66667) \bumpeq 0.9525.
\]
This is the best approximation we can obtain from the table.
\end{enumerate}

\end{document} 