Skip to content
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 docs/source/game_theory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Game theory
game_theory/random
game_theory/support_enumeration
game_theory/utilities
game_theory/vertex_enumeration
7 changes: 7 additions & 0 deletions docs/source/game_theory/vertex_enumeration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vertex_enumeration
==================

.. automodule:: quantecon.game_theory.vertex_enumeration
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions quantecon/game_theory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .support_enumeration import support_enumeration, support_enumeration_gen
from .lemke_howson import lemke_howson
from .mclennan_tourky import mclennan_tourky
from .vertex_enumeration import vertex_enumeration, vertex_enumeration_gen
108 changes: 108 additions & 0 deletions quantecon/game_theory/tests/test_vertex_enumeration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""
Author: Daisuke Oyama

Tests for vertex_enumeration.py

"""
import numpy as np
from numpy.testing import assert_allclose, assert_array_equal
from nose.tools import eq_
from quantecon.game_theory import NormalFormGame, vertex_enumeration
from quantecon.game_theory.vertex_enumeration import _BestResponsePolytope


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

# From von Stengel 2007 in Algorithmic Game Theory
bimatrix = [[(3, 3), (3, 2)],
[(2, 2), (5, 6)],
[(0, 3), (6, 1)]]
d = {'g': NormalFormGame(bimatrix),
'NEs': [([1, 0, 0], [1, 0]),
([4/5, 1/5, 0], [2/3, 1/3]),
([0, 1/3, 2/3], [1/3, 2/3])]}
self.game_dicts.append(d)

# Degenerate game
bimatrix = [[(3, 3), (3, 3)],
[(2, 2), (5, 6)],
[(0, 3), (6, 1)]]
d = {'g': NormalFormGame(bimatrix),
'NEs': [([1, 0, 0], [1, 0]),
([1, 0, 0], [2/3, 1/3]),
([0, 1/3, 2/3], [1/3, 2/3])]}
self.game_dicts.append(d)

def test_vertex_enumeration(self):
for d in self.game_dicts:
NEs_computed = vertex_enumeration(d['g'])
eq_(len(NEs_computed), len(d['NEs']))
for NEs in (NEs_computed, d['NEs']):
NEs.sort(key=lambda x: (list(x[0]), list(x[1])))
for actions_computed, actions in zip(NEs_computed, d['NEs']):
for action_computed, action in zip(actions_computed, actions):
assert_allclose(action_computed, action)


class TestBestResponsePolytope:
def setUp(self):
# From von Stengel 2007 in Algorithmic Game Theory
bimatrix = [[(3, 3), (3, 2)],
[(2, 2), (5, 6)],
[(0, 3), (6, 1)]]
g = NormalFormGame(bimatrix)

# Original best reponse polytope for player 0
vertices_P = np.array([
[0, 0, 0], # 0
[1/3, 0, 0], # a
[2/7, 1/14, 0], # b
[0, 1/6, 0], # c
[0, 1/8, 1/4], # d
[0, 0, 1/3] # e
])
labelings_P = np.array([
[0, 1, 2], # 0
[1, 2, 3], # a
[2, 3, 4], # b
[0, 2, 4], # c
[0, 3, 4], # d
[0, 1, 3] # e
])

# Sort rows lexicographically
K = labelings_P.shape[1]
ind = np.lexsort([labelings_P[:, K-k-1] for k in range(K)])
self.labelings_P = labelings_P[ind]
self.vertices_P = vertices_P[ind]

# Translated best reponse polytope for player 0
self.brp0 = _BestResponsePolytope(g.players[1], idx=0)

def test_best_response_polytope(self):
# Sort each row
labelings_computed = np.sort(self.brp0.labelings, axis=1)

# Sort rows lexicographically
K = labelings_computed.shape[1]
ind = np.lexsort([labelings_computed[:, K-k-1] for k in range(K)])
labelings_computed = labelings_computed[ind]
vertices_computed = \
-self.brp0.equations[:, :-1] / self.brp0.equations[:, [-1]] + \
1/self.brp0.trans_recip
vertices_computed = vertices_computed[ind]

assert_array_equal(labelings_computed, self.labelings_P)
assert_allclose(vertices_computed, self.vertices_P, atol=1e-15)


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

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