Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R vs. Matlab: Explanation for speed difference for rnorm, qnorm and pnorm functions

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)?

like image 969
user1372987 Avatar asked Feb 14 '13 07:02

user1372987


People also ask

What is the difference between Qnorm and Pnorm in R?

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.

What is difference between Dnorm and Pnorm?

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.

What does the Pnorm function do in R?

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.

What does Pnorm stand for in R?

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 σ.


1 Answers

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.

like image 189
Dirk Eddelbuettel Avatar answered Oct 24 '22 18:10

Dirk Eddelbuettel