Skip to main content
 首页 » 编程设计

python之使用 scipy.stats 根据双曲线分布拟合经验分布

2024年10月17日10soundcode

目前,我正在根据理论分布拟合经验分布,如 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 中,您可以将其添加到包中或尝试“手动”执行操作。

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

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