close all force
clear all
  s = RandStream('mt19937ar','Seed',0);
RandStream.setGlobalStream(s);
 
n=150;
pvalmoran =[]; pvaldarling=[]; kimball=[]; pvalks=[];
for i = 1:1000
x =   trnd(2,[n 1]);
y=normcdf(x,0,1); 
yy =[0; sort(y); 1];
% MORAN
Sd = sum(diff(yy,1).^2); 
M  = sqrt(n)*(n * Sd/2 - 1);%Moran's Stat
p = 1-normcdf(M);
pvalmoran = [pvalmoran p];
% KOLMOGOROV TEST
[h pv dn] = kstest(x, [x normcdf(x, 0, 1)]);
pvalks = [pvalks pv];
% KIMBALL
  vec=[];
 range=1.1:0.01:3;
  for alpha = range
  Sd = sum(diff(yy,1).^alpha); 
  muna = gamma(alpha+1)/(n^(alpha-1));
  s2na= 1/(n^(2*alpha - 1)) * (gamma(2 * alpha+1)-...
       (alpha^2+1)*(gamma(alpha + 1))^2);
Kna  =  (Sd - muna)/sqrt(s2na);% 
p = 1-normcdf(Kna);
vec = [vec  p];
  end
  kimball=[kimball; vec];
end

sum(pvalmoran < 0.05)  %201
sum(pvalks < 0.05)         %116
figure(1)
plot(range, median(kimball),'-')
hold on
plot(range, median(pvalmoran) * ones(size(range)),'r-')
plot(range, median(pvalks) * ones(size(range)),'g-')
legend('Kimball','Moran','Kolmogorov','location','northwest')

figure(2)
bihist(pvalmoran, pvalks, 40)
 