Skip to content
LeetQuant
QT04 Random Walks and Optimal Stopping

QT04 · Random Walks and Optimal Stopping · Lesson 4 of 4

Backward Induction for Games of Chance

Solve single-player games of chance by computing a value for every state from the end backwards: limited-reroll dice games, rerolls with a cost, and card games with a value table.

17 min

The method

A game of chance against nature is solved by giving every state a value: the expected payoff from that state under optimal play. Then

V(state)=max⁡(payoff from stopping,  E[V(next state)]−cost of continuing).V(\text{state}) = \max\Big(\text{payoff from stopping},\ \ \mathbb{E}\big[V(\text{next state})\big] - \text{cost of continuing}\Big) .

The work is choosing the state so that it captures everything that matters for the future, and finding an order in which to fill in values so that each one uses only values already known:

  • Finite horizon (at most nn rolls): the state includes the number of rolls left, and you fill in from "no rolls left" upwards.
  • Shrinking resources (cards leave a deck): the state is what is left, and you fill in from the empty deck upwards.
  • Stationary problems (unlimited rerolls): the state looks the same after every reroll, so the value solves a fixed-point equation instead of a recursion.

This lesson is single-player: the only opponent is chance. Games where another player chooses (bargaining, auctions, sequential games) are solved by the same backward logic but need equilibrium ideas, covered in the game theory module.

Limited rerolls

Roll a fair dd-sided die (faces 1,…,d1, \dots, d), and after each roll either keep it or reroll, with at most nn rolls in total. Let VnV_n be the value with nn rolls available. With one roll left you keep whatever comes: V1=d+12V_1 = \tfrac{d + 1}{2}. With nn rolls, keep the first roll xx iff x>Vn−1x > V_{n-1}:

Vn=1d∑x=1dmax⁡(x, Vn−1).V_n = \frac1d \sum_{x=1}^{d} \max(x,\, V_{n-1}) .

Two rules of thumb for interviews:

  • The keep-threshold with nn rolls left is the smallest face above Vn−1V_{n-1}. Write the table one row at a time; never try to reason about the whole game at once.
  • Values increase with nn but with shrinking steps, and the threshold can stay put for a few rows before moving up.

Worked example

a ten-sided die with up to five rolls

A fair die has faces 1 to 10. You are paid the face you keep and may roll up to five times. Build the value table and give the strategy.

Show solution

Start from one roll and move up. "Keep" means keep the current roll iff it is at least this face.

  • n=1n = 1: V1=5.5V_1 = 5.5.
  • n=2n = 2: keep ≥6\ge 6. V2=6+7+8+9+1010+510(5.5)=4+2.75=6.75V_2 = \tfrac{6 + 7 + 8 + 9 + 10}{10} + \tfrac{5}{10}(5.5) = 4 + 2.75 = 6.75.
  • n=3n = 3: keep ≥7\ge 7. V3=7+8+9+1010+610(6.75)=3.4+4.05=7.45V_3 = \tfrac{7 + 8 + 9 + 10}{10} + \tfrac{6}{10}(6.75) = 3.4 + 4.05 = 7.45.
  • n=4n = 4: keep ≥8\ge 8. V4=8+9+1010+710(7.45)=2.7+5.215=7.915V_4 = \tfrac{8 + 9 + 10}{10} + \tfrac{7}{10}(7.45) = 2.7 + 5.215 = 7.915.
  • n=5n = 5: still keep ≥8\ge 8, since 7.915<87.915 < 8. V5=2.7+0.7(7.915)=8.2405V_5 = 2.7 + 0.7(7.915) = 8.2405.
Rolls available12345
Keep if at leastany6788
Value5.56.757.457.9158.2405

Strategy with five rolls: keep an 8, 9 or 10 on the first or second roll, a 7 or better on the third, a 6 or better on the fourth, and take the fifth. A sixth roll would raise the threshold to 9, since V5=8.24>8V_5 = 8.24 > 8.

Unlimited rerolls with a cost

Now each reroll costs cc and there is no limit. After every roll the future looks the same, so the value VV (with the first roll free) satisfies

V=E[max⁡(X, V−c)].V = \mathbb{E}\big[\max(X,\ V - c)\big] .

You stop iff the roll beats V−cV - c, the value of paying for another go. Two ways to solve it:

  1. Guess the threshold. With "stop iff X≥tX \ge t", the number of rolls is geometric with success probability πt=P(X≥t)\pi_t = P(X \ge t), so V(t)=E[X∣X≥t]−c (1πt−1)V(t) = \mathbb{E}[X \mid X \ge t] - c\,\big(\tfrac{1}{\pi_t} - 1\big). Maximise over tt.
  2. Solve the fixed point. For X∼Uniform(0,1)X \sim \text{Uniform}(0, 1), put t=V−ct = V - c. Then E[max⁡(X,t)]=12(1+t2)\mathbb{E}[\max(X, t)] = \tfrac12(1 + t^2), so t+c=12(1+t2)t + c = \tfrac12(1 + t^2), giving t=1−2ct = 1 - \sqrt{2c} and
V=1−2c+c.V = 1 - \sqrt{2c} + c .

The square root is the signature of these problems: a small cost per try still costs you a lot, because you search for a long time.

Always confirm a guessed threshold is self-consistent: the threshold you used must be the smallest roll above V−cV - c for the VV it produces.

Worked example

a twenty-sided die, \$1 per reroll

Roll a fair 20-sided die; the first roll is free and each reroll costs $1. You may reroll as often as you like and are paid the final face. What is the optimal strategy and value?

Show solution

With threshold tt, πt=21−t20\pi_t = \tfrac{21 - t}{20} and E[X∣X≥t]=t+202\mathbb{E}[X \mid X \ge t] = \tfrac{t + 20}{2}.

tt1314151617
E[X∣X≥t]\mathbb{E}[X \mid X \ge t]16.51717.51818.5
Expected paid rerolls 1/πt−11/\pi_t - 11.51.8572.33334
V(t)V(t)1515.14315.1671514.5

The best threshold is 15, with V=17.5−73=916≈15.17V = 17.5 - \tfrac{7}{3} = \tfrac{91}{6} \approx 15.17.

Consistency check. Rerolling is worth V−1≈14.17V - 1 \approx 14.17, so stop on 15 or more and reroll on 14 or less: exactly the threshold assumed. Stopping on 14 gives up 14.17−1414.17 - 14 on average each time you do it, which is why t=14t = 14 is slightly worse.

A value table: the red and black card game

A deck has rr red and bb black cards, shuffled. You turn cards one at a time; each red pays +1+1, each black costs 11. You may stop at any time, keeping your running total. What is the game worth?

The state is (r,b)(r, b), the cards remaining; your past total does not affect future decisions, so it drops out. With V(0,b)=0V(0, b) = 0 (only losses remain, so stop) and V(r,0)=rV(r, 0) = r (draw everything),

V(r,b)=max⁡(0,  rr+b(1+V(r−1,b))+br+b(−1+V(r,b−1))).V(r, b) = \max\Big(0,\ \ \frac{r}{r + b}\big(1 + V(r - 1, b)\big) + \frac{b}{r + b}\big(-1 + V(r, b - 1)\big)\Big) .

Each entry uses the entry above and the entry to the left, so fill the table in order of r+br + b:

b=0b = 0b=1b = 1b=2b = 2b=3b = 3
r=0r = 00000
r=1r = 111/200
r=2r = 224/32/31/5
r=3r = 339/43/217/20

A fair deck (r=br = b) has a positive value even though drawing every card nets exactly zero: the option to stop when ahead is worth something. For the full deck the code below gives about 2.622.62.

from fractions import Fraction
from functools import lru_cache


@lru_cache(maxsize=None)
def value(r, b):
    """Value of the card game with r red and b black cards left."""
    if r == 0:
        return Fraction(0)
    if b == 0:
        return Fraction(r)
    n = r + b
    draw = Fraction(r, n) * (1 + value(r - 1, b)) + Fraction(b, n) * (-1 + value(r, b - 1))
    return max(Fraction(0), draw)


print(value(2, 2), value(3, 3))
print(float(value(26, 26)))

Output:

2/3 17/20
2.624475548993925

Contrast this with the red-card game in the optimal stopping lesson, which was worth exactly 12\tfrac12. There the payoff was a martingale (the chance the next card is red); here the payoff is the running total, and stopping lets you lock in gains that a martingale would have given back.

Worked example

three red, two black

Using the recursion, find V(3,2)V(3, 2) and the optimal first moves.

Show solution

You need V(2,2)=23V(2, 2) = \tfrac23 and V(3,1)=94V(3, 1) = \tfrac94 from the table.

V(3,2)=max⁡(0, 35(1+23)+25(−1+94))=35⋅53+25⋅54=1+12=32.V(3, 2) = \max\Big(0,\ \tfrac35\big(1 + \tfrac23\big) + \tfrac25\big(-1 + \tfrac94\big)\Big) = \tfrac35 \cdot \tfrac53 + \tfrac25 \cdot \tfrac54 = 1 + \tfrac12 = \tfrac32 .

Drawing is worth 32>0\tfrac32 > 0, so draw. After a red you are at +1+1 with (2,2)(2, 2) left, worth 23>0\tfrac23 > 0: draw again. After a black you are at −1-1 with (3,1)(3, 1) left, worth 94\tfrac94: draw again. Stopping is only right in states with V=0V = 0, such as (1,2)(1, 2), where the remaining cards are expected to cost you more than the chance of a late red can recover.

Check the edge case (1,1)(1, 1): drawing gives 12(1+0)+12(−1+1)=12\tfrac12(1 + 0) + \tfrac12(-1 + 1) = \tfrac12. If the red comes first you stop at +1+1; if the black comes first you draw the red and finish at 00.

Knowledge check

A fair ten-sided die (faces 1 to 10) may be rolled at most twice; you keep the last roll. What is the optimal rule for the first roll and the game's value?

Knowledge check

You draw U∼Uniform(0,1)U \sim \text{Uniform}(0, 1), are paid the value you stop at, and each redraw costs 0.080.08 (the first draw is free). What is the game worth?

Knowledge check

In the red/black card game (red +1, black −1, stop any time), what is the value of a deck with 2 red cards and 1 black card?

Key takeaways

  • Value of a state = max(stop payoff, expected value of the next state minus cost). Choose a state that captures everything relevant to the future, and fill values in an order where each uses only known ones.
  • Limited rerolls of a dd-sided die: Vn=1d∑xmax⁡(x,Vn−1)V_n = \tfrac1d\sum_x \max(x, V_{n-1}); keep iff the roll beats Vn−1V_{n-1}. For a d10: 5.5, 6.75, 7.45, 7.915, 8.24.
  • Unlimited rerolls at cost cc: solve V=E[max⁡(X,V−c)]V = \mathbb{E}[\max(X, V - c)], or maximise over thresholds and check consistency. Uniform case: V=1−2c+cV = 1 - \sqrt{2c} + c.
  • Card games: tabulate V(r,b)V(r, b) over what is left. The red/black game is worth about 2.62 for a full deck, and the value comes entirely from the option to stop.

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).