scipy genextreme fit 在相同数据上返回与 MATLAB gev fit 函数不同的参数

huangapple go评论77阅读模式
英文:

scipy genextreme fit returns different parameters from MATLAB gev fit function on the same data

问题

MATLAB:

% 创建 MATLAB 数组
x = [0.5700, 0.8621, 0.9124, 0.6730, 0.5524, 0.7608, 0.2150, 0.5787, ...
           0.7210, 0.7826, 0.8181, 0.5449, 0.7501, 1.1301, 0.7784, 0.5378, ...
           0.9550, 0.9623, 0.6865, 0.6863, 0.6153, 0.4372, 0.5485, 0.6318, ...
           0.5501, 0.8333, 0.8044, 0.9111, 0.8560, 0.6178, 1.0688, 0.7535, ...
           0.7554, 0.7123, 0.7589, 0.8415, 0.7586, 0.3865, 0.3087, 0.7067];
       
disp(x);

parmHat = gevfit(x);

disp('估计的参数 (A, B):');
disp(parmHat);

Python:

import numpy as np
import scipy.stats as stats

x = np.array([0.5700, 0.8621, 0.9124, 0.6730, 0.5524, 0.7608, 0.2150, 0.5787,
                    0.7210, 0.7826, 0.8181, 0.5449, 0.7501, 1.1301, 0.7784, 0.5378,
                    0.9550, 0.9623, 0.6865, 0.6863, 0.6153, 0.4372, 0.5485, 0.6318,
                    0.5501, 0.8333, 0.8044, 0.9111, 0.8560, 0.6178, 1.0688, 0.7535,
                    0.7554, 0.7123, 0.7589, 0.8415, 0.7586, 0.3865, 0.3087, 0.7067])

# 将 GEV 分布拟合到数据
parameters3 = stats.genextreme.fit(x)

print("估计的 GEV 参数:", parameters3)

估计的 GEV 参数: (1.0872284332032054, 0.534605335200113, 0.6474387313912493)

我希望得到相同的参数,但结果完全不同。有任何帮助吗?

英文:

I'm trying to port some code from MATLAB to PYTHON and I realized gevfit function in MATLAB seems to behave differently from scipy genextreme, so I realized this minimal example:

MATLAB

% Create the MATLAB array
x = [0.5700, 0.8621, 0.9124, 0.6730, 0.5524, 0.7608, 0.2150, 0.5787, ...
           0.7210, 0.7826, 0.8181, 0.5449, 0.7501, 1.1301, 0.7784, 0.5378, ...
           0.9550, 0.9623, 0.6865, 0.6863, 0.6153, 0.4372, 0.5485, 0.6318, ...
           0.5501, 0.8333, 0.8044, 0.9111, 0.8560, 0.6178, 1.0688, 0.7535, ...
           0.7554, 0.7123, 0.7589, 0.8415, 0.7586, 0.3865, 0.3087, 0.7067];

disp(numbers);

parmHat = gevfit(x);

disp('Estimated parameters (A, B):');
disp(parmHat);

> Estimated parameters (A, B): -0.3351 0.1962 0.6466

PYTHON

import numpy as np
import scipy.stats as stats

x = np.array([0.5700, 0.8621, 0.9124, 0.6730, 0.5524, 0.7608, 0.2150, 0.5787,
                    0.7210, 0.7826, 0.8181, 0.5449, 0.7501, 1.1301, 0.7784, 0.5378,
                    0.9550, 0.9623, 0.6865, 0.6863, 0.6153, 0.4372, 0.5485, 0.6318,
                    0.5501, 0.8333, 0.8044, 0.9111, 0.8560, 0.6178, 1.0688, 0.7535,
                    0.7554, 0.7123, 0.7589, 0.8415, 0.7586, 0.3865, 0.3087, 0.7067])

# Fit the GEV distribution to the data
parameters3 = stats.genextreme.fit(x)

print("Estimated GEV parameters:", parameters3)

> Estimated GEV parameters: (1.0872284332032054, 0.534605335200113,
> 0.6474387313912493)

I'd expect the same parameters, but results are totally different. Any help?

答案1

得分: 1

The method genextreme.fit无法计算出正确的结果。您可以通过为数值求解器提供比genextreme.fit默认值更好的初始值来帮助生成正确的值。初始值是通过为形状、位置和尺度参数提供值来设置的:

In [29]: from scipy.stats import genextreme

In [30]: x = np.array([0.5700, 0.8621, 0.9124, 0.6730, 0.5524, 0.7608, 0.2150, 0.5787,
    ...:                     0.7210, 0.7826, 0.8181, 0.5449, 0.7501, 1.1301, 0.7784, 0.5378,
    ...:                     0.9550, 0.9623, 0.6865, 0.6863, 0.6153, 0.4372, 0.5485, 0.6318,
    ...:                     0.5501, 0.8333, 0.8044, 0.9111, 0.8560, 0.6178, 1.0688, 0.7535,
    ...:                     0.7554, 0.7123, 0.7589, 0.8415, 0.7586, 0.3865, 0.3087, 0.7067])
    ...: 

In [31]: genextreme.fit(x, 0.34, loc=0.65, scale=0.20)  # 包括参数的初始猜测
Out[31]: (0.33513328610099824, 0.6466250071208526, 0.19615018966970216)

请注意,SciPy的genextreme使用的参数c是Matlab中参数k的负值。此外,请注意,SciPy中参数的顺序为clocationscale,而Matlab中的顺序为kscalelocation

英文:

The method genextreme.fit is failing to compute the correct result. You can help it generate the correct value by providing initial values for the numerical solver that are better than the default used by genextreme.fit. The initial values are set by providing values for the shape, location and scale parameters:

In [29]: from scipy.stats import genextreme

In [30]: x = np.array([0.5700, 0.8621, 0.9124, 0.6730, 0.5524, 0.7608, 0.2150, 0.5787,
    ...:                     0.7210, 0.7826, 0.8181, 0.5449, 0.7501, 1.1301, 0.7784, 0.5378,
    ...:                     0.9550, 0.9623, 0.6865, 0.6863, 0.6153, 0.4372, 0.5485, 0.6318,
    ...:                     0.5501, 0.8333, 0.8044, 0.9111, 0.8560, 0.6178, 1.0688, 0.7535,
    ...:                     0.7554, 0.7123, 0.7589, 0.8415, 0.7586, 0.3865, 0.3087, 0.7067])
    ...: 

In [31]: genextreme.fit(x, 0.34, loc=0.65, scale=0.20)  # Include initial guess of the parameters
Out[31]: (0.33513328610099824, 0.6466250071208526, 0.19615018966970216)

Note that the parameter c used by SciPy's genextreme is the negative of the parameter k in Matlab. Also note that the order of the parameters in SciPy is c, location, scale, while in Matlab it is k, scale, location.

huangapple
  • 本文由 发表于 2023年7月31日 23:23:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76805028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定