# Two-dimensional Kolmogorov–Smirnov

I would like to run some two dimensional Kolmogorov–Smironov tests to determine whether a two-dimensional distribution fits with a reference.

Is there any package or application that I could use in a relatively straightforward fashion? Or is there a different algorithm that is preferrable? I have just a basic statistical knowledge.

• Perhaps I am missing something, but I think the Kolmogorov–Smirnov test applies to one-dimensional distributions. If you are interested on one of the proposal extensions (there are several because there is no natural extension to the multivariate case), please specify which one.
– user10525
Commented Apr 28, 2012 at 10:42
• I'd rather say there is a natural extension to the multivariate case, but it involves the distribution of $\sup |K(t)|$ where $K$ is the Kiefer process (two-dimensional Brownian bridge), and this distribution is badly known. Commented Apr 28, 2012 at 11:47

## Python implementation

I have written a python implementation using numpy. You can find the code here, you may find more infomation in the docstring in the code.

And here's another one (not by me). This Notebook provide a Python implementation for 2D K-S test with 2 samples. The .py file can be downloaded here. The code seems to be a straight translation of C code, the efficiency might be a problem if sample size is large.

However you'd better check the codes (no matter which one) with the original papers/books before you use. The python implementations of 2d KS test are far less checked than the ones in R.

The algorithm is first developed in two papers (as I see)

A nice introduction and the C implementation can be found in

Here's a post titled Beware the Kolmogorov-Smirnov test is also related to the subject, you may want to have a look. It encourages using resample method to evaluate the p-value with given KS distance.

• using a 3-element list for each of x1, y1, x2, y2 I get the error TypeError: '<=' not supported between instances of 'list' and 'int' when applying code from the first link. Next two links don't work Commented Jul 18 at 14:18
• It should work if you convert your "list" to "array" by x1 = np.array(x1), etc, before giving them to the function. @MohammadAthar Commented Jul 19 at 13:07

A two-dimensional extension of the Kolmogorov-Smirnov test has been described by Justel, Pena and Zamar in a "A multivariate Komogorov-Smirnov test of goodness of fit". @Procrastinator's comments suggests there may be other such proposals.

However, I haven't seen a package with a straightforward implementation.

Depending on what you want to do, kde.test() in Tarn Duong's ks package for R might be more useful.

you may find this Matlab code to be useful.

http://www.mathworks.com/matlabcentral/fileexchange/38617-two-dimensional-2d-paired-kolmogorov-smirnov-test

• Welcome to this site! Could you provide a brief description of the resources available on that page?
– chl
Commented Apr 10, 2013 at 17:43