The rust
package implements the multivariate generalized
ratio-of-uniforms method of simulating random variates from a
d-dimensional continuous distribution. The user specifies (the log of) a
positive target function f
that is proportional to the
density function of the distribution.
We use the function ru
to simulate a sample of size 1000
from a two-dimensional standard normal distribution with strong positive
correlation between the components. Of course, this particular example
is purely illustrative: there are better ways to simulate from a
multivariate normal distribution.
<- 0.9
rho <- matrix(c(1, rho, rho, 1), 2, 2)
covmat <- function(x, mean = rep(0, d), sigma = diag(d)) {
log_dmvnorm <- matrix(x, ncol = length(x))
x <- ncol(x)
d - 0.5 * (x - mean) %*% solve(sigma) %*% t(x - mean)
}<- ru(logf = log_dmvnorm, sigma = covmat, d = 2, n = 1000, init = c(0, 0)) x
From version 1.2.0 onwards the faster function ru_rcpp
can be used. See the vignette “Rusting Faster: Simulation using Rcpp”
for details.
# Create an external pointer to a C++ function to evaluate the log-density.
<- create_xptr("logdnorm2")
ptr_bvn # Pass the external pointer to `ru_rcpp`.
<- ru_rcpp(logf = ptr_bvn, rho = rho, d = 2, n = 1000, init = c(0, 0)) x
To get the current released version from CRAN:
install.packages("rust")
See vignette("rust-a-vignette", package = "rust")
for an
overview of the package,
vignette("rust-b-when-to-use-vignette", package = "rust")
for guidance on when rust
can be used and
vignette("rust-c-using-rcpp-vignette", package = "rust")
for information on how to take advantage of the Rcpp package.