% LMex060403_4th.m
% Page 457-458 in
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Application of poisspdf
% Four measurements — k1, k2, k3 & k4 — are taken on a Poisson random 
% variable, X for the purpose of testing Ho: ?=0.8 vs. ? > 0.8
% A) What is the decision rule if the level of significance is to be 0.10? 
% B) What will the power of the test be when ? = 1.2?
% For A, the expected value of 4 observations is 4*lambda (Example 31210),
% So analyze the pdf for lambda=4*0.8
% Written in 2003 by Eugene.Gallagher@umb.edu, revised 3/11/2011
lambda=4*0.8;alpha=0.1;
% Right tailed test
X=0:20;
% for a right-tailed test:
testcdf=1-poisscdf(X,lambda);
% for a left tailed test use: testcdf=poisscdf(X,lambda);
i=min(find(testcdf<alpha));
fprintf('The critical value for alpha=%3.1f is %2.0f\n',alpha,X(i));
fprintf('With crtical value = %2.0f, the alpha level is %6.4f\n',...
    X(i),testcdf(i))
% B) What is the power of the test if lambda= 1.2?
lambdah1=1.2;
% Right tailed test, appropriate for this problem:
PTypeII=poisscdf(X(i-1),lambdah1*4);
% Left tailed test: %PTypeII=1-poisscdf(X(i-1),lambdah1*4)
Power=1-PTypeII;
fprintf('With critical value=%2.0f & lambda=%3.1f, P(Type II error)=%6.4f, and the power of the test is %5.2f%%\n',...
    X(i),lambdah1,PTypeII,Power*100)
% Power is 0.349