### Is there a Python method to calculate lognormal mean and variance?

7

I am trying to understand if there is a built in python function to calculate the lognormal mean and variance. I require this information only to then feed it into `scipy.stats.lognorm` for a plot overlaid on top of a histogram.

Simply using the `numpy.mean` and `numpy.std` does not seem to be the correct idea, as the lognormal mean and variance are specific and quite different than the numpy methods. In Matlab they have a handy function called `lognstat` that returns the mean and variance of a lognormal distribution, and I can't seem to track down an analogous method in Python. It is easy enough to code a work around, but I am wondering if this method exists in a library. Thanks.

2012-04-03 19:58
by Jason

5

For whatever it's worth, all `lognstat` in matlab does is this:

``````import numpy as np

def lognstat(mu, sigma):
"""Calculate the mean of and variance of the lognormal distribution given
the mean (`mu`) and standard deviation (`sigma`), of the associated normal
distribution."""
m = np.exp(mu + sigma**2 / 2.0)
v = np.exp(2 * mu + sigma**2) * (np.exp(sigma**2) - 1)
return m, v
``````

There may be a function for it in `scipy.stats` or `scikits-statsmodels`, but I'm not aware of it offhand. Either way, it's just a couple of lines of code.

2012-04-03 21:33
by Joe Kington
"`given the mean (mu) and standard deviation (sigma), of the associated normal distribution.`" But what if the original data isn't a normal distribution - John M. 2018-02-06 13:17

0

(I do not have rpy install on my notebook at the moment, so I cannot try this out)

You can consider to install Rpy, which is a python interface to R.

Then you may be able to use this R function http://rss.acs.unt.edu/Rdoc/library/stats/html/Lognormal.html

2012-04-03 20:59
by Anthony Kong