加速蒙特卡罗模拟的最佳技巧是什么?

发布于 2024-08-05 03:53:18 字数 1436 浏览 8 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枯寂 2024-08-12 03:53:18
  • 如果您只是使用并行独立复制,那么使用多核/机器应该很简单,但要注意随机数生成器的常见缺陷(例如,如果使用当前时间作为种子,会产生许多每个进程都有一个 RNG 可能会产生相关的随机数,这会导致无效结果 - 请参见例如 本文

  • 您可能需要使用方差减少减少所需的重复次数,即缩小所需样本的大小。更先进的方差减少技术可以在许多教科书中找到,例如这本书

  • Using multiple cores/machines should be simple if you're just using parallel independent replications, but be aware of common deficiencies of random number generators (e.g. if using the current time as seed, spawning many processes with one RNG for each might produce correlated random numbers, which leads to invalid results - see e.g. this paper)

  • You might want to use variance reduction to reduce the number of required replications, i.e. to shrink the size of the required sample. More advanced variance reduction techniques can be found in many textbooks, e.g. in this one.

小傻瓜 2024-08-12 03:53:18

预先分配你的向量!

> 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 
> 

Preallocate 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 
> 
紫竹語嫣☆ 2024-08-12 03:53:18

拉丁超立方采样很容易应用,并且对结果有重大影响。基本上,您从均匀分布中获取拉丁超立方体样本(例如,使用lhs包中的randomLHS()),并使用例如qnorm(uniformsample)将其转换为您想要的分布。

Latin Hypercube Sampling is easily applied and has a major influence on the results. Basically you take a latin hypercube sample from a uniform distribution (e.g., using randomLHS() in the package lhs) and transform this to your desired distribution using e.g., qnorm(uniformsample).

无人接听 2024-08-12 03:53:18

我知道这个线程确实很旧,但是如果有人偶然发现它并正在寻找更快的方法,我认为以下方法有效:

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

I know this thread is really old, but if anyone stumbles upon 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文