Fundamentals 13 min read

Game Theory in Python: Payoff Matrices, Dominant Strategies & Nash Equilibria

This article introduces key game theory concepts such as payoff matrices, dominant strategies, and Nash equilibria, and demonstrates how to model and solve two‑player games using the Python library Nashpy, including code examples for pure and mixed strategy analysis.

Model Perspective
Model Perspective
Model Perspective
Game Theory in Python: Payoff Matrices, Dominant Strategies & Nash Equilibria

This article introduces several important concepts in game theory—payoff matrix, dominant strategy, and Nash equilibrium—and shows how to use the Python library Nashpy to work with them.

Payoff matrix

For a two‑player game, the row player’s strategies are listed on the left, the column player’s strategies on the top, and each cell contains the payoffs for both players (left number for player 1, right number for player 2).

Two players A and B each have two strategies: A can choose “TOP” or “Bottom”, B can choose “Left” or “Right”. The resulting payoff matrix shows A’s payoffs in red and B’s in blue.

We use Nashpy (install with pip install nashpy ) to solve the two‑player game. First we construct the payoff matrices:

<code>import nashpy as ns
import numpy as np

A = np.array([[2, 0],[4, 2]])
B = np.array([[4, 2],[2, 0]])
payoff = ns.Game(A, B)
</code>

Combining the matrices with ns.Game yields:

<code>Bi matrix game with payoff matrices:

Row player:
[[2 0]
 [4 2]]

Column player:
[[4 2]
 [2 0]]
</code>

If only one matrix is supplied, Nashpy assumes a zero‑sum game, converting the second player’s payoffs to the negatives of the first’s:

<code>Zero sum game with payoff matrices:

Row player:
[[2 0]
 [4 2]]

Column player:
[[-2  0]
 [-4 -2]]
</code>

Dominant strategy

A dominant strategy is the best response for a player regardless of the opponent’s choice.

If A chooses TOP, B prefers Left (payoff 4 vs 2).

If A chooses Bottom, B still prefers Left (payoff 2 vs 0).

If B chooses Left, A prefers Bottom (payoff 4 vs 2).

If B chooses Right, A prefers Bottom (payoff 2 vs 0).

Thus B always chooses Left and A always chooses Bottom; the equilibrium is (A = Bottom, B = Left).

We can verify optimal responses with Nashpy:

<code>s1 = np.array([0,1])
s2 = np.array([0,1])
payoff.is_best_response(s1, s2)
</code>

The result (True, False) shows that A’s strategy is a best response while B’s is not.

Nash equilibrium

A Nash equilibrium is a strategy profile where no player can improve their payoff by unilaterally deviating. It can be pure (each player chooses a single strategy) or mixed (players randomize over strategies).

Finding equilibria for the original game:

<code>equilibria = payoff.support_enumeration()
for eq in equilibria:
    print(eq)
</code>

Output:

<code>(array([0., 1.]), array([1., 0.]))
</code>

This indicates the pure‑strategy equilibrium (A = Bottom, B = Left).

To illustrate a mixed‑strategy equilibrium, we modify the payoff matrices (see image below) and run the same enumeration:

If A chooses TOP, B prefers Left (payoff 2 vs 0).

If A chooses Bottom, B prefers Right (payoff 4 vs 0).

If B chooses Left, A prefers TOP (payoff 4 vs 0).

If B chooses Right, A prefers Bottom (payoff 4 vs 0).

Running Nashpy:

<code>import nashpy as ns
import numpy as np

A = np.array([[4, 0],[0, 2]])
B = np.array([[2, 0],[0, 4]])
payoff = ns.Game(A, B)

equilibrium = payoff.support_enumeration()
for eq in equilibrium:
    print(eq)
</code>

Output:

<code>(array([1., 0.]), array([1., 0.]))
(array([0., 1.]), array([0., 1.]))
(array([0.66666667, 0.33333333]), array([0.33333333, 0.66666667]))
</code>

The first two lines are the pure‑strategy equilibria; the third line represents the mixed‑strategy equilibrium where A plays TOP with probability 2/3 and Bottom with 1/3, while B plays Left with 1/3 and Right with 2/3.

Mixed‑strategy payoff calculation

Mixed strategies are probability distributions over pure actions. For the example above, the expected payoffs can be computed by weighting the payoff matrices with the strategy probabilities.

Using Nashpy to evaluate a specific mixed profile (A: 0.5/0.5, B: 0.7/0.3):

<code>import nashpy as ns
import numpy as np

A = np.array([[4, 0],[0, 2]])
B = np.array([[2, 0],[0, 4]])
payoff = ns.Game(A, B)

r = np.array([0.5, 0.5])  # row player probabilities
c = np.array([0.7, 0.3])  # column player probabilities

print(payoff[r, c])
</code>

Result: array([1.7, 1.3]) (A’s expected payoff 1.7, B’s 1.3).

Evaluating the mixed‑strategy equilibrium probabilities (A: 2/3, 1/3; B: 1/3, 2/3):

<code>r = np.array([2/3, 1/3])
c = np.array([1/3, 2/3])
print(payoff[r, c])
</code>

Result: array([1.33333333, 1.33333333]) , showing equal expected payoffs for both players at the equilibrium.

For more details on using Nashpy, see the official tutorial at https://nashpy.readthedocs.io/en/stable/text-book/index.html .

References:

Game Theory Python Simulation (Part 1) – https://blog.csdn.net/Hao_ge_666/article/details/124984306

Nashpy documentation – https://nashpy.readthedocs.io/en/stable/

Pythongame theorynash equilibriumpayoff matrixnashpy
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.