## Test-stat for Kolmogorov-Smirnov test

on 26 Sep 2013

on 30 Sep 2013

### Luiz (view profile)

Dear Sir or Madam, I am trying to apply the two-sample Kolmogorov-Smirnov test implemented in Matlab (ktest2), but the description of the test statistic (ks2stat) used (as shown in the current documentation) is not clear enough. How is the test statistic calculated? The documentation provides some references but does not clearly specify what the ktest2 formula is really using as test stat. Could provide some more clarity on that? Tks!

## Products

No products are associated with this question.

### Luiz (view profile)

on 30 Sep 2013

Indeed, tks Wayne...appreciated.

### Wayne King (view profile)

on 26 Sep 2013
Edited by Wayne King

### Wayne King (view profile)

on 26 Sep 2013

The two-sided test as implemented is essentially described here:

http://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test

The two-sided test computes the empirical CDFs of the two data samples and computes the statistic

T = max|F1(x) - F2(x)| where F1(x) is the empirical CDF of one sample and F2(x) is the empirical CDF of the other.

The p-value is computed from the result described on the above link in the "Kolmogorov distribution" section.

You can see that in MATLAB code here:

```     j   =  (1:101)';
pValue  =  2 * sum((-1).^(j-1).*exp(-2*lambda*lambda*j.^2));
pValue  =  min(max(pValue, 0), 1);```

The one-sided cases are a bit different, are you interested in the one-sided or two-sided case?

Luiz

### Luiz (view profile)

on 26 Sep 2013

Hi Wayne, tks for you answer. My problem is that test-stat (ks2stat) calculated by ktest2 is quite different from what is suggested by Wikipedia. E.g. using the Wikipedia formula D=c(alpha)*sqrt((n1+n2)/(n1*n2)), the test statistic for a K-S test between two samples of 100 observations each at a confidence level (alpha) of 5% equals 0.86014. However, the ks2stat given by ktest2 gives me 0.08, using the following similar input:

x=normrnd(0,1,100,2)

[h p ks2stat]=kstest2(x(:,1), x(:,2),0.05)

So, i guess that the test stat provided by Matlab is calculated differently.....any insight on this?

tks, luiz

### Luiz (view profile)

on 26 Sep 2013

Sorry, just saw the first part of your answer, with the link. I will go through the rest now.

### Luiz (view profile)

on 26 Sep 2013

I am interested into the two-sided case

Wayne King

### Wayne King (view profile)

on 26 Sep 2013

Then I've shown you how you can find the information.

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi