Implement a function in MATLAB called sim_exp.m that simulates observations of a
ID: 3847394 • Letter: I
Question
Implement a function in MATLAB called sim_exp.m that simulates observations of an exponential random variable. Your function should take as input parameters n and lambda. The output of your function should be a vector of size nx1 containing n samples drawn from an exponential distribution with parameter lambda. You can use the build-in function rand(n,1) in order to generate n uniformly distributed random numbers.
a. Set lambda= 5 and generate a histogram of n = 5000 data points with 20 bins using hist.
Overlay the true exponential pdf with parameter lambda. Comment on the results.
b. Compute the empirical mean and variance of the points you generated. Compare them to the
theoretical mean and variance of an exponential random variable with rate lambda. Comment on the
results.
Explanation / Answer
The inverse transformation method is used to convert a uniformrandom distribution over [0, 1] to a distribution of anytype. Given the uniform random variable X and arbitrarydistribution Y with a cdf of F(y), F^(-1)(X) [F inverse of X] hasdistribution of F. In other words, if all we have availableis a set of random numbers between 0 and 1, we can convert thisinto a random distribution of any type by applying the inverse cdfof the distribution to these random numbers.
In your case, the cdf is X = 1 - e^(-y).
Taking the inverse we get...
X = 1 - e^(-Y)
1 - X = e^(-Y)
ln(1 - X) = -Y
-1/ * ln(1 - X) = Y
However, since X is uniform between [0, 1], 1 - X = X
Thus, our transform is
Y = -1/ * ln(X)
function Y = exponential_dist(n, lambda)
X = rand(n, 1); %generate nx1 random vector
Y = -1/lambda .* ln(X); %transform from uniform to exponential
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.