Sun, 02 Sep 2012

Scott Chamberlain blogged about faster creation of
binomial matrices the other day, and even referred to our RcppArmadillo
package as a possible solution (though claiming he didn't get it to work, tst tst -- that is what the
rcpp-devel list is here to help with).

The post also fell short of a good aggregated timing comparison for which we love the
rbenchmark package. So in order to rectify this, and to see
what we can do here with Rcpp, a quick post revisiting the
issue.

As preliminaries, we need to load three packages: inline to
create compiled code on the fly (which, I should mention, is also used together with
Rcpp by the
Stan / RStan MCMC sampler which is creating some buzz this week), the compiler
package included with R to create byte-compiled code and lastly the aforementioned
rbenchmark package to do the timings. We also set row and
column dimension, and set them a little higher than the original example to actually have something measurable:

library(inline)library(compiler)library(rbenchmark)
n <-500
k <-100

The first suggestion was the one by Scott himself. We will wrap this one, and all the following ones, in a function so
that all approaches are comparable as being in a function of two dimension arguments:

We also immediatly compute a byte-compiled version (just because we now can) to see if this helps at all with the code.
As there are no (explicit !) loops, we do not expect a big pickup. Scott's function works, but sweeps the
sample() function across all rows and columns which is probably going to be (relatively) expensive.

Next is the first improvement suggested to Scott which came from Ted Hart.

This is very clever as it uses sample() over zero and one rather than making (expensive) draws from random
number generator.

Next we have a version from Luis Apiolaza:

luis <-function(m, n) {round(matrix(runif(m * n), m, n))}

It draws from a random uniform and rounds to zero and one, rather than deploying the binomial.

Then we have the version using RcppArmadillo
hinted at by Scott, but with actual arguments and a correction for row/column dimensions. Thanks to
inline we can write the C++ code as an R character string;
inline takes care of everything and we end up with C++-based
solution directly callable from R:

which uses the the old rounding approximation of adding 1/2 before truncating.

With Armadillo in the picture, we do wonder how Rcpp sugar would do. Rcpp sugar, described in one of the eight vignettes
of the Rcpp package, is using template meta-programming to
provide R-like expressiveness (aka "syntactic sugar") at the C++ level. In particular, it gives access to R's RNG
functions using the exact same RNGs as R making the results directly substitutable (whereas Armadillo uses its
own RNG).

Here Rcpp::RNGScope deals with setting/resetting the R RNG state. This draws a vector of N time K uniforms
similar to Luis' function -- and just like Luis' R function does so without looping -- and then shapes a matrix of dimension N by K from it.

And it does of course have the same problem as the RcppArmadillo approach
earlier and we can use the same solution:

Rcpp sugar wins, which is something we have seen in previous posts on this blog. One hundred replication take only
72 milliseconds (or 88 in the corrected version) --- less than one millisecond per matrix creation.

RcppArmadillo does well too, and I presume
that the small difference is due not to code in Armadillo but the fact that we need one more 'mapping' of data types
on the way back to R

The sample() idea by David and Rafael is very, very fast too. This proves once again that
well-written R code can be competitive. It also suggest how to make the C++ solution by foregoing (expensive)
RNG draws in favour of sampling

The approaches by Ted and Luis are also pretty good. In practice, the are probably good enough.

Scott's function is not looking so hot (particularly as we increased the problem dimensions) and byte-compilation
does not help at all.

Thanks to Scott and everybody for suggesting this interesting problem. Trying the rbinom() Rcpp sugar
function, or implementing sample() at the C++ level is, as the saying goes, left as an exercise to the reader.