The normalization of histcounts
38 views (last 30 days)
Show older comments
I would like to get the probability density function (PDF) from an array of data A (contained in the attached "a.mat" file).
If I understood correctly, if I use the normalization option called "probability", I would get the "relative frequency histogram".
Instead, if I use the normalization option called "pdf", I would get an "empirical estimate of the Probability Density Function".
However, when I check the sum of the probabilities ,I get "1" if I use the "probability" option, but I do not get "1" if I use the "pdf" option:
load('a.mat', 'A')
num_bins = 70;
B = histcounts(A,num_bins,'Normalization','probability');
sum(B)
C = histcounts(A,num_bins,'Normalization','pdf');
sum(C)
Shouldn't "sum(B)" give the sum of the relative frequencies, and "sum(C)" the sum of the the blocks' areas representing percentages?
What did I do wrong?
0 Comments
Accepted Answer
Steven Lord
on 4 Aug 2023
For probability, each element in the output is the number of elements in the input that fall into that bin divided by the total number of elements in the input. So if you sum the elements in the output, what you get is the total number of elements in the input that fall into any of the bins divided by the total number. That's why its row in the table in the description of the 'Normalization' name-value argument says "The sum of the bin values is less than or equal to 1." It can be less than 1 if the 'BinLimits' or 'BinEdges' that you specified exclude one or more of the points in the input from being assigned into any of the bins, for example.
For pdf, each element in the output is the number of elements in the input that fall into that bin divided by the product of the width of the bin and the total number of elements in the input. If each of your bins were 1 unit wide, the 'pdf' and the 'probability' would be the same. If each of your bins were 0.1 units wide, each element in the output normalized by 'pdf' would be ten times as large as the corresponding element in the output normalized by 'probability' and if I summed the output of 'pdf' normalization I'd expect to get a result of 10.
x = randn(1, 1e5);
prob_BW1 = histcounts(x, 'BinWidth', 1, 'Normalization', 'probability');
pdf_BW1 = histcounts(x, 'BinWidth', 1, 'Normalization', 'pdf');
prob_BWtenth = histcounts(x, 'BinWidth', 0.1, 'Normalization', 'probability');
pdf_BWtenth = histcounts(x, 'BinWidth', 0.1, 'Normalization', 'pdf');
format longg
shouldBeSame = [prob_BW1.', pdf_BW1.']
BWtenth_results = [prob_BWtenth; pdf_BWtenth; pdf_BWtenth./prob_BWtenth].'
All the elements in the third column of BWtenth_results are either 10 (or close to it) or NaN (if there's no data in x that fell into that particular bin.)
And as I said above, the sum of the probabilities is 1 but the sum of the PDF values is 10 because the bin width was 1/10.
[sum(prob_BWtenth), sum(pdf_BWtenth)]
All those calculations I did assumed that the bin width was the same for each bin. If your bins had different widths (because you selected a non-uniformly spaced set of BinEdges) then the equivalent of the third column of BWtenth_results for that set of bins would reflect the spacing for each different bin.
More Answers (1)
the cyclist
on 4 Aug 2023
Edited: the cyclist
on 4 Aug 2023
PDF is the probability density, not the probability. To get the probability for a given bin, you need to multiply by the bin width.
Your sum of C does not take that into account. MATLAB's "probability" normalization (your B calculation) is doing that for you.
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!