In: Statistics and Probability
Use the Rejection Method to generate n = 1000 values for a random variable X distributed as Exponential (lambda=5). Create a density histogram for your generated values and superimpose the probability density function of the target distribution to the histogram. Use the generated values to estimate the mean, standard deviation, and the probability that X < 2. Compare them with the theoretic values which are 0.2, 0.2, and 0.97725, respectively. Report the rejection rate. Write in R code
We have to generate random variable from distribution with density
Acceptance-Rejection Algorithm for continuous random variables
1. Generate a RV distributed as .
2. Generate (independent from )
3. If , then set ; otherwise go back to 1, (“reject”).
Take
. Then
To find the maximum value of ,
. We can write,
.Thus,
The R code generate 10000 samples from distribution using rejection method and plotting the histogram and superimpose the probability density function of the target distribution to the histogram is given below:
N <- 1000
c <- 5
n <- 1
X <- array(dim = N)
g<- function (x)
{
exp(-x)
}
f<- function (x)
{
5*exp(-5*x)
}
while(n<=N)
{
Y <- rexp(1,1)
U <- runif(1)
if(U < f(Y)/g(Y)/c)
{
X[n] <- Y
n <- n+1
}
}
plot(1:1)
dev.new()
hist(X,prob = TRUE, col = "skyblue", main = "Histogram of
Exponential Distribution")
curve(dexp(x,5),add=TRUE,lwd=2,col="blue")
mean(X)
sd(X)
length(X[X<2])/N
The simulated mean is 0.1917748, standard deviation is 0.1816255, and the probability .
The rejection rate is proportional to .