Properly normalize a pdf histogram

30 次查看(过去 30 天)
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')

采纳的回答

Jonathan LeSage
Jonathan LeSage 2013-10-14
编辑:Jonathan LeSage 2013-10-14
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 个评论
Christoph
Christoph 2013-10-14
Dear Jonathan!
Thank you for your explanation and help!
I think this is the perfect solution now!
Best, Chris

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by