(* Gambler's Ruin: absorbing Markov chain for betting $1 at a time against a bank with $n. The game stops if the player or the bank goes broke. *)
(* We can take as states how many dollars the gambler has, ordered as {1,2, .., n-1, 0, n}, where 0, n are the absorbing states. *)
n=10;
Q = SparseArray[Join[Table[{i,i+1}->1/2,{i,1,n-2}],Table[{i+1,i}->1/2,{i,1,n-2}]]];
R = SparseArray[{{1,1}->1/2, {n-1,2}->1/2}];

P = SparseArray[Join[Table[{i,i+1}->1/2,{i,1,n-2}],Table[{i+1,i}->1/2,{i,1,n-2}],{{1,n}->1/2, {n-1,n+1}->1/2,{n,n}->1, {n+1,n+1}->1}]];
MatrixForm[P]

MatrixForm[Q]

(* The eigenvalues of Q are less than 1 in magnitude, so Q^n -> 0 as n-> Infinity*)
N[Eigenvalues[Q]]

(*What happens over time?*)
MatrixForm[MatrixPower[N[P],40]]

(* Expected time till absorbtion?*)
MatrixForm[N[Inverse[IdentityMatrix[n-1]-Q]].Table[1,{n-1}]]

(* Probabilities of going broke / winning the bank? *)
MatrixForm[Inverse[IdentityMatrix[n-1]-Q].R]