Layout 1 has two balloons.

layout1=(({{0, 0, 0, 0, 0, 1}, {0, 0, 1, 0, 0, 0}, {1, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1}, {0, 0, 1, 0, 0, 0}, {0, 1, 0, 0, 1, 0}}))

MatrixPower[layout1,20] // MatrixForm

Layout 2 has two ears and a rainbow.

layout2=(({{1, 0, 1, 0, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 1, 1}, {0, 0, 0, 1, 0, 0}}))

MatrixPower[layout2,20] //MatrixForm

Layout 3 has two rainbows.

layout3=(({{0, 0, 1, 0, 0, 0}, {0, 0, 1, 0, 0, 0}, {1, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 1}, {0, 0, 0, 1, 1, 0}}))

MatrixPower[layout3,20] //MatrixForm

Layout 4 has one snake, one ear, and one balloon.

layout4=(({{1, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 1}, {0, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1}, {0, 0, 0, 1, 0, 0}}))

MatrixPower[layout4,20] //MatrixForm

Layout 5 has one rainbow and two snakes.

layout5=(({{0, 1, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1}, {0, 0, 1, 1, 0, 0}, {0, 1, 0, 0, 0, 0}}))

MatrixPower[layout5,20] //MatrixForm

Now make Markov chains!

Layout 1 has two balloons.

layout1m=(({{0, 0, 0, 0, 0, 1}, {0, 0, 1, 0, 0, 0}, {1/2, 0, 0, 1/2, 0, 0}, {0, 0, 0, 0, 0, 1}, {0, 0, 1, 0, 0, 0}, {0, 1/2, 0, 0, 1/2, 0}}))

MatrixPower[layout1m,100] // MatrixForm

Layout 2 has two ears and a rainbow.

layout2m=(({{1/2, 0, 1/2, 0, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 1/2, 1/2}, {0, 0, 0, 1, 0, 0}}))

MatrixPower[1. layout2m,1] //MatrixForm

Layout 3 has two rainbows.

layout3m=(({{0, 0, 1, 0, 0, 0}, {0, 0, 1, 0, 0, 0}, {1/2, 1/2, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 1}, {0, 0, 0, 1/2, 1/2, 0}}))

MatrixPower[layout3m,1] //MatrixForm

Layout 4 has one snake, one ear, and one balloon.

layout4m=(({{1/2, 0, 1/2, 0, 0, 0}, {0, 0, 0, 0, 0, 1}, {0, 1/2, 0, 0, 1/2, 0}, {0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1}, {0, 0, 0, 1, 0, 0}}))

MatrixPower[1. layout4m,1] //MatrixForm

Layout 5 has one rainbow and two snakes.

layout5m=(({{0, 1, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1/2, 1/2}, {0, 0, 1/2, 1/2, 0, 0}, {0, 1, 0, 0, 0, 0}}))

MatrixPower[1. layout5m,100] //MatrixForm

Absorbing states

layout2m //MatrixForm

newmat2 = switch[newmat,4,5]

newmat2//MatrixForm

meanvar[newmat2, 2]

layout4m //MatrixForm

newmat4=switch[layout4m,4,6]

newmat4//MatrixForm

meanvar[newmat4,1]

kittymat=(({{1, 0, 0, 0, 0, 0, 0}, {3/5, 0, 2/5, 0, 0, 0, 0}, {0, 3/5, 0, 2/5, 0, 0, 0}, {0, 0, 3/5, 0, 2/5, 0, 0}, {0, 0, 0, 3/5, 0, 2/5, 0}, {0, 0, 0, 0, 3/5, 0, 2/5}, {0, 0, 0, 0, 0, 0, 1}}));

MatrixPower[1.0 kittymat,100] // MatrixForm

kittymat2=moveinorder[kittymat,1,6];

MatrixForm[%]

TeXForm[%]

1. nmat[qmaker[kittymat2,2] ].{1,1,1,1,1}//TeXForm

meanvar[kittymat2,2]

gamemat=1/6(({{2, 2, 2, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 2, 2, 0, 0, 0, 0, 0, 0}, {1, 0, 1, 2, 2, 0, 0, 0, 0, 0}, {1, 0, 0, 1, 2, 2, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 2, 2, 0, 0, 0}, {1, 0, 0, 0, 0, 1, 2, 2, 0, 0}, {1, 0, 0, 0, 0, 0, 1, 2, 2, 0}, {1, 0, 0, 0, 0, 0, 0, 1, 2, 2}, {1, 0, 0, 0, 0, 0, 0, 0, 3, 2}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 6}}))

TeXForm[%]

exactexplength[gamemat,1]

TeXForm[%]

Matrix Manipulation Commands

This command makes the matrix Q by eliminating the last k rows and k columns from the matrix given as input, where k is the number of absorbing states.

qmaker[mat_,k_] := Table[mat[[i,j]], {i,1,Length[mat]-k},  {j,1,Length[mat]-k}]

The next two functions combine to switch rows i and j in a matrix, usually a transition matrix.  The first generates the appropriate matrix, and the second applies it.

switchmatrix[n_,i_,j_] :=Block[{zeromatrix},
zeromatrix=Table[0,{count1,1,n},{count2,1,n}];
zeromatrix[[i,i]]=-1; zeromatrix[[i,j]]=1; zeromatrix[[j,j]]=-1; zeromatrix[[j,i]]=1;
Return[IdentityMatrix[n]+zeromatrix];
]

switch[mat_, i_, j_] :=
Inverse[switchmatrix[Length[mat],i,j]].mat.switchmatrix[Length[mat],i,j]

This function switches rows i and j in a matrix, but does it by moving row i into row j by sliding all intermediate rows up one position.  It assumes that i<j.

moveinorder[mat_,i_,j_]:= Block[
{newmat=mat},
For[k=i,k≤j-i,k++,
newmat=switch[newmat,k,k+1]
];
Return[newmat]
]

Given a matrix assumed to be Q, this function produces N.  The next produces N in decimal form - often much more useful.

nmat[q1_] :=Inverse[IdentityMatrix[Length[q1]]-q1]

decnmat[q1_] := Inverse[IdentityMatrix[Length[q1]]-1.q1]

This command finds the minimum length of a game.  The flag indicates whether the probability of having a game of minimum length is printed.

minfinder[gmat_, flag_] := Block[{decgmat, counter, rows},
decgmat=1.gmat;
counter=1;
rows=Length[gmat];
While[
MatrixPower[decgmat,counter][[1,Length[gmat]]]<=0,
counter++
];
If[flag,Return[{counter, MatrixPower[decgmat,counter][[1,Length[gmat] ]]} ], Return[counter] ];
]

This function produces values for the cumulative distribution function for the value "upper".  Do not set the flag to True to compute exact values unless you know that you want them!

cumulfunct[gmat_,upper_,exact_]:=If[exact,MatrixPower[gmat,upper][[1,Length[gmat] ]],MatrixPower[1. gmat,upper][[1,Length[gmat] ]] ];

This function finds the probability of a game of a specific length.

lengthprob[gmat_,upper_,exact_]:=If[exact,MatrixPower[gmat,upper][[1,Length[gmat] ]]-MatrixPower[gmat,upper-1][[1,Length[gmat] ]],MatrixPower[1. gmat,upper][[1,Length[gmat] ]] -MatrixPower[1. gmat,upper-1][[1,Length[gmat] ]] ];

The next two compute the expected length of a game, either in decimals or exact.  Again, don't use the exact unless you know you want it!

expectedlength[mat_,k_] := First[decnmat[qmaker[mat,k]].Table[1,{i,1,Length[mat]-k}]]

exactexplength[mat_,k_] := First[nmat[qmaker[mat,k]].Table[1,{i,1,Length[mat]-k}]]

This command computes the expected length vector and the variance vector for a game.  You give the command the moves matrix in canonical form and the number of absorbing states.

meanvar[mat_,k_] := Block[{q1,matsize, psi, tau, nsq, ndg, nmat2, tau2},
q1=1. qmaker[mat,k];
matsize=Length[q1];
psi=Table[1,{i,1,matsize}];
tau=nmat[q1].psi;
nsq=nmat[q1]^2;
ndg=Diagonal[nmat[q1]] IdentityMatrix[matsize];
nmat2=nmat[q1].(2 ndg - IdentityMatrix[matsize])-nsq;
tau2=(2 nmat[q1]-IdentityMatrix[matsize]).tau-tau^2;
Print[tau];
Print[tau2];
outtau=tau;
outtau2=tau2;
]