% Watson Criterion
clear all
alpha=0.05
S=0;
pvals=[]
B=100000
for i=1:B
xx=normrnd(10,5, [1 500]);
x=sort(xx);
n= length(x);
y=normcdf(x, 10, 5);
    %0.1151    0.2743    0.3446    0.4207    0.6554    0.9452  0.9641    0.9861    0.9987    1.0000
    ii=1:n;
    iii = ( 2*ii -1)/(2 * n);
    u2 = sum( (y - iii - mean(y) + 1/2).^2 )+ 1/(12 * n);
    u2pr = (u2 - 0.1/n + 0.1/n^2)*(1+0.8/n);
    a=(21 * n -56)/(840*(n-3/2));
    b=(n-3/2)/(42 * n);  
    df = 49 * n * (n-1)/( 20 * (n-3/2)^2 );
    % 
    
    chi2 = (u2-a)/b;
     chi2pr = (u2pr-a)/b;
    
    % u2
     u2crit = a + b* chi2inv(1-alpha, df)  ;
     pvals=[pvals 1-chi2cdf(chi2pr, df)];
     S=S+ (u2 > u2crit);
     end
    S=S/B
    close all
    hist(pvals, 30)
    