% -------------- CDA6530  Fall 2014 =====================
% example to illustrate confidence interval

clear all;
sampleN = 100; lambda = 5;
X = -log(rand(sampleN,1)) / lambda;  % exponential distribution.
sampleMean = mean(X); 
sampleVar  = sum( (X-sampleMean).*(X-sampleMean) ) / (sampleN - 1);
% ----- 95% confidence interval --------
confidenceInterval = 1.96*sqrt(sampleVar)/sqrt(sampleN);

%============================
sampleN2 = 10000; lambda = 5;
Y = -log(rand(sampleN2,1)) / lambda;  % exponential distribution.

sampleMean2 = mean(Y); 
sampleVar2  = sum( (Y-sampleMean2).*(Y-sampleMean2) ) / (sampleN2 - 1);
% ----- 95% confidence interval --------
confidenceInterval2 = 1.96*sqrt(sampleVar2)/sqrt(sampleN2);

errorbar(1:2, [sampleMean sampleMean2], [confidenceInterval confidenceInterval2], 
[confidenceInterval confidenceInterval2]);
%axis([0 3 0 0.3]);