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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With