I calculate the Kolmogrov-samirnov test statistic for the following data:R = {0.171434, 0.134263, 0.155931, 0.135479, 0.196356, 0.152357, 0.133084, 0.10537, 0.14654, 0.116676, 0.123145, 0.145377, 0.12366, 0.156681, 0.208564, 0.202139, 0.227931, 0.15622, 0.118042, 0.104006, 0.322, 0.327, 0.331, 0.34, 0.383, 0.454}
with weibull distribution with parameters (3.08, 5.39) and it was0.226532
but i recalculate it by excel by the same data and same parameters then the result was0.614
so i want help to know the formula behind the Kolmogorov-Smirnov test statistic in mathematica
the formula behind the Kolmogorov-Smirnov test statistic in excel isD=max|i/n-CDF|
Thanks
Answer
You have likely made an error in computing the test statistic by not sorting R and by using incorrect limits on empirical CDF values.
R = {0.171434, 0.134263, 0.155931, 0.135479, 0.196356, 0.152357,
0.133084, 0.10537, 0.14654, 0.116676, 0.123145, 0.145377, 0.12366,
0.156681, 0.208564, 0.202139, 0.227931, 0.15622, 0.118042,
0.104006, 0.322, 0.327, 0.331, 0.34, 0.383, 0.454};
dist = WeibullDistribution[3.08, 1/5.39];
n = Length[R];
Max[Abs[Range[0,n-1]/n - CDF[dist, Sort@R]]]
(* 0.226532 *)
This is precisely what we get using the built in test.
KolmogorovSmirnovTest[R, dist, "TestStatistic"]
(* 0.226532 *)
I can replicate what you found in Excel by shifting the empirical values by one and by not sorting R.
Max[Abs[Range[1,n]/n - CDF[dist, R]]]
(* 0.614413 *)
The implementation in Mathematica is more general than this method. If you happen to have ties in the data then you have to actually compute the empirical CDF or they won't be handled properly. For example...
SeedRandom[1];
r2 = RandomChoice[R, n];
Max[Abs[Range[0, n - 1]/n - CDF[dist, r2]]]
(* 0.762458 *)
Which is not equivalent to the correct value...
MaxValue[Abs[CDF[EmpiricalDistribution[r2], x] - CDF[dist, x]], {x}]
(* 0.150597 *)
Comments
Post a Comment