Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to normalize a histogram in MATLAB?

How to normalize a histogram such that the area under the probability density function is equal to 1?

like image 214
edgarmtze Avatar asked Mar 16 '11 03:03

edgarmtze


People also ask

How do you normalize a histogram in Python?

To normalize a histogram in Python, we can use hist() method. In normalized bar, the area underneath the plot should be 1.


1 Answers

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

enter image description here

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 
like image 136
abcd Avatar answered Sep 26 '22 04:09

abcd