function moments =moment_test(X)
%--------------MATLAB 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.
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_ind=randi(n,1,B*nn);
y=X(y_ind);
%y=datasample(X,B*nn,'Replace',true);
Y=reshape(y,[B,nn]);
mu_star=mean(Y,2);
MU=mean(X);
MM=double(mu_star>xi*MU);
moments=mean(MM); %p-value - reject the hypothesis that the first moment exhist if it is < significance level, accept otherwise
end