\documentclass[12pt]{article}

\usepackage{Math203}

\usepackage{amsmath,amssymb}
\usepackage{hyperref}
\usepackage{color}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{xcolor}
\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$}

\def\ctitle{Math 203: Principles of Statistics 1}
\def\ttitle{Fall 2024}
\def\stitle{Hypothesis Testing: One Sample Problems}

\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 will test hypotheses regarding the parameters $\mu$ or $p$ :  for the \textbf{null hypothesis}
\[
H_{0}: \mu=\mu_{0} \quad \text { or } \quad H_{0}: p=p_{0},
\]
and for the \textbf{alternative hypothesis}, one of
\begin{align*}
& H_{a}: \mu>\mu_{0} \quad \text { or } \quad H_{a}: p>p_{0} \\
& H_{a}: \mu<\mu_{0} \quad \text { or } \quad H_{a}: p<p_{0} \\
& H_{a}: \mu \neq \mu_{0} \quad \text { or } \quad H_{a}: p \neq p_{0}
\end{align*}
In this specification, $\mu_0$ and $p_0$ represent specific \textbf{numerical} values that represent the hypothesized null values of the data generating parameters.   The steps are

\begin{enumerate}[label=\arabic*.,itemsep=0in]
\item Determine $H_{0}$ and $H_{a}$ from the question (null hypothesis and alternative hypothesis)

\item Determine an appropriate \textbf{test statistic} $T$:  $T$ is a random variable whose value is determined by the individual sample random variables.

\item Identify the \textbf{null distribution} of $T$ assuming $H_0$ is true.

\item Determine the \textbf{Rejection Region}: this will be a set of possible values of the Test Statistic $T$ for which we will reject $H_{0}$ in favour of $H_{a}$

\item Compute the \textbf{observed value}, $T_{obs}$, of $T$ using the observed data

\item Compare the observed value of the $T$ to the Rejection Region

\item Decide and conclude \textbf{``Reject $H_0$ in favour of $H_a$''} or \textbf{``Do not reject $H_0$''}

\end{enumerate}

\bigskip

We pre-specify a level of the test, $\alpha$, to determine the Rejection Region.  We have that $\alpha$ is the \textbf{probability of a Type 1 error}
\[
\alpha = P_0(\textrm{$T$ lies in Rejection Region})
\]
where $P_0$ indicates that this probability is computed \textbf{using the null distribution}.

\bigskip
 
Key quantities in setting up the test are

\begin{itemize}
  \item $\overline{X}$: sample mean random variable
  \item $S^2$: sample variance random variable
  \[
  S^2 = \frac{1}{n-1} \sum_{i=1}^n (X_i - \overline{X})^2
  \]

  \item $t_\nu$: $t$-distribution with $\nu$ degrees of freedom

  \item $z_a$: upper $a$ point of the standard Normal distribution

  \item $t_{a,\nu}$: upper $a$ point of the $t$-distribution with $\nu$ degrees of freedom

\end{itemize}
and the key quantities to complete the test are the \textbf{sample quantities} $\xbar$ and $s^2$.  

\pagebreak

<<chunk1,fig.height=5, fig.width=8,echo=FALSE,size='small'>>=
set.seed(34)
n<-15
mu<-6.6
sig<-2.0
x<-rnorm(n,mu,sig)
x<-round(x,2)
alph<-0.05
xbar<-mean(x)
sval<-sd(x)
tobs<-(xbar-6)/(sval/sqrt(n))
@

\textbf{Example:} A random sample of size $n=\Sexpr{n<-15;n}$ is available from a Normal population.   We wish to test the hypotheses
\begin{align*}
  H_0 & : \mu = 6.0 \\
  H_a & : \mu > 6.0
\end{align*}
with $\alpha = 0.05$.  The observed data are
\[
\Sexpr{myrnd(x,2)}
\]
Here we are in Case 1, and the test statistic is
\[
T = \frac{\Xbar - \mu_0}{S/\sqrt{n}}
\]
with $\mu_0 = 6.0$.  Assuming that $H_0$ is true, we have that the null distribution has $T \sim t_{n-1} = t_{\Sexpr{n-1}}$.  The upper $\alpha=0.05$ point of the $t_{\Sexpr{n-1}}$ distribution is $t_{\Sexpr{alph},\Sexpr{n-1}} = \Sexpr{myrnd(qt(1-alph,n-1),4)}$.  Therefore
\[
\text{Rejection Region: } \quad \{T \geq \Sexpr{myrnd(qt(1-alph,n-1),4)}\}.
\]
We compute the observed version of the test statistic: the summary statistics are (carrying full precision) $\xbar = \Sexpr{mean(x)}$ and $s = \Sexpr{sd(x)}$, so the observed value of the test statistic is
\[
T_{obs} = \frac{\xbar - \mu_0}{s/\sqrt{n}} = \Sexpr{tobs}.
\]
We have that $T_{obs} > \Sexpr{myrnd(qt(1-alph,n-1),4)}$, $T_{obs}$ lies in the Rejection Region, and so we \textbf{reject} $H_0$ in favour of $H_a$ at $\alpha = \Sexpr{alph}$.  

<<chunk2,fig.height=4.0, fig.width=8,echo=FALSE,size='small'>>=
xv<-seq(-4,4,by=0.01)
yv<-dt(xv,n-1)
rrx<-seq(qt(1-alph,n-1),4,by=0.01)
rry<-dt(rrx,n-1)
par(mar=c(4,4,1,0))
plot(xv,yv,type='l',xlab=expression(x),ylab=expression(paste(t[14],' pdf')),ylim=range(0,0.5))
polygon(c(rrx,rev(rrx)),c(0*rry,rev(rry)),col='red')
points(tobs,0,pch=17,cex=1.1)
abline(v=qt(1-alph,n-1),lty=2)
text(qt(1-alph,n-1)+0.02,0.4,expression(paste(t[paste(0.05, ",", 14)],'=')),adj=0)
text(qt(1-alph,n-1)+0.7,0.40,round(qt(1-alph,n-1),4),cex=0.9,adj=0)

arrows(3,0.2,tobs+0.05,0.01,angle=10)
text(3,0.22,expression(paste(T[obs],'=')))
text(3.3,0.22,round(tobs,4),cex=0.9,adj=0)
@

Finally, we can compute the $p$-value using the cdf function for the t-distribution, \texttt{pt}, as
\[
P_0(T \geq T_{obs}) = P_0(T \geq \Sexpr{tobs}) = 1 - \texttt{pt}(\Sexpr{tobs},\Sexpr{n-1}) = 1 - \Sexpr{pt(tobs,n-1)} = \Sexpr{1-pt(tobs,n-1)}.
\]
If we only had access to the t-distribution table, then we can observe that for the $t_{\Sexpr{n-1}}$ distribution, the $a=0.025$ and $a=0.02$ upper points are 2.1448 and 2.2638 respectively, so the $p$-value is between 0.025 and 0.02.

<<chunk3,fig.height=5, fig.width=8,echo=FALSE,size='small'>>=
set.seed(34)
n<-83
mu<-9.45
sig<-3.0
x<-rnorm(n,mu,sig)
alph<-0.01
xbar<-round(mean(x),3)
sval<-round(sd(x),3)
tobs<-(xbar-10)/(sval/sqrt(n))
@ 

\textbf{Example:} A random sample of size $n=\Sexpr{n}$ is available, and the summary statistics are recorded as $\xbar = \Sexpr{myrnd(xbar,3)}$ and $s = \Sexpr{myrnd(sval,3)}$.  We wish to test the hypotheses
\begin{align*}
  H_0 & : \mu = 10.0 \\
  H_a & : \mu < 10.0
\end{align*}
with $\alpha = 0.01$.  Here we are in Case 3, and the test statistic is
\[
T = \frac{\Xbar - \mu_0}{S/\sqrt{n}}
\]
with $\mu_0 = 10.0$.  Assuming that $H_0$ is true, we have that the null distribution is given (approximately) by the Central Limit Theorem as $T \sim Normal(0,1)$ .  The upper $\alpha=\Sexpr{alph}$ point of the $Normal(0,1)$ distribution is $z_{\Sexpr{alph}} = \Sexpr{myrnd(qnorm(1-alph),4)}$.  Therefore 
\[
\text{Rejection Region: } \quad \{T \leq -\Sexpr{myrnd(qnorm(1-alph),4)}\}.
\]
We compute the observed version of the test statistic: 
\[
T_{obs} = \frac{\xbar - \mu_0}{s/\sqrt{n}} = \Sexpr{tobs}.
\]
We do \textbf{not} have that $T_{obs} \leq -\Sexpr{myrnd(qnorm(1-alph),4)}$, $T_{obs}$ \textbf{does not} lie in the Rejection Region, and so we \textbf{do not reject} $H_0$ in favour of $H_a$ at $\alpha = \Sexpr{alph}$.  

<<chunk4,fig.height=4.5, fig.width=8,echo=FALSE,size='small'>>=
xv<-seq(-4,4,by=0.01)
yv<-dnorm(xv)
rrx<-seq(-4,-qnorm(1-alph),by=0.01)
rry<-dnorm(rrx)
par(mar=c(4,4,1,0))
plot(xv,yv,type='l',xlab=expression(x),ylab='Standard Normal pdf',ylim=range(0,0.5))
polygon(c(rrx,rev(rrx)),c(0*rry,rev(rry)),col='red')
points(tobs,0,pch=17,cex=1.1)
abline(v=-qnorm(1-alph),lty=2)
text(-qnorm(1-alph)+0.02,0.4,expression(paste(z[0.01],'=')),adj=0)
text(-qnorm(1-alph)+0.55,0.4,round(-qnorm(1-alph),4),cex=0.9,adj=0)

arrows(1,0.425,tobs+0.05,0.01,angle=10)
text(1,0.45,expression(paste(T[obs],'=')))
text(1.25,0.45,round(tobs,4),cex=0.9,adj=0)

@

Finally, we can compute the $p$-value using the cdf function for the Normal distribution, \texttt{pnorm}, as
\[
P_0(T \leq T_{obs}) = P_0(T \leq \Sexpr{tobs}) = \texttt{pnorm}(\Sexpr{tobs}) = \Sexpr{pnorm(tobs)}.
\]
If we only had access to the Normal distribution table, we could quote the approximate $p$-value by rounding the test statistic to two decimal places
\[
P_0(T \leq \Sexpr{myrnd(tobs,2)}) = 1-P_0(T \leq \Sexpr{myrnd(-tobs,2)}) = 1 - \Sexpr{myrnd(pnorm(round(-tobs,2)),4)} = \Sexpr{myrnd(1-pnorm(round(-tobs,2)),4)}.
\]

<<chunk5,fig.height=5, fig.width=8,echo=FALSE,size='small'>>=
set.seed(304)
n<-225
p<-0.30
p0<-0.25
x<-rbinom(1,n,p)
alph<-0.05
phat<-x/n
sval<-sqrt(p0*(1-p0)/n)
tobs<-(phat-p0)/sval
@ 

\textbf{Example:} A random Bernoulli sample of size $n=\Sexpr{n}$ is available, and we observe $x=\Sexpr{x}$ successes, so that we have the point \textbf{estimate} $\widehat{p} = \Sexpr{phat}$. We wish to test the hypotheses
\begin{align*}
  H_0 & : p = \Sexpr{p0} \\
  H_a & : p \neq \Sexpr{p0}
\end{align*}
with $\alpha = \Sexpr{alph}$.  Here we are in Case 4, and the test statistic is
\[
T = \dfrac{\widehat{p} - p_0}{\sqrt{\dfrac{p_0 (1-p_0)}{n}}} = \dfrac{\widehat{p} - \Sexpr{p0}}{\sqrt{\dfrac{\Sexpr{p0} \times \Sexpr{1-p0}}{\Sexpr{n}}}}
\]
where $\widehat{p}$ here is the \textbf{estimator}, $\widehat{p} = X/n$, where $X$ is the random variable recording the total number of successes.  Assuming that $H_0$ is true, we have that the null distribution is given (approximately) by the Central Limit Theorem as $T \sim Normal(0,1)$. 

\medskip

For the two-sided alternative hypothesis, we need the upper $\alpha/2=\Sexpr{alph/2}$ point of the standard Normal distribution, which is $z_{\Sexpr{alph}/2} = \Sexpr{myrnd(qnorm(1-alph/2),4)}$.  Therefore 
\[
\text{Rejection Region: } \quad \{|T| \geq \Sexpr{myrnd(qnorm(1-alph/2),4)}\}.
\]
We compute the observed version of the test statistic: 
\[
T_{obs} = \dfrac{\Sexpr{phat} - \Sexpr{p0}}{\sqrt{\dfrac{\Sexpr{p0} \times \Sexpr{1-p0}}{\Sexpr{n}}}} = \Sexpr{tobs}.
\]
We do \textbf{not} have that $|T_{obs}| \geq \Sexpr{myrnd(qnorm(1-alph/2),4)}$, $T_{obs}$ \textbf{does not} lie in the Rejection Region, and so we \textbf{do not reject} $H_0$ in favour of $H_a$ at $\alpha = \Sexpr{alph}$.  

<<chunk6,fig.height=4.75, fig.width=8,echo=FALSE,size='small'>>=
xv<-seq(-4,4,by=0.01)
yv<-dnorm(xv)
rrx<-seq(-4,-qnorm(1-alph/2),by=0.01)
rry<-dnorm(rrx)
par(mar=c(4,4,1,0))
plot(xv,yv,type='l',xlab=expression(x),ylab='Standard Normal pdf',ylim=range(0,0.5))
polygon(c(rrx,rev(rrx)),c(0*rry,rev(rry)),col='red')
rrx<-seq(qnorm(1-alph/2),4,by=0.01)
rry<-dnorm(rrx)
polygon(c(rrx,rev(rrx)),c(0*rry,rev(rry)),col='red')
points(tobs,0,pch=17,cex=1.1)
abline(v=-qnorm(1-alph/2),lty=2)
abline(v=qnorm(1-alph/2),lty=2)
text(-qnorm(1-alph/2)+0.02,0.4,expression(paste(-z[0.025],'=')),adj=0)
text(-qnorm(1-alph/2)+0.7,0.4,round(-qnorm(1-alph/2),4),cex=0.9,adj=0)
text(qnorm(1-alph/2)+0.02,0.4,expression(paste(z[0.025],'=')),adj=0)
text(qnorm(1-alph/2)+0.6,0.4,round(qnorm(1-alph/2),4),cex=0.9,adj=0)

arrows(1,0.425,tobs,0.02,angle=10)
text(1,0.45,expression(paste(T[obs],'=')))
text(1.25,0.45,round(tobs,4),cex=0.9,adj=0)

@

Finally, we can compute the $p$-value using the cdf function for the Normal distribution, \texttt{pnorm}, as
\begin{align*}
2 P_0(T \geq |T_{obs}|) & = 2 P_0(T \geq \Sexpr{tobs}) \\[6pt]
& = 2 \times (1-P_0(T < \Sexpr{tobs})) \\[6pt]
& = 2 \times (1-\Sexpr{pnorm(tobs)}) \\[6pt]
& = 2 \times (\Sexpr{1-pnorm(tobs)}) \\[6pt]
& = \Sexpr{2*(1-pnorm(tobs))}
\end{align*}
If we only had access to the Normal distribution table, we could quote the approximate $p$-value by rounding the test statistic to two decimal places
\begin{align*}
2 P_0(T \geq |T_{obs}|) & = 2 P_0(T \geq \Sexpr{tobs}) \\[6pt]
& = 2 \times (1-P_0(T < \Sexpr{tobs})) \\[6pt]
& \approx 2 \times (1-P_0(T < \Sexpr{round(tobs,2)})) \\[6pt]
& = 2 \times (1-\Sexpr{round(pnorm(round(tobs,2)),4)}) \\[6pt]
& = 2 \times \Sexpr{1-round(pnorm(round(tobs,2)),4)}\\[6pt]
& = \Sexpr{2*(1-round(pnorm(round(tobs,2)),4))}
\end{align*}


\end{document} 