拟合和绘制对数正态 [英] Fitting and Plotting Lognormal

查看:123
本文介绍了拟合和绘制对数正态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做相对简单的事情时遇到了麻烦:

I'm having trouble doing something as relatively simple as:

  1. 从具有一定均值和方差的高斯中抽取N个样本
  2. 将日志记录到这N个样本中
  3. 使用对数正态分布(使用stats.lognorm.fit)
  4. 吐出一个漂亮且平滑的对数正态pdf,不带inf值(使用stats.lognorm.pdf)

这是我得到的输出的一个小示例:

Here's a small working example of the output I'm getting:

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
import math

%matplotlib inline


def lognormDrive(mu,variance):
    size = 1000
    sigma = math.sqrt(variance)
    np.random.seed(1)
    gaussianData = stats.norm.rvs(loc=mu, scale=sigma, size=size)
    logData = np.exp(gaussianData)
    shape, loc, scale = stats.lognorm.fit(logData, floc=mu)
    return stats.lognorm.pdf(logData, shape, loc, scale)

plt.plot(lognormDrive(37,0.8))

您可能会注意到,这种情节绝对没有任何意义.

And as you might notice, the plot makes absolutely no sense.

有什么想法吗?

我关注了这些帖子: POST1 提前谢谢!

详细说明:我正在构建一个小型脚本,该脚本将

Elaboration: I am building a small script that will

  1. 获取原始数据并拟合内核分布(经验值)
  2. 假设给定数据的均值和方差,则使用不同的分布.这将是高斯和对数正态
  3. 使用interact绘制这些分布以及皇帝距离
  4. 当人们旋转均值和方差(最终偏斜)的旋钮时,计算不同分布之间的Kullbeck-Leibler散度

推荐答案

在对lognorm.fit()的调用中,使用floc=0,而不是floc=mu.

In the call to lognorm.fit(), use floc=0, not floc=mu.

(lognorm发行版的 location 参数仅转换该发行版.您几乎永远不想使用对数正态分布.)

(The location parameter of the lognorm distribution simply translates the distribution. You almost never want to do that with the log-normal distribution.)

请参见 Python中的对数正态分布

顺便说一句,您正在绘制未排序的样本值的PDF,因此校正后的脚本中的图表看起来不会有太大不同.您可能会发现,将PDF与已排序的值相对应更为有用.这是对脚本的修改,可以使用排序后的示例创建PDF绘图:

By the way, you are plotting the PDF of the unsorted sample values, so the plot in the corrected script won't look much different. You might find it more useful to plot the PDF against the sorted values. Here's a modification of your script that creates a plot of the PDF using the sorted samples:

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
import math


def lognormDrive(mu,variance):
    size = 1000
    sigma = math.sqrt(variance)
    np.random.seed(1)
    gaussianData = stats.norm.rvs(loc=mu, scale=sigma, size=size)
    logData = np.exp(gaussianData)
    shape, loc, scale = stats.lognorm.fit(logData, floc=0)
    print "Estimated mu:", np.log(scale)
    print "Estimated var: ", shape**2
    logData.sort()
    return logData, stats.lognorm.pdf(logData, shape, loc, scale)

x, y = lognormDrive(37, 0.8)
plt.plot(x, y)
plt.grid()
plt.show()

脚本打印:

Estimated mu: 37.0347152587
Estimated var:  0.769897988163

并创建以下图:

这篇关于拟合和绘制对数正态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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