当前位置: 代码迷 >> python >> 在 Python scipy 中定义帕累托分布
  详细解决方案

在 Python scipy 中定义帕累托分布

热度:79   发布时间:2023-06-16 10:12:42.0

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

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

这是帕累托函数的文档页面我找不到对构造函数在那里。

您可以为 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()

由于我并不完全相信,我进行了一些测试。

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

这是输出。

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