
## com351c of october 2024
#  with the Vilde Ung stk4011 oppgave kurtosis rather slow to find its aim etc.
#  when data are exponential and the true kurtosis is 6
#  can use the code for data from other distributions 
#  i track the percentage of times \hat\gamma_4 is inside [5.80, 6.20]
#  i write this as \hat\gamma_3 inside truekurt plus-minus eps 
#  and we need enormous n to be sure of this 
#  in this version of my code: data from the exponential only 
#  eps = 0.20: need about 700,000 as Vilde said
#  eps = 0.30: need about 400,000
#  eps = 0.40: need about 150,000
#  eps = 0.50: need about 100,000 

nval <- seq(10^4,8*10^5,by=10^4) # i decide interactively
sim <- 100                       # i decide interactively
kurthat <- 0*(1:sim) 

truekurt <- 6.00 # for exponential data
eps <- 0.20
# truekurt <- 0.00 # for normal data
# where things are better and i can have a smaller eps 

kurt <- function(xx)
{
aux <- (xx - mean(xx))/sd(xx) 
mean(aux^4) -3
}

checkAval <- 0*nval # the mean of \hat\gamma_4
checkBval <- 0*nval # the stdev of \hat\gamma_4
checkEval <- 0*nval # how often is \hat\gamma_4 inside (5.80, 6.20) 
# or rather inside (truekurt - eps, truekurt + eps)

for (j in 1:length(nval))
{
nn <- nval[j]
#
for (ss in 1:sim)
{
yy <- rexp(nn) # or another distribution if i wish 
kurthat[ss] <- kurt(yy)
}
#
checkAval[j] <- mean(kurthat)
checkBval[j] <- sd(kurthat)
aha <- 1*(kurthat <= truekurt + eps)*(kurthat >= truekurt - eps)
checkEval[j] <- mean(aha) 
print(c(nn,checkAval[j],checkBval[j],checkEval[j]))
#
matplot(nval[1:j], checkEval[1:j],type="l")
}

lower <- checkAval - 1.96*checkBval
upper <- checkAval + 1.96*checkBval

matplot(nval,cbind(checkAval,lower,upper),
   col=c(1,2,2),lty=c(1,2,2),type="l",lwd=3)

par(bty="l")
par(cex.lab=1.4142) 

matplot(nval,checkEval,ylim=c(0,1),type="l",
   lwd=3,xlab="sample size n", ylab="slow convergence towards 1")
   
matlines(nval,0.95+0*nval,type="l",lwd=3,lty=2,col=2)
