% LMex040201_4th.m
% Example 4.2.1 in 
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Table 4.21 & Table 4.2.2 Larsen & Marx (2006, p. 277)
% Written by Eugene Gallagher for EEOS601 in 2001, revised 1/9/11
% Eugene.Gallagher@umb.edu
% Comparing Poisson pdf and binomial pdf
p=1/5;
n=5;
k = [0:n]';
lambda=1;
p_pdf = poisspdf(k,lambda);
p_pdf=[p_pdf;1-sum(p_pdf)];
b_pdf= binopdf(k,n,p);
b_pdf=[b_pdf;1-sum(b_pdf)];
kdisp=[0:6]';
fprintf('Table 4.2.1\n')
fprintf('%f1.0  %5.3f   %5.3f\n',[kdisp b_pdf p_pdf]');

% Plot as a grouped histogram
    bar(kdisp,[b_pdf p_pdf],'grouped');
    axis([-.6 6.6 0 0.5])
    legend('Binomial pdf','Poisson pdf')
    xlabel('k','Fontsize',16);
    ylabel('Frequency','Fontsize',16)
    title('Table 4.2.1','Fontsize',20);
    text(3.03,0.4,'n = 5','FontSize',18);
    figure(gcf);pause
  %      [X1,Y1]=ginput(1)  % Used to position the text statement
p=1/100;
n=100;
ndisp=[0:9]';
k = [0:n]';
lambda=1;
p_pdf = poisspdf(ndisp,lambda);
p_pdf=[p_pdf;1-sum(p_pdf)];
b_pdf= binopdf(ndisp,n,p);
b_pdf=[b_pdf;1-sum(b_pdf)];
kdisp=[0:10]';
fprintf('Table 4.2.1\n')
fprintf('%f1.0  %5.3   %5.3f\n',[kdisp b_pdf p_pdf])
% Plot as a grouped histogram
    bar(kdisp,[b_pdf p_pdf],'grouped');
    legend('Binomial pdf','Poisson pdf','FontSize',18)
    axis([-.6 10.6 0 0.4])
    xlabel('k','FontSize',16);
    ylabel('Frequency','FontSize',16)
    title('Table 4.2.2','FontSize',20);
    text(5,0.32,'n = 100','FontSize',18);
    figure(gcf);pause
  %  [X2,Y2]=ginput(1)   Used to position the text statement       
    