% LMex030310_4th.m
% Example 3.3.10 Page 159 in 
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th Edition
% Application of binomial cdf
% Written by E. Gallagher, Eugene.Gallagher@umb.edu
% http://alpha.es.umb.edu/faculty/edg/files/edgwebp.html
% Written 10/7/2010, revised 1/12/10
% This problem is a simple demonstration that the sum of the elements of
% a discrete binomial pdf can also be computed as the difference of two
% cumulative distribution functions. This applies to any of the 
% discrete pdf's.
P1=sum(binopdf(21:40,50,0.6));
fprintf('         P using sum of binomial pdfs is %6.5f\n',P1)
P2=binocdf(40,50,0.6)-binocdf(20,50,0.6);
fprintf('P using difference of cumulative pdfs is %6.5f\n',P2)
% Note numerical calculations using different algorithms
% aren't exactly equivalent, P1-P2 is equal to
% -5.55e-016
% With numerical processors, non-integer numbers can not be represented
% exactly. The precision, called eps by Matlab, is the numeric procession
% with the computer and processor on which Matlab being run. If the
% difference is less than eps, two variables should be regarded as
% identical. On my processor, eps is 2.2204e-016, so the difference above
% while trivial is not less than the numeric precision of the processor
% It is unusual for Matlab to not produce exactly equivalent results, or
% results within eps, but anything this close should be regarded as
% identical
fprintf('The difference should be 0, but is %5.3g, which is > eps=%5.3g.\n',...
    P2-P1,eps)