gpt4 book ai didi

Python - 根据这些值拟合 GEV 分布

转载 作者:行者123 更新时间:2023-12-01 09:01:03 25 4
gpt4 key购买 nike

我对 Python 很陌生,我在互联网上浏览过,但找不到任何可以帮助我解决问题的逻辑。

我在图中有降水值,现在我需要根据图中的这些值拟合 GEV 分布。每个值等于从 1974 年到 2017 年的一年的最大值(因此总共有 43 个值)。

这些是值:

max_precip = [9.4, 38.0, 12.5, 35.3, 17.6, 12.9, 12.4, 19.6, 15.0, 13.2, 12.3, 16.9, 16.9, 29.4, 13.6, 11.1, 8.0, 16.6, 12.0, 13.1, 9.1, 9.7, 21.0, 11.2, 14.4, 18.8, 14.0, 19.9, 12.4, 10.8, 21.6, 15.4, 17.4, 14.8, 22.7, 11.5, 10.5, 11.8, 12.4, 16.6, 11.7, 12.9, 17.8]

我发现我需要使用gev.fit,所以我想使用以下内容:

t = np.linspace(1,43,43)
fit = gev.fit(max_precip,loc=3)
pdf = gev.pdf(t, *fit)
plt.plot(t,pdf)
plt.plot(t, max_precip, "o")

但这仅打印图表中 max_precip 的点,而不打印 GEV 分布。

有人可以帮助我吗?抱歉,如果这个问题已经被问过,我找不到类似的问题。

我使用了这些导入:

import csv
import matplotlib.pyplot as plt
import numpy as np

from dateutil.rrule import rrule, YEARLY
import datetime
from matplotlib.dates import DateFormatter
from scipy.stats import genextreme as gev
from scipy.stats import genpareto as gpd
from scipy.optimize import minimize

最佳答案

我已尝试适应您的数据

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import genextreme as gev

def main(rvs):
shape, loc, scale = gev.fit(rvs)
return shape, loc, scale

if __name__ == '__main__':
rvs = [9.4, 38.0, 12.5, 35.3, 17.6, 12.9, 12.4, 19.6, 15.0, 13.2, 12.3, 16.9, 16.9, 29.4, 13.6, 11.1, 8.0, 16.6, 12.0, 13.1, 9.1, 9.7, 21.0, 11.2, 14.4, 18.8, 14.0, 19.9, 12.4, 10.8, 21.6, 15.4, 17.4, 14.8, 22.7, 11.5, 10.5, 11.8, 12.4, 16.6, 11.7, 12.9, 17.8]

shape, loc, scale = main(rvs)

print(shape)
print(loc)
print(scale)

l = loc + scale / shape

xx = np.linspace(l+0.00001, l+0.00001+35, num=71)
yy = gev.pdf(xx, shape, loc, scale)

hist, bins = np.histogram(rvs, bins=12, range=(-0.5, 23.5), density=True)
plt.bar(bins[:-1], hist, width = 2, align='edge')

plt.plot(xx, yy, 'ro')
plt.show()

但我得到的是

-0.21989526255575445
12.749780017954315
3.449061347316184

对于形状loc比例。如果你看GEV distribution as defined in scipy ,当 shape 为负数时,有效区间为 [loc + scale/shape...+infinity]。我计算了后一个值,它等于

-2.935417290135696

应该可以...

Python3、Anaconda、scipy 1.1、Windows 10 64 位

更新

好的,我已经更新了代码并添加了绘图,看起来有些合理。这是您要找的吗?基本上,技巧是对其进行直方图绘制并绘制与 PDF 重叠的密度箱

enter image description here

关于Python - 根据这些值拟合 GEV 分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52455797/

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