Properly normalize a pdf histogram
조회 수: 34 (최근 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')
댓글 수: 0
채택된 답변
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')
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!