% LMex030308_4th.m
% LMex030202 in the 3rd edition
% Example 3.2.2 Basketball problem.
% Page 157 in 
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Rhonda has a 65% probability of making a foul shot and two baskets are
% required to win
% Solve using absorbing Markov chain & by the method
% in Larsen & Marx, p. 157
% The 3 states for the Markov chain are 0 baskets made, 
% 1 basket, 2 baskets.
% Solution using Larsen & Marx methods followed by solution
% using an absorbing Markov Chain.
% Solve for the discrete pdf for up to 20 free throws.
% Need to find p_xk = P (drill ends on the kth throw)
k=2:20;
lk=length(k);
% Initialize two vectors to store results
p_xk=zeros(lk,1);  % Probability that the drill ends on the kth throw
Ek=zeros(lk,1);    % Expected values
for i=1:lk
    p_xk(i)=(k(i)-1)*(0.35).^(k(i)-2)*(0.65).^2; % Equation 3.3.4 in text
    Ek(i)=p_xk(i)*k(i);  % Expected value introduced on p 173-175
end
Ek=sum(Ek);  % Expected value, L&M 3rd edition p. 192, 4th ed p 157-158
             % this will equal tau(1) from 
             % the absorbing Markov chain described in the Advanced section
disp('The probability density function:')
disp('       k      p_x(k)')
disp([k(1:12)' p_xk(1:12)])
bar(k,p_xk,1);xlabel('Number of free throws,k','FontSize',16);
set(get(gca,'Children'),'FaceColor',[.8 .8 1])
ylabel('p_x(k)','FontSize',16),title('Example 3.3.8','FontSize',20)
figure(gcf)
disp('The expected number of free throws for Rhonda to win:')
disp(Ek)
disp('Sum the final states');
i=find(k>=8);
j=find(k==8);
p_xk(j)=sum(p_xk(i));
k=find(k>8);p_xk(k)=[]; 
kstring=['2  ';'3  ';'4  ';'5  ';'6  ';'7  ';'8+ '];
disp('Table 3.3.5')
disp([kstring num2str(p_xk)])

% Solution by absorbing Markov chains
% A very advanced topic: not needed for EEOS601
% Solution using absorbing Markov Chain theory, with absorb.m
% based on Kemeny & Snell (1976) see documentation in absorb.m
% The transition matrix P, with the 3 states, Made 2 is
% an absorbing state.
%         Made 0   Made 1  Made 2
% Made 0    0.35    0.65      0
% Made 1      0     0.35    0.65
% Made 2      0       0       1
P=[.35 .65 0
    0 .35 .65
    0   0   1]
tau=absorb(P);  % Tau is  the expected no. of throws to win
fprintf('The number of free throws required to get 2 is %6.4f \n',tau(1))

% New game: how long until Rhonda gets 2 in a row?
% Let's use the absorbing Markov chain to find the expected number of free
% throws if winning requires 2 made throws in a row:
% Solution using absorbing Markov Chain theory, with absorb.m
% based on Kemeny & Snell (1976) see documentation in absorb.m
% The transition matrix P, with the 3 states, Made 2 is
% an absorbing state.
% P=
%         Made 0   Made 1  Made 2
% Made 0    0.35    0.65      0
% Made 1    0.35      0     0.65
% Made 2      0       0       1
P=[.35 .65 0
   .35  0 .65
    0   0   1]
tau=absorb(P);  % Tau is  the expected no. of throws to win
fprintf(...
    'The number of free throws required to get 2 in a row is %6.4f \n',...
    tau(1))
