0

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

Alex
  • 15
  • 2

1 Answers1

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)

enter image description here

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