【发布时间】:2021-12-18 03:45:42
【问题描述】:
我需要最小化一个函数,比如说f(theta)。在f(theta) 中,我从给定的分布中绘制了非常大的矩阵。所以,在整个优化过程中,我必须保持相同的矩阵。但是,由于矩阵太大,我无法保存它们。因此,对于theta 的每个值,我决定找到一种方法来绘制相同的矩阵。如果我定义一个种子,就可以做到这一点,每次调用函数时都会使用它。但我不希望用户设置种子,我需要软件获取当前种子,然后将该种子用于 theta 的每个新值。
如果我使用 R,这是一个简化示例。
f <- function(theta, SEED) {
.Random.seed <- SEED
# I simplify the problem, I replace the big matrices by a small vector
return(sum((rnorm(10) - theta)^2))
}
optim(par = 0, fn = f, method = "Brent", lower = -1, upper = 1, SEED = .Random.seed)
.Random.seed 允许我获得将用于f 的种子。这确保了在优化期间,rnorm(10) 不会被更改,尽管它没有被保存。如何使用Rcpp 构造类似的函数?
【问题讨论】:
标签: r rcpp random-seed rcpparmadillo