Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 912
Jason Avatar asked Apr 03 '12 19:04

Jason


1 Answers

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.

like image 111
Joe Kington Avatar answered Sep 30 '22 10:09

Joe Kington