Search code examples
rexp

Using r : How to generate exponential dist with rate 1 using just runif function


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


Solution

  • 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