How does histfit()/fitdist() compute its fit parameters?

I am trying to fit a curve to a histogram of some data and found that Weibull distributions do a really good job. I would like to report the Weibull parameters that fit my data but am having issues. I can use histfit() to plot the histogram and the best fit line together, but it doesn't return the correct parameters, so I used fitdist() to get them. However, when I try to plot my Weibull distribution from these parameters obtained from fitdist(), it never looks very good. Here is some sample code I use to do this with fitdist():
[counts1,bins1] = hist(data,numel(data)^0.5); %bins1 contains the locations of bin centers
weibull_p1 = fitdist(data,'weibull');
a_ = weibull_p1.A;
b_ = weibull_p1.B;
weibull_fit1 = (b_/a_)*(bins1./a_).^(b_-1).*exp((-bins1./a_).^b_);
So I plugged in the locations of the bin centers as x-values to compute the y-values of the Weibull function. However, the fit is terrible and is not even close to what histfit() plots. I thought histfit might use a different method of fitting, but I opened it in the editor and discovered that all it does is call fitdist() exactly as I did. The difference is apparently how it calculates y-values (copied from the histfit() function):
xd = get(hh,'Xdata'); % Gets the x-data of the bins.
rangex = max(xd(:)) - min(xd(:)); % Finds the range of this data.
binwidth = rangex/nbins; % Finds the width of each bin.
area = n * binwidth;
y = area * pdf(pd,x);
Why doesn't Matlab use the equation for the Weibull distribution to calculate y-values? Am I doing this wrong? I want to be able to report the Weibull parameters that best fit my data so that anyone can plug them in and get the same best fit curve that I found. But when I try to check this myself, it doesn't match up.
Thank you!

1 Comment

I just figured this out myself. I had the equation wrong. The exponent at the very end of that equation is supposed to be evaluated before the negative sign is applied. It should be this instead:
weibull_fit1 = (b_/a_)*(bins1./a_).^(b_-1).*exp(-((bins1./a_).^b_));
This fixes my problem. Sorry! False alarm.

Sign in to comment.

Answers (0)

Asked:

on 14 Jun 2016

Commented:

on 15 Jun 2016

Community Treasure Hunt

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

Start Hunting!