% LMFig070406_4th.m
% Pages 495-498 in 
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.edu 3/12/11, revised 3/12/11
% Use 7.2.1b as a model
K=menu('Pick a Figure','7.46a Uniform','7.46b exponential, n6',...
    '7.46c Poisson','7.46d Exponential n15');
n=6;N=100;
Binsize=1;
tx='t ratio (n=6)';
ymax=0.42;
if K==1
    % Plot the distribution figure
    y=[0 1];fy=[1 1];
    plot(y,fy,'--k','LineWidth',3)
    axis([0 1.05 0 1.1]);
    set(gca,'ytick',[0 1],'xtick',[0 1],'FontSize',20)
    xlabel('y','FontSize',20);ylabel('Probability Density','FontSize',20);
    text(0.5,0.92,'f_y(y) = 1','FontSize',18);
    title('Figure 7.4.6: Uniform Distribution');figure(gcf);pause
    % settings for the Monte Carlo simulated data:
    tl='Figure 7.4.6a: Uniform';
    R=rand(n,N);
    xmin=-6.2;xmax=6.2;
    mu=0.5;
elseif K==2
    % Plot the distribution figure
    YMAX=6.2;y=[0:0.01:YMAX];fy=exppdf(y,1);
    plot(y,fy,'--k','LineWidth',3)
    axis([0 YMAX 0 1.1]);
    set(gca,'ytick',[0:.5:1],'xtick',[0:2:6],'FontSize',20)
    xlabel('y','FontSize',20);ylabel('Probability Density','FontSize',20);
    text(1.5,0.35,'f_y(y) = e^{-y}','FontSize',18);
    title('Figure 7.4.6: Exponential Distribution');figure(gcf);pause
    % Settings for the Monte Carlo Simulated data
    tl='Figure 7.4.6b: Exponential';
    R=exprnd(1,n,N);
    xmin=-7.2;xmax=4.2;
    mu=1;
elseif K==3
    % Plot the distribution figure
    % use Figure 5.1.1 as a model
    lambda=5;k5=0:10;pxk5=poisspdf(k5,lambda);
    h2=stem(k5,pxk5,'Marker','none');
    xlabel('k','FontSize',20)
    axis([-.5 10.5 0 0.19])
    set(h2,'LineWidth',3)
    figure(gcf);pause
    set(gca,'ytick',[0:.08:.16],'xtick',[0:2:10],'FontSize',20)
    xlabel('k','FontSize',20);ylabel('Probability Density','FontSize',20);
    text(7.5,0.08,'f_y(y) = {e^{-5 5^k}}/k!','FontSize',18);
    title('Figure 7.4.6: Poisson Distribution');figure(gcf);pause
    % Settings for the Monte Carlo Simulated data
    tl='Figure 7.4.6c: Poisson';
    lambda=5;
    R=poissrnd(lambda,n,N);
    xmin=-5.2;xmax=4.2;
    mu=lambda;
elseif K==4
    % Plot the distribution figure same as before
    YMAX=6.2;y=[0:0.01:YMAX];fy=exppdf(y,1);
    plot(y,fy,'--k','LineWidth',3)
    axis([0 YMAX 0 1.1]);
    set(gca,'ytick',[0:.5:1],'xtick',[0:2:6],'FontSize',20)
    xlabel('y','FontSize',20);ylabel('Probability Density','FontSize',20);
    text(1.5,0.35,'f_y(y) = e^{-y}','FontSize',18);
    title('Figure 7.4.6: Exponential Distribution');figure(gcf);pause
    % Settings for the Monte Carlo Simulated data
    n=15;
    tx='t ratio (n=15)';
    tl='Figure 7.4.6d';
    R=exprnd(1,n,N); 
    xmin=-4.2;xmax=4.2;
    mu=1;
end
edges=ceil(xmin):Binsize:floor(xmax);
tdf=n-1;
V=[xmin xmax 0 ymax];
    mr=mean(R)';
    s=std(R)';
    mu=repmat(mu,N,1);
    Z1=(mr-mu)./(s./sqrt(n));
[N1,BIN1] = histc(Z1,edges);
bar(edges,N1/N,'histc');
axis(V)
set(get(gca,'Children'),'FaceColor',[.8 .8 1])
ylabel('Density','FontSize',20);
ax1=gca;set(ax1,'xtick',[ceil(xmin):floor(xmax)],'FontSize',20);
set(ax1,'ytick',[0.1:0.1:0.6],'FontSize',20)
hold on
figure(gcf);pause
y_j=xmin:0.01:xmax;
fy_j=tpdf(y_j,tdf)*Binsize; 
h1=line(y_j,fy_j);
set(h1,'linestyle','--','color','r','linewidth',3)
xlabel(tx,'FontSize',20),ylabel('Density','FontSize',20)
title(tl,'FontSize',22)
figure(gcf);pause
hold off