pythonpython-3.xmatplotlibopenturns

Plot PDF of Pareto distribution in Python


I have a specific Pareto distribution. For example,

Pareto(beta=0.00317985, alpha=0.147365, gamma=1.0283)

which I obtained from this answer and now I want to plot a graph of its Probability Density Function (PDF) in matplotlib. So I believe that the x-axis will be all positive real numbers, and the y-axis will be the same.

How exactly can I obtain the appropriate PDF information and plot it? Programmatically obtaining the mathematical PDF function or coordinates is a requirement for this question.


UPDATE:

The drawPDF method returns a Graph object that contains coordinates for the PDF. However, I don't know how to access these coordinates programmatically. I certainly don't want to convert the object to a string nor use a regex to pull out the information:

In [45]: pdfg = distribution.drawPDF()

In [46]: pdfg
Out[46]: class=Graph name=pdf as a function of X0 implementation=class=GraphImplementation name=pdf as a function of X0 title= xTitle=X0 yTitle=PDF axes=ON grid=ON legendposition=topright legendFontSize=1
 drawables=[class=Drawable name=Unnamed implementation=class=Curve name=Unnamed derived from class=DrawableImplementation name=Unnamed legend=X0 PDF data=class=Sample name=Unnamed implementation=class=Sam
pleImplementation name=Unnamed size=129 dimension=2 data=[[-1610.7,0],[-1575.83,0],[-1540.96,0],[-1506.09,0],[-1471.22,0],[-1436.35,0],[-1401.48,0],[-1366.61,0],...,[-1331.7,6.95394e-06],[2852.57,6.85646e-06]] color
=red fillStyle=solid lineStyle=solid pointStyle=none lineWidth=2]

Solution

  • I assume that you want to perform different tasks:

    1. To plot the PDF
    2. To compute the PDF at a single point
    3. To compute the PDF for a range of values

    Each of these needs requires a different script. Please let me detail them.

    I first create the Pareto distribution:

    import openturns as ot
    import numpy as np
    beta = 0.00317985
    alpha = 0.147365
    gamma = 1.0283
    distribution = ot.Pareto(beta, alpha, gamma)
    print("distribution", distribution)
    

    To plot the PDF, use drawPDF() method. This creates a ot.Graph which can be viewed directly in Jupyter Notebook or IPython. We can force the creation of the plot with View:

    import openturns.viewer as otv
    graph = distribution.drawPDF()
    otv.View(graph)
    

    This plots:

    PDF of Pareto distribution

    To compute the PDF at a single point, use computePDF(x), where x is a ot.Point(). This can also be a Python list or tuple or 1D numpy array, as the conversion is automatically managed by OpenTURNS:

    x = 500.0
    y = distribution.computePDF(x)
    print("y=", y)
    

    The previous script prints:

    y= 5.0659235352823877e-05
    

    To compute the PDF for a range of values, we can use the computePDF(x), where x is a ot.Sample(). This can also be a Python list of lists or a 2D numpy array, as the conversion is automatically managed by OpenTURNS.

    x = ot.Sample([[v] for v in np.linspace(0.0, 1000.0)])
    y = distribution.computePDF(x)
    print("y=", y)
    

    The previous script prints:

    y= 
     0 : [ 0           ]
     1 : [ 0.00210511  ]
     [...]
     49 : [ 2.28431e-05 ]