我正在处理一个软件包,它使用RcppArmadillo中的随机数。该软件包运行MCMC算法,为了确保精确可重现性,用户应该能够设置随机数种子。当这样做时,似乎用于生成来自Gamma分布的随机数的
以下是我在运行R3.5.2的macOS 10.13.6上得到的结果:
arma::randg()
函数在不同平台上返回不同的值。而对于arma::randu()
或arma::randn()
则不是这种情况。这是否与arma::randg()
需要C++11有关呢?以下是我在运行R3.5.2的macOS 10.13.6上得到的结果:
library(Rcpp)
library(RcppArmadillo)
sourceCpp(code = '
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
double random_gamma() {
return arma::randg();
}
// [[Rcpp::export]]
double random_uniform() {
return arma::randu();
}
// [[Rcpp::export]]
double random_normal() {
return arma::randn();
}
'
)
replicate(2, {set.seed(1); random_gamma()})
#> [1] 1.507675 1.507675
replicate(2, {set.seed(432); random_gamma()})
#> [1] 0.02234341 0.02234341
replicate(2, {set.seed(1); random_uniform()})
#> [1] 0.2655087 0.2655087
replicate(2, {set.seed(1); random_normal()})
#> [1] -1.390378 -1.390378
本文创建于2019年02月22日,使用reprex包(v0.2.1)
这是我在运行R3.5.2的Windows 10上得到的结果:
library(Rcpp)
library(RcppArmadillo)
sourceCpp(code = '
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
double random_gamma() {
return arma::randg();
}
// [[Rcpp::export]]
double random_uniform() {
return arma::randu();
}
// [[Rcpp::export]]
double random_normal() {
return arma::randn();
}
'
)
replicate(2, {set.seed(1); random_gamma()})
#> [1] 0.2549381 0.2549381
replicate(2, {set.seed(432); random_gamma()})
#> [1] 0.2648896 0.2648896
replicate(2, {set.seed(1); random_uniform()})
#> [1] 0.2655087 0.2655087
replicate(2, {set.seed(1); random_normal()})
#> [1] -1.390378 -1.390378
这是由reprex package (v0.2.1)于2019-02-22创建的
可以看到,使用arma::randg()
生成的随机数在内部一致,但在不同平台上有所不同。
我尝试按照Armadillo文档中的说明设置种子:
library(Rcpp)
library(RcppArmadillo)
sourceCpp(code = '
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
double random_gamma(int seed) {
arma::arma_rng::set_seed(seed);
return arma::randg();
}
'
)
replicate(4, random_gamma(1))
#> Warning in random_gamma(1): When called from R, the RNG seed has to be set
#> at the R level via set.seed()
#> [1] 1.3659195 0.6447221 1.1771862 0.9099034
本文创建于2019年2月22日,使用 reprex package (v0.2.1)。
然而,正如警告所述,并且结果显示的那样,这种方式并不能设置种子。
在使用 arma::randg()
时,是否有一种方法可以在平台之间获得可重复的结果,或者我需要使用RcppArmadillo中提供的其他随机数生成器来实现伽马分布?
更新
正如评论中指出的那样,使用 R::rgamma()
可以解决这个问题。以下代码在Mac和Windows上返回相同的数字:
library(Rcpp)
sourceCpp(code = '
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double random_gamma() {
return R::rgamma(1.0, 1.0);
}
'
)
replicate(2, {set.seed(1); random_gamma()})
#> [1] 0.1551414 0.1551414
本文创建于2019年2月22日,使用reprex package (v0.2.1)。
这对我解决了问题。但是,我不确定问题是否已经解决,因为这似乎是意外的行为,所以保持开放状态。
Rcpp::rgamma()
等效函数时问题没有持续发生,您可以在计算中使用该函数的输出,或者如果类型不同,则在计算过程中将其转换为所需的类型。 - Oliver<random>
头文件中的std::gamma_distribution
。不幸的是,这些分布是"实现定义"的,这可能是 WRE 不建议使用该头文件的原因。 - Ralf Stubner<random>
库已经包含在内,而arma::randg()
仅支持 C++ 11。 - Øystein S