
% 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;
fexp=lambda*exp(-lambda*x); %define f as exponential distribution
N=1000; %number of samples
samples=rand(N,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/N)*(101/10);
figure; bar(x,hs,'r'); %plots normalized histogram
hold on
plot(x,fexp,'LineWidth',3); %plots the exponential distribution
title('exponential pdf and the histogram of samples from the exponential pdf')


%Kernel density estimate
sigma=0.1;
for i=1:length(x)
    f(i)=(1/N)*sum(normpdf(x(i)-samples,0,sigma)); %we have a gaussian centered at each sample point
end

figure; plot(x,f,'r','LineWidth',3); %plots the kernel density estimate
hold on
plot(x,fexp,'LineWidth',3); 
title('exponential pdf and the kernel density estimate from samples from the exponential pdf')

