2

I ma creating the exponential distribution using inverse method. I want to normalize histogram. How can I do it?

This is my code

N=100;
Lambda=2;
r=rand(N,1);
X=-log(1-r)/Lambda;
hist(X), colormap(bone);

t = 0:0.01:5;
pdf=Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2)
2
  • 2
    Please define what you mean by normalizing a histogram
    – Dan
    Commented Oct 15, 2015 at 11:47
  • You multiply each element with a given number, pdf=c*Lambda*exp(-Lambda*t);. Commented Oct 15, 2015 at 11:52

1 Answer 1

2

The histogram should be normalized to unit area so that it can be compared with the theoretical pdf. To normalize to unit area you need to divide by the number of samples and by the bin width:

N = 100;
Lambda=2;
r = rand(N,1);
X = -log(1-r)/Lambda;
[hy, hx] = hist(X); %/ get histogram values
hy = hy/numel(X)/(hx(2)-hx(1)); %//normalize histogram
bar(hx, hy) %// plot histogram
t = 0:0.01:5;
pdf = Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2) %// plot pdf

enter image description here

Or use the new histogram function (introduced in R2014b), which automatically normalizes according to the specified normalization option:

N = 100;
Lambda=2;
r = rand(N,1);
X = -log(1-r)/Lambda;
histogram(X, 'Normalization', 'pdf') %// plot normalized histogram
t = 0:0.01:5;
pdf = Lambda*exp(-Lambda*t);
hold on, plot(t,pdf,'LineWidth',2) %// plot pdf
1
  • For lay readers such as myself, unit area means the total area of the curve must be equal to 1 (unity). Commented Nov 8, 2018 at 2:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.