Algorithm Search⌘K

Definitioncore

Subgame perfect equilibrium

Also called SPE, subgame perfect Nash equilibrium.

A strategy profile that is a Nash equilibrium of every subgame. In finite perfect-information games backward induction finds one.

A subgame perfect equilibrium is a strategy profile of an extensive-form game that is a Nash equilibrium not only of the whole game but also of every subgame, the game that starts at any node where all earlier moves are known. It removes equilibria that rest on threats a player would not carry out if the moment came. Selten (1965) defined it. In a finite game of perfect information, backward induction from the leaves finds one in pure strategies, in time linear in the size of the tree. Backward induction for two-player win or lose games is also on Zermelo's theorem and retrograde analysis; this page treats the general case with any number of players and any payoffs.

Definition

Let Γ\Gamma be a finite extensive-form game. A node hh starts a subgame if

  • hh is alone in its information set, and
  • every information set that contains a node below hh contains only nodes below hh.

The subgame Γh\Gamma_h is the part of the tree below hh, with the same players, actions, chance probabilities, information sets and payoffs. The whole game is a subgame of itself. A strategy of Γ\Gamma (pure, mixed through its behavior form, or behavior) restricts to a strategy of Γh\Gamma_h by keeping only its choices at the information sets below hh.

Definition. A profile bb of behavior strategies is a subgame perfect equilibrium if, for every subgame Γh\Gamma_h, the restriction of bb to Γh\Gamma_h is a Nash equilibrium of Γh\Gamma_h.

In a game of perfect information, every inner node starts a subgame, so the profile must be an equilibrium below every node, also below nodes that the equilibrium play never reaches. That is where the definition differs from Nash equilibrium: a Nash equilibrium can prescribe any action at an unreached node, because a change there does not change any payoff.

Backward induction. In a finite game of perfect information, solve the tree from the leaves up. At a leaf, the value is its payoff vector. At a chance node, the value is the expected value of the children. At a node of player ii, pick an action whose child has the largest value for player ii, and take that child's value.

  1. BACKWARD-INDUCTION(h) ▷ returns the payoff vector of the subgame at h
  2. 1if h is a leaf then return u(h)
  3. 2if h is a chance node then return Σ_a p_c(a | h) · BACKWARD-INDUCTION(h a)
  4. 3i ← P(h); best ← none
  5. 4for each action a in A(h), in order
  6. 5v ← BACKWARD-INDUCTION(h a)
  7. 6if best is none or v_i > best_i then best ← v; s(h) ← a
  8. 7return best

The profile ss that this builds is a pure subgame perfect equilibrium. It visits each node once, so it takes time O(∣T∣)O(|T|) for a tree with ∣T∣|T| nodes, with one comparison per edge. Ties go to the first action. The implementation has this procedure for trees with chance nodes in exact fractions, and its tests compare it with a brute force check that tries every change of every player below every node: on random trees with distinct payoffs the brute force finds exactly one subgame perfect profile, the output of backward induction, and on random trees with ties the subgame perfect profiles are exactly the outputs of backward induction under all rules for ties.

Proof that ss is subgame perfect. By induction on the height of hh, ss is a Nash equilibrium of Γh\Gamma_h. For a leaf there is nothing to prove. At a chance node, a deviation of player jj in Γh\Gamma_h is a deviation in each child subgame, and by induction it gains in none of them, so it gains nothing on average. At a node of player ii, a deviation of a player j≠ij \ne i changes only the play inside the child that ss chooses, where by induction it gains nothing. A deviation of player ii may change the action at hh to some a′a' and also the play inside the children. The play then goes into Γha′\Gamma_{h a'}, where ss is a Nash equilibrium by induction, so player ii gets at most v(ha′)iv(h a')_i, and v(ha′)i≤v(h)iv(h a')_i \le v(h)_i by the choice at L6. □\square

The one-shot deviation property. In a finite game of perfect information, a pure profile is subgame perfect if and only if no player gains by changing the action at a single node, with the rest of the profile fixed and the payoff measured in the subgame at that node. So a profile is subgame perfect exactly when backward induction with some rule for ties produces it.

Existence in general. In a finite game with perfect recall, a subgame perfect equilibrium in behavior strategies exists. Take a subgame with no smaller proper subgame, pick a Nash equilibrium of it in behavior strategies (by Nash's theorem and Kuhn's theorem), replace the subgame by a leaf with its equilibrium payoffs, and repeat. Selten (1975) also showed it as a consequence of the existence of trembling-hand perfect equilibria, which are subgame perfect.

Examples and non-examples

The entry game. Player 1 chooses Out, which ends the game with payoffs (0,2)(0, 2), or In; after In, player 2 chooses Fight, with payoffs (−1,−1)(-1, -1), or Accommodate, with payoffs (1,1)(1, 1). The pure Nash equilibria are (Out, Fight) and (In, Accommodate). In (Out, Fight), player 2 never moves, so the threat to fight costs nothing. In the subgame after In, Fight gives player 2 −1-1 and Accommodate gives 1, so (Out, Fight) is not a Nash equilibrium of that subgame. Backward induction gives (In, Accommodate) only, with payoffs (1,1)(1, 1), as on normal-form game.

The centipede game. Two players take turns, 6 stages in all, and player 1 moves at the even stages. At stage k=0,…,5k = 0, \ldots, 5 the mover can take, which ends the game with k+2k + 2 for the mover and kk for the other, or pass. If both pass at every stage, both get 6. At the last stage, taking gives the mover 7 against 6 for passing, so the mover takes. At each earlier stage, taking gives k+2k + 2, while passing leads to the other player taking at once, which gives the mover k+1k + 1. So the only subgame perfect equilibrium takes at every stage, and the play ends at once with payoffs (2,0)(2, 0), although both players could get 6. In experiments people usually pass for several stages.

An ultimatum game with ties. Player 1 offers x∈{0,1,2}x \in \{0, 1, 2\} of 2 units to player 2, who accepts, with payoffs (2−x,x)(2 - x, x), or rejects, with (0,0)(0, 0). After x=1x = 1 or x=2x = 2, accepting is strictly better. After x=0x = 0, player 2 is indifferent. So there are two pure subgame perfect equilibria: player 2 accepts every offer and player 1 offers 0, with payoffs (2,0)(2, 0); or player 2 rejects 0 and accepts the rest and player 1 offers 1, with payoffs (1,1)(1, 1). Nash equilibria are many more: for each xx, "accept only offers of at least xx" against the offer xx is one. With payoffs that are all different for each player, backward induction never meets a tie, and the subgame perfect equilibrium is unique.

Non-example: a game without proper subgames. Player 1 chooses Out, with payoffs (2,2)(2, 2), or L or R. Player 2 then chooses ℓ\ell or rr without seeing whether L or R was played. The payoffs are (3,1)(3, 1) after (L, ℓ\ell), (0,0)(0, 0) after (L, rr) and (R, ℓ\ell), and (1,3)(1, 3) after (R, rr). The two nodes of player 2 form one information set, so the only subgame is the whole game, and every Nash equilibrium is subgame perfect. One of them is (Out, rr): it asks player 2 to answer as if player 1 had played R, which is worse for player 1 than Out whatever player 2 does. Subgame perfection does not rule this out. Refinements that put beliefs on information sets, such as sequential equilibrium (Kreps and Wilson 1982), or that use trembles, are needed for such games.

  • Zero-sum games. In a two-player zero-sum game of perfect information, backward induction is the minimax rule, and alpha-beta pruning finds the same value with fewer node visits. Games given as graphs with cycles, such as chess endgames, are solved by retrograde analysis.
  • Commitment. A Stackelberg equilibrium is the subgame perfect equilibrium of the game in which the leader first commits to a strategy and the follower then responds.
  • Infinite horizon. In games with infinitely long plays, backward induction has no last node to start from, but the definition still applies. In the Rubinstein bargaining model, the subgame perfect equilibrium is unique, and in infinitely repeated games the folk theorems describe a large set of them.
  • Trembling-hand perfection. A trembling-hand perfect equilibrium is subgame perfect, and in addition it handles unreached information sets that do not start a subgame.
  • Computation. For perfect information, the algorithm above is linear in the tree, which can itself be exponential in the description of the game, as for chess. With imperfect information, the bottom-up method needs a Nash equilibrium of each minimal subgame, which is PPAD-hard in general (computation).

Code

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

subgame_perfect_equilibrium.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/subgame-perfect-equilibrium/. The authors are listed in prov/E96CN1.jsonl.
"""Backward induction on a finite game tree of perfect information.

A tree is a dict from node ids to nodes, with the root at id 0:

    ("leaf", (u_1, ..., u_n))              payoffs, as Fraction
    ("chance", [(p, child), ...])          probabilities as Fraction, summing to 1
    ("move", i, [(action, child), ...])    player i (0-based) chooses an action

A pure strategy profile of the whole game is a dict from the ids of the
"move" nodes to actions. Every node of a perfect information game starts a
subgame, so a subgame perfect equilibrium is a profile that is a Nash
equilibrium below every node.
"""

from fractions import Fraction
from itertools import product


def backward_induction(tree, root=0):
    """Return (value, profile, calls).

    value[h] is the payoff vector of the subgame at h under the returned
    profile, profile[h] is the action chosen at each move node, and calls is
    the number of nodes visited (each node once). Ties go to the first
    action in the list of the node.
    """
    value = {}
    profile = {}
    calls = [0]

    def solve(h):
        calls[0] += 1
        node = tree[h]
        if node[0] == "leaf":  # @line L1
            value[h] = tuple(node[1])
            return value[h]
        if node[0] == "chance":  # @line L2
            parts = [(p, solve(c)) for p, c in node[1]]
            n = len(parts[0][1])
            value[h] = tuple(sum((p * v[k] for p, v in parts), Fraction(0)) for k in range(n))
            return value[h]
        _, i, moves = node  # @line L3
        best = None
        for a, c in moves:  # @line L4
            v = solve(c)  # @line L5
            if best is None or v[i] > best[i]:  # @line L6
                best = v
                profile[h] = a
        value[h] = best  # @line L7
        return best

    solve(root)
    return value, profile, calls[0]


def play_value(tree, profile, h=0):
    """The expected payoff vector of the subgame at h when everyone follows profile."""
    node = tree[h]
    if node[0] == "leaf":
        return tuple(node[1])
    if node[0] == "chance":
        parts = [(p, play_value(tree, profile, c)) for p, c in node[1]]
        n = len(parts[0][1])
        return tuple(sum((p * v[k] for p, v in parts), Fraction(0)) for k in range(n))
    _, _, moves = node
    child = dict(moves)[profile[h]]
    return play_value(tree, profile, child)


def move_nodes(tree, h=0):
    """The ids of the move nodes in the subgame at h, in depth-first order."""
    node = tree[h]
    if node[0] == "leaf":
        return []
    if node[0] == "chance":
        out = []
        for _, c in node[1]:
            out += move_nodes(tree, c)
        return out
    out = [h]
    for _, c in node[2]:
        out += move_nodes(tree, c)
    return out


def all_pure_profiles(tree):
    """Every pure strategy profile of the whole game, as dicts."""
    nodes = move_nodes(tree)
    choices = [[a for a, _ in tree[h][2]] for h in nodes]
    for pick in product(*choices):
        yield dict(zip(nodes, pick))


def is_nash_in_subgame(tree, profile, h):
    """Brute force: no player gains by any change of their own actions below h."""
    base = play_value(tree, profile, h)
    nodes = move_nodes(tree, h)
    players = {tree[g][1] for g in nodes}
    for i in players:
        own = [g for g in nodes if tree[g][1] == i]
        choices = [[a for a, _ in tree[g][2]] for g in own]
        for pick in product(*choices):
            dev = dict(profile)
            dev.update(zip(own, pick))
            if play_value(tree, dev, h)[i] > base[i]:
                return False
    return True


def is_subgame_perfect(tree, profile):
    """A pure profile is subgame perfect if it is Nash below every inner node."""
    inner = [h for h, node in tree.items() if node[0] != "leaf"]
    return all(is_nash_in_subgame(tree, profile, h) for h in inner)
test_subgame_perfect_equilibrium.py 165 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/subgame-perfect-equilibrium/. The authors are listed in prov/E96CN1.jsonl.

import random
from fractions import Fraction as F
from itertools import product

from subgame_perfect_equilibrium import (
    all_pure_profiles,
    backward_induction,
    is_nash_in_subgame,
    is_subgame_perfect,
    move_nodes,
    play_value,
)

# The entry game: player 0 chooses Out (0, 2) or In; after In, player 1
# chooses Fight (-1, -1) or Accommodate (1, 1).
ENTRY = {
    0: ("move", 0, [("Out", 1), ("In", 2)]),
    1: ("leaf", (F(0), F(2))),
    2: ("move", 1, [("Fight", 3), ("Accommodate", 4)]),
    3: ("leaf", (F(-1), F(-1))),
    4: ("leaf", (F(1), F(1))),
}


def test_entry_game() -> None:
    value, profile, calls = backward_induction(ENTRY)
    assert profile == {0: "In", 2: "Accommodate"}
    assert value[0] == (1, 1)
    assert calls == 5
    nash = [p for p in all_pure_profiles(ENTRY) if is_nash_in_subgame(ENTRY, p, 0)]
    assert sorted((p[0], p[2]) for p in nash) == [("In", "Accommodate"), ("Out", "Fight")]
    spe = [p for p in all_pure_profiles(ENTRY) if is_subgame_perfect(ENTRY, p)]
    assert spe == [{0: "In", 2: "Accommodate"}]


def centipede(stages: int):
    # At stage k the mover can take, ending the game, or pass. Taking at stage
    # k gives the mover k + 2 and the other k; passing at the last stage gives
    # both stages.
    tree = {}
    for k in range(stages):
        mover = k % 2
        take = 2 * k + 1
        nxt = 2 * k + 2 if k + 1 < stages else 2 * stages
        tree[2 * k] = ("move", mover, [("Take", take), ("Pass", nxt)])
        pay = [F(k), F(k)]
        pay[mover] = F(k + 2)
        tree[take] = ("leaf", tuple(pay))
    tree[2 * stages] = ("leaf", (F(stages), F(stages)))
    return tree


def test_centipede_takes_at_once() -> None:
    tree = centipede(6)
    value, profile, _ = backward_induction(tree)
    assert all(a == "Take" for a in profile.values())
    assert value[0] == (2, 0)
    assert is_subgame_perfect(tree, profile)


def random_tree(rng, depth, players, generic, chance_prob):
    tree = {}
    counter = [0]
    used = [set() for _ in range(players)]

    def new_id():
        counter[0] += 1
        return counter[0] - 1

    def payoff(i):
        if not generic:
            return F(rng.randint(0, 1))
        while True:
            v = rng.randint(-50, 50)
            if v not in used[i]:
                used[i].add(v)
                return F(v)

    def build(d):
        h = new_id()
        if d == 0 or (d < depth and rng.random() < 0.25):
            tree[h] = ("leaf", tuple(payoff(i) for i in range(players)))
            return h
        if rng.random() < chance_prob:
            k = rng.randint(2, 3)
            w = [rng.randint(1, 4) for _ in range(k)]
            probs = [F(x, sum(w)) for x in w]
            tree[h] = ("chance", [(p, build(d - 1)) for p in probs])
            return h
        i = rng.randrange(players)
        k = rng.randint(2, 3)
        tree[h] = ("move", i, [(a, build(d - 1)) for a in range(k)])
        return h

    build(depth)
    return tree


def small_random_tree(rng, generic, chance_prob):
    while True:
        tree = random_tree(rng, 3, rng.randint(2, 3), generic, chance_prob)
        if 1 <= len(move_nodes(tree)) <= 5:
            return tree


def all_tie_breaks(tree, h=0):
    # Every (value, partial profile) that backward induction can return at h
    # under some rule for breaking ties.
    node = tree[h]
    if node[0] == "leaf":
        return [(tuple(node[1]), {})]
    if node[0] == "chance":
        options = [all_tie_breaks(tree, c) for _, c in node[1]]
        out = []
        for pick in product(*options):
            n = len(pick[0][0])
            v = tuple(sum((p * r[0][k] for (p, _), r in zip(node[1], pick)), F(0)) for k in range(n))
            prof = {}
            for r in pick:
                prof.update(r[1])
            out.append((v, prof))
        return out
    _, i, moves = node
    options = [all_tie_breaks(tree, c) for _, c in moves]
    out = []
    for pick in product(*options):
        best = max(r[0][i] for r in pick)
        for (a, _), r in zip(moves, pick):
            if r[0][i] == best:
                prof = {}
                for s in pick:
                    prof.update(s[1])
                prof[h] = a
                out.append((r[0], prof))
    return out


def test_generic_payoffs_give_a_unique_spe() -> None:
    rng = random.Random(1)
    for _ in range(25):
        tree = small_random_tree(rng, generic=True, chance_prob=0.0)
        value, profile, calls = backward_induction(tree)
        assert calls == len(tree)
        spe = [p for p in all_pure_profiles(tree) if is_subgame_perfect(tree, p)]
        assert spe == [profile]
        assert play_value(tree, profile) == value[0]


def test_ties_and_chance_match_brute_force() -> None:
    rng = random.Random(2)
    several = 0
    for _ in range(25):
        tree = small_random_tree(rng, generic=False, chance_prob=0.3)
        value, profile, _ = backward_induction(tree)
        assert is_subgame_perfect(tree, profile)
        assert play_value(tree, profile) == value[0]
        spe = [p for p in all_pure_profiles(tree) if is_subgame_perfect(tree, p)]
        breaks = [p for _, p in all_tie_breaks(tree)]
        key = lambda p: tuple(sorted(p.items()))
        assert sorted(map(key, spe)) == sorted(set(map(key, breaks)))
        several += len(spe) > 1
    assert several >= 5  # with ties, a game can have several pure SPE

References

  1. Selten 1965, the definition of a subgame perfect equilibrium point, for an oligopoly model with demand inertia
  2. Harold W. Kuhn, Extensive games and the problem of information, pp. 193 to 216 (every finite game of perfect information has an equilibrium in pure strategies, by induction on the tree)
  3. Reinhard Selten, Reexamination of the perfectness concept for equilibrium points in extensive games, International Journal of Game Theory 4(1) (1975), 25-55, subgame perfectness recalled, its limits in games without proper subgames, and existence through perfect equilibria
  4. Martin J. Osborne and Ariel Rubinstein, A Course in Game Theory, MIT Press, 1994, Chapter 6 (extensive games with perfect information, subgame perfect equilibrium, the one deviation property and Kuhn's theorem)
  5. Yoav Shoham and Kevin Leyton-Brown, Multiagent Systems, Algorithmic, Game-Theoretic, and Logical Foundations, Cambridge University Press, 2009, Chapter 5 (subgame perfect equilibrium and backward induction)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/E96CN1/text.yaml