问题
Suppose I have a set of univariate data held in the array errors
.
I would like to fit a PDF to my observed data distribution.
My PDF is defined in a function poissvmwalkpdf
, whose definition line looks like this:
function p = poissvmwalkpdf(theta, mu, kappa, xi)
Here, theta
is the error (the variable for which values in errors
are instances), and mu
, kappa
, and xi
are parameters of the PDF for which I want to find the best fit using maximum-likelihood estimation. This function returns the probability density at a given value of theta
.
Given all this, how would I use fminsearch
to find the values for mu
, kappa
, and xi
that best fit my observed errors
? The fminsearch
documentation doesn't make this clear. None of the examples in the documentation are examples of distribution fitting.
Note: The tutorial here clearly describes what distribution fitting is (as distinguished from curve fitting), but the example given does not use fminsearch
.
回答1:
Here is a minimal example of using fminsearch
to obtain maximum likelihood estimates (as requested in the comments):
function mle_fit_minimal
n = 100;
% for reproducibility
rng(333)
% generate dummy data
errors = normrnd(0,1,n,1);
par0 = [1, 1];
[par_hat, nll] = fminsearch(@nloglike, par0)
% custom pdf
function p = my_pdf(data, par)
mu = par(1);
sigma = par(2);
p = normpdf(data, mu, sigma);
end
% negative loglikelihood function -- note that the parameters must be passed in a
% single argument (here called par).
function nll = nloglike(par)
nll = -sum(log(my_pdf(errors, par)));
end
end
After formulating the likelihood function (or negative loglikelihood) it is just a simple optimization.
来源:https://stackoverflow.com/questions/35951934/using-fminsearch-to-perform-distribution-fitting