How to normalize a histogram such that the area under the probability density function is equal to 1?
To normalize a histogram in Python, we can use hist() method. In normalized bar, the area underneath the plot should be 1.
My answer to this is the same as in an answer to your earlier question. For a probability density function, the integral over the entire space is 1. Dividing by the sum will not give you the correct density. To get the right density, you must divide by the area. To illustrate my point, try the following example.
[f, x] = hist(randn(10000, 1), 50); % Create histogram from a normal distribution. g = 1 / sqrt(2 * pi) * exp(-0.5 * x .^ 2); % pdf of the normal distribution % METHOD 1: DIVIDE BY SUM figure(1) bar(x, f / sum(f)); hold on plot(x, g, 'r'); hold off % METHOD 2: DIVIDE BY AREA figure(2) bar(x, f / trapz(x, f)); hold on plot(x, g, 'r'); hold off
You can see for yourself which method agrees with the correct answer (red curve).
Another method (more straightforward than method 2) to normalize the histogram is to divide by sum(f * dx)
which expresses the integral of the probability density function, i.e.
% METHOD 3: DIVIDE BY AREA USING sum() figure(3) dx = diff(x(1:2)) bar(x, f / sum(f * dx)); hold on plot(x, g, 'r'); hold off
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With