In: Statistics and Probability
Write a function to generate random variates from a Lognormal(mu, sigma) distribution using a transformation method, and generate a random sample size of 1000 in R. Compare the histogram with the lognormal density curve given by dlnorm function in R.
Inverse Transform method:
If has distribution, then such that is an observation from the probability distribution , this means that we can generate observations from the distribution by generating random variables (which most software programs can do easily) and applying the transformation.
Here .
The CDF is
Now, .
The R code for generating 1000 log normal random variables with and plotting the histogram and density is given below:
n <- 1000
U <- runif(n)
mu <- 0.3
sigma <- 0.25
X <- array (dim =n)
for (i in 1:n)
{
X[i] <- exp(mu+sigma*qnorm(U[i]))
}
plot(1:1)
dev.new()
hist(X,prob =TRUE, ylim=c(0,1.2),col = "skyblue", main = "Histogram
of Lognormal Distribution")
curve(dlnorm(x,meanlog=mu,sdlog=sigma),col="blue",lwd=2,add=TRUE)
The plots are: