-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow vectors as distribution parameters for the RNGs #35
Comments
That sounds reasonable. Implementation note: The meaning of using a vector in the different places is quite different in base R:
|
The function // [[Rcpp::export]]
Rcpp::NumericVector rscalar_direct2(int n, Rcpp::NumericVector rate) {
// TODO handle case where n is a vector
if (rate.length() < n) rate = rep_len(rate, n); // recycling
Rcpp::NumericVector result(Rcpp::no_init(n));
for (int i = 0; i < n; ++i) {
using parm_t = decltype(exponential)::param_type;
exponential.param(parm_t(rate[i]));
result[i] = rexp_impl();
}
return result;
} |
@ChristK Interesting. Renaming the function to
So a special case for |
This might work better? // [[Rcpp::export]]
Rcpp::NumericVector dqrexp3(Rcpp::IntegerVector n, Rcpp::NumericVector rate) {
int n_ = n[0];
if (n.length() > 1) n_ = n.length();
Rcpp::NumericVector result(Rcpp::no_init(n_));
if (rate.length() == 1)
{
using parm_t = decltype(exponential)::param_type;
exponential.param(parm_t(rate[0]));
for (int i = 0; i < n_; ++i)
result[i] = rexp_impl();
}
else
{
if (rate.length() < n_) rate = rep_len(rate, n_); // recycling
for (int i = 0; i < n_; ++i) {
using parm_t = decltype(exponential)::param_type;
exponential.param(parm_t(rate[i]));
result[i] = rexp_impl();
}
};
return result;
} for large vectors (n> 1e6) in my machine that gives better timings than currently implemented |
The performance difference is interesting. And since the function pointer |
I am considering to close this with appropriate documentation since the current development version allows the RNG to be registered as user defined RNG. This way, one can use the functions from library(dqrng)
bench::mark(stats::rnorm(1e6), dqrng::dqrnorm(1e6), check = FALSE)[ , 1:4]
#> # A tibble: 2 × 4
#> expression min median `itr/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl>
#> 1 stats::rnorm(1e+06) 21.11ms 21.49ms 46.7
#> 2 dqrng::dqrnorm(1e+06) 4.96ms 5.29ms 188.
register_methods()
bench::mark(stats::rnorm(1e6), dqrng::dqrnorm(1e6), check = FALSE)[ , 1:4]
#> # A tibble: 2 × 4
#> expression min median `itr/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl>
#> 1 stats::rnorm(1e+06) 7.64ms 8.21ms 120.
#> 2 dqrng::dqrnorm(1e+06) 3.52ms 3.7ms 268.
set.seed(42)
dqrunif(6)
#> [1] 0.90837042 0.33061240 0.95093543 0.20447122 0.34282983 0.09195553
set.seed(42)
runif(6, c(0,1), c(1,2))
#> [1] 0.9083704 1.3306124 0.9509354 1.2044712 0.3428298 1.0919555
restore_methods() Created on 2024-04-23 with reprex v2.1.0 @ChristK: What do you think about that possibility? |
Yes, of course, feel free to close it. It was a mere suggestion and I totally understand if that's not a priority. Many thanks for all your hard work with this package. I use it daily for my simulations. |
Closing as out of scope (see discussion above) |
Thank you for all your hard work in this excellent package.
Please consider an enhancement to the RNG functions to allow vectors for all arguments in the RNGs. This would make dq RNGs a direct replacement to R RNGs.
For example
runif(c(1, 2), c(-1, 0), c(0, 1))
works butdqrunif(c(1, 2), c(-1, 0), c(0, 1))
is notThe text was updated successfully, but these errors were encountered: