MATLAB: Fitting probability distributions to the data (allfitdist)

allfitdistfittingprobability distribution

Hello
I have found the amazing script allfitdist which fits all valid parametric distributions to the data and sorts them using a metric (e.g. BIC or AIC). In a blog post there is a example for a normal distribution:
% Create a normally distributed (mu: 5, sigma: 3) random data set
x = normrnd(5, 3, 1e4, 1);
% Compute and plot results. The results are sorted by "Bayesian information
% criterion".
[D, PD] = allfitdist(x, 'PDF');
If I do this, I'm getting the Rayleigh as winning distribution. In the blog post they got the normal distribution (as expected). The plot I'm getting is in the attachment.
Does somebody see from where this problem comes? I have the newest version of Matlab. Is this the problem?

Best Answer

It is probably a result of changes in default behavior across MATLAB versions. You are getting that, because the log likelihood value for Raleigh Distribution returns a complex number, which shouldn't happen. You can go to line 212 to and append this there:
if ~isreal(NLL)
error('non-finite NLL');
end
Now the program will catch the complex log likelihood and disregard the distribution like it should.