2

我使用以下代码为示例数据拟合了一些分布:

import numpy as np 
import pylab
import matplotlib.pyplot as plt
from scipy.stats import norm

samp = norm.rvs(loc=0,scale=1,size=150) # (example) sample values. 

figprops = dict(figsize=(8., 7. / 1.618), dpi=128)                       
adjustprops = dict(left=0.1, bottom=0.1, right=0.97, top=0.93, wspace=0.2, hspace=0.2)

import pylab
fig = pylab.figure(**figprops)                                            
fig.subplots_adjust(**adjustprops)
ax = fig.add_subplot(1, 1, 1)  
ax.hist(samp,bins=10,density=True,alpha=0.6,color='grey', label='Data')
xmin, xmax = plt.xlim()

# Distributions. 
import scipy.stats
dist_names = ['beta', 'norm','gumbel_l'] 
for dist_name in dist_names:
    dist = getattr(scipy.stats, dist_name)
    param = dist.fit(samp)
    x = np.linspace(xmin, xmax, 100) # 
    ax.plot(x,dist(*param).pdf(x),linewidth=4,label=dist_name)

ax.legend(fontsize=14)
plt.savefig('example.png')

如何自动将图例中的分布名称从最佳拟合(顶部)排序到最差拟合?我在循环中生成了随机变量,每次迭代的最佳拟合结果可能不同。

4

1 回答 1

2

好吧,您可以使用 Kolmogorov-Smirnov (KS) 测试来计算、fe、p 值并按其排序

修改你的循环

for dist_name in dist_names:
    dist = getattr(scipy.stats, dist_name)
    param = dist.fit(samp)
    x = np.linspace(xmin, xmax, 100) # 
    ax.plot(x,dist(*param).pdf(x),linewidth=4,label=dist_name)

    ks = scipy.stats.kstest(samp, dist_name, args=param)
    print((dist_name, ks))

你可以得到类似的输出

('beta', KstestResult(statistic=0.033975289251035434, pvalue=0.9951529119440156))
('norm', KstestResult(statistic=0.03164417055025992, pvalue=0.9982475331007705))
('gumbel_l', KstestResult(statistic=0.113229070386386, pvalue=0.039394595923043355))

这告诉你正常和测试版都很好,但口香糖应该是最后的。基于 P 值或统计数据的排序应该很容易添加

您的结果可能会有所不同,并且取决于 RNG 初始状态。

更新

关于 KS 检验不适用于拟合优度估计,我强烈反对。我没有看到不使用它的科学理由,我自己也一直使用它。

通常,您有黑盒来生成随机数据,比如说网络延迟的一些测量

通常,它可以通过 Gamma 的混合来描述,并且您可以使用某种二次效用函数来进行拟合并取回一组参数

然后,您使用 KS 或任何其他经验与理论分布方法来估计拟合的好坏。如果不使用 KS 方法进行拟合,那么使用 KS 是非常好的方法。

您基本上有一个黑盒生成数据,另一个黑盒拟合数据,并且想知道数据拟合的程度。然后 KS 将完成这项工作。

并声明“它通常用作正态性测试,以查看您的数据是否正态分布。” 完全关闭,在我的拙见中。KS 是关于 CDF-vs-CDF 最大差异,它不关心正常性,它更普遍

于 2020-04-18T16:42:00.250 回答