How can I fit histogram?
6 views (last 30 days)
Show older comments
studentmatlaber
on 29 Oct 2021
Edited: studentmatlaber
on 18 Nov 2021
I want to normalize my histogram. I know there are commands histogram(x, nbits, 'Normalization','probability') and histogram(x, nbits, 'Normalization','pdf'). But what is the difference between 'probability' and 'pdf'?
0 Comments
Accepted Answer
the cyclist
on 30 Oct 2021
Edited: the cyclist
on 30 Oct 2021
Answering the first part of your question ...
With probability normalization, the sum of the bin heights will be 1. With pdf normalization, the integral of the bins (i.e. the sum of the bin heights times widths) will be 1.
Here is a silly example that illustrates the difference:
rng default
N = 5000;
x = binornd(1,0.5,N,1)/2;
figure
histogram(x, 5, 'Normalization','probability')
figure
histogram(x, 5, 'Normalization','pdf')
Answering the second part of your question ...
I believe you can use histcounts to get the normalized bin counts, then fit those values with normfit:
[binCounts, binEdges] = histcounts(x, 5, 'Normalization','probability');
binCenters = (binEdges(1:end-1) + binEdges(2:end))/2;
[muHat,sigmaHat] = normfit(binCenters,binCounts)
2 Comments
the cyclist
on 1 Nov 2021
You need to plot it according to the formula for a normal distribution. (See, e.g., this wikipedia page.)
You could just code that formula from scratch, but instead you could create a distribution object:
mu = 2;
sigma = 3;
pd_norm = makedist('Normal','mu',mu,'sigma',sigma)
x = -3:0.01:7;
plot(x,pdf(pd_norm,x))
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!