# (1) the Torus algorithm
#
torus(100)
# example of setting the seed
setSeed(1)
torus(5)
setSeed(6)
torus(5)
#the same
setSeed(1)
torus(10)
#no use of the machine time
torus(10, use=FALSE)
#Kolmogorov Smirnov test
#KS statistic should be around 0.0019
ks.test(torus(1000), punif)
#KS statistic should be around 0.0003
ks.test(torus(10000), punif)
#the mixed Torus sequence
torus(10, mixed=TRUE)
if (FALSE) {
par(mfrow = c(1,2))
acf(torus(10^6))
acf(torus(10^6, mixed=TRUE))
}
#usage of the init argument
torus(5)
torus(5, init=FALSE)
#should be equal to the combination of the two
#previous call
torus(10)
# (2) Halton sequences
#
# uniform variate
halton(n = 10, dim = 5)
# normal variate
halton(n = 10, dim = 5, normal = TRUE)
#usage of the init argument
halton(5)
halton(5, init=FALSE)
#should be equal to the combination of the two
#previous call
halton(10)
# some plots
par(mfrow = c(2, 2), cex = 0.75)
hist(halton(n = 500, dim = 1), main = "Uniform Halton",
xlab = "x", col = "steelblue3", border = "white")
hist(halton(n = 500, dim = 1, norm = TRUE), main = "Normal Halton",
xlab = "x", col = "steelblue3", border = "white")
# (3) Sobol sequences
#
# uniform variate
sobol(n = 10, dim = 5)
# normal variate
sobol(n = 10, dim = 5, normal = TRUE)
# some plots
hist(sobol(500, 1), main = "Uniform Sobol",
xlab = "x", col = "steelblue3", border = "white")
hist(sobol(500, 1, normal = TRUE), main = "Normal Sobol",
xlab = "x", col = "steelblue3", border = "white")
#usage of the init argument
sobol(5)
sobol(5, init=FALSE)
#should be equal to the combination of the two
#previous call
sobol(10)
# (4) computation times on my 2017 macbook (on my 2007 macbook), mean of 1000 runs
#
if (FALSE) {
# algorithm time in seconds for n=10^6
# Torus algo 0.012 (0.058)
# mixed Torus algo 0.018 (0.087)
# Halton sequence 0.180 (0.878)
# Sobol sequence 0.027 (0.214)
n <- 1e+06
mean( replicate( 1000, system.time( torus(n), gcFirst=TRUE)[3]) )
mean( replicate( 1000, system.time( torus(n, mixed=TRUE), gcFirst=TRUE)[3]) )
mean( replicate( 1000, system.time( halton(n), gcFirst=TRUE)[3]) )
mean( replicate( 1000, system.time( sobol(n), gcFirst=TRUE)[3]) )
}
Run the code above in your browser using DataLab