以通用方式从 scipy.stats 获取 pdf

Getting a pdf from scipy.stats in a generic way

我 运行 在 Python 2.7.10 中使用 scipy.stats 进行了一些拟合优度测试。

for distrName in distrNameList:
    distr = getattr(distributions, distrName)
    param = distr.fit(sample)
    pdf   = distr.pdf(???)

我将什么传递到 distr.pdf() 以获取兴趣样本点 list 上的最佳 pdf 值,称为 abscissas

要评估 abscissas 处的 pdf,您可以将 abcissas 作为第一个参数传递给 pdf。要指定参数,use the * operator 解压缩 param 元组并将这些值传递给 distr.pdf:

pdf = distr.pdf(abscissas, *param)

例如,

import numpy as np
import scipy.stats as stats

distrNameList = ['beta', 'expon', 'gamma']
sample = stats.norm(0, 1).rvs(1000)
abscissas = np.linspace(0,1, 10)
for distrName in distrNameList:
    distr = getattr(stats.distributions, distrName)
    param = distr.fit(sample)
    pdf = distr.pdf(abscissas, *param)
    print(pdf)

根据文档,.fit() method returns:

shape, loc, scale : tuple of floats MLEs for any shape statistics, followed by those for location and scale.

并且 .pdf() method 接受:

x : array_like quantiles

arg1, arg2, arg3,... : array_like The shape parameter(s) for the distribution (see docstring of the instance object for more information)

loc : array_like, optional location parameter (default=0)

scale : array_like, optional

所以基本上你会做这样的事情:

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


# some random variates drawn from a beta distribution
rvs = stats.beta.rvs(2, 5, loc=0, scale=1, size=1000)

# estimate distribution parameters, in this case (a, b, loc, scale)
params = stats.beta.fit(rvs)

# evaluate PDF
x = np.linspace(0, 1, 1000)
pdf = stats.beta.pdf(x, *params)

# plot
fig, ax = plt.subplots(1, 1)
ax.hold(True)
ax.hist(rvs, normed=True)
ax.plot(x, pdf, '--r')