フィルターのクリア

Properly normalize a pdf histogram

16 ビュー (過去 30 日間)
Christoph
Christoph 2013 年 10 月 13 日
コメント済み: Christoph 2013 年 10 月 14 日
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 Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by