Steven R. Dunbar
Department of Mathematics
203 Avery Hall
University of Nebraska-Lincoln
Lincoln, NE 68588-0130
Voice: 402-472-3731
Fax: 402-472-8466

Stochastic Processes and
Advanced Mathematical Finance


Duration of the Gambler’s Ruin


Note: To best read these pages, you will need the latest version of the Mozilla Firefox browser, with the STIX fonts installed, The Chrome browser will also present a mostly readable version. In a few sections, you will also need the latest Java plug-in, and JavaScript must be enabled. If you use a browser other than Firefox you should be able to access the pages and run the applets. However, mathematical expressions will probably not display correctly. Firefox is currently the only browser that fully supports all of the standards.




Mathematically Mature: may contain mathematics beyond calculus with proofs.


Section Starter Question

Section Starter Question

Consider a gambler who wins or loses a dollar on each turn of a fair game with probabilities p = 12 and q = 12 respectively. Let his initial capital be $10. The game continues until the gambler’s capital either is reduced to 0 or has increased to $20. What is the length of the shortest possible game the gambler could play? What are the chances of this shortest possible game? What is the length of the second shortest possible game? How would you find the probability of this second shortest possible game occurring?


Key Concepts

Key Concepts

  1. The principle of first-step analysis, also known as conditional expectations, provides equations for important properties of coin-flipping games and random walks. The important properties include ruin probabilities and the duration of the game until ruin.
  2. Difference equations derived from first-step analysis or conditional expectations provide the way to deduce the expected length of the game in the gambler’s ruin, just as for the probability of ruin or victory.




  1. Expectation by conditioning is the process of deriving an expectation by conditioning the outcome over an exhaustive, mutually exclusive set of events, each of which leads to a simpler probability calculation, then weighting by the probability of each outcome of the conditioning events.
  2. First Step Analysis is how J. Michael Steele refers to the simple expectation by conditioning that we use to analyze the ruin probabilities and expected duration. It is a more specific description for coin-tossing games of the more general technique of expectation by conditioning.


Mathematical Ideas

Mathematical Ideas

Understanding a Stochastic Process

We start with a sequence of Bernoulli random variables, Y 1,Y 2,Y 3, where Y i = +1 with probability p and Y i = 1 with probability q. We start with an initial value T0 and set Y 0 = T0 for convenience. We define the sequence of sums Tn = i=0nY i. We are interested in the stochastic process T1,T2,T3,. It turns out this is a complicated sequence to understand in full, so we single out particular simpler features to understand first. For example, we can look at how many trials the process will experience until it achieves the value 0 or a. In symbols, consider N = min{n : Tn = 0,  or Tn = a} It is possible to consider the probability distribution of this newly defined random variable. Even this turns out to be fairly complicated, so we look at the expected value of the number of trials, D = E N. This is a special case of a larger class of probability problems called first-passage distributions for first-passage times.

Expected length of the game

Note that in the following we implicitly assume that the expected duration of the game is finite. This fact is true, see below for a proof.

Theorem 1. The expected duration of the game in the classical ruin problem is

DT0 = T0 q p a q p 1 (qp)T0 1 (qp)a    for pq


T0(a T0)  for p = 12 = q.

Proof. If the first trial results in success, the game continues as if the initial position had been T0 + 1. The conditional expectation of the duration conditioned on success at the first trial is therefore DT0+1 + 1. Likewise if the first trial results in a loss, the duration conditioned on the loss at the first trial is DT01 + 1.

This argument shows that the expected duration satisfies the difference equation, obtained by expectation by conditioning

DT0 = pDT0+1 + qDT01 + 1

with the boundary conditions

D0 = 0,Da = 0.

The appearance of the term 1 makes the difference equation non-homogeneous. Taking a cue from linear algebra, or more specifically the theory of linear non-homogeneous differential equations, we need to find the general solution to the homogeneous equation

DT0h = pD T0+1h + qD T01h

and a particular solution to the non-homogeneous equation. We already know the general solution to the homogeneous equation is DT0h = A + B(qp)T0. The best way to find the particular solution is inspired guessing, based on good experience. We can re-write the non-homogeneous equation for the particular solution as

1 = pDT0+1 DT0 + qDT01.

The right side is a weighted second difference, a difference equation analog of the second derivative. Functions whose second derivative is a constant are quadratic functions. Therefore, it make sense to try a function of the form DT0p = k + lT 0 + mT02. In the exercises, we show that the particular solution is actually DT0 = T0(q p) if pq.

It follows that the general solution of the duration equation is:

DT0 = T0(q p) + A + B(qp)T0 .

The boundary conditions require that A + B = 0, and A + B(qp)a + a(q p) = 0. Solving for A and B, we find

DT0 = T0 q p a q p 1 (qp)T0 1 (qp)a .

The calculations are not valid if p = 12 = q. In this case, the particular solution T0(q p) no longer makes sense for the equation

DT0 = (12)DT0+1 + (12)DT01 + 1

The reasoning about the particular solution remains the same however, and we can show that the particular solution is T02. It follows that the general solution is of the form DT0 = T02 + A + BT 0. The required solution satisfying the boundary conditions is

DT0 = T0(a T0).

Corollary 1. Playing until ruin with no upper goal for victory against an infinitely rich adversary, the expected duration of the game until ruin is

T0(q p)  for pq


 for p = 12 = q.

Proof. Pass to the limit a in the preceding formulas. □

Illustration 1

The duration can be considerably longer than we expect naively. For instance in a fair game, with two players with $500 each flipping a coin until one is ruined, the average duration of the game is 250,000 trials. If a gambler has only $1 and his adversary $1000, with a fair coin toss, the average duration of the game is 999 trials, although some games will be quite short! Very long games can occur with sufficient probability to give a long average.

Some Calculations for Illustration

pqT0aProbability Expected
of Ruin Duration

0.5 0.5 9 10 0.1000 9
0.5 0.590 100 0.1000 900
0.5 0.5 900 1,000 0.1000 90,000
0.5 0.5950 1,000 0.0500 47,500
0.5 0.58,000 10,000 0.200016,000,000

0.45 0.559 10 0.2101 11
0.45 0.55 90 100 0.8656 766
0.45 0.5599 100 0.1818 172
0.4 0.6 90 100 0.9827 441
0.4 0.699 100 0.3333 162

Proof that the duration is finite

The following discussion of finiteness of the duration of the game is adapted from [2] by J. Michael Steele.

When we check the arguments for the probability of ruin or the duration of the game, we find a logical gap. We have assumed that the duration DT0 of the game is finite. How do we know for sure that the gambler’s net winnings will eventually reach a or 0? This important fact requires proof.

The proof uses a common argument in probability, an “extreme case argument”. We identify an “extreme” event with a small but positive probability of occurring. We are interested in the complementary “good” event which at least avoids the extreme event. Therefore the complementary event must happen with probability not quite 1. The avoidance must happen infinitely many independent times, but the probability of such a run of “good” events must go to zero.

For the gambler’s ruin, we are interested in the event of the game continuing forever. Consider the extreme event that the gambler wins a times in a row. If the gambler is not already ruined (at 0), then such a streak of a wins in a row is guaranteed to boost his fortune above a and end the game in victory for the gambler. Such a run of luck is unlikely, but it has positive probability, in fact, probability P = pa. We let Ek denote the event that the gambler wins on each turn in the time interval [ka, (k + 1)a 1], so the Ek are independent events. Hence the complementary events EkC = Ω E k are also independent. Then D > na at least implies that all of the Ek with 0 k n fail to occur. Thus, we find

DT0 > na k=0nE kC = (1 P)n.

Note that

DT0 = |T0 = z D > na|T0 = z

for all n. Hence, DT0 = = 0, justifying our earlier assumption.


This section is adapted from [2] with additional background information from [1].

Algorithms, Scripts, Simulations

Algorithms, Scripts, Simulations


The goal is to simulate the duration until ruin or victory as a function of starting value. First set the probability p, number of Bernoulli trials n, and number of experiments k. Set the ruin and victory values r and v, also interpreted as the boundaries for the random walk. For each starting value from ruin to victory, fill an n × k matrix with the Bernoulli random variables. For languages with multi-dimensional arrays each the data is kept in a three-dimensional array of size n × k × (v r + 1). Cumulatively sum the Bernoulli random variables to create the fortune or random walk. For each starting value, for each random walk or fortune path, find the duration until ruin or victory is encountered. For each starting value, find the mean of the duration until ruin or victory. Finally, find a least squares polynomial fit for the duration as a function of the starting value.



R script for duration..

< 0.5 
< 300 
< 200 
victory < 10 
# top boundary for random walk 
ruin < 0 
# bottom boundary for random walk 
interval < victory  ruin + 1 
winLose < 2  (array( 0+(runif(nkinterval) <= p), dim=c(n,k, 
interval)))  1 
# 0+ coerces Boolean to numeric 
totals < apply( winLose, 2:3, cumsum
# the second argument ‘‘2:3’’ means columnwise in each panel 
start < outerarray(1, dim=c(n+1,k)), ruin:victory, ””) 
paths < array( 0 , dim=c(n+1, k, interval) ) 
paths[2:(n+1), 1:k, 1:interval] < totals 
paths < paths + start 
hitVictory < apply(paths, 2:3, (function(x)match(victory,x, nomatch=n+2))); 
hitRuin    < apply(paths, 2:3, (function(x)match(ruin,   x, nomatch=n+2))); 
# the second argument ‘‘2:3’’ means columnwise in each panel 
# If no ruin or victory on a walk, nomatch=n+2 sets the hitting 
# time to be two more than the number of steps, one more than 
# the column length.  Without the nomatch option, get NA which 
# works poorly with the comparison hitRuin < hitVictory next. 
duration < pmin(hitVictory, hitRuin)  1 
# Subtract 1 since R arrays are 1based, so duration is 1 less than index = duration > n 
# Remove durations greater than length of trials 
meanDuration = colMeans( duration, na.rm=TRUE) 
startValues < (ruin:victory); 
durationFunction < lm( meanDuration ˜ poly(startValues,2,raw=TRUE) ) 
# lm is the R function for linear models, a more general view of 
# least squares linear fitting for response ˜ terms 
plot(startValues, meanDuration, col = ”blue”); 
lines(startValues, predict(durationFunction, data=startValues), col = ”red”) 
     coefficients(durationFunction)[1], coefficients(durationFunction)[2], 
     coefficients(durationFunction)[3] ))

Octave script for ruin probabilities.

p = 0.5; 
n = 300; 
k = 200; 
victory =  10; 
# top boundary for random walk 
ruin    = 0; 
# bottom boundary for random walk 
probRuinBeforeVictory = zeros(1, victoryruin+1); 
for start = ruin:victory 
    winLose = 2  (rand(n,k) <= p)  1; 
    # 1 for Tails, 1 for Heads 
    totals = cumsum(winLose); 
    # n..n (every other integer) binomial rv sample 
    paths = [zeros(1,k); totals] + start; 
    victoryOrRuin = zeros(1,k); 
    for j = 1:k 
        hitVictory = find(paths(:,j) >= victory); 
        hitRuin  = find(paths(:,j) <= ruin); 
        if ( !rows(hitVictory) && !rows(hitRuin) ) 
           # no victory, no ruin 
           # do nothing 
        elseif ( rows(hitVictory) && !rows(hitRuin) ) 
           # victory, no ruin 
           victoryOrRuin(j) = hitVictory(1)1; 
           ## subtract 1 since vectors are 1based 
        elseif ( !rows(hitVictory) && rows(hitRuin) ) 
           # no victory, but hit ruin 
           victoryOrRuin(j) = (hitRuin(1)1); 
           ## subtract 1 since vectors are 1based 
        else # ( rows(hitvictory) && rows(hitruin) ) 
           # victory and ruin 
           if ( hitVictory(1) < hitRuin(1) ) 
             victoryOrRuin(j) = hitVictory(1)1; 
             # code hitting victory 
             ## subtract 1 since vectors are 1based 
             victoryOrRuin(j) = (hitRuin(1)1); 
             # code hitting ruin as negative 
             ## subtract 1 since vectors are 1based 
    durationUntilVictoryOrRuin(start + (ruin+1)) = mean(abs( victoryOrRuin )); 
durationFunction = polyfit([ruin:victory], durationUntilVictoryOrRuin,  
plot([ruin:victory], durationUntilVictoryOrRuin, ’+r’); 
hold on; 
x = linspace(ruin, victory, 101); 
fittedDuration = polyval(durationFunction, x); 
plot(x, fittedDuration, ’’); 
hold off; 
disp(”a_2”), disp(durationFunction(3)), 
disp(”a_1”), disp(durationFunction(2)), 
disp(”a_0”), disp(durationFunction(1))

Perl PDL script for ruin probabilities.

use PDL::NiceSlice; 
$p        = 0.5; 
$n        = 300; 
$k        = 200; 
$victory  = 10; 
$ruin     = 0; 
$interval = $victory  $ruin + 1; 
$winLose  = 2  ( random( $k, $n, $interval ) <= $p )  1; 
$totals   = ( cumusumover $winLose>xchg( 0, 1 ) )>transpose; 
$start    = zeroes( $k, $n + 1, $interval )>zlinvals( $ruin, $victory ); 
$paths = zeroes( $k, $n + 1, $interval ); 
# use PDL:NiceSlice on next line 
# Note the use of the concat operator here. 
$paths ( 0 : ( $k  1 ), 1 : $n, 0 : ( $interval  1 ) ) .= $totals; 
$paths      = $paths + $start; 
$hitVictory = $paths>setbadif( $paths < $victory ); 
$hitRuin    = $paths>setbadif( $paths > $ruin ); 
$victoryIndex = 
    ( $hitVictory ( ,, : )>xchg( 0, 1 )>minimum_ind ) 
    >inplace>setbadtoval( $n + 1 ); 
$ruinIndex = 
    ( $hitRuin ( ,, : )>xchg( 0, 1 )>maximum_ind ) 
    >inplace>setbadtoval( $n + 1 ); 
$durationUntilRuinOrVictory = 
    ( $victoryIndex>glue( 2, $ruinIndex )>xchg( 2, 1 ) )>xchg( 0, 1 ) 
    >setvaltobad( $n + 1 )>minimum; 
( $mean, $prms, $median, $min, $max, $adev, $rms ) = 
use PDL::Fit::Polynomial; 
$x = zeroes($interval)>xlinvals( $ruin, $victory ); 
( $ruinFunction, $coeffs ) = fitpoly1d $x, $mean, 3; 
print ”Duration_function_is:_”, $coeffs (0), ”+”, $coeffs (1), ”x+”, 
    $coeffs (2), ”xˆ2”, ”n”;

Scientific Python script for ruin probabilities.

import scipy 
p = 0.5 
n = 300 
k = 200 
victory = 10; 
ruin = 0; 
interval = victory  ruin + 1 
winLose = 2( scipy.random.random((n,k,interval)) <= p )  1 
totals = scipy.cumsum(winLose, axis = 0) 
start = scipy.multiply.outer( scipy.ones((n+1,k), dtype=int), scipy.arange(ruin, victory+1, dtype=int)) 
paths = scipy.zeros((n+1,k,interval), dtype=int) 
paths[ 1:n+1, :,:] = totals 
paths = paths + start 
def match(a,b,nomatch=None): 
    return  b.index(a) if a in b else nomatch 
# arguments: a is a scalar, b is a python list, value of nomatch is scalar 
# returns the position of first match of its first argument in its second argument 
# but if a is not there, returns the value nomatch 
# modeled on the R function match”, but with less generality 
hitVictory = scipy.apply_along_axis(lambda x:( match(victory,x.tolist(),nomatch=n+2)), 0, paths) 
hitRuin = scipy.apply_along_axis(lambda x:match(ruin,x.tolist(),nomatch=n+2), 0, paths) 
# If no ruin or victory on a walk, nomatch=n+2 sets the hitting 
# time to be two more than the number of steps, one more than 
# the column length. 
durationUntilRuinOrVictory = scipy.minimum(hitVictory, hitRuin) 
durationMasked =, n) 
meanDuration = scipy.mean(durationUntilRuinOrVictory, axis = 0) 
durationFunction = scipy.polyfit( scipy.arange(ruin, victory+1, dtype=int), meanDuration, 2) 
print ”Duration_function_is:_”, durationFunction[2], ”+”, durationFunction[1], ”x+”, durationFunction[0], ”xˆ2” 
# should return coefficients to (xruin)(victory  x), descending degree order


Problems to Work

Problems to Work for Understanding

    1. Using a trial function of the form DT0p = k + lT 0 + mT02, show that a particular solution of the non-homogeneous equation
      DT0 = pDT0+1 + qDT01 + 1

      is T0(q p).

    2. Using a trial function of the form DT0p = k + lT 0 + mT02, show that a particular solution of the non-homogeneous equation
      DT0 = 1 2DT0+1 + 1 2DT01 + 1

      is T02.

  1. A gambler starts with $2 and wants to win $2 more to get to a total of $4 before being ruined by losing all his money. He plays a coin-flipping game, with a coin that changes with his fortune.
    1. If the gambler has $2 he plays with a coin that gives probability p = 12 of winning a dollar and probability q = 12 of losing a dollar.
    2. If the gambler has $3 he plays with a coin that gives probability p = 14 of winning a dollar and probability q = 34 of losing a dollar.
    3. If the gambler has $1 he plays with a coin that gives probability p = 34 of winning a dollar and probability q = 14 of losing a dollar.

    Use “first step analysis” to write three equations in three unknowns (with two additional boundary conditions) that give the expected duration of the game that the gambler plays. Solve the equations to find the expected duration.

  2. A gambler plays a coin flipping game in which the probability of winning on a flip is p = 0.4 and the probability of losing on a flip is q = 1 p = 0.6. The gambler wants to reach the victory level of $16 before being ruined with a fortune of $0. The gambler starts with $8, bets $2 on each flip when the fortune is $6, $8, $10 and bets $4 when the fortune is $4 or $12. Compute the probability of ruin in this game.
  3. This problem is adapted from Stochastic Calculus and Financial Applications by J. Michael Steele, Springer, New York, 2001, Chapter 1, Section 1.6, page 9. Information on buy-backs is adapted from This problem suggests how results on biased random walks can be worked into more realistic models.

    Consider a naive model for a stock that has a support level of $20/share because of a corporate buy-back program. (This means the company will buy back stock if shares dip below $20 per share. In the case of stocks, this reduces the number of shares outstanding, giving each remaining shareholder a larger percentage ownership of the company. This is usually considered a sign that the company’s management is optimistic about the future and believes that the current share price is undervalued. Reasons for buy-backs include putting unused cash to use, raising earnings per share, increasing internal control of the company, and obtaining stock for employee stock option plans or pension plans.) Suppose also that the stock price moves randomly with a downward bias when the price is above $20, and randomly with an upward bias when the price is below $20. To make the problem concrete, we let Y n denote the stock price at time n, and we express our stock support hypothesis by the assumptions that

    Y n+1 = 21|Y n = 20 = 910 Y n+1 = 19|Y n = 20 = 110

    We then reflect the downward bias at price levels above $20 by requiring that for k > 20:

    Y n+1 = k + 1|Y n = k = 13 Y n+1 = k 1|Y n = k = 23.

    We then reflect the upward bias at price levels below $20 by requiring that for k < 20:

    Y n+1 = k + 1|Y n = k = 23 Y n+1 = k 1|Y n = k = 13

    Using the methods of “single-step analysis” calculate the expected time for the stock to fall from $25 through the support level all the way down to $18. (There is no reasonable way to solve this problem using formulas. Instead you will have to go back to basic principles of single-step or first-step analysis to solve the problem.)

  4. Perform some simulations of the coin-flipping game, varying p and the start value. How does the value of p affect the experimental duration of victory and ruin?
  5. Modify the simulations by changing the value of p and comparing the experimental results for each starting value to the theoretical duration function.
  6. Modify the duration scripts to perform simulations of the duration calculations in the table in the section Some Calculations for Illustration and compare the results.



Reading Suggestion:


[1]   William Feller. An Introduction to Probability Theory and Its Applications, Volume I, volume I. John Wiley and Sons, third edition, 1973. QA 273 F3712.

[2]   J. Michael Steele. Stochastic Calculus and Financial Applications. Springer-Verlag, 2001. QA 274.2 S 74.



Outside Readings and Links:

  1. Virtual Labs in Probability. Games of Chance. Scroll down and select the Red and Black Experiment (marked in red in the Applets Section. Read the description since the scenario is slightly different but equivalent to the description above.)
  2. University of California, San Diego, Department of Mathematics, A.M. Garsia. A java applet that simulates how long it takes for a gambler to go broke. You can control how much money you and the casino start with, the house odds, and the maximum number of games. Results are a graph and a summary table. Submitted by Matt Odell, September 8, 2003.


I check all the information on each page for correctness and typographical errors. Nevertheless, some errors may occur and I would be grateful if you would alert me to such errors. I make every reasonable effort to present current and accurate information for public use, however I do not guarantee the accuracy or timeliness of information on this website. Your use of the information from this website is strictly voluntary and at your risk.

I have checked the links to external sites for usefulness. Links to external websites are provided as a convenience. I do not endorse, control, monitor, or guarantee the information contained in any external website. I don’t guarantee that the links are active at all times. Use the links here with the same caution as you would all information on the Internet. This website reflects the thoughts, interests and opinions of its author. They do not explicitly represent official positions or policies of my employer.

Information on this website is subject to change without notice.

Steve Dunbar’s Home Page,

Email to Steve Dunbar, sdunbar1 at unl dot edu

Last modified: Processed from LATEX source on April 5, 2013