Properly normalize a pdf histogram

26 views (last 30 days)
Dear Matlab experts!
I am currently a bit confused about how to normalize a probability distribution histogram properly to its area (so that the sum over all bin-areas would be one).
I am creating random numbers following a logistic pdf accordingly to x_logistic=ln(p/(1-p)) where p is a random number.
Then I use histc and an edge vector to create my histogram data, normalize it by dividing it by the area. However the deviation between histogram and pdf is quite significant. Maybe someone can comment on the script? (I expect the histogram getting closer to the actual pdf for high sample numbers and small bins...?)
Thank you in advance!
All the best, Chris
clear all, close all;
maxiter=10^5;
x_limit=15;
x_res=0.01;
X=log((rand(maxiter,1)./(1-rand(maxiter,1))));
x=[-x_limit:x_res:x_limit];
[N, BIN]=histc(X, x);
Nnorm=N./trapz(x,N);
subplot(2,1,1)
h=bar(x,Nnorm, 'hist'), hold on
plot(x, exp(-x)./(1+exp(-x)).^2,'r--')
title('PDF for logistic distribution')
hold off
A1=trapz(x, exp(-x)./(1+exp(-x)).^2)
A2=trapz(x,Nnorm)
subplot(2,1,2)
bar(x, cumsum(Nnorm*x_res)), hold on
plot(x,exp(x)./(1+exp(x)),'r--')
title('Cumulative PDF for logistic distribution')

Accepted Answer

Jonathan LeSage
Jonathan LeSage on 14 Oct 2013
Edited: Jonathan LeSage on 14 Oct 2013
Your code looks good, and you've definitely normalized the histogram correctly. However, you've got one thing that needs fixing. The random values, p, in your probability density function should be the same random values. You have generated them independently, but p only need to be randomly generated once. I've made a simple fix to your code and commented on the lines!
maxiter = 10^5;
x_limit = 15;
x_res = 0.01;
% Generate vector of uniform random numbers
randVec = rand(maxiter,1);
% f(p) = ln(p/(1-p)) => PDF random values utilize same p vector
X = log(randVec./(1-randVec));
x = -x_limit:x_res:x_limit;
[N, BIN] = histc(X, x);
Nnorm = N./trapz(x,N);
subplot(2,1,1)
h = bar(x,Nnorm, 'hist'); hold on;
plot(x, exp(-x)./(1+exp(-x)).^2,'r--')
title('PDF for logistic distribution')
hold off
A1 = trapz(x, exp(-x)./(1+exp(-x)).^2);
A2 = trapz(x,Nnorm);
subplot(2,1,2)
bar(x, cumsum(Nnorm*x_res)), hold on
plot(x,exp(x)./(1+exp(x)),'r--')
title('Cumulative PDF for logistic distribution')
  1 Comment
Christoph
Christoph on 14 Oct 2013
Dear Jonathan!
Thank you for your explanation and help!
I think this is the perfect solution now!
Best, Chris

Sign in to comment.

More Answers (0)

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!