gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:09:12 25 4
gpt4 key购买 nike

目前,我正在根据理论分布拟合经验分布,如 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 中的双曲线分布吗?或者有什么解决方法吗?

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

提前致谢!

最佳答案

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

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

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

关于python - 使用 scipy.stats 根据双曲线分布拟合经验分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38606505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com