Reputation: 1365
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.
Upvotes: 9
Views: 3990
Reputation: 6667
The python equivalent of Joe Kington answer would be:
from scipy.stats import lognorm
# Mean of normal distribution
mu = 0
# Standard Deviation of normal distribution
sigma = 1
mean, var, *rest = lognorm.stats(sigma, moments='mvsk', scale=np.exp(mu))
Where mean and var would be the loc and scale parameters respectively for a lognorm distribution.
Upvotes: 1
Reputation: 284642
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.
Upvotes: 6
Reputation: 40664
(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
Upvotes: 0