Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add brute force method for finding pure nash equilibria. #276

Merged
merged 1 commit into from
Nov 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions quantecon/game_theory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .random import random_game, covariance_game
from .support_enumeration import support_enumeration, support_enumeration_gen
from .lemke_howson import lemke_howson
from .pure_nash import pure_nash_brute
65 changes: 65 additions & 0 deletions quantecon/game_theory/pure_nash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
Author: Zejin Shi

Methods for computing pure Nash equilibria of a normal form game.
(For now, only brute force method is supported)

"""

import numpy as np


def pure_nash_brute(g):
"""
Find all pure Nash equilibria of a normal form game by brute force.

Parameters
----------
g : NormalFormGame

Returns
-------
NEs : list(tuple(int))
List of tuples of Nash equilibrium pure actions.
If no pure Nash equilibrium is found, return empty list.

Examples
--------
Consider the "Prisoners' Dilemma" game:

>>> PD_bimatrix = [[(1, 1), (-2, 3)],
... [(3, -2), (0, 0)]]
>>> g_PD = NormalFormGame(PD_bimatrix)
>>> pure_nash_brute(g_PD)
[(1, 1)]

If we consider the "Matching Pennies" game, which has no pure nash
equilibirum:

>>> MP_bimatrix = [[(1, -1), (-1, 1)],
... [(-1, 1), (1, -1)]]
>>> g_MP = NormalFormGame(MP_bimatrix)
>>> pure_nash_brute(g_MP)
[]

"""
return list(pure_nash_brute_gen(g))


def pure_nash_brute_gen(g):
"""
Generator version of `pure_nash_brute`.

Parameters
----------
g : NormalFormGame

Yields
------
out : tuple(int)
Tuple of Nash equilibrium pure actions.

"""
for a in np.ndindex(*g.nums_actions):
if g.is_nash(a):
yield a
68 changes: 68 additions & 0 deletions quantecon/game_theory/tests/test_pure_nash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Author: Zejin Shi

Tests for pure_nash.py

"""

import numpy as np
import itertools
from nose.tools import eq_
from quantecon.game_theory import NormalFormGame, pure_nash_brute


class TestPureNashBruteForce():
def setUp(self):
self.game_dicts = []

# Matching Pennies game with no pure nash equilibrium
MP_bimatrix = [[(1, -1), (-1, 1)],
[(-1, 1), (1, -1)]]
MP_NE = []

# Prisoners' Dilemma game with one pure nash equilibrium
PD_bimatrix = [[(1, 1), (-2, 3)],
[(3, -2), (0, 0)]]
PD_NE = [(1, 1)]

# Battle of the Sexes game with two pure nash equilibria
BoS_bimatrix = [[(3, 2), (1, 0)],
[(0, 1), (2, 3)]]
BoS_NE = [(0, 0), (1, 1)]

# Unanimity Game with more than two players
N = 4
a, b = 1, 2
g_Unanimity = NormalFormGame((2,)*N)
g_Unanimity[(0,)*N] = (a,)*N
g_Unanimity[(1,)*N] = (b,)*N

Unanimity_NE = [(0,)*N]
for k in range(2, N-2+1):
for ind in itertools.combinations(range(N), k):
a = np.ones(N, dtype=int)
a[[ind]] = 0
Unanimity_NE.append(tuple(a))
Unanimity_NE.append((1,)*N)

for bimatrix, NE in zip([MP_bimatrix, PD_bimatrix, BoS_bimatrix],
[MP_NE, PD_NE, BoS_NE]):
d = {'g': NormalFormGame(bimatrix),
'NEs': NE}
self.game_dicts.append(d)
self.game_dicts.append({'g': g_Unanimity,
'NEs': Unanimity_NE})

def test_brute_force(self):
for d in self.game_dicts:
eq_(sorted(pure_nash_brute(d['g'])), sorted(d['NEs']))


if __name__ == '__main__':
import sys
import nose

argv = sys.argv[:]
argv.append('--verbose')
argv.append('--nocapture')
nose.main(argv=argv, defaultTest=__file__)