One of my class tasks is to make a simulation. I've gathered the data and done distribution fitting to it and the result of the distribution is log-normal. I have the code to generate random numbers in Java using a linear congruential generator, so can I somehow convert them to a log-normal distribution with parameters mu and sigma?
1 Answer
You should be able to use your random number generator to get a random uniform $U \sim \mathcal{U}(0, 1)$ (just a random number between zero and one, the default for many random number generators). Then you can use the Box-Muller transform to get a random normal $X \sim \mathcal{N}(0, 1)$. This involves generating two random uniforms $U_1$ and $U_2$ and then forming the variables:
$$\sqrt{-2 \log(U_1)} \cos(2 \pi U_2)$$
and
$$\sqrt{-2 \log(U_1)} \sin(2 \pi U_2)$$
Both of these resulting random variables are normally distributed with mean $0$ and standard deviation $1$.
Finally, take one of your standard normals $X$. The transformation $e^{\mu + \sigma X}$ is the log-normal you're after.
Here's a quick implementation of this in R, plus a histogram to show it working its magic:
mu <- 0; sigma <- 1
u1 <- runif(50000); u2 <- runif(50000)
n <- sqrt(-2 * log(u1)) * cos(2 * pi * u2)
e <- exp(mu + sigma * n)
hist(e[e < 4], breaks=100)
-
$\begingroup$ and where do i set my own parameters for lognormal (mu and sigma)? $\endgroup$– theoCommented May 16, 2015 at 7:29
-
$\begingroup$ In the exponentiation step $e^{\mu + \sigma X}$. $\endgroup$ Commented May 16, 2015 at 15:22
-
1$\begingroup$ @MatthewDrury This looks like a good answer to the question. Unfortunately it also looks like you've completed (at least part of) the OP's homework for them, which isn't consistent with our policy. The idea is to give hints and guidance on anything which is effectively routine bookwork (as might be set for.homework, even when it isn't homework). $\endgroup$– Glen_bCommented May 31, 2015 at 23:24
-
$\begingroup$ @Glen_b I see that you are correct. I almost certainly did not read the introduction carefully enough, and the "one of my class tasks is to make a simulation" went through my head like noise. I'll be more careful in the future, thank you for the heads up. $\endgroup$ Commented Jun 1, 2015 at 0:00
self-study
tag and edit your question to follow the guidelines at its tag wiki, including explaining what you understand, outlining what attempts you made to solve your problem and identifying the specific help you need. $\endgroup$