% Cramer-von Mises and Watson Tests
clear all
alpha=0.05
load('C:\STAT\Goodness\Goodnessdat\diasi.dat')
x = sort(diasi);
n= length(x);
%model X ~ Ga(3, 3/10), 3/10 rate parameter.
 y=gamcdf(x', 3, 10/3); %10/3 scale parameter
%y=chi2cdf(x', 10);     %10 mean
i1n = ( 2*(1:n) -1 )/(2 * n);
% Cramer-von Mises W2 
w2 = 1/(12 * n) + sum( (y - i1n).^2 )                                           %0.1793
a1 = (48 * n^2 - 137 * n + 87)/(30*(32*n^2 - 61 * n + 30))    %0.0498
b1= (32 *n^2 - 61 * n + 30)/(84*n*(4*n-3))                              %0.0948
df1 = (98*n*(4*n - 3)^3)/(5*(32 * n^2 - 61*n + 30)^2)          %1.2333
w2crit = a1 + b1* chi2inv(1-alpha, df1)                                     %0.4662
chi2 = (w2-a1)/b1                                                                         %1.3667                                    
pval11 =  1-chi2cdf(chi2, df1)                                                    %0.3056
pval12 = 1-cvmcdf(w2)                                                               %0.3114
%insens.m continued...Watson U2
 u2=w2 - n*(mean(y)-1/2)^2                                                      %0.1134
   %or directly
 u2 = sum( (y - i1n - mean(y) + 1/2).^2 )+ 1/(12 * n)             %0.1134
           a2= (3*n - 8)/(60*(2*n - 3))                                             %0.0249
           b2=(2*n - 3)/(84*n)                                                          %0.0237
           df2 = 49 * n * (n-1)/(5*(2*n -3)^2)                                %2.4713
    % 
     u2crit = a2 + b2* chi2inv(1-alpha, df2)                              % 0.1875
     chi2 = (u2-a2)/b2                                                                  %3.7413
     pval2 = 1-chi2cdf(chi2, df2)                                                % 0.2152
    
    