R Random Number Generator
I was programming a particle filter routine that makes heavy use of rnorm. When I completed the coding effort, I was not satisfied at all with the speed of the computation.
Speed is paramount here as the output of my routine is a likelihood function that is then passed to a Metropolis-Hasting mcmc.
With my surprise, the biggest improvement came from changing the normal random number from “Inversion” (the default) to “Kinderman-Ramage”.
RNGkind(normal.kind = 'Kinderman-Ramage')
I am getting a speed up of about 25%: not bad at all. But then I am wondering why “Kinderman-Ramage” is not the default normal RNG. The “Kenderman-Ramage” algorithm used to be buggy in R, but it is fixed in recent implementations (see this article).
This is an example of the speed up on my machine:
> RNGkind(normal.kind = 'Inversion')
> system.time(rnorm(1000000))
user system elapsed
0.125 0.006 0.131
> RNGkind(normal.kind = 'Kinderman-Ramage')
> system.time(rnorm(1000000))
user system elapsed
0.06 0.00 0.06
Recent Comments