% LMFig060405_4th.m
% Using as a model %LMcs030201_4th.m, page 450 in
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written 3/10/11 (11:22 -11:57 am, )
% Eugene.Gallagher@umb.edu for EEOS601 UMASS/Boston
xmin=22.9;xmax=27.1;mu=25;
X1=xmin:0.005:mu;
X2=mu:0.005:xmax;
X=[X1 X2];
sigmaj=2.4;n=30;n2=60;alpha=0.05
CritVal=norminv(1-alpha/2,mu,sigmaj/sqrt(n));
CritVal2=norminv(1-alpha/2,mu,sigmaj/sqrt(n2));
CritVal3=norminv(alpha/2,mu,sigmaj/sqrt(n));
CritVal4=norminv(alpha/2,mu,sigmaj/sqrt(n2));
Power =[normcdf(CritVal3,X1,sigmaj/sqrt(n))  1-normcdf(CritVal,X2,sigmaj/sqrt(n))];
Power2=[normcdf(CritVal4,X1,sigmaj/sqrt(n2)) 1-normcdf(CritVal2,X2,sigmaj/sqrt(n2))];
x=[25.75 26.8]; 
Pow=1-normcdf(CritVal,x,sigmaj/sqrt(n));
Pow2=1-normcdf(CritVal2,x,sigmaj/sqrt(n2));
plot(X,Power,'k','LineWidth',3)
axis([xmin xmax 0, 1.1])
hold on
plot(X,Power2,'-.r','LineWidth',3)
set(gca,'Xtick',[23:0.5:25.5 x(1) 26 26.5 x(2) 27],'Ytick',[0:.25:1],'FontSize',18)
% This plots horizontal and vertical lines on the graph
plot([mu mu]',[0 1]','k','LineWidth',2)
plot([x(1) x(1);mu x(1);x(2) x(2);mu x(2)]',[0 Pow(1);...
    Pow(1) Pow(1);0 Pow(2);Pow(2) Pow(2)]','--m','LineWidth',3);
plot([x(1) x(1);mu x(1);x(2) x(2);mu x(2)]',[0 Pow2(1);...
    Pow2(1) Pow2(1);0 Pow2(2);Pow2(2) Pow2(2)]',':c','LineWidth',3);
ylabel ('Power = 1 - \beta','FontSize',20);
xlabel('Presumed value for \mu','FontSize',20)
title('Figure 6.4.5','FontSize',22)
t1=sprintf('Power = %6.4f',Pow(1));
t2=sprintf('Power = %6.4f',Pow(2));
t3=sprintf('Power = %6.4f',Pow2(1));
t4=sprintf('Power = %6.4f',Pow2(2));
text(mu+0.1,Pow(1)+.03,t1,'FontSize',18);
text(mu+0.1,Pow(2)-.03,t2,'FontSize',18);
text(mu+0.1,Pow2(1)+.03,t3,'FontSize',18);
text(mu+0.1,Pow2(2)+.03,t4,'FontSize',18);
figure(gcf)
pause
hold off