% LMex030306_4th.m
% Creating a discrete pdf from the sum of two dice
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Example 3.3.6, 4th edition page 155
% Written by Eugene.Gallagher@umb.edu, 10/7/2010, revised 1/12/11
% http://alpha.es.umb.edu/faculty/edg/files/edgwebp.html
% Find pdf for two dice by full enumeration
% See also crapsmof65.m, crapstable.m
die=1:6;
twodice=repmat(die,6,1)+repmat(die',1,6);
twodice=twodice(:); % convert to a column vector.
mink=min(twodice);maxk=max(twodice);
k=[mink:maxk]';lenk=length(k);ltwod=length(twodice);
% Simple problem, find P(X=5)
fprintf('The probability of rolling a 5 with 2 dice is:\n')
format rat
disp(sum(twodice==5)/ltwod);
fprintf('The entire discrete pdf in rational format:\n\n')
fprintf('       k             p_x(k)\n')
pxk=sum(repmat(twodice,1,lenk)==repmat(k',ltwod,1))'/ltwod;
disp([k pxk])
format
bar(k,pxk,1);xlabel('Sum of 2 dice'); ylabel('p_x(k)');
title('Example 3.3.6'),figure(gcf)
fprintf('The entire discrete pdf in numeric format:\n\n')
fprintf(' k  p_x(k)\n');
for i=1:lenk
    fprintf('%2.0f  %5.4f\n',k(i),pxk(i));
end