Thursday, 28 July 2011

Create multidimensional uniform distribution



#run with: $R -f dataset_uniform.R

# Creates a multi-dimensional uniform distribution.
# Each axis has coordinates between min=MIN and max=MAX.
# Plots 2d output by pairwise dimensions, i.e. 1-2,2-3,3-4,etc.
# micharg@gmail.com

MIN <- 0.0     #xmin=ymin=min
MAX <- 1000.0  #xmax=ymax=max
N <- 1000000    #number of samples (100K)
DIM <- 7    #number of dimensions

data <- c();
for(i in c(1:DIM)) {
    x <- runif(N, min=MIN, max=MAX)

    #corrections (needed?)
    print(sum(x>MAX))
    x[x>MAX]=MAX
    print(sum(x<MIN))
    x[x<MIN]=MIN

    data=cbind(data,x)                  #first columns:data, last column:x
}

#save plot into file
step <- 1;
for(i in seq(1,DIM-1,step)) {
    filename <- paste("dataset_uniform",i,(i+1),".jpg",sep="_")
    jpeg(file=filename)
    plot(data[,i], data[,i+1], type='p', main="Uniform Dataset", xlab=i, ylab=i+1)
#    dev.off()
}

#save [data] into file
filename <- paste("dataset_uniform_", 
  "Data",N,
  "_",
  "Dim",DIM,
  "_",
  "X", MIN, "-", MAX,
  "_",
  "Y", MIN, "-", MAX,
  ".csv", sep="")
write.table(data, file = filename, sep=",", col.names=F, row.names=F, quote=F)

No comments:

Post a Comment