Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
As I mentioned in a recent post, I’ve just received a copy of Advanced Markov Chain Monte Carlo Methods. Chapter 1.4 in the book (very quickly) covers random variable generation.
Inverse CDF Method
A standard algorithm for generating random numbers is the inverse cdf method. The continuous version of the algorithm is as follows:
1. Generate a uniform random variable
2. Compute and return
where
Example: Logistic distribution
I teach this algorithm in one of my classes and I’m always on the look-out for new examples. Something that escaped my notice is that it is easy to generate RN’s using this technique from the Logistic distribution. This distribution has CDF
and so we can generate a random number from the logistic distribution using the following formula:
Which is easily converted to R code:
myRLogistic = function(mu, s){
u = runif(1)
return(mu + s log(u/(1-u)))
}
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.