Note
Go to the end to download the full example code.
Markov chains: gambler’s ruin as an absorbing chain#
A Markov chain moves between states with probabilities that depend only
on the current state, not on how it got there, so the whole process is
described by one transition matrix. In gambler’s ruin, a gambler with
capital i (out of a target N) makes fair even-money bets until
reaching 0 (ruin) or N (target); the capital after each bet is a
Markov chain on the states 0..N with two absorbing ends. Both the
absorption probabilities and the expected number of steps have simple
closed forms for a fair game, used here to check the numerical solve.
import numpy as np
from mathematicskit.probability import MarkovChain
from mathematicskit.probability.visualizers.plots import plot_transition_matrix
Build the transition matrix for capital 0..N#

<Axes: title={'center': 'Transition matrix'}, xlabel='to state', ylabel='from state'>
Absorption probabilities and expected duration#
transient = list(range(1, n_capital))
b = chain.absorption_probabilities(transient, absorbing=[0, n_capital])
t = chain.expected_steps_to_absorption(transient)
for i, (row, steps) in zip(transient, zip(b, t)):
print(
f"start at {i}: P(ruin)={row[0]:.4f}, P(reach {n_capital})={row[1]:.4f} "
f"(closed form {i / n_capital:.4f}), E[steps]={steps:.2f} (closed form {i * (n_capital - i)})"
)
start at 1: P(ruin)=0.8333, P(reach 6)=0.1667 (closed form 0.1667), E[steps]=5.00 (closed form 5)
start at 2: P(ruin)=0.6667, P(reach 6)=0.3333 (closed form 0.3333), E[steps]=8.00 (closed form 8)
start at 3: P(ruin)=0.5000, P(reach 6)=0.5000 (closed form 0.5000), E[steps]=9.00 (closed form 9)
start at 4: P(ruin)=0.3333, P(reach 6)=0.6667 (closed form 0.6667), E[steps]=8.00 (closed form 8)
start at 5: P(ruin)=0.1667, P(reach 6)=0.8333 (closed form 0.8333), E[steps]=5.00 (closed form 5)
Total running time of the script: (0 minutes 0.021 seconds)