简体   繁体   English

Python 中的帕累托分布 Plot PDF

[英]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 .我从这个答案中获得,现在我想 plot matplotlib 中的概率密度图Function (PDF)。 So I believe that the x-axis will be all positive real numbers, and the y-axis will be the same.所以我相信x轴都是正实数,y轴也是一样的。

How exactly can I obtain the appropriate PDF information and plot it?我究竟如何才能获得相应的 PDF 信息和 plot 呢? Programmatically obtaining the mathematical PDF function or coordinates is a requirement for this question.以编程方式获得数学 PDF function 或坐标是此问题的要求。


UPDATE:更新:

The drawPDF method returns a Graph object that contains coordinates for the PDF. drawPDF方法返回一个图形 object,其中包含 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:我当然不想将 object 转换为字符串,也不想使用正则表达式来提取信息:

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]

I assume that you want to perform different tasks:我假设您要执行不同的任务:

  1. To plot the PDF至 plot PDF
  2. To compute the PDF at a single point在单点计算 PDF
  3. To compute the PDF for a range of values计算一系列值的 PDF

Each of these needs requires a different script.这些需求中的每一个都需要不同的脚本。 Please let me detail them.请让我详细说明它们。

I first create the Pareto distribution:我首先创建Pareto分布:

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.对于 plot PDF,使用drawPDF()方法。 This creates a ot.Graph which can be viewed directly in Jupyter Notebook or IPython.这将创建一个ot.Graph可以直接在 Jupyter Notebook 或 IPython 中查看。 We can force the creation of the plot with View :我们可以使用View强制创建 plot :

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

This plots:这情节:

帕累托分布 PDF

To compute the PDF at a single point, use computePDF(x) , where x is a ot.Point() .要在单个点计算 PDF,请使用computePDF(x) ,其中xot.Point() This can also be a Python list or tuple or 1D numpy array , as the conversion is automatically managed by OpenTURNS:这也可以是 Python listtuple或一维 numpy array ,因为转换由 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() .要计算一系列值的 PDF,我们可以使用computePDF(x) ,其中 x 是ot.Sample() This can also be a Python list of lists or a 2D numpy array , as the conversion is automatically managed by OpenTURNS.这也可以是 Python list列表或 2D numpy array ,因为转换由 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 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM