【问题标题】:Fitting empirical distribution against a hyperbolic distribution using scipy.stats使用 scipy.stats 对双曲线分布拟合经验分布
【发布时间】:2016-07-27 07:27:40
【问题描述】:

目前,我正在根据 Fitting empirical distribution to theoretical ones with Scipy (Python)? 中解释的理论分布拟合经验分布

使用scipy.stats 分布,结果显示与hyperbolic secant 分布非常吻合。

这是我目前使用一些 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()

提供如下图:

但我也想针对(广义的)hyperbolic distribution 测试拟合度,因为我假设它可能会提供更好的拟合度。

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

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

提前致谢!

【问题讨论】:

  • 为什么投反对票?当然有一些关系,但我的问题是关于 scipy.stats 明确。
  • 在这里的评论中,有些人问了同样的问题(scipy.stats 中双曲分布的实现)但没有得到答案:stackoverflow.com/questions/24011209/…
  • 啊,好吧..我不想创建冗余。但是我现在添加了我的代码和一个情节,以便问题变得更加清晰!

标签: python scipy statistics statsmodels scientific-computing


【解决方案1】:

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

对于前者,请查看 scipy.stats 包中的 source code - 添加新发行版可能不是很多工作!

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

【讨论】:

  • 感谢您的回答!
猜你喜欢
  • 2020-12-16
  • 2019-03-31
  • 2011-10-01
  • 2019-06-04
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 2021-03-14
相关资源
最近更新 更多