\documentclass[12pt]{article}

\usepackage{Math203}

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

\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$}
\renewcommand\hat{\widehat}

\def\ctitle{Math 203: Principles of Statistics 1}
\def\ttitle{Fall 2024}
\def\stitle{Constructing a Confidence Interval}

\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

We seek two random variables
\[
L=L\left(X_{1}, \ldots, X_{n}\right)
\qquad 
\text{and}
\qquad
R=R\left(X_{1}, \ldots, X_{n}\right)
\]
which are functions of the random sample $X_1,\ldots,X_n$ such that for a specified probability $\alpha$
\[
P(L<\theta<R)=1-\alpha.
\]
Both random variables $L\left(X_{1}, \ldots, X_{n}\right)$ and $R\left(X_{1}, \ldots, X_{n}\right)$ have their own (sampling) probability distributions.

\bigskip

The interval $(L, R)$ is called a $100(1-\alpha) \%$ \textbf{confidence interval} (C. I.) for $\theta$.

\begin{itemize}
\item The \textbf{random interval} $(L, R)$ will `cover' the true value $\theta$ with probability $1-\alpha$.

\item When we write $P(L<\theta<R)$, the probability calculation is done with respect to the sampling distribution of $L$ and $R$.

\item Recall that here we are assuming $\theta$ is unknown; we will only see the data, and make assumptions about the distribution of the data.
\end{itemize}

We interpret the interval $(L, R)$ as follows: if we were to carry out the same study infinitely often under the same conditions, and each time compute a $100(1-\alpha) \%$ C.I. using the same procedure, then $100(1-\alpha) \%$ of these intervals will contain $\theta$.

\bigskip

\textbf{Procedure:}  We will study the case where $\theta$ is either 
\begin{itemize}
  \item population mean $\mu$ when the data are Normally distributed
  \item population probability $p$ when the data are Binomially distributed.
\end{itemize}

\medskip

The steps are
\begin{enumerate}[label=\arabic*.]

\item Identify the parameter of interest: either $\mu$ or $p$.

\item Specify the value of $\alpha$: often, we assume $\alpha=0.05$.

\item Identify the experimental setting: the formula for $(L, R)$ depends on the setting.

\item Once we know the setting, we use the given formulas for $L$ and $R$.

\item Substitute the data $x_1,\ldots,x_n$ into the formulas to obtain the observed interval $(l, r)$, where
\[
l = L(x_1,\ldots,x_n) \qquad r = R(x_1,\ldots,x_n).
\]

\end{enumerate}

\pagebreak


To determine the correct formulas, we need to identify

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

  \item the assumed probability model/parameter of interest
  
  \begin{itemize}
    \item Normal, $\mu$
    \item Binomial, $p$
  \end{itemize}

  \item the sample size assumption
  
  \begin{itemize}
    \item $n$ small ($n < 30$)
    \item $n$ large ($n \geq 30$)
  \end{itemize}

  \item the variance assumption for the Normal case

  \begin{itemize}
    \item $\sigma$ known
    \item $\sigma$ unknown
  \end{itemize}  
  
\end{enumerate}

\medskip
We will base confidence intervals on the summary statistic random variables
\[
\Xbar = \frac{1}{n} \sum_{i=1}^n X_i \qquad S^2 = \frac{1}{n-1} \sum_{i=1}^n (X_i - \Xbar)^2
\]
that is, the \textit{sample mean} and \textit{sample variance} random variables.  We will also refer to the \textit{upper $a$ point} of the Normal and (Student) t-distributions:

\begin{itemize}
\item The upper $a$ point of a standard Normal distribution is the point on the horizontal axis, $z_{a}$ such that $P\left(Z>z_{a}\right)=a$.

\item The upper $a$ point of a $t$-distribution with $\nu$ degrees of freedom is the point on the horizontal axis, $t_{a, \nu}$ such that $P\left(T>t_{a, \nu}\right)=a$, where the random variable $T$ has a $t$-distribution with $\nu$ degrees of freedom; $\nu$ depends on the sample size, and here $\nu = n-1$.  The Table below gives the upper $a$ points for a range of $\nu$ values.

\end{itemize}

<<chunk1,echo=FALSE>>=
nu<-c(4:20,25,30,Inf)
pv<-c(0.1,0.05,0.025,0.01,0.001)
amat<-matrix(0,nrow=length(nu),ncol=length(pv))
for(i in 1:length(nu)){
    amat[i,]<-round(qt(1-pv,nu[i]),3)
}
library(kableExtra)
atab<-cbind(nu[1:10],amat[1:10,],nu[11:20],amat[11:20,])
colnames(atab)<-c(" ",as.character(pv),' ',as.character(pv))
kbl(atab, "latex", booktabs = T, caption = "Upper $a$ points of $t_{\\nu}$ distribution") %>%
add_header_above(c("$\\\\nu$" = 1, "$a$" = 5, "$\\\\nu$" = 1, "$a$" = 5), escape=FALSE) %>%
kable_styling(latex_options = c("repeat_header","hold_position"))
@

\pagebreak

\textbf{Case 1:}  Suppose that $X_{1}, \ldots, X_{n}$ are a random sample from a \textbf{Normal} distribution with \textbf{unknown} $\mu$ and $\sigma$, and $n<30$.  A $100(1-\alpha) \%$ confidence interval for $\mu$ is $(L, R)$, where:
\[
L =\overline{X}-t_{\alpha / 2, n-1} \frac{S}{\sqrt{n}} \qquad
R =\overline{X}+t_{\alpha / 2, n-1} \frac{S}{\sqrt{n}}.
\]
Once we have recorded the data, we report the interval
\[
(l,r) = \left(\xbar-t_{\alpha / 2, n-1} \frac{s}{\sqrt{n}},\xbar+t_{\alpha / 2, n-1} \frac{s}{\sqrt{n}}\right).
\]

<<chunk2,echo=FALSE>>=
n<-15
xm<-4.68
sval<-2.31
ci<-matrix(xm,nrow=3,ncol=2)
ci[,1]<-xm-sval*qt(1-c(0.05,0.025,0.005),n-1)/sqrt(n)
ci[,2]<-xm+sval*qt(1-c(0.05,0.025,0.005),n-1)/sqrt(n)
ci<-round(ci,3)
@

\textbf{Example:} Suppose we have a random sample of $n=\Sexpr{n}$ measurements from a Normal population, and observe that $\xbar = \Sexpr{xm}$ and $s = \Sexpr{sval}$. Here $n-1 = 14$, so we can report the following intervals
\begin{table}[ht]
\centering
\begin{tabular}{ccccc}
& $\alpha$ & $t_{\alpha / 2, \Sexpr{n-1}}$ & \multicolumn{2}{c}{C.I.} \\
\hline
90\% & 0.10 & \Sexpr{myrnd(qt(0.95,n-1),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qt(0.95,n-1),3)} \times \Sexpr{sval})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{ci[1,1]}, \Sexpr{ci[1,2]})\\
95\% & 0.05 & \Sexpr{myrnd(qt(0.975,n-1),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qt(0.975,n-1),3)} \times \Sexpr{sval})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{ci[2,1]}, \Sexpr{ci[2,2]})\\
99\% & 0.01 & \Sexpr{myrnd(qt(0.995,n-1),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qt(0.995,n-1),3)} \times \Sexpr{sval})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{ci[3,1]}, \Sexpr{ci[3,2]})
\end{tabular}
\end{table}


\bigskip

\textbf{Case 2: } Suppose that $X_{1}, \ldots, X_{n}$ are a random sample from a Normal distribution with unknown $\mu$, with $\sigma$ \textbf{known}.  A $100(1-\alpha) \%$ confidence interval for $\mu$ is $(L, R)$, where
\[
L =\overline{X}-z_{\alpha / 2} \frac{\sigma}{\sqrt{n}} \qquad
R =\overline{X}+z_{\alpha / 2} \frac{\sigma}{\sqrt{n}}
\]
Here we know that the sampling distribution of $\Xbar \sim Normal(\mu,\sigma^2/n)$, so therefore
\[
Z = \frac{\Xbar - \mu}{\sigma/\sqrt{n}} \sim Normal(0,1).
\]
We know that $z_{\alpha / 2}$ is the upper $\alpha/2$ value for the standard Normal, so therefore, by symmetry of the Normal pdf
\[
P(-z_{\alpha / 2} < Z \leq z_{\alpha / 2}) = 1-\alpha
\qquad \text{i.e.} \qquad
P\left( - z_{\alpha / 2} < \frac{\Xbar - \mu}{\sigma/\sqrt{n}} \leq z_{\alpha / 2}\right) = 1-\alpha.
\]
Rearranging the inequalities, this means that
\[
P(L < \mu \leq R) = P\left( \Xbar - z_{\alpha / 2}\sigma/\sqrt{n} < \mu \leq \Xbar + z_{\alpha / 2}\sigma/\sqrt{n}\right) = 1-\alpha.
\]


\medskip

<<chunk3,echo=FALSE>>=
n<-15
xm<-4.68
sig<-0.5
ci<-matrix(xm,nrow=3,ncol=2)
ci[,1]<-xm-sig*qnorm(1-c(0.05,0.025,0.005))/sqrt(n)
ci[,2]<-xm+sig*qnorm(1-c(0.05,0.025,0.005))/sqrt(n)
ci<-round(ci,3)
@

\textbf{Example:} Suppose we have a random sample of $n=\Sexpr{n}$ measurements from a Normal population with $\sigma=0.5$, and observe that $\xbar = \Sexpr{xm}$.  We can report the following intervals.  
\begin{table}[ht]
\centering
\begin{tabular}{ccccc}
& $\alpha$ & $z_{\alpha / 2}$ & \multicolumn{2}{c}{C.I.} \\
\hline
90\% & 0.10 & \Sexpr{myrnd(qnorm(0.95),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qnorm(0.95),3)} \times \Sexpr{sig})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{ci[1,1]}, \Sexpr{ci[1,2]})\\
95\% & 0.05 & \Sexpr{myrnd(qnorm(0.975),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qnorm(0.975),3)} \times \Sexpr{sig})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{ci[2,1]}, \Sexpr{ci[2,2]})\\
99\% & 0.01 & \Sexpr{myrnd(qnorm(0.995),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qnorm(0.995),3)} \times \Sexpr{sig})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{ci[3,1]}, \Sexpr{ci[3,2]})
\end{tabular}
\end{table}



\textbf{Case 3:} Suppose that $X_{1}, \ldots, X_{n}$ are a random sample from a distribution with unknown $\mu$ and $\sigma$ (normality of the $X$s is not required), where $n \geq 30$. A $100(1-\alpha) \%$ confidence interval for $\mu$ is $(L, R)$, where:
\[
L =\overline{X}-z_{\alpha / 2} \frac{S}{\sqrt{n}} \qquad
R =\overline{X}+z_{\alpha / 2} \frac{S}{\sqrt{n}}
\]
Here by the Central Limit Theorem, we have the approximate result 
\[
\Xbar \approxsim Normal(\mu, \sigma^2/n),
\]
and hence, approximately, as above
\[
P(L < \mu \leq R) = P\left( \Xbar - z_{\alpha / 2}\sigma/\sqrt{n} < \mu \leq \Xbar + z_{\alpha / 2}\sigma/\sqrt{n}\right) = 1-\alpha.
\]
However, we do not know $\sigma$, so we use instead a point estimator of $\sigma$, namely $S$.


\medskip

<<chunk4,echo=FALSE>>=
n<-68
xm<-4.68
sval<-1.6
ci<-matrix(xm,nrow=3,ncol=2)
ci[,1]<-xm-sval*qnorm(1-c(0.05,0.025,0.005))/sqrt(n)
ci[,2]<-xm+sval*qnorm(1-c(0.05,0.025,0.005))/sqrt(n)
ci<-round(ci,3)
@

\textbf{Example:} Suppose we have a random sample of $n=\Sexpr{n}$ measurements from a population and observe that $\xbar = \Sexpr{xm}$ and $s = \Sexpr{sval}$.  We can report the following intervals.  
\begin{table}[ht]
\centering
\begin{tabular}{ccccc}
& $\alpha$ & $z_{\alpha / 2}$ & \multicolumn{2}{c}{C.I.} \\
\hline
90\% & 0.10 & \Sexpr{myrnd(qnorm(0.95),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qnorm(0.95),3)} \times \Sexpr{sval})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{myrnd(ci[1,1],3)}, \Sexpr{myrnd(ci[1,2],3)})\\
95\% & 0.05 & \Sexpr{myrnd(qnorm(0.975),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qnorm(0.975),3)} \times \Sexpr{sval})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{myrnd(ci[2,1],3)}, \Sexpr{myrnd(ci[2,2],3)})\\
99\% & 0.01 & \Sexpr{myrnd(qnorm(0.995),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qnorm(0.995),3)} \times \Sexpr{sval})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{myrnd(ci[3,1],3)}, \Sexpr{myrnd(ci[3,2],3)})
\end{tabular}
\end{table}


\bigskip

\textbf{Case 4:} Let $X_{1}, \ldots, X_{n}$ be a random sample of Bernoulli random variables with unknown probability of success $p$.  A $100(1-\alpha) \%$ confidence interval for $p$ is $(L, R)$, where:
\[
L =\hat{p}-z_{\alpha / 2} \sqrt{\frac{\hat{p}(1-\hat{p})}{n}} \qquad
R =\hat{p}+z_{\alpha / 2} \sqrt{\frac{\hat{p}(1-\hat{p})}{n}}
\]
where
\[
\hat{p}=\frac{\text { Number of ``successes'' in the sample }}{n} = \frac{X}{n}
\]
say.  This applies when $n$ is large enough, that is, when $n \hat{p} \geq 15$ and $n(1-\hat{p}) \geq 15$.  Here by the Central Limit Theorem, we have the approximate result
\[
\Xbar \approxsim Normal(p, p(1-p)/n)
\]
so we can apply the formula for Case 3 with $S$ replaced by the point estimator of $p(1-p)$, that is, $\hat{p} (1-\hat{p})$.

\bigskip

<<chunk5,echo=FALSE>>=
n<-124
x<-72
xm<-round(x/n,4)
sval<-round(xm*(1-xm),4)
ci<-matrix(xm,nrow=3,ncol=2)
ci[,1]<-xm-sqrt(sval)*qnorm(1-c(0.05,0.025,0.005))/sqrt(n)
ci[,2]<-xm+sqrt(sval)*qnorm(1-c(0.05,0.025,0.005))/sqrt(n)
ci<-round(ci,3)
@
\textbf{Example:} Suppose we have a random sample of $n=\Sexpr{n}$ measurements from a Bernoulli population and observe that the total number of successes is $x=\Sexpr{x}$, so that the point estimate of $p$ is $\widehat{p} = \xbar = \Sexpr{xm}$ and $\widehat{p} (1-\widehat{p}) = \Sexpr{sval}$.  We can report the following intervals.  

\begin{table}[ht]
\centering
\begin{tabular}{ccccc}
& $\alpha$ & $z_{\alpha / 2}$ & \multicolumn{2}{c}{C.I.} \\
\hline
90\% & 0.10 & \Sexpr{myrnd(qnorm(0.95),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qnorm(0.95),3)} \times \sqrt{\Sexpr{sval}})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{ci[1,1]}, \Sexpr{ci[1,2]})\\
95\% & 0.05 & \Sexpr{myrnd(qnorm(0.975),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qnorm(0.975),3)} \times \sqrt{\Sexpr{sval}})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{ci[2,1]}, \Sexpr{ci[2,2]})\\
99\% & 0.01 & \Sexpr{myrnd(qnorm(0.995),3)}& $\Sexpr{xm} \pm (\Sexpr{myrnd(qnorm(0.995),3)} \times \sqrt{\Sexpr{sval}})/\sqrt{\Sexpr{n}})$ & = (\Sexpr{ci[3,1]}, \Sexpr{ci[3,2]})
\end{tabular}
\end{table}

\end{document} 

