MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Kolmogorov-Smirnov 2-sample test

  • To: mathgroup at smc.vnet.net
  • Subject: [mg111083] Re: Kolmogorov-Smirnov 2-sample test
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Tue, 20 Jul 2010 03:40:57 -0400 (EDT)
  • References: <i20q8i$kje$1@smc.vnet.net>

On Jul 18, 11:10 pm, Aaron Bramson <aaronbram... at gmail.com> wrote:
> Hello Everybody,
>
> I would like to perform a 2-sample k-s test.  I've seen some posts on the
> archive about the one-sample goodness-of-fit version of the
> Kolgomorov-Smirnov test, but I'm interested in the 2-sample version.
>
> Here's a description of the method:

> http://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test#Two-sample_Kolmogorov.E2.80.93Smirnov_test

>
> I think all the necessary components are available; e.g.
> Accumulate[BinCounts[_list_]] to get the ecdf of both datasets, abs, max of
> a list, etc.  But the data management is a bit above my current skill
> level.  Also, since all other software packages seem to include this test
> capability, I would be really surprised if there
> wasn't a package somewhere that included it by now, but I've searched a lot
> and can't find it.  Can anybody help me locate this this?
>
> Alternatively, would anybody like to work with me to build this in case it
> can't be found?
>
> Thanks in Advance,
> Aaron

This is based on code by Thomas Waterhouse, taken from
https://stat.ethz.ch/pipermail/r-devel/2009-July/054106.html

The arguments y1 and y2 are lists of data. It returns {x,p},
where x is the usual K-S test statistic n1*n2*Max|cdf1-cdf2|,
and p is the corresponding p-value, conditional on
the distribution of ties in the pooled data.

ks2[y1_,y2_] := Block[{n1 = Length@y1, n2 = Length@y2,
pool = Sort@Join[y1,y2], x,n,u}, {x =
Max@Abs[n2*Tr@UnitStep[y1-#]-n1*Tr@UnitStep[y2-#]&/@Rest@Union@pool],
n = n1+n2; u = Table[0,{n2+1}]; Do[ Which[
i+j == 0, u[[j+1]] = 1,
i+j < n && pool[[i+j]] < pool[[i+j+1]] && Abs[n2*i-n1*j] >= x,
          u[[j+1]] = 0,
i == 0, u[[j+1]] = u[[j]],
j > 0, u[[j+1]] += u[[j]]], {i,0,n1},{j,0,n2}];
N[1 - Last@u/Multinomial[n1,n2]]} ]


  • Prev by Date: Scoping constructs Block, Module, ModuleBlock violate principle of
  • Next by Date: Re: Transform differential equation by tranformation rule
  • Previous by thread: Re: Kolmogorov-Smirnov 2-sample test
  • Next by thread: Re: Kolmogorov-Smirnov 2-sample test