【问题标题】:Defining Pareto distribution in Python scipy在 Python scipy 中定义帕累托分布
【发布时间】:2019-12-18 19:37:13
【问题描述】:

我正在尝试在 Python 中使用 scypi 定义帕累托分布。我记住了 alpha 和 xm 的值,就像它们在分布的经典定义中一样,例如在维基百科中:https://en.wikipedia.org/wiki/Pareto_distribution 假设我想要 alpha = 4 和 xm = 3。 如何使用这些参数初始化 scipy.stats.pareto?

import scipy.stats as sts
pareto_alpha = 4
pareto_xm = 3
pareto_rv = sts.pareto(???)

这里是帕累托函数https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pareto.html#scipy.stats.pareto的文档页面 我在那里找不到构造函数的清晰描述。

【问题讨论】:

  • 我想你想要一个概率密度函数?
  • sts.pareto.pdf(np.linspace(0, 5), 4, 3)

标签: python scipy


【解决方案1】:

您可以为 b(形状参数)的不同值绘制 pdf,如下所示:

import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import pareto

xm = 1 # scale 
alphas = [1, 2, 3] # shape parameters
x = np.linspace(0, 5, 1000)

output = np.array([pareto.pdf(x, scale = xm, b = a) for a in alphas])
plt.plot(x, output.T)
plt.show()

【讨论】:

  • 峰值看起来比维基百科页面上的略低。那是因为您的x 没有准确地达到 1 吗?
  • 是的,这是因为x的划分。我会调整它以使网格更精细。
【解决方案2】:

由于我没有完全相信,所以我进行了一些测试。

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import pareto

def my_pareto_pdf(x, a, x_m):
    """
    Returns the value of the pareto density function at
    the point x.
    """
    if x >= x_m:
        pdv = a
        pdv *= x_m**a
        pdv /= x**(a+1)
        return pdv
    else:
        return 0

x = np.linspace(0, 10, 100)

plt.plot(x, pareto.pdf(x, b=1.3), color='k', label='Scipy: b=1.3')
plt.plot(x, [my_pareto_pdf(val, a=1.3, x_m=1) for val in x], color='tab:blue', alpha=0.5, lw=5, label='Mypdf: a=1.3 x_m=1')

plt.plot(x, pareto.pdf(x, b=1.7, scale=3), color='k', label='Scipy: b=1.7 scale=3')
plt.plot(x, [my_pareto_pdf(val, a=1.7, x_m=3) for val in x], color='tab:blue', alpha=0.5, lw=5, label='Mypdf: a=1.7 x_m=3')

plt.plot(x, pareto.pdf(x, b=2.3, scale=6), color='k', label='Scipy: b=2.3 scale=6')
plt.plot(x, [my_pareto_pdf(val, a=2.3, x_m=6) for val in x], color='tab:blue', alpha=0.5, lw=5, label='Mypdf: a=2.3 x_m=6')

plt.legend(loc='best')
plt.title('Pareto PDFs')
plt.show()

这是输出。

Pareto PDFs

因此,在 scipy 中,参数 b 用作经典定义的 alpha,而 scale 用作 xm。

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多