Martingale — optional stopping (gambler's ruin)

A fair random walk (a martingale) starts at start and is stopped the first time it hits the lower or upper boundary. The optional stopping theorem says the win probability is exactly start/upper (with lower = 0): no stopping rule can beat the fair game. Press Run trials to watch the empirical win rate converge to the theoretical value (red tick).

fortune 30 steps 0 outcome playing theoretical win prob 0.300 empirical win prob —
win rate
green = empirical, red tick = theoretical
fortune path upper boundary (win) lower boundary (bust) start level