probability density function normalization

I would like to illustrate the probability density function and the histogram of a data set. This is the code I used so far:
clc;
xValues = 0:0.001:0.5;
for i = [21,24]
figure;
grid on;
hold on;
% newcolors = [0 0 0; 1 0 0; 0.3010 0.7450 0.9330; 0.9290 0.6940 0.1250];
% colororder(newcolors);
for j = 0:c:(3*c) %alle 3 Messarten vergleichen
% histfit(T_mean{i+j},20,'kernel')
histogram(T_mean{i+j},20,'Normalization','pdf','DisplayStyle','stairs');
pd = fitdist(T_mean{i+j},'Kernel');
y = pdf(pd,xValues);
plot(xValues,y)
% ksdensity(T_mean{i+j})
end
hold off;
end
where c is 24. The T_mean is a table composed of 4 tables with length of 24, which are 24 different sets of data. In this case I only need 21 and 24, which each contain a vector. With this code, the probability density function and the histogram have the same normalization. But the y-axis is do large. The area under the pdf should be smaller than 1, so the y-axis could be read in %. Perhaps I don't understand the pdf function correctly. Here is a picture of one of the graph outputs:
The pdf seems to have different definitions in Matlab:
and
Matlab seems to use the second one in this case.
How can I normalize the histogram as 'probability' but also normalize the pdf the same way?

 Accepted Answer

The pdf values are defined so that the total area under the pdf curve equals 1, but these values will exceed 1 (and, hence, not look like probabilities) when the range of X is less than one unit. To make pdf values look more like probabilities, you can adjust for that roughly like this:
% Generate some example values that look a little like yours:
mu = 0.25;
sigma = 0.1;
data = randn(500,1)*sigma + mu;
range = max(data) - min(data);
figure;
hold on;
nbins = 20; % for the histogram
histogram(data,nbins,'Normalization','probability','DisplayStyle','stairs');
pd = fitdist(data,'Kernel');
xValues = 0:0.001:0.5; % for computing the pdf
y = pdf(pd,xValues) / nbins * range; % adjust to match probability based on nbins & range
plot(xValues,y)

4 Comments

Oh, nice, it works! Thank you.
I just have one question: Why does the pdf of the histogram depend on the number of bins, shouldn't the pdf be the same for a different amount of bins?
If you use 'normalization','pdf', the bin pdfs will be independent of the number of bins. But when you normalize for probability, the heights of the bins have to add up to 1.0. So when there are more bins, they each have to have lower heights to still add up to 1.
Maybe it is helpful to think of the limiting case where the bins are so numerous (and thus narrow) that there is at most one score per bin. In that case the probability-based bin heights must all be 1/n. As the bins get wider, you'll start getting some bins with heights 2/n, then 3/n, etc.
Now I understand, thank you.
Welcome
I have some data consisting of three parameters and I want to know the probability density function of this data in Matlab
I hope you can help me
Thank you so much

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!