One of the most common exersices given to Statistical Computing,Simulation or relevant classes is the generation of random numbers from a gamma distribution. At first this might seem straightforward in terms of the lifesaving relation that exponential and gamma random variables share. So, it’s easy to get a gamma random variate using the fact that
$$ {{X}_{i}}\tilde{\ }Exp(\lambda )\Rightarrow \sum\limits_{i}{{{X}_{i}}}\tilde{\ }Ga(k,\lambda )$$.
The code to do this is the following
rexp1 <- function(lambda, n) {
u <- runif(n)
x <- -log(u)/lambda
}
rgamma1 <- function(k, lambda) {
sum(rexp1(lambda, k))
}
This works unfortunately only for the case $$ k\in \mathbb{N}$$.
Read the rest of this entry »

LinkedIn
Facebook
Youtube
Twitter