2011-05-20 61 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