Skip to content

Instantly share code, notes, and snippets.

@ramhiser
Created June 28, 2013 22:24
Show Gist options
  • Save ramhiser/5888640 to your computer and use it in GitHub Desktop.
Save ramhiser/5888640 to your computer and use it in GitHub Desktop.
Kolmogorov-Smirnov Test to Determine Nearest Sample
set.seed(42)
n <- 50
x1 <- rnorm(n, mean = 5)
x2 <- rnorm(n, mean = 0)
x3 <- rnorm(n, mean = 20)
plot(density(x1), xlim = c(-5, 25))
lines(density(x2), col = "red")
lines(density(x3), col = "blue")
ecdf1 <- ecdf(x1)
ecdf2 <- ecdf(x2)
ecdf3 <- ecdf(x3)
plot(ecdf1, xlim = c(-5, 25))
lines(ecdf2, col = "red")
lines(ecdf3, col = "blue")
# The statistics are equal. So with this criterion, samples 1 and 2 have the same distance as samples 1 and 3
ks.test(x1, x2)$statistic
ks.test(x1, x3)$statistic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment