# st: RE: RE: simulating a lognormal distribution

 From "Nick Cox" To Subject st: RE: RE: simulating a lognormal distribution Date Wed, 13 Sep 2006 20:12:08 +0100

```This came up a few years ago. I think you can
do it directly by algebra, or at least I
thought I could do it with my version of algebra.
I used various equations in Aitchison and
Brown's book from 1957.

This is my do file. You supply three arguments
which are the new variable name, mean of
new variable and sd of new variable.

---------------------- lognorm.do
scalar mean = ln((`2'^2) / sqrt((`2'^2) + (`3'^2)))
scalar sd   = sqrt(ln(1 + (`3'^2) / (`2'^2)))
gen `1' = scalar(mean) + scalar(sd) * invnorm(uniform())
replace `1' = exp(`1')
---------------------

Nick
n.j.cox@durham.ac.uk

Feiveson, Alan H. (JSC-SK311)

> Jeph - Let X = log(Y). You can use the fact that E(exp(tX)) =
> exp(mu*t +
> t*t*sig2/2) where X~N(mu,sig2). Taking t=1,we see that E(Y) = mu +
> sig2/2. Also the median of Y is exp(median of X) = exp(mu).
> So from the
> mean and median of Y, you can get values for mu and sig. The sd of Y
> adds additional information if you want to use it. Taking t=2, gives
> E(Y^2) = E( (exp(x))^2 ) = E(exp(2X)) = 2*mu + 4*sig2/2 =
> 2*mu + 2*sig2.
> From this you can get Var(Y)=E(Y^2) - (E(y))^2 also as some
> function of
> mu and sig2. Then you have three equations and two unknowns (mu and
> sig)and you could solve for mu and sig by some sort of weighted least
> squares. But I think the sample variance (or sd) of a lognormal RV is
> not very reliable statistic and I would go with just the mean
> and median
> to estimate mu and sig.

Jeph Herrin

> This is not, strictly speaking, a Stata problem, but I am
> hoping to find
> a Stata solution.
>
> I am trying to simulate a lognormal distribution, but have only the
> median (which I'm willing to assume is the log mean) plus the mean and
> sd of the non-log transformed data. Put another way, I have
> median, mean
> and sd of Y, but log(Y) is normal, and I would like to simulate Y.
>
> I have found -rndlgn-, which will simulate a random log normal
> distribution, but only if I have the mean and sd of log(Y). The only
> idea I have is to generate a series of log normal distributions X with
> mean = log(median(Y)) and a range of sds, until I end up with
> one where
> exp(X) has the mean and sd of Y. But this seems overly computing
> intensive for my purposes, as I will need to do this for a
> large number
> of obs with a loop that runs many times.
>
> Any other ideas? In particuar, am I missing an obvious way to get the
> mean & sd of log(Y) from the mean and sd of Y?

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```