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')
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!