
% Longin Jan Latecki, latecki@temple.edu, Sep. 2009
% plots histogram of samples drawn from exponential distribution
% and plots the exponential distribution
% the values of histogram are normalized so that the sum of the area of the
% histogram bars (rectangles) is equal to one
% This way both plots are directly comparable.

x = 0:0.1:10;
lambda=1/2;
f=lambda*exp(-lambda*x); %define f as exponential distribution

samples=rand(1000,1); %sample from uniform distribution U(0,1)
%samples = normrnd(0,1,1000,1);
samples=-(1/lambda)*log(1-samples); %apply F^inv
[hs, hx] = hist(samples,x);
hs=(hs/1000)*(101/10);
figure; bar(x,hs,'r'); %plots normalized histogram
hold on
plot(x,f,'LineWidth',3); %plots the exponential distribution
title('exponential pdf and the histogram of samples from the exponential pdf')

