\documentclass[12pt]{article}

\usepackage{Math203}

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



\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{Large sample testing consistency}

\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

For a two sample problem, to test
\begin{align*}
  H_0  & : \mu_1 = \mu_2 \\
  H_a  & : \mu_1 \neq \mu_2
\end{align*}
we compare the observed value of test statistic
\[
T = \dfrac{\overline{X} - \overline{Y}}{\sqrt{\dfrac{S_1^2}{m} + \dfrac{S_2^2}{n}}}
\]
with the Rejection Region
\[
\{|T| \geq z_{\alpha/2}\} = \{ T \leq -z_{\alpha/2}\} \cup \{ T \geq z_{\alpha/2}\}
\]

If $H_0$ is \textbf{true}, we see in the plot of the average rejection rate over $N=10000$ replicate data sets that as the sample size grows, the probability with which we reject $H_0$ converges to $\alpha$.  This is by design, as $\alpha$ is the probability of a Type 1 error.  With $\alpha=0.05$, the Rejection Region is
\[
\{|T| \geq 1.96\} 
\]

<<chunk1,echo=FALSE,fig.height=4.75,cache=TRUE>>=
N<-10000
n<-seq(10,1000,by=10)
Rej<-rep(0,length(n))
alpha<-0.05
crit<-qnorm(1-alpha/2)
sim.test<-function(nv,cval){
    xbar<-rnorm(1,0,1/sqrt(nv))
    ybar<-rnorm(1,0,1/sqrt(nv))
    s1sq<-rgamma(1,(nv-1)/2,1/2)/(nv-1)
    s2sq<-rgamma(1,(nv-1)/2,1/2)/(nv-1)
    tobs<-(xbar-ybar)/sqrt(s1sq/nv+s2sq/nv)
    res<-ifelse(abs(tobs) >= cval,1,0)
    return(res)
}
for(i in 1:length(n)){
    Rej[i]<-mean(replicate(N,sim.test(n[i],crit)))
}
par(mar=c(4,4,1,2))
plot(n,Rej,type='l',ylim=range(0,0.1),xlab='Sample size',ylab='Rejection Rate')
abline(h=alpha,lty=2,col='red')
@

However, if $H_0$ is not true, we will eventually reject the null hypothesis in favour of the alternative.  In the simulation, $\mu_1 = 0$ and $\mu_2 = 0.25$, and $\sigma_1 = \sigma_2 = 1$, with the data Normally distributed.  For simplicity, the two sample sizes are equal, with $m=n$ taking values between 10 and 1000.

<<chunk2,echo=FALSE,fig.height=4.75,cache=TRUE>>=
N<-10000
n<-seq(10,1000,by=10)
Rej<-rep(0,length(n))
alpha<-0.05
crit<-qnorm(1-alpha/2)
sim.test<-function(nv,cval){
    xbar<-rnorm(1,0,1/sqrt(nv))
    ybar<-rnorm(1,0.25,1/sqrt(nv))
    s1sq<-rgamma(1,(nv-1)/2,1/2)/(nv-1)
    s2sq<-rgamma(1,(nv-1)/2,1/2)/(nv-1)
    tobs<-(xbar-ybar)/sqrt(s1sq/nv+s2sq/nv)
    res<-ifelse(abs(tobs) >= cval,1,0)
    return(res)
}
for(i in 1:length(n)){
    Rej[i]<-mean(replicate(N,sim.test(n[i],crit)))
}
par(mar=c(4,4,2,2))
plot(n,Rej,type='l',ylim=range(0,1),xlab='Sample size',ylab='Rejection Rate')
abline(h=alpha,lty=2,col='red')
@

In this problem, we have in reality that
\[
\Xbar - \Ybar \sim Normal\left(\mu_1 - \mu_2, \left (\frac{1}{m}+\frac{1}{n}  \right)\right) = Normal\left(0.25, \frac{2}{n} \right).
\]
as $m=n$.  Thus, using the z-transform approach, we have
\[
Z \sim \frac{(\Xbar - \Ybar)-0.25}{\sqrt{2/n}} = \sqrt{\frac{n}{2}} \left( (\Xbar - \Ybar)-0.25 \right) \sim Normal(0,1).
\]
However, the null hypothesis assumes (at least approximately) that
\[
\Xbar - \Ybar \sim Normal\left(\mu_1 - \mu_2, \left (\frac{1}{m}+\frac{1}{n}  \right)\right) = Normal\left(0, \frac{2}{n} \right).
\]
so that
\[
T = \frac{(\Xbar - \Ybar)}{\sqrt{1/m+1/n}} = \sqrt{\frac{n}{2}}  (\Xbar - \Ybar) \sim Normal(0,1)
\]
so we have the relation
\[
Z = T - 0.25 \sqrt{\frac{n}{2}} 
\]
which shows that $Z$ becomes arbitrarily large and negative as $n$ grows, and so eventually it falls into the Rejection Region.
\end{document} 