% LMcs030201_4th.m
% Case study 3.2.1 p 145 in 
% Larsen & Marx (2006) Introduction to Mathematical Statistics 4th edition
% Written by Eugene.Gallagher@umb.edu, written 2001, revised 1/12/11
% http://alpha.es.umb.edu/faculty/edg/files/edgwebp.html
% uses the statistics toolbox m.file hygepdf, the hypergeometric pdf
% If there were 10/144 (6.9%) defective apples in the shipment,
% A shipment is accepted if 2 or fewer apples in 15 are
% found to have a firmness less than 12 lbs. What is the probability
% that a 6.9% defective rate box would be deemed acceptable?
if exist('hygepdf','file')
    P=sum(hygepdf(0:2,144,15,10)); 
elseif exist ('LMtheorem030202_4th','file')
    P=sum(LMtheorem030202_4th(15,0:2,10,144));
else
    disp('Hypergeometric functions not on Matlab path.')
end
fprintf('If defective rate is %4.1f%%, acceptance rate is %4.1f%%.\n', ...
    10/144*100,P*100);
% If the defective rate was 30 of 144 or 21%.
if exist('hygepdf','file')
    P=sum(hygepdf(0:2,144,15,30));
elseif exist ('hypergeop','file')
    P=sum(hypergeop(144,15,30,0:2));
else
    disp('Hypergeometric functions not on Matlab path.')
end
fprintf('If defective rate is %4.1f%%, acceptance rate is %4.1f%%.\n', ...
    30/144*100,P*100);
% Produce the operating characteristic curve, Figure 3.2.5
PPD=0:.05:1;
DefectiveApples=round(PPD*144);
lengthApples=length(DefectiveApples);
OCP=zeros(lengthApples,1);
for i=1:lengthApples
    SoftApples=DefectiveApples(i);
    if exist('hygepdf','file')
        P=sum(hygepdf(0:2,144,15,SoftApples)); 
    elseif exist ('LMtheorem030202_4th','file')
        P=sum(LMtheorem030202_4th(15,0:2,SoftApples,144));
    else
        disp('Hypergeometric functions not on Matlab path.')
    end
    OCP(i)=P;
end
plot(PPD*100,OCP,'.k',PPD*100,OCP,'-k','LineWidth',1.5);
ax1=gca;
ax1=gca; % save the handle for the axes of the graph
ylabel('P(sample passes)','Color','k','FontSize',16)
set(ax1,'Ytick',[0:0.2:1],'YColor','k','FontSize',12)
title('Figure 3.2.5','FontSize',20)
set(ax1,'Xtick',[0:10:100],'XColor','k','FontSize',12)
xlabel('Presumed percent defective','FontSize',16);
hold on
% This plot simply puts a vertical line on the graph
plot([20.8 20.8],[0 .355],'-.b','LineWidth',2);
% This plot simply puts a horizontal line on the graph
plot([0 20.8],[.355 .355],'-.b','LineWidth',2);
text(22,0.08,'20.8%','FontSize',17)
text(22,0.355,'35.5% Pass Rate','FontSize',17)
title('Figure 3.2.5','FontSize',20)
figure(gcf)
hold off
