In: Statistics and Probability
1. Assume Y is an exponential random variable with rate parameter λ=2.
(1) Generate 1000 samples from this exponential distribution using inverse transform method
(2) Compare the histogram of your samples with the true density of Y.
Please provide MATLAB programming code below with graphs if possible. Thank you!
solution:
Assume;
\(Y\) is an exponential random variable with rate parameter \(\lambda = 2\). Recall that the probability density function is \(p(y) = 2e^{-2y}\), for \(y > 0\). First, we compute the CDF: \[F_Y(x) = P(Y\leq x) = \int_0^x 2e^{-2y} dy = 1 - e^{-2x}\]
Solving for the inverse CDF:
we get that \[F_Y^{-1}(y) = -\frac{\ln(1-y)}{2}\]
Using algorithm;
we first generate \(U \sim \text{Unif}(0,1)\), then set \(X = F_Y^{-1}(U) = -\frac{\ln(1-U)}{2}\).
R code below:
compare the histogram of our samples with the true density of \(Y\).
# inverse transform 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)
Indeed, the plot indicates that our random variables are following the intended distribution
Past versions of unnamed-chunk-2-1.png
NOTE: IF you like my answering please rate first . THANK YOU.