411 REFERENCE · Wikibooks
Scientific Calculations with Julia/Game Theory
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Scientific Calculations with Julia Game Theory Supply and Demand Population Growth
Example 1: Supermarkets Open on Sundays
Suppose there are two supermarkets that must decide whether to open on Sundays or not. If both supermarkets don't open, they earn 150 and 250; if they do open, they earn 200 and 300; if one doesn't open and the other does, the latter earns 350 while the other earns 100; if the second doesn't open and the first does, the latter earns 250 while the other earns 200. This yields the payoff matrix, which can be stored using Julia's GameTheory library as follows:
using GameTheory
g = NormalFormGame((2, 2))
g[1, 1] = [200, 300]
g[1, 2] = [250, 200]
g[2, 1] = [100, 350]
g[2, 2] = [150, 250];
print(g)
2×2 NormalFormGame{2, Float64}:
[200.0, 300.0] [250.0, 200.0]
[100.0, 350.0] [150.0, 250.0]
To find possible Nash equilibria, use the following in Julia:
pure_nash(g)
1-element Vector{Tuple{Int64, Int64}}:
(1, 1)
Therefore, there is only one Nash equilibrium contained in cell (1,1) equal to [200, 300], so it is convenient for both supermarkets to remain open on Sundays.
Example 2: Low or High Prices
Suppose two firms produce a good in a duopoly and must decide whether to sell it at a high or low price. If both sell it at a high price, they earn 6; if both sell it at a low price, they earn 2; if the first sells it at a high price and the second at a low price, the latter earns 9 while the other earns 0; if the second sells it at a high price and the first sells it low, the latter earns 9 while the other earns 0. This yields the payoff matrix, which can be stored using Julia's GameTheory library as follows:
using GameTheory
g = NormalFormGame((2, 2))
g[1, 1] = [6, 6]
g[1, 2] = [0, 9]
g[2, 1] = [9, 0]
g[2, 2] = [2, 2];
print(g)
2×2 NormalFormGame{2, Float64}:
[6.0, 6.0] [0.0, 9.0]
[9.0, 0.0] [2.0, 2.0]
To find possible Nash equilibria, use the following in julia:
pure_nash(g)
1-element Vector{Tuple{Int64, Int64}}:
(2, 2)
Therefore, there is only one Nash equilibrium contained in cell (2,2) equal to [2, 2], so it is advantageous for both companies to sell at low prices.
Example 3
Suppose a game is represented by the following payoff matrix:
g2 = NormalFormGame((3, 2))
g2[1, 1] = [3, 3]
g2[1, 2] = [3, 2]
g2[2, 1] = [2, 2]
g2[2, 2] = [5, 6];
g2[3, 1] = [0, 3]
g2[3, 2] = [0, 1];
print(g2)
3×2 NormalFormGame{2, Float64}:
[3.0, 3.0] [3.0, 2.0]
[2.0, 2.0] [5.0, 6.0]
[0.0, 3.0] [0.0, 1.0]
There are two Nash equilibria:
pure_nash(g2)
2-element Vector{Tuple{Int64, Int64}}:
(1, 1)
(2, 2)
...that is, [3.0, 3.0] and [5.0, 6.0]...
Further examples of using Julia in game theory can be found in the tutorial:
https://nbviewer.org/github/QuantEcon/game-theory-notebooks/blob/main/game_theory_jl.ipynb .