有Python方法来计算对数正态均值和方差吗? [英] Is there a Python method to calculate lognormal mean and variance?

查看:124
本文介绍了有Python方法来计算对数正态均值和方差吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解是否有内置的python函数来计算对数正态均值和方差.我仅需要此信息,然后将其输入到scipy.stats.lognorm中,以覆盖直方图上方的图.

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.

仅使用numpy.meannumpy.std似乎不是正确的主意,因为对数正态均值和方差是特定的,并且与numpy方法完全不同.在Matlab中,它们有一个方便的函数,称为lognstat,该函数返回对数正态分布的均值和方差,而且我似乎无法追踪Python中的类似方法.编写变通方法很容易,但是我想知道此方法是否存在于库中.谢谢.

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.

推荐答案

对于所有有价值的东西,matlab中的所有lognstat都是这样的:

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

scipy.statsscikits-statsmodels中可能有针对它的功能,但我暂时不知道它.无论哪种方式,它都只是几行代码.

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.

这篇关于有Python方法来计算对数正态均值和方差吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆