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

时间:2015-06-18 20:54:13

标签: python scipy distribution

我正在使用Python 2.7.10中的$ hadoop checknative -a | grep snappy 15/06/18 14:50:31 INFO bzip2.Bzip2Factory: Successfully loaded & initialized native-bzip2 library system-native 15/06/18 14:50:31 INFO zlib.ZlibFactory: Successfully loaded & initialized native-zlib library snappy: true /usr/lib64/libsnappy.so.1 运行一些拟合优度测试。

scipy.stats

我将for distrName in distrNameList: distr = getattr(distributions, distrName) param = distr.fit(sample) pdf = distr.pdf(???) 传递到distr.pdf()的{​​{1}}感兴趣的样本点list上获取最佳拟合pdf的值?

2 个答案:

答案 0 :(得分:7)

从文档中,.fit() method返回:

  

形状,位置,比例:浮点数的元组   MLE用于任何形状统计,然后是位置和比例的统计。

.pdf() method接受:

  

x:array_like   位数

     

arg1,arg2,arg3,...:array_like   分布的形状参数(有关更多信息,请参阅实例对象的docstring)

     

loc:array_like,可选   location参数(默认= 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')

enter image description here

答案 1 :(得分:5)

要评估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)