Simulation of exponential distribution

Suppose I have an exponential distribution with rate 0.5 and n=1000 together with a uniform distribution between 0 and 1. And I also need to draw a histogram. Write the R code.
The R code I wrote is :
x=rexp(1000,0.5)
u=runif(1000,min=0,max=1)
hist(x)

Just wanna know whether my R code is correct? Do I need to perform the inversion method here?

Well since you have mentioned 'together' with uniform distribution, I think it would be more appropriate to go with inverse transform sampling as normalization is required to work out the inverse CDF.

set.seed(1)
numSamples <- 1000
lambdaRate <- 0.5
U <- runif(numSamples, min = 0, max = 1)
X <- -log(1-U)/lambdaRate

# plot
hist(X, freq=F, xlab='X', main='graph')
curve(
  dexp(x, rate = lambdaRate), 
  from = 0, to = 11, lwd=2, xlab = "", ylab = "", add = T
)

[REF](https://stephens999.github.io/fiveMinuteStats/inverse_transform_sampling.html)
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

thanks !!!! i think i get it