2011-05-20 72 views
3

(务必阅读过深地进入源之前检查出在帖子的末尾编辑)Matplotlib直方图日志拉普拉斯PDF

我绘制的人口,这似乎是一个直方图log Laplacian分布: enter image description here

我试图画出一条最适合它来验证我的假设,但我有问题获得有意义的结果。

我正在使用来自Wikipedia的拉普拉斯PDF定义,并且取得10的幂作为PDF(以“反转”对数直方图的效果)。

我在做什么错?

这是我的代码。我通过标准输入(cat pop.txt | python hist.py) - here's管样东西。

from pylab import * 
import numpy  
def laplace(x, mu, b): 
    return 10**(1.0/(2*b) * numpy.exp(-abs(x - mu)/b))  
def main(): 
    import sys 
    num = map(int, sys.stdin.read().strip().split(' ')) 
    nbins = max(num) - min(num) 
    n, bins, patches = hist(num, nbins, range=(min(num), max(num)), log=True, align='left') 
    loc, scale = 0., 1. 
    x = numpy.arange(bins[0], bins[-1], 1.) 
    pdf = laplace(x, 0., 1.) 
    plot(x, pdf) 
    width = max(-min(num), max(num)) 
    xlim((-width, width)) 
    ylim((1.0, 10**7)) 
    show() 
if __name__ == '__main__': 
    main() 

编辑

OK,这里是匹配到正规拉普拉斯分布(而不是一个日志拉普拉斯)的尝试。从上面的尝试差异:

  • 直方图赋范
  • 直方图是线性(不记录)作为维基百科文章

输出在规定的精确限定的

  • laplace功能: enter image description here

    正如你所看到的,它不是最好的匹配,但数字(直方图和拉普拉斯PDF)在l东现在在同一个球场。我认为日志拉普拉斯将会更好地匹配。我的方法(上面的源代码)不起作用。任何人都可以建议一种方法来做到这一点有效吗?

    来源:似乎

    from pylab import * 
    import numpy 
    def laplace(x, mu, b): 
        return 1.0/(2*b) * numpy.exp(-abs(x - mu)/b) 
    def main(): 
        import sys 
        num = map(int, sys.stdin.read().strip().split(' ')) 
        nbins = max(num) - min(num) 
        n, bins, patches = hist(num, nbins, range=(min(num), max(num)), log=False, align='left', normed=True) 
        loc, scale = 0., 0.54 
        x = numpy.arange(bins[0], bins[-1], 1.) 
        pdf = laplace(x, loc, scale) 
        plot(x, pdf) 
        width = max(-min(num), max(num)) 
        xlim((-width, width)) 
         show() 
    if __name__ == '__main__': 
        main() 
    
  • 回答

    1
    1. 你拉普拉斯()函数不会是一个拉普拉斯分布。此外,numpy.log()是自然对数(基数e),不是十进制。

    2. 您的直方图似乎没有正常化,而分布是。

    编辑:

    1. 不要使用毛毯的进口from pyplot import *,它会咬你的。

    2. 如果你正在检查与拉普拉斯分布(或它的日志)的一致性,使用的事实,后者是对称的左右mu:以最高的直方图的修复mu,和你有一个单一的参数问题。你只能使用直方图的一半。

    3. 使用numpy的直方图函数 - 这样您就可以得到直方图本身,然后您可以使用拉普拉斯分布(和/或其日志)。卡方会告诉你一致性是好还是不好。为了装配,您可以使用,例如scipy.optimize.leastsq程序(http://www.scipy.org/Cookbook/FittingData)

    +0

    @Zhenya,感谢您的意见。你为什么说我的'laplace()'函数不是拉普拉斯分布?如果你看看维基百科页面,你会看到我已经完全按照定义实现了拉普拉斯PDF(除了之后我正在采用基数10的指数,正如我原来的帖子中所提到的)。看起来你是对的日志基础。我对y轴上的蜱是10的幂的事实感到困惑。看起来基地确实是'e'。关于你的第二点,我会研究规范直方图,谢谢。 – misha 2011-05-20 10:48:48

    +0

    @misha:我只是不明白你为什么拿10 ** laplace_distribution;也许它与你的原始数据实际上有关。 – 2011-05-20 11:01:50

    +0

    @ Zhenya:我这样做是因为我的人口PDF格式的** log **看起来像拉普拉斯(不是通常的PDF本身)。看看y轴的比例是如何对数的?普通的拉普拉斯分布在这里不起作用。另外,由于日志是以“e”为基础的,因此它应该是'exp(laplace_distribution)'。 – misha 2011-05-20 13:05:48

    1

    我找到一个变通的我是有这个问题。我不使用matplotlib.hist,而是使用numpy.histogram结合matplotlib.bar来计算直方图并在两个单独的步骤中绘制它。

    我不确定是否有办法使用matplotlib.hist来做到这一点 - 但它肯定会更方便。 enter image description here

    你可以看到它是一个更好的匹配。

    我现在的问题是我需要估计PDF的scale参数。

    来源:

    from pylab import * 
    import numpy 
    
    def laplace(x, mu, b): 
        """http://en.wikipedia.org/wiki/Laplace_distribution""" 
        return 1.0/(2*b) * numpy.exp(-abs(x - mu)/b) 
    
    def main(): 
        import sys 
        num = map(int, sys.stdin.read().strip().split(' ')) 
        nbins = max(num) - min(num) 
        count, bins = numpy.histogram(num, nbins) 
        bins = bins[:-1] 
        assert len(bins) == nbins 
        # 
        # FIRST we take the log of the histogram, THEN we normalize it. 
        # Clean up after divide by zero 
        # 
        count = numpy.log(count) 
        for i in range(nbins): 
         if count[i] == -numpy.inf: 
          count[i] = 0 
        count = count/max(count) 
    
        loc = 0. 
        scale = 4. 
        x = numpy.arange(bins[0], bins[-1], 1.) 
        pdf = laplace(x, loc, scale) 
        pdf = pdf/max(pdf) 
    
        width=1.0 
        bar(bins-width/2, count, width=width) 
        plot(x, pdf, color='r') 
        xlim(min(num), max(num)) 
        show() 
    
    if __name__ == '__main__': 
        main() 
    
    +0

    你几乎在那里:-)。请参阅我的编辑以获取有关拟合的建议。 – 2011-05-20 15:25:07