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

时间:2016-07-27 07:27:41

标签: python scipy statistics statsmodels scientific-computing

目前,正如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()

提供如下图:

enter image description here

但是我想测试对(一般化的)hyperbolic distribution的拟合,因为我有一些假设,它可以提供更好的拟合。

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

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

提前致谢!

1 个答案:

答案 0 :(得分:2)

由于您的发行版不在scipy.stats,您可以将其添加到包中或尝试做事#34;手动"。

前者看一下scipy.stats包的source code - 添加新发行版可能不是那么多工作!

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

相关问题