Schematex
markov·Kemeny & Snell, Finite Markov Chains·education, probability·complexity 3/3·since v0.8.0

Gambler's ruin (absorbing chain)

The classic absorbing Markov chain with two sinks. The engine classifies the transient and absorbing states and computes the fundamental matrix — the probability of ending broke vs rich and the expected number of steps to absorption.

For the probability instructor

Open in Playground →
markov·§
↘ preview
100%
Markov chain — Gambler's ruin 5 states, 8 transitions. Classification: 0 recurrent, 3 transient, 2 absorbing. Absorbing: Broke, Rich. Expected steps to absorption t: { S1=3, S2=4, S3=3 }. Absorption probabilities B: S1→(Broke=0.75, Rich=0.25); S2→(Broke=0.5, Rich=0.5); S3→(Broke=0.25, Rich=0.75). Gambler's ruin 1 1 0.5 0.5 0.5 0.5 0.5 0.5 $0 $4 S1 S2 S3
UTF-8 · LF · 15 lines · 292 chars✓ parsed·3.2 ms·5.7 KB SVG

What this shows

The canonical absorbing Markov chain. A gambler with $1, $2, or $3 (states S1–S3) bets on a fair coin, moving up or down $1 each round, and stops only at ruin ($0) or the target ($4). The two endpoints are declared absorbing — once entered, never left (a self-loop of probability 1) — and the engine cross-checks that assertion against the matrix.

This is where the engine's linear algebra pays off. It classifies S1–S3 as transient and Broke/Rich as absorbing, then forms the fundamental matrix N = (I − Q)⁻¹ to compute the two answers an absorbing chain is built for: the absorption probabilities (starting from each state, the chance of ending broke vs rich — for a fair game, proportional to the distance to each barrier) and the expected number of steps to absorption. Those computed values, carried in data-*, are the result no drawing tool can give.

Markov chain syntax