MATLAB Answers

Histogram Fitting in Matlab

31 views (last 30 days)
Umang Patel
Umang Patel on 10 Oct 2019
Edited: Umang Patel on 11 Oct 2019
Hello,
How to do histogram fitting?

  0 Comments

Sign in to comment.

Accepted Answer

Jeff Miller
Jeff Miller on 11 Oct 2019
First, I suggest you change the histogram plot so that the frequencies are represented by the areas of the different bars rather than the heights (this is more standard when the bars are different widths), like this:
A = histogram('BinEdges',bin_e,'BinCounts',MessData.profile(i).strip_current(idx),'Normalization','PDF');
Second, you can estimate distribution parameters from unequal bin widths with the Cupid toolbox. For example, add the following code after the modified histogram statement in your loop:
probs = MessData.profile(i).strip_current(idx); % I guess these are frequencies
probs = probs / sum(probs); % normalize to probabilities
bintops = bin_e(2:end); % EstChiSq just wants the tops of the different bins.
myNormal = Normal(0,1);
myNormal.EstChiSq(bintops,probs)
thisrangemin = myNormal.InverseCDF(0.001);
thisrangemax = myNormal.InverseCDF(0.999);
x = linspace(thisrangemin,thisrangemax,100);
pdf = myNormal.PDF(x);
hold on
plot(x,pdf); % Superimpose predicted curve on histogram
I attach the result for one of your datasets.

  2 Comments

Jeff Miller
Jeff Miller on 11 Oct 2019
This error indicates that you did not install the Cupid toolbox:
Undefined function or variable 'Normal'.
You must do that to use the solution I suggested. Sorry if that wasn't clear in my original post.
Jeff Miller
Jeff Miller on 11 Oct 2019
The parameter searching routine starts with the current values. For example, when you use
myNormal = Normal(0,1);
the parameter search always starts with mean 0 and standard deviation 1. If this mean and sd are very far off for the dataset that you are trying to fit, then the search routine will not converge properly on the best estimates. So, you need to look a little bit at your data and pick more realistic starting parameter values for your search. For example, you might use something like:
guessMu = (bintops(1) + bintops(end)) / 2;
guessSD = (bintops(end) - bintops(1)) / 5;
myNormal = Normal(guessMu,guessSD);

Sign in to comment.

More Answers (1)

J. Alex Lee
J. Alex Lee on 10 Oct 2019
If you have access to the underlying data...
...and you have a model for the distribution, then you can just do parameter estimation based on the data. A common distribution should have well-established parameter estimation techniques. (your example looks normal, so mean() and std() would work well).
...and you have a model for the distribution but not a way to estimate parameters for it, you can try to force your histogram-generating function to use equal bin sizes, then proceed to do your curve fitting.
If you don't have access to the underlying data...
...maybe you can weight your residuals inversely to the bin widths?

  0 Comments

Sign in to comment.

Sign in to answer this question.