# Kolmogorov-Smirnov test in likelihood function

I want to test how well my data fits a uniform distribution and use this as one factor in a likelihood function I am constructing.

Unfortunately, I have no solid basis in statistics. So far, I figured out that a good criterion to use is the Kolmogorov-Smirnov test. I can do the test, but I do not know what the resulting number I get means.

What I want to have is the value of the PDF describing whether or not my sample is coming from the given distribution, so that I can plug this into my likelihood function. How do I get it?

Thank you!

-
What are you using to perform the test? If you can, post the code you're using, and the output, so that people can see what you're looking at. –  naught101 Dec 15 '12 at 1:08
Unfortunately, I do not have the code here at the moment. I basically follow the Wikipedia entry at en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test until I am left with the number $D_n$, which one, as far as I understand, usually looks up in a table to accept or reject the distribution. –  gozero Dec 15 '12 at 1:19
You might take interest in my answer on how one-sample K-S test is calculated. –  ttnphns Dec 15 '12 at 16:35

You are right that you need to compare $D$ to a reference distribution, traditionally (though not very often these days) looked up in a table.

The footnotes to the Wikipedia article you link to include some references to routines that will calculate the reference tables for the statistic (which can be complex) so you can calculate a p value - which is the probability that a value as extreme as you have calculated would have been generated by your reference distribution.

Stats packages will do this automatically, if you can draw on one of these, eg see the following input and output for sample data in R:

> # generate data from a normal distribution, mean=1, standard deviation=1
> x <- rnorm(100,1,1)
>
> # test - could it have come from a slightly different normal distribution?
> ks.test(x, "pnorm", .8, 1.5)

One-sample Kolmogorov-Smirnov test

data:  x
D = 0.1594, p-value = 0.01245
alternative hypothesis: two-sided

>
> # test against the distribution it really came from
> ks.test(x, "pnorm", 1, 1)

One-sample Kolmogorov-Smirnov test

data:  x
D = 0.0806, p-value = 0.5338
alternative hypothesis: two-sided

>
> # test against a uniform distribution
> ks.test(x, "punif", min(x), max(x))

One-sample Kolmogorov-Smirnov test

data:  x
D = 0.1499, p-value = 0.02238
alternative hypothesis: two-sided

-
Thank you for the answer. My problem lies more on the side of understanding what exactly this number $D$ is. I am comparing the CDF of my data to another CDF, then using the supremum of this and compare it to what exactly from a reference distribution? –  gozero Dec 15 '12 at 10:24
You calculate the probability of seeing a more extreme value than $D$ from the reference distribution. That calculation, as Peter Ellis has written, is reflected in the "p-value" number in the output from most K-S test routines. –  jbowman Dec 15 '12 at 15:17
I see, that should have made the thing clear from the beginning. Thank you all! I suppose there is no easy way to get the PDF of the underlying distribution $p(x_0)$, where $x_0$ corresponds to the point which has $P(x_0) = D$? –  gozero Dec 15 '12 at 23:44
Yes, D under the null hypothesis has a distribution that is non-trivial to describe. But there are well documented methods out there for calculating it yourself if you need to for some reason. –  Peter Ellis Dec 15 '12 at 23:47