I compared the performance of the inbuilt R functions rnorm
, qnorm
and pnorm
to the equivalent Matlab functions.
It seems as if the rnorm
and pnorm
functions are 3-6 times slower in R than in Matlab, whereas the qnorm
function is ca. 40% faster in R. I tried the Rcpp package to speed up the R functions by using the corresponding C libraries which resulted in a decrease in runtime by ~30% which is still significantly slower than Matlab for rnorm
and pnorm
.
Is there a package availabe which provides a faster way of simulating normally distributed random variables in R (other than using the standard rnorm
function)?
The pnorm function provides the cumulative density of the normal distribution at a specific quantile. The qnorm function provides the quantile of the normal distribution at a specified cumulative density.
dnorm gives the density, pnorm gives the distribution function, qnorm gives the quantile function, and rnorm generates random deviates. The length of the result is determined by n for rnorm , and is the maximum of the lengths of the numerical arguments for the other functions.
The pnorm function gives the Cumulative Distribution Function (CDF) of the Normal distribution in R, which is the probability that the variable X takes a value lower or equal to x.
The pnorm in R is a built-in function that returns the value of the cumulative density function (cdf) of the normal distribution given a certain random variable q, and a population mean μ, and the population standard deviation σ.
I see two distinct issues here, one in each paragraph:
Yes, there are difference between languages / systems such as R and Matlab. Part of it has to do with the interpreter, speed of loops, speed of function calls etc pp. Rcpp can help there with respect to Matlab which has a genuine JIT compiler. We have a comparison between Matlab, R and R+Rcpp for a Kalman filter in the recent paper on RcppArmadillo.
There also are difference in the underlying compiled code, and yes, R does not always have the faster implementation as R Core (IMHO rightly) goes for precision first. (And Rcpp does not help per se: We just call what R has internally.) This had come up eg with the Gibbs Sampler example for MCMC which Darren Wilkinson started. I noticed that R's rgamma()
is much slower than other systems. So to get to your question regarding N(0,1) draws in a faster way: I think we need a contributed Ziggurat implementation. That is one of the faster N(0,1) generators out there, and a few other systems use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With