Matplotlib histogram with Laplacian log PDF

(Remember to check the EDIT at the end of the post before reading the source too deeply)

I draw a histogram of a population that apparently refers to log Laplacian : enter image description here

I am trying to draw the line most suitable for it to test my hypothesis, but I have problems getting meaningful results.

I use the Laplacian definition of PDF from Wikipedia and taking 10 to the power of PDF (to β€œflip” the effects of the log histogram).

What am I doing wrong?

Here is my code. I push things through standard input ( cat pop.txt | python hist.py) - here's a selection.

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()

EDIT

, ( ). :

  • (not log)
  • laplace, , .

: enter image description here

, , ( Laplace PDF) . , . ( ) . - , ?

:

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()
+3
2
  • laplace(), -, . , numpy.log() ( e), .

  • , -.

EDIT:

  • from pyplot import *, .

  • ( ), , mu: fix mu , - . .

  • numpy - , (/ ). - , ( ) . , . scipy.optimize.leastsq (http://www.scipy.org/Cookbook/FittingData)

+1

, . matplotlib.hist numpy.histogram matplotlib.bar .

, matplotlib.hist - . enter image description here

, .

scale PDF.

:

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()
+1

All Articles