What is the best trick to speed up Monte Carlo simulation?

Whenever I run large-scale monte carlo simulations in S-Plus, I always end up growing a beard while I wait for it to end.

What are the best tricks to run a monte carlo simulation in R? Any good examples of starting processes in a distributed way?

+4
source share
4 answers
  • Using multiple cores / machines should be simple if you just use parallel independent replications , but keep in mind the common flaws of random number generators (for example, if you use the current time as a seed, creating many processes with one RNG for each of them can create correlated random numbers, which leads to invalid results - see, for example, in this article )

  • You might want to use a decrease in deviation to reduce the number of replicas required , i.e. reduce the size of the required sample. More sophisticated methods of reducing variance can be found in many textbooks, for example. into this one .

+9
source

Pre-select your vectors!

> nsims <- 10000 > n <- 100 > > system.time({ res <- NULL for (i in 1:nsims) { res <- c(res,mean(rnorm(n))) } }) user system elapsed 0.761 0.015 0.783 > > system.time({ res <- rep(NA, nsims) for (i in 1:nsims) { res[i] <- mean(rnorm(n)) } }) user system elapsed 0.485 0.001 0.488 > 
+5
source

The Latin Hypercube Sampling is easy to apply and has a big impact on the results. Basically you take a sample of the Latin hypercube from a uniform distribution (for example, using randomLHS () in the lhs package) and convert it to the desired distribution, for example, qnorm (uniformsample).

+3
source

I know that this branch is really old, but if someone came across it and is looking for an even faster method, I think the following works:

 library(data.table) library(microbenchmark) nsims <- 10000 n <- 100 # Answer from @Eduardo_Leoni: preallocate<-function(nsims, n) { res <- rep(NA, nsims) for (i in 1:nsims) { res[i] <- mean(rnorm(n)) } return(res) } # Answer using data.table: datatable<-function(nsims,n) { dt <- data.table(i=1:nsims)[,list(res=mean(rnorm(1:n))),by=i] return(dt) } # Timing benchmark: microbenchmark(preallocate(nsims,n), datatable(nsims,n), times=100) #Unit: milliseconds # expr min lq median uq max neval # preallocate(nsims, n) 428.4022 432.3249 434.2910 436.4806 489.2061 100 # datatable(nsims, n) 238.9006 242.3517 244.1229 246.5998 303.6133 100 
+3
source

All Articles