Python 使用scipy.stats拟合经验分布与双曲分布

Python 使用scipy.stats拟合经验分布与双曲分布,python,scipy,statistics,statsmodels,scientific-computing,Python,Scipy,Statistics,Statsmodels,Scientific Computing,目前,我正在将经验分布与理论分布进行拟合,如中所述 使用这些分布,结果显示了对分布的良好拟合 以下是我当前使用一些scipys发行版的方法: # -*- coding: utf-8 -*- import pandas as pd import numpy as np import scipy.stats import matplotlib.pyplot as plt # Sample data with random numbers of hypsecant distribution da

目前,我正在将经验分布与理论分布进行拟合,如中所述

使用这些分布,结果显示了对分布的良好拟合

以下是我当前使用一些scipys发行版的方法:

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt


# Sample data with random numbers of hypsecant distribution
data = scipy.stats.hypsecant.rvs(size=8760, loc=1.93, scale=7.19)

# Distributions to check
dist_names = ['gausshyper', 'norm', 'gamma', 'hypsecant']

for dist_name in dist_names:

    dist = getattr(scipy.stats, dist_name)

    # Fit a distribution to the data
    param = dist.fit(data)

    # Plot the histogram
    plt.hist(data, bins=100, normed=True, alpha=0.8, color='g')

    # Plot and save the PDF
    xmin, xmax = plt.xlim()
    x = np.linspace(xmin, xmax, 100)
    p = dist.pdf(x, *param[:-2], loc=param[-2], scale=param[-1])
    plt.plot(x, p, 'k', linewidth=2)
    title = 'Distribution: ' + dist_name
    plt.title(title)
    plt.savefig('fit_' + dist_name + '.png')
    plt.close()
其提供的绘图如下所示:

但我也想用a(广义)来测试拟合度,因为我有这样的假设,即它可能提供更好的拟合度

我可以使用scipy.stats中的双曲线分布吗?或者有解决办法吗

使用其他软件包也是一种选择


提前谢谢

由于您的发行版不在
scipy.stats
中,您可以将其添加到软件包中,也可以尝试“手动”操作

对于前者,请看一下
scipy.stats
包的功能-添加一个新发行版可能不需要那么多工作


对于后一种选择,可以使用最大似然法。为此,首先定义一个方法,为您提供发行版的pdf。基于pdf构造一个函数,计算给定特定分布参数的数据的对数似然。最后,通过使用
scipy.optimize

最大化此对数似然函数,使您的模型与数据相匹配。为什么要进行向下投票?当然有一些关系,但我的问题是关于scipy.stats的。在这里的评论中,一些人问了同样的问题(在scipy.stats中实现双曲距离),但没有得到答案:啊,好吧,我不想造成冗余。但是我现在添加了我的代码和一个绘图,这样问题就会变得更清楚!谢谢你的回答!