# Histogram Fitting in Matlab

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

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.

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 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);

J. Alex Lee on 10 Oct 2019