I observed a strange behavior of the 2 sample Kolmogorov - Smirnov test in Matlab and I'm not sure if I missing the obvious or if there is indeed an issue with implementation of the test?
Below is a code example. I estimate the power of the test (counting how often H_0 is rejected) as a function of the number of observations (from 10 to 50). I take the observations from two normal distributions which are shifted by 1.5 units in their mean and then apply the built-in KS-Test ("kstest2").
The resulting figure doesn't look right, but the power of the test depends a lot on the sample size in a non-trivial sense. For example: With 14 observations the rejection rate is 51%, with 15 observations it is 38% and with 16 observations it is again 47%. This issue remains when I increase the sample size and I suspect it is related how the p-values are approximated within the kstest2 function. However, according to the Matlab description, the asymptotic assumption should be good for n > 8.
Do yo have any ideas on this?
n_samples = 10000;
n_obs = 10:50;
h_vec = NaN(length(n_obs),n_samples);
for effi = 1:length(n_obs)
for casi = 1:n_samples
vec1 = normrnd(0,1,n_obs(effi),1);
vec2 = normrnd(1,1.5,n_obs(effi),1);
h_KS = kstest2(vec1,vec2); % KS Test
h_vec(effi,casi) = h_KS;
end
end
plot(n_obs,100*sum(h_vec,2)./n_samples)