Skip to main content
added 572 characters in body
Source Link
Luis Mendo
  • 112.2k
  • 13
  • 78
  • 150

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:

figureN = 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

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:

figure
[hy, hx] = hist(X); %/ get histogram values
hy = hy/numel(X)/(hx(2)-hx(1)); %//normalize histogram
bar(hx, hy) %// plot histogram
hold on, plot(t,pdf,'LineWidth',2) %// plot pdf

enter image description here

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
Source Link
Luis Mendo
  • 112.2k
  • 13
  • 78
  • 150

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:

figure
[hy, hx] = hist(X); %/ get histogram values
hy = hy/numel(X)/(hx(2)-hx(1)); %//normalize histogram
bar(hx, hy) %// plot histogram
hold on, plot(t,pdf,'LineWidth',2) %// plot pdf

enter image description here