matlab mle function error for gev parameter fitness

I'm trying to study using mle function to fit the parameter. The normal distribution example passes but a more complex gev fitness is wrong. The codes are as follows
clear;
clc;
f=@normtest;
x=normrnd(0,1,[1000,1]);
pha=mle(x,'pdf',f,'Start',[0,1])
pha = 1×2
-0.0297 1.0135
normfit(x)
ans = -0.0297
f=@gev_test;
mle(x,'pdf',f,'start',[1 0 0])
Error using mlecustom
Error in the custom pdf function 'gev_test'

Error in mle (line 304)
phat = mlecustom(data,varargin{:},'frequency',freq,'censoring',cens);

Caused by:
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise power.
function f=normtest(x,mu,sigma)
f=1/sigma/sqrt(2*pi)*exp(-(x-mu).^2/2/sigma^2);
end
function f=gev_test(x,k,mu,sigma)
t=(1+k*(x-mu)/sigma)^(-1/k);
f=1/sigma*t^(1+k)*exp(-t);
end
I am sure that the pdf of gev is right but there is some wrong with my code

Respuestas (1)

Hi Jianyu, The code you provided seems to have a few issues. Here are the modifications you need to make:
1. In the code, the `mle` function is being used to fit the parameters for both the normal and GEV distributions. However, for the GEV distribution, you should use the `gevfit` function instead of `mle`. The `gevfit` function is specifically designed for fitting GEV parameters.
2. The `gevfit` function does not require a PDF function as an input. Instead, it directly fits the parameters to the data using the maximum likelihood estimation (MLE) method.
clear;
clc;
% Fit parameters for the normal distribution
x = normrnd(0, 1, [1000, 1]);
norm_params = normfit(x);
norm_params
norm_params = -0.0563
% Fit parameters for the GEV distribution
x = normrnd(0, 1, [1000, 1]);
gev_params = gevfit(x);
gev_params
gev_params = 1×3
-0.2016 0.9880 -0.3490
In the modified code, `normfit` is used to fit the parameters for the normal distribution, and `gevfit` is used to fit the parameters for the GEV distribution. The parameters are then stored in the variables `norm_params` and `gev_params`, respectively.
Note that the GEV distribution has three parameters: location (mu), scale (sigma), and shape (k). The `gevfit` function returns estimates for these parameters.
I removed the function definitions `normtest` and `gev_test` because they are not needed for fitting the parameters using the built-in functions.
I hope this helps! Let me know if you have any further questions.

1 comentario

Jianyu
Jianyu el 11 de Jul. de 2023
Thanks for your answer.
But actually I want to define a new pdf that matlab does not include, which is more complex pdf than gev distribution. So firstly, I have to make sure that this "easy" normal distribution can work and it really works! And next I have to try a little bit more comples gev distribution but an error happens. The gevfit, as a defined function, performs better but the thing I want to do is to construct a self-defined pdf.
Thank you for your advice anyway.

Iniciar sesión para comentar.

Productos

Versión

R2022b

Preguntada:

el 11 de Jul. de 2023

Comentada:

el 11 de Jul. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by