Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

Payoff Matrices in NumPy

Represent two-player games as pair of NumPy payoff matrices, look up outcomes, compute expected payoffs with matrix products, and build a reusable Game class.

A game with two players and a handful of actions fits neatly in a table. Each row is an action of one player, each column an action of the other, and each cell holds the two payoffs. Programmers have a perfect tool for tables of numbers: NumPy arrays. In this lesson you will turn a game table into two matrices, learn to read payoffs by index, compute expected payoffs with matrix multiplication, and wrap the whole thing in a small class that the rest of the course reuses.

We assume you saw the idea of payoffs in What Is Game Theory? and the role of numbers in Rational Choice and Utility. You also need basic NumPy: creating arrays and indexing them with A[i, j].

One game, two matrices

Take a simple game. The row player, call her Ann, chooses Top or Bottom. The column player, Bob, chooses Left or Right. The table below lists (Ann's payoff, Bob's payoff).

Left Right
Top (3, 2) (0, 0)
Bottom (1, 1) (2, 3)

Instead of one table of pairs, we store two matrices of the same shape: A for Ann's payoffs and B for Bob's. Entry A[i, j] is Ann's payoff when she plays row i and Bob plays column j; B[i, j] is Bob's payoff for the same pair. A game stored like this is called a bimatrix game.

flowchart LR
    T["Game table<br/>(3,2) (0,0)<br/>(1,1) (2,3)"] --> A["A = Ann's payoffs<br/>[[3, 0],[1, 2]]"]
    T --> B["B = Bob's payoffs<br/>[[2, 0],[1, 3]]"]

Splitting the table into one matrix per player.

Python Build and read a bimatrix game

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The slices at the end are exactly what you need for reasoning: a column of A lists what Ann gets for each of her actions when Bob's action is fixed. A row of B lists what Bob gets for each of his actions when Ann's action is fixed.

Zero-sum shortcut

In a zero-sum game Bob's payoff is the negative of Ann's, so B = -A and one matrix is enough. You will see this in Zero-Sum and Constant-Sum Games.

Mixed choices and expected payoffs

So far each player picks one action for sure. Players can also pick randomly. A mixed strategy is a probability vector over actions. If Ann plays Top with probability 0.25 and Bottom with 0.75, her strategy is x = [0.25, 0.75]. If Bob plays y = [0.6, 0.4], then cell (i, j) happens with probability x[i] * y[j] (the players choose independently).

The expected payoff to Ann is the sum of x[i] * A[i, j] * y[j] over all cells, and that is exactly the matrix product x @ A @ y. Likewise Bob gets x @ B @ y.

Python Expected payoffs with x @ A @ y

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

np.outer(x, y) builds the whole table of outcome probabilities in one line. Notice also that A @ y is the vector of Ann's expected payoff for each of her pure actions, given Bob's mixture. That vector is the engine behind best responses in the next chapters.

A reusable Game class

Rather than pasting matrices everywhere, we wrap them in a class that checks the shapes and offers the common operations. The course reuses this pattern; you can copy the class into any TryIt.

Python A small BimatrixGame class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The class stores the arrays as floats so that probabilities and payoffs combine without surprises. It validates the shape, which catches the most common bug: a payoff matrix for one player that is accidentally transposed.

Games with more than two actions

Nothing changes when a player has three or more actions: the matrices simply get bigger. Rock, paper, scissors is a 3 by 3 game. Win is 1, loss is -1, tie is 0.

Python Rock, paper, scissors as a matrix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Against a Bob who leans on Rock, the vector A @ y shows that Paper earns +0.4. The game itself is symmetric, but a predictable opponent is exploitable. That tension, between exploiting and being exploited, drives mixed strategies.

Common mistakes

  • Mixing up who is rows and who is columns. Always write the convention at the top of your code: rows belong to Ann, columns to Bob. B[i, j] is still Bob's payoff in row i, column j; do not transpose it.
  • Probabilities that do not sum to 1. x @ A @ y still runs, but the result means nothing. Check with np.isclose(x.sum(), 1).
  • Integer arrays. Mixing integer matrices with fractions can hide mistakes in older code. Converting to float costs nothing.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a bimatrix game, what does B[i, j] hold?

    Both matrices use the same indexing: i is the row action and j is the column action. A holds the row player's payoffs and B the column player's.
  2. 2. Which expression gives the row player's expected payoff when the players use mixed strategies x and y?

    x @ A @ y sums x[i] * A[i, j] * y[j] over all cells. x @ B @ y would be the column player's expectation.
  3. 3. In a zero-sum game with row payoffs A, what are the column player's payoffs?

    Zero-sum means the two payoffs add to zero in every cell, so B = -A.
  4. 4. What does the vector A @ y contain?

    Entry i of A @ y is the sum over j of A[i, j] * y[j], the expected payoff of playing row i against y. Picking the largest entry gives a best response.

Summary

  • A two-player game is a pair of equally shaped matrices: A for the row player, B for the column player.
  • Slices of A and B list the payoffs of one player's options against a fixed action of the other.
  • A mixed strategy is a probability vector; expected payoffs are x @ A @ y and x @ B @ y.
  • A @ y gives the row player's payoff for every pure action against y.
  • A small BimatrixGame class keeps shapes consistent and is reused throughout the course.

Next: A First Game: The Beauty Contest