使用scipy.stats拟合经验分布到双曲线分布

3
目前,我正在根据Fitting empirical distribution to theoretical ones with Scipy (Python)?中的说明,将经验分布与理论分布进行拟合。使用scipy.stats分布,结果显示双曲正切分布的拟合效果很好。以下是我当前使用一些 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()

以下是类似于下面的图表:

enter image description here

但我还想测试一下(广义)双曲线分布是否更适合,因为我认为它可能会提供更好的拟合。

在scipy.stats中是否有可用的双曲线分布?或者是否有其他解决方法?

也可以使用其他软件包。

提前感谢!


相关:http://stackoverflow.com/questions/28934454/fitting-hyperbolic-and-harmonic-functions-with-curvefit - Eli Korvigo
为什么要踩我?当然有一些关系,但我的问题明确是关于scipy.stats的。 - Cord Kaldemeyer
在这里的评论中,有人问了同样的问题(在scipy.stats中实现双曲线距离),但没有得到答案:http://stackoverflow.com/questions/24011209/how-to-normalize-a-histogram-of-an-exponential-distributionin-scipy - Cord Kaldemeyer
啊,好的。我不想创建冗余。但是我现在添加了我的代码和一个图表,这样问题应该会更清晰! - Cord Kaldemeyer
1个回答

3
由于您的分布不在scipy.stats中,您可以将其添加到软件包中,或尝试手动进行操作。
对于前者,请查看scipy.stats软件包的源代码 - 将新分布添加到其中可能并不需要太多工作!
对于后一种选项,您可以使用最大似然方法。为此,请首先定义一个可提供分布概率密度函数(pdf)的方法。基于pdf构建一个函数,计算给定分布特定参数的数据的对数似然度。最后,通过使用scipy.optimize最大化该对数似然函数来将模型拟合到数据上。

感谢您的回答! - Cord Kaldemeyer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接