Home > R > R Random Number Generator

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

Categories: R
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.