Skip to content

Instantly share code, notes, and snippets.

@tslumley
Created September 14, 2015 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tslumley/373e3759a766af106a4e to your computer and use it in GitHub Desktop.
Save tslumley/373e3759a766af106a4e to your computer and use it in GitHub Desktop.
Nearest-neighbour distances and dimension
library(FNN)
x1<-runif(5e4)
x2<-matrix(runif(5e4*2),ncol=2)
x10<-matrix(runif(5e4*10),ncol=10)
x100<-matrix(runif(5e4*100),ncol=100)
system.time(d1<-knn.dist(x1,k=1))
system.time(d2<-knn.dist(x2,k=1))
system.time(d10<-knn.dist(x10,k=1))
system.time(d100<-knn.dist(x100,k=1))
#coordinate-wise distance has triangular distribution with mean 1/3, variance 1/18
mean(d1/sqrt(1)/(1/3))
mean(d2/sqrt(2)/(1/3))
mean(d10/sqrt(10)/(1/3))
mean(d100/sqrt(100)/(1/3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment