% LMex020415_4th.m
% Example 2.4.15 Bayes theorem & burglar alarms
% Using 2.4.14 as a model. Pp 66-67 in 
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene Gallagher 2001
% Eugene.Gallagher@umb.edu, revised 1/15/11
% http://alpha.es.umb.edu/faculty/edg/files/edgwebp.html
% Application of Bayes theorem
% PA_1       = Probability of burglaries in population = Prevalence Rate
% PA_2       = 1-PA_1=Probability that patient is cancer-free
% PB         = Burglar alarm goes off
% PBgivenA1  = 0.95  = Sensitivity of the test
% PBgivenA2  = 5/370 = False Positive Frequency
% PBcgivenA1 = False Negative Frequency
% PBcgivenA2 = No alarm and no burglary = Specificity
% Bayes Theorem for a test's yield or predictive value
% Yield = Predictive Value = (Prevalence)*(Sensitivity)/
%                (Prevalence)*(Sensitivity)+ (1-Prevalence)*(1-Sensitivity)                    
PA_1=2/1e4
PA_2=1-PA_1
PBgivenA1=0.95
% False positive rate:
PBgivenA2=5/730
PA1givenB=PBgivenA1*PA_1/(PBgivenA1*PA_1+PBgivenA2*PA_2)

PBgivenA1=[.95 .97 .99 .999];
PA1givenB=(PBgivenA1*PA_1)./(PBgivenA1*PA_1+PBgivenA2*PA_2);
disp('                    Table 2.4.4')
disp('                      P(B|A_1)  ')
disp('                     Sensitivity')
fprintf('          %4.2f    %4.2f    %4.2f   %6.4g\n',PBgivenA1)
fprintf('Yield     %5.3f   %5.3f   %5.3f   %5.3f\n',PA1givenB)
