#prva statistika
Tn<-function(x){
  x<-sort(x)
  n<-length(x)
  s<-0
  for(i in 1:n){
    s<-s+2*(2*(n-i)+1)*x[i]-sum(x+x[i])/2
  }
  return(s/(mean(x)*n^2))
}
#druga statistika
Mn<-function(x){
  s<-0
  n<-length(x)
  x=sort(x)/mean(x)
  s<-2*sum((2*(n-(1:n))+1)*x)/n^2-mean(x)
  return(2*s^2)
}

n<-20
#niz vrednosti test statistike pri nultoj hipotezi
ts0T<-rep(0,10000)
for(i in 1:10000){
  x<-rexp(n)
  ts0T[i]<-Tn(x)
}
#niz vrednosti test statistike pri nultoj hipotezi ali sa drugim parametrom
ts1T<-rep(0,10000)
for(i in 1:10000){
  x<-rexp(n,2)
  ts1T[i]<-Tn(x)
}
#posto je statistika slobodna od raspodele ovo ce dati nivo znacajnosti (mogao je drugi niz da bude ponovo iz eksponencijalne sa parametrom 1)
1-ecdf(ts1T)(quantile(ts0T,0.95))
#niz vrednosti test statistike pri alternativnoj hipotezi
ts1eT<-rep(0,10000)
for(i in 1:10000){
  x<-rgamma(n, 2,1)
  ts1eT[i]<-Tn(x)
}
#moc testa 
1-ecdf(ts1eT)(quantile(ts0T,0.95))
#sve isto za drugu statistiku
ts0M<-rep(0,10000)
for(i in 1:10000){
  x<-rexp(n)
  ts0M[i]<-Mn(x)
}
ts1M<-rep(0,10000)
for(i in 1:10000){
  x<-rexp(n,2)
  ts1M[i]<-Mn(x)
}
1-ecdf(ts1M)(quantile(ts0M,0.95))

ts1eM<-rep(0,10000)
for(i in 1:10000){
  x<-rgamma(n,2,1)
  ts1eM[i]<-Mn(x)
}
1-ecdf(ts1eM)(quantile(ts0M,0.95))