(務必閱讀過深地進入源之前檢查出在帖子的末尾編輯)Matplotlib直方圖日誌拉普拉斯PDF
我繪製的人口,這似乎是一個直方圖log Laplacian分佈:
我試圖畫出一條最適合它來驗證我的假設,但我有問題獲得有意義的結果。
我正在使用來自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
功能: 
正如你所看到的,它不是最好的匹配,但數字(直方圖和拉普拉斯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()
@Zhenya,感謝您的意見。你爲什麼說我的'laplace()'函數不是拉普拉斯分佈?如果你看看維基百科頁面,你會看到我已經完全按照定義實現了拉普拉斯PDF(除了之後我正在採用基數10的指數,正如我原來的帖子中所提到的)。看起來你是對的日誌基礎。我對y軸上的蜱是10的冪的事實感到困惑。看起來基地確實是'e'。關於你的第二點,我會研究規範直方圖,謝謝。 – misha 2011-05-20 10:48:48
@misha:我只是不明白你爲什麼拿10 ** laplace_distribution;也許它與你的原始數據實際上有關。 – 2011-05-20 11:01:50
@ Zhenya:我這樣做是因爲我的人口PDF格式的** log **看起來像拉普拉斯(不是通常的PDF本身)。看看y軸的比例是如何對數的?普通的拉普拉斯分佈在這裏不起作用。另外,由於日誌是以「e」爲基礎的,因此它應該是'exp(laplace_distribution)'。 – misha 2011-05-20 13:05:48