% LMex020303_4th.m
% Example 2.3.3 p 39 from
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by E. Gallagher 8/4/01; revised 1/14/11
% Written by Eugene.Gallagher@umb.edu 1/14/11
% http://alpha.es.umb.edu/faculty/edg/files/edgwebp.html
% You draw two cards at random. What is the probability that the
% second card is greater than the first?
% Solved via Monte Carlo simulation.
fprintf('The expected value is %6.4f\n\n',8/17)
% Monte Carlo simulation
deck=1:13;
deck=repmat(deck',1,4); % tile 4 times for the 4 suits
deck=deck(:);
trials=1e5;
twocards=zeros(trials,2); % initialize a matrix to store results
for i=1:trials
j=randperm(52); % This shuffles the numbers 1 to 52
% saves the 1st 2 cards after randperm has shuffled the deck
twocards(i,:)=[deck(j(1)) deck(j(2))];
end
i=find(twocards(:,2)>twocards(:,1)); % compares columns in each row
p=length(i)/trials;
% Confidence intervals will be introduced in Chapter 6:
fprintf('In %6.0f trials, P(2nd card > first) was %6.4f\n',trials, p)
% 95% percent confidence interval for p:
halfwidth=1.96*sqrt(p*(1-p)/trials);
fprintf(...
'The upper and lower confidence limits are %6.4f and %6.4f\n',...
p-halfwidth,p+halfwidth)