Difference between manually fitting data with a distribution and Histfit() function?

25 views (last 30 days)
I'm trying to fit my time series data with a distribution. In the beginning, I used the function fitdist() to get the parameters and then used makedist() and pdf() to plot the histogram and pdf. However, the plot is a little bit different from the plot obtained with the function histfit(). Here's my code and plots (The left one is manual fitting while the right is plotted by function histfit()). Could anyone point out my mistakes? Is it due to the different scaling of y axis for the pdf?
a = n_rt(2:991); % time series data
subplot(1,2,1)
yyaxis left
histogram(a, 20); % histogram
yyaxis right
pd = fitdist(a', 'Gamma'); % fit the data with gamma distribution
pd2 = makedist('Gamma', pd.a, pd.b); % make the pdf
y = pdf(pd2, 0.2:0.01:1.8);
y = y/sum(y);
plot(0.2:0.01:1.8, y, 'LineWidth', 2);
subplot(1,2,2)
histfit(a, 20, 'gamma');

Accepted Answer

Jeff Miller
Jeff Miller on 18 May 2018
"Is it due to the different scaling of y axis for the pdf?" Yes.
  2 Comments
Yujian Wu
Yujian Wu on 19 May 2018
Hi Jeff. As is shown in the figure above, I plotted the histogram and PDF to different y axises. If I plot them to one y axis as the function histfit() does, the magnitude of PDF will be much lower than the histogram. Do you have any ideas of scaling the PDF?
Jeff Miller
Jeff Miller on 19 May 2018
histfit is really plotting the histogram and pdf to different y axes, even though it does not show you the scaling of the y2 axis. Note that the histogram and pdf are on inherently different scales; for example, if your sample had 10 times as many observations, then the histogram's Y axis would expand by a factor of 10, but of course the pdf values don't change at all.
Since the histogram and pdf are on completely different scales, histfit cleverly chooses a Y scale for the PDF so that some of the bars are above the curve and some are below, but this is entirely arbitrary. Too bad histfit doesn't label that y2 axis so that you could see the scale they used.
If you want a common y axis scale for the histogram and pdf values, try the histogram normalization options: either 'probability' or 'pdf' might do what you want.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!