Using R, how will I be able to generate an exponentially distributed random variable with rate 1 by only generating a uniformly distributed random variable(runif function in r).
Asked
Active
Viewed 469 times
0
-
I forgot how to do it in R, but you have to use the Inverse Transform Method. It's not hard. – igorkf Feb 28 '19 at 01:01
-
Is there a reason why you can't just use `rexp()`? – dave-edison Feb 28 '19 at 01:01
-
yes, I am trying to understand how to generate the numbers without using the functions. I started with this: myexp<-function(x,n){ z<- runif(x) – Alex Feb 28 '19 at 01:05
-
And I'm trying to figure out the general case, not just unif(0,1), but unif(1,n) – Alex Feb 28 '19 at 01:18
1 Answers
1
Found in https://stephens999.github.io/fiveMinuteStats/inverse_transform_sampling.html:
# inverse transfrom sampling
num.samples <- 1000
U <- runif(num.samples)
X <- -log(1-U)/2
# plot
hist(X, freq=F, xlab='X', main='Generating Exponential R.V.')
curve(dexp(x, rate=2) , 0, 3, lwd=2, xlab = "", ylab = "", add = T)

igorkf
- 3,159
- 2
- 22
- 31
-
What if if the number is uniform between something like 1-100 instead of 0-1? – Alex Feb 28 '19 at 01:10
-
If anyone is curious I got it for an example runif (1,50) the transformation would be -log(((U-1)/49),base=exp(1)) – Alex Feb 28 '19 at 02:00