From 14548919b9a7b7414ca48c8ae7d9aa8e0afff010 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sun, 20 May 2018 14:54:26 +0900 Subject: [PATCH 1/3] EHN: Add `'interior-point'` option to `is_dominated` https://github.com/QuantEcon/QuantEcon.py/pull/327#issue-136392329 --- quantecon/game_theory/normal_form_game.py | 7 ++++--- quantecon/game_theory/tests/test_normal_form_game.py | 11 +++++++---- setup.py | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/quantecon/game_theory/normal_form_game.py b/quantecon/game_theory/normal_form_game.py index f6c8da9da..7ab4e2835 100644 --- a/quantecon/game_theory/normal_form_game.py +++ b/quantecon/game_theory/normal_form_game.py @@ -395,8 +395,9 @@ def is_dominated(self, action, tol=None, method=None): method : str, optional(default=None) If None, `lemke_howson` from `quantecon.game_theory` is used to solve for a Nash equilibrium of an auxiliary zero-sum - game. If `method='simplex'`, `scipy.optimize.linprog` is - used with `method='simplex'`. + game. If `method` is set to `'simplex'` or + `'interior-point'`, `scipy.optimize.linprog` is used with + the method as specified by `method`. Returns ------- @@ -425,7 +426,7 @@ def is_dominated(self, action, tol=None, method=None): g_zero_sum = NormalFormGame([Player(D), Player(-D.T)]) NE = lemke_howson(g_zero_sum) return NE[0] @ D @ NE[1] > tol - elif method in ['simplex']: + elif method in ['simplex', 'interior-point']: from scipy.optimize import linprog m, n = D.shape A = np.empty((n+2, m+1)) diff --git a/quantecon/game_theory/tests/test_normal_form_game.py b/quantecon/game_theory/tests/test_normal_form_game.py index ed3b7c464..710ad4053 100644 --- a/quantecon/game_theory/tests/test_normal_form_game.py +++ b/quantecon/game_theory/tests/test_normal_form_game.py @@ -15,6 +15,9 @@ # Player # +LP_METHODS = [None, 'simplex', 'interior-point'] + + class TestPlayer_1opponent: """Test the methods of Player with one opponent player""" @@ -69,7 +72,7 @@ def test_is_best_response_against_mixed(self): def test_is_dominated(self): for action in range(self.player.num_actions): - for method in [None, 'simplex']: + for method in LP_METHODS: eq_(self.player.is_dominated(action, method=method), False) @@ -106,7 +109,7 @@ def test_best_response_list_when_tie(self): def test_is_dominated(self): for action in range(self.player.num_actions): - for method in [None, 'simplex']: + for method in LP_METHODS: eq_(self.player.is_dominated(action, method=method), False) @@ -126,7 +129,7 @@ def test_player_corner_cases(): player = Player(np.zeros((n, m))) for action in range(n): eq_(player.is_best_response(action, [1/m]*m), True) - for method in [None, 'simplex']: + for method in LP_METHODS: eq_(player.is_dominated(action, method=method), False) e = 1e-8 @@ -134,7 +137,7 @@ def test_player_corner_cases(): action = 0 eq_(player.is_best_response(action, [1/2, 1/2], tol=e), True) eq_(player.is_best_response(action, [1/2, 1/2], tol=e/2), False) - for method in [None, 'simplex']: + for method in LP_METHODS: eq_(player.is_dominated(action, tol=e, method=method), False) eq_(player.is_dominated(action, tol=e/2, method=method), True) diff --git a/setup.py b/setup.py index 62cb44c9c..2df3ab4c8 100644 --- a/setup.py +++ b/setup.py @@ -115,7 +115,7 @@ def write_version_py(filename=None): install_requires=[ 'numba>=0.36.2', 'numpy', - 'scipy', + 'scipy>=1.0.0', 'sympy', ] ) From 0404f1a0220590a4bced5c859a823330d201660f Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Tue, 5 Jun 2018 21:51:04 +0900 Subject: [PATCH 2/3] EHN: Add `dominated_actions` to `Player` --- quantecon/game_theory/normal_form_game.py | 31 +++++++++++++++++ .../tests/test_normal_form_game.py | 34 ++++++++++++++----- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/quantecon/game_theory/normal_form_game.py b/quantecon/game_theory/normal_form_game.py index 7ab4e2835..8313144f6 100644 --- a/quantecon/game_theory/normal_form_game.py +++ b/quantecon/game_theory/normal_form_game.py @@ -450,6 +450,37 @@ def is_dominated(self, action, tol=None, method=None): else: raise ValueError('Unknown method {0}'.format(method)) + def dominated_actions(self, tol=None, method=None): + """ + Return a list of actions that are strictly dominated by some + mixed actions. + + Parameters + ---------- + tol : scalar(float), optional(default=None) + Tolerance level used in determining domination. If None, + default to the value of the `tol` attribute. + + method : str, optional(default=None) + If None, `lemke_howson` from `quantecon.game_theory` is used + to solve for a Nash equilibrium of an auxiliary zero-sum + game. If `method` is set to `'simplex'` or + `'interior-point'`, `scipy.optimize.linprog` is used with + the method as specified by `method`. + + Returns + ------- + list(int) + List of integers representing pure actions, each of which is + strictly dominated by some mixed action. + + """ + out = [] + for action in range(self.num_actions): + if self.is_dominated(action, tol=tol, method=method): + out.append(action) + return out + class NormalFormGame: """ diff --git a/quantecon/game_theory/tests/test_normal_form_game.py b/quantecon/game_theory/tests/test_normal_form_game.py index 710ad4053..2e3ea0c2b 100644 --- a/quantecon/game_theory/tests/test_normal_form_game.py +++ b/quantecon/game_theory/tests/test_normal_form_game.py @@ -2,8 +2,6 @@ Tests for normal_form_game.py """ -from __future__ import division - import numpy as np from numpy.testing import assert_array_equal from nose.tools import eq_, ok_, raises @@ -75,6 +73,10 @@ def test_is_dominated(self): for method in LP_METHODS: eq_(self.player.is_dominated(action, method=method), False) + def test_dominated_actions(self): + for method in LP_METHODS: + eq_(self.player.dominated_actions(method=method), []) + class TestPlayer_2opponents: """Test the methods of Player with two opponent players""" @@ -112,6 +114,10 @@ def test_is_dominated(self): for method in LP_METHODS: eq_(self.player.is_dominated(action, method=method), False) + def test_dominated_actions(self): + for method in LP_METHODS: + eq_(self.player.dominated_actions(method=method), []) + def test_random_choice(): n, m = 5, 4 @@ -139,7 +145,10 @@ def test_player_corner_cases(): eq_(player.is_best_response(action, [1/2, 1/2], tol=e/2), False) for method in LP_METHODS: eq_(player.is_dominated(action, tol=e, method=method), False) + eq_(player.dominated_actions(tol=e, method=method), []) + eq_(player.is_dominated(action, tol=e/2, method=method), True) + eq_(player.dominated_actions(tol=e/2, method=method), [action]) # NormalFormGame # @@ -292,25 +301,32 @@ class TestPlayer_0opponents: def setUp(self): """Setup a Player instance""" - payoffs = [0, 1] - self.player = Player(payoffs) + self.payoffs = [0, 1, -1] + self.player = Player(self.payoffs) + self.best_response_action = 1 + self.dominated_actions = [0, 2] def test_payoff_vector(self): """Trivial player: payoff_vector""" - assert_array_equal(self.player.payoff_vector(None), [0, 1]) + assert_array_equal(self.player.payoff_vector(None), self.payoffs) def test_is_best_response(self): """Trivial player: is_best_response""" - ok_(self.player.is_best_response(1, None)) + ok_(self.player.is_best_response(self.best_response_action, None)) def test_best_response(self): """Trivial player: best_response""" - eq_(self.player.best_response(None), 1) + eq_(self.player.best_response(None), self.best_response_action) def test_is_dominated(self): """Trivial player: is_dominated""" - eq_(self.player.is_dominated(0), True) - eq_(self.player.is_dominated(1), False) + for action in range(self.player.num_actions): + eq_(self.player.is_dominated(action), + (action in self.dominated_actions)) + + def test_dominated_actions(self): + """Trivial player: dominated_actions""" + eq_(self.player.dominated_actions(), self.dominated_actions) class TestNormalFormGame_1p: From 89e1b4ad445dc10c29d10f56934a971cf2ecc983 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Tue, 5 Jun 2018 22:38:18 +0900 Subject: [PATCH 3/3] TEST: Make conservative the test failing on Travis CI but passing locally --- quantecon/game_theory/tests/test_normal_form_game.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quantecon/game_theory/tests/test_normal_form_game.py b/quantecon/game_theory/tests/test_normal_form_game.py index 2e3ea0c2b..a993b256e 100644 --- a/quantecon/game_theory/tests/test_normal_form_game.py +++ b/quantecon/game_theory/tests/test_normal_form_game.py @@ -144,8 +144,8 @@ def test_player_corner_cases(): eq_(player.is_best_response(action, [1/2, 1/2], tol=e), True) eq_(player.is_best_response(action, [1/2, 1/2], tol=e/2), False) for method in LP_METHODS: - eq_(player.is_dominated(action, tol=e, method=method), False) - eq_(player.dominated_actions(tol=e, method=method), []) + eq_(player.is_dominated(action, tol=2*e, method=method), False) + eq_(player.dominated_actions(tol=2*e, method=method), []) eq_(player.is_dominated(action, tol=e/2, method=method), True) eq_(player.dominated_actions(tol=e/2, method=method), [action])