#--------------R code for testing the existence of the first FINITE MOMENT (FINITE MEAN). #If you test for a fininte second, third, fourth or k-th moment, raise x to the power of 2, 3, 4 or k. # H0: the first moment is finite, H1: the moment is not finite (Not necessarily infinite, it can be undefined) # Cite as Fedotenkov I., (2013) A bootstrap method to test for the existence of finite moments. Journal of Nonparametric Statistics 25(2), p. 315-322. #-------------------------------------------------------------------- #Input: sample as a vector tail<-function(x){ x<-as.vector(x) #Make a vector, if x is in another format xi<-0.999 # Xi, as in the paper n<-length(x) #Number of observations nn<-max(round(0.4*log(n)),2) # Size of bootstrap subsamples as in the paper. Minimal size of the subsample is equal to 2. B<-100000 #Number of bootstrap subsamples y<-sample(x, B*nn, replace = TRUE) Y<-matrix(y,nrow = B,ncol = nn) mu_star<-apply(matrix(Y,nrow = B,ncol = nn),1,mean) MU<-mean(x) MM<-as.numeric(mu_star>xi*MU) cat("p-value: ") #p-value mean(MM) } #---------------------------------------------------------------------- #EXAMPLE x<-abs(rcauchy(100000)) #Test works only for positive numbers tail(x)