Nino Hardt, Dicko Ahmadou — written Jul 13, 2013 — source

The Multivariate Normal density function is used frequently for a number of problems. Especially for MCMC problems, fast evaluation is important. Multivariate Normal Likelihoods, Priors and mixtures of Multivariate Normals require numerous evaluations, thus speed of computation is vital. We show dramatic increases in speed by using efficient algorithms, RcppArmadillo, and some extra gain by using OpenMP. The code is based on the latest version of RcppArmadillo (0.3.910.0).

While the `dmvnorm()`

function from the `mvtnorm`

package is quite popular,
and in an earlier version of this article we demonstrated that an
Rcpp implementation would lead to faster computation.

Peter Rossi, author of `bayesm`

, called our attention to the `bayesm`

pure R
implementation which is much faster than `dmvnorm()`

.
The function `dMvn()`

is used internally by the mixture of normals model in
`bayesm`

. It is the matrix-equivalent version of `lndMvn`

:

Translating the vectorized approach into RcppArmadillo,
we precompute the inverse root of the covariance matrix ahead
of the main loop over the rows of `X`

.
The loop can easily be parallelized, and the code is easy to read and
manipulate. For instance, the inverse root can be put inside the main
loop, if varying covariance matrices are necessary.
The use of `trimatu`

allows to exploit the diagonality of the Cholesky
root of the covariance matrix.

Additionally, we can make use of the OpenMP library to use multiple cores. For the OpenMP implementation, we need to enable OpenMP support. One way of doing so is by adding the required compiler and linker flags as follows:

Rcpp version 0.10.5 and later will also provide a plugin to set these variables for us:

We also need to set the number of cores to be used before running the
compiled functions. One way is to use `detectCores()`

from the `parallel`

package.

Only two additional lines are needed to enable multicore processing. In this example, a dynamic schedule is used for OpenMP. A static schedule might be faster in some cases. However,this is left to further experimentation.

Likewise, it is easy to translate ‘dmvnorm’ from the ‘mvtnorm’ package into Rcpp:

Now we simulate some data for benchmarking:

And run the benchmark:

[1] "Using 8 cores for _mc versions"

Loading required package: rbenchmark

test replications elapsed relative 4 dmvnrm_arma_mc(X, means, sigma, F, cores) 100 9.636 1.000 3 dmvnrm_arma(X, means, sigma, F) 100 18.848 1.956 2 dmvnorm_arma(X, means, sigma, F) 100 23.459 2.435 5 dMvn(X, means, sigma) 100 29.501 3.062 1 mvtnorm::dmvnorm(X, means, sigma, log = F) 100 44.556 4.624

Lastly, we show that the functions yield the same results:

[1] TRUE

The use of RcppArmadillo brings about a significant increase in speed. The addition of OpenMP leads to only little additional performance.

This example also illustrates that Rcpp does not completely substitute the need to look for faster algorithms. Basing the code of ‘lndMvn’ instead of ‘dmvnorm’ leads to a significantly faster function.

Tweet- Introducing RcppArrayFire — Ralf Stubner
- Using RcppArmadillo to price European Put Options — Davis Vaughan and Dirk Eddelbuettel
- Custom progress bars for RcppProgress — Clemens Schmid and Karl Forner