3
$\begingroup$

What is an easy to understand step by step procedure on how to compute a distance between a cumulative distribution function and an empirical distribution function given a random sample using Kolmogorov-Smirnov distance.

An actual illustrative implementaton in R would be helpful.

$\endgroup$

2 Answers 2

2
$\begingroup$

Your problem of computing the KS distance, i.e. $$d_{KS}(F_1,F_2)=\sup\{ |F_1(x) - F_2(x)| \mbox{ for } x\in\mathbb{R}\}$$ is very much simplified for empirical data because the emprical CDF is a step function with a finite number of steps. You thus simply need to compare the empirical CDF with the other CDF at the observed data points. Example:

x <- rnorm(50)
# build empirical CDF (beware: returns a FUNCTION)
x.cdf <- ecdf(x)
# compute the maximum of absolute difference at the observed data points
d.KS <- max(abs(x.cdf(x) - pnorm(x)))
print(d.KS)
# for comparison (should yield the same value):
ks.test(x, pnorm)$statistic

If you want to compare two empirical CDFs of two random variables $X$ and $Y$, you must evaluate the difference at the union of data points observed for $X$ and $Y$. Example:

x <- rnorm(50)
y <- rnorm(50)
xy <- c(x,y)
d.KS <- max(abs(ecdf(x)(xy) - ecdf(y)(xy)))
$\endgroup$
2
  • $\begingroup$ This covered (1) ecdf vs absolutely continuous cdf and (2) ecdf vs step cdf. But what should I do to compare an ecdf with Cantor distribution function? $\endgroup$ Commented Jul 14 at 12:43
  • 1
    $\begingroup$ @ZackFisher you'd be surprised how often that doesn't come up. But I think you still only need to check the points where the ecdf jumps and the limit at the left of those points. $\endgroup$ Commented Jul 14 at 23:38
0
$\begingroup$

The way to compare empirical distributions is to make cumulative distribution functions out of the raw data [use ecdf()] then compare them by plotting [via qqplot()]. And then use KS. Discrete distributions can have direct functions called probability mass functions. Continuous can not because any single realization instance (say 3.57867363474624) can not have a probability, only an interval can (say 3-4). You could compare histograms but ecdf are used far far more often.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.