%  Exercise with Markov chains.  Determine probability
%  of winning a simplified game of baseball with different
%  batting orders.

%---------------------------------------------------------
%states:  runs, outs, onbase
%  1       0,0,0
%  2       0,0,1
%  3       0,1,0
%  4       0,1,1
%  5       0,2
%  6       1,0,0
%  7       1,0,1
%  8       1,1,0
%  9       1,1,1
% 10       1,2
% 11       2
%---------------------------------------------------------

%  Transition matrix for A:
%  A makes an out with probability .6, a base hit with 
%  probability .3, and a homerun with probability .1.

PA=zeros(11,11);
PA(6,1) = .1;  PA(2,1) = .3;  PA(3,1) = .6;
PA(11,2) = .1;  PA(7,2) = .3;  PA(4,2) = .6;
PA(8,3) = .1;  PA(4,3) = .3;  PA(5,3) = .6;
PA(11,4) = .1;  PA(9,4) = .3;  PA(5,4) = .6;
PA(5,5) = 1;
PA(11,6) = .1;  PA(7,6) = .3;  PA(8,6) = .6;
PA(11,7) = .4;  PA(9,7) = .6;
PA(11,8) = .1;  PA(9,8) = .3;  PA(10,8) = .6;
PA(11,9) = .4;  PA(10,9) = .6;
PA(10,10) = 1;
PA(11,11) = 1;

%  Transition matrix for B:
%  B makes an out with probability .7, a base hit with 
%  probability .1, and a homerun with probability .2.

PB=zeros(11,11);
PB(6,1) = .2;  PB(2,1) = .1;  PB(3,1) = .7;
PB(11,2) = .2;  PB(7,2) = .1;  PB(4,2) = .7;
PB(8,3) = .2;  PB(4,3) = .1;  PB(5,3) = .7;
PB(11,4) = .2;  PB(9,4) = .1;  PB(5,4) = .7;
PB(5,5) = 1;
PB(11,6) = .2;  PB(7,6) = .1;  PB(8,6) = .7;
PB(11,7) = .3;  PB(9,7) = .7;
PB(11,8) = .2;  PB(9,8) = .1;  PB(10,8) = .7;
PB(11,9) = .3;  PB(10,9) = .7;
PB(10,10) = 1;
PB(11,11) = 1;

%  Initial state vector:  no runs, no outs, no one on base.

x0 = zeros(11,1);  x0(1) = 1;

%  By analysis, each batter hits at most twice before the
%  game is over.

BABAx0 = PB*PA*PB*PA*x0,
ABABx0 = PA*PB*PA*PB*x0,
fprintf('Probability of winning if A hits first = %f\n',BABAx0(11))
fprintf('Probability of winning if B hits first = %f\n',ABABx0(11))
