% LMFig040301_4th.m
% Page 293 in Larsen & Marx (2006) Introduction to Mathematical Statistics
% 4th edition
% Generate 20 binomial random numbers, p(success)=0.5
% Written in 2001 by Eugene.Gallagher@umb.edu, revised 1/7/11
% Use the statistical toolbox's binopdf
X=0:20;n=20;p=0.5;
Y = binopdf(X,n,p);
bar(X,Y,1);axis([0 21 0 0.2]);title('Figure 4.3.1','FontSize',20);
set(get(gca,'Children'),'FaceColor',[.8 .8 1])
ylabel('Probability','FontSize',16);
ax1=gca;set(ax1,'xtick',0:2:20,'FontSize',14);
set(ax1,'ytick',0:0.05:0.2,'FontSize',14)
figure(gcf);pause
% Superimpose the normal probability pdf:
% The more general equation(Legendre & Legendre, 1998 p. 147, 
% or LM eq 7.1.1 is:
% f(y_j)=1/(sqrt(2*pi)*sigma_j)*exp(-1/2*((y_j-mu_j)/sigma_j)^2)
mu_j=n*p;
sigma_j=sqrt(n*p*(1-p)); % sigmaj is the standard deviation
y_j=0:0.2:20;
%fy_j=1/(sqrt(2*pi)*sigma_j)*exp(-1/2*((y_j-mu_j)./sigma_j).^2);
% or the identical function:
fy_j=normpdf(y_j,mu_j,sigma_j);
% fyj=1/(sqrt(2*pi)*sigmaj)*exp(-1/2*((y-muj)/sigmaj).^2); 
% Plot using ax1 handle, saved above,to save this graph
% on top of the previous graph.
h1=line(y_j,fy_j,'Color','r','Parent',ax1,'Linewidth',3);
set(h1,'linestyle','--','color','r','linewidth',2)
xlabel(''),ylabel('Probability','FontSize',16)
title('Figure 4.3.1','FontSize',20)
figure(gcf);pause
  