-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
TEST: Increase coverage on game_theory #343
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3390569
TEST: Add tests for mclennen_tourky.py
QBatista b8277b4
TEST: Add test for exceptions in support_enumeration.py
QBatista 5453ed3
TEST: Add tests for vertex_enumeration.py
QBatista 7b3feea
Merge branch 'tests_mclennan_tourky' into tests_increase_coverage
QBatista 1a6fc35
Merge branch 'tests_support_enumeration' into tests_increase_coverage
QBatista 6e33cf6
Merge branch 'tests_vertex_enumeration' into tests_increase_coverage
QBatista 917b473
FIX: Correct typo
QBatista cf32a4b
RFC: Change assert_allclose to assert_array_equal
QBatista File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,12 @@ | |
|
||
""" | ||
import numpy as np | ||
from nose.tools import ok_ | ||
from numpy.testing import assert_allclose | ||
from nose.tools import ok_, raises | ||
from quantecon.game_theory import Player, NormalFormGame, mclennan_tourky | ||
from quantecon.game_theory.mclennan_tourky import _best_response_selection, \ | ||
_flatten_action_profile, \ | ||
_is_epsilon_nash | ||
|
||
|
||
class TestMclennanTourky(): | ||
|
@@ -54,13 +58,102 @@ def test_pure_nash(self): | |
NE, res = mclennan_tourky(d['g'], init=init, full_output=True) | ||
ok_(res.num_iter==1) | ||
|
||
def test_epsilon_nash(self): | ||
|
||
class TestMclennanTourkyInvalidInputs(): | ||
def setUp(self): | ||
self.bimatrix = [[(3, 3), (3, 2)], | ||
[(2, 2), (5, 6)], | ||
[(0, 3), (6, 1)]] | ||
self.g = NormalFormGame(self.bimatrix) | ||
|
||
@raises(TypeError) | ||
def test_mclennan_tourky_invalid_g(self): | ||
mclennan_tourky(self.bimatrix) | ||
|
||
@raises(TypeError) | ||
def test_mclennan_tourky_invalid_init_type(self): | ||
mclennan_tourky(self.g, 1) | ||
|
||
@raises(ValueError) | ||
def test_mclennan_tourky_invalid_init_length(self): | ||
mclennan_tourky(self.g, [1]) | ||
|
||
|
||
class TestEpsilonNash(): | ||
def setUp(self): | ||
def anti_coordination(N, v): | ||
payoff_array = np.empty((2,)*N) | ||
payoff_array[0, :] = 1 | ||
payoff_array[1, :] = 0 | ||
payoff_array[1].flat[0] = v | ||
g = NormalFormGame((Player(payoff_array),)*N) | ||
return g | ||
|
||
def p_star(N, v): | ||
# Unique symmetric NE mixed action: [p_star, 1-p_star] | ||
return 1 / (v**(1/(N-1))) | ||
|
||
def epsilon_nash_interval(N, v, epsilon): | ||
# Necessary, but not sufficient, condition: lb < p < ub | ||
lb = p_star(N, v) - epsilon / ((N-1)*(v**(1/(N-1))-1)) | ||
ub = p_star(N, v) + epsilon / (N-1) | ||
return lb, ub | ||
|
||
self.game_dicts = [] | ||
v = 2 | ||
epsilon = 1e-5 | ||
|
||
Ns = [2, 3, 4] | ||
for N in Ns: | ||
g = anti_coordination(N, v) | ||
lb, ub = epsilon_nash_interval(N, v, epsilon) | ||
d = {'g': g, | ||
'epsilon': epsilon, | ||
'lb': lb, | ||
'ub': ub} | ||
self.game_dicts.append(d) | ||
|
||
self.bimatrix = [[(3, 3), (3, 2)], | ||
[(2, 2), (5, 6)], | ||
[(0, 3), (6, 1)]] | ||
self.g = NormalFormGame(self.bimatrix) | ||
|
||
def test_epsilon_nash_with_full_output(self): | ||
for d in self.game_dicts: | ||
NE, res = \ | ||
mclennan_tourky(d['g'], epsilon=d['epsilon'], full_output=True) | ||
for i in range(d['g'].N): | ||
ok_(d['lb'] < NE[i][0] < d['ub']) | ||
|
||
def test_epsilon_nash_without_full_output(self): | ||
for d in self.game_dicts: | ||
NE = mclennan_tourky(d['g'], epsilon=d['epsilon'], | ||
full_output=False) | ||
for i in range(d['g'].N): | ||
ok_(d['lb'] < NE[i][0] < d['ub']) | ||
|
||
def test_is_epsilon_nash_no_indptr(self): | ||
ok_(_is_epsilon_nash([1., 0., 0., 1., 0.], self.g, 1e-5)) | ||
|
||
|
||
def test_flatten_action_profile(): | ||
unflattened_actions = [[1/3, 1/3, 1/3], [1/2, 1/2]] | ||
flattened_actions = [1/3, 1/3, 1/3, 1/2, 1/2] | ||
test_obj = _flatten_action_profile(unflattened_actions, [0, 3, 5]) | ||
assert_allclose(test_obj, flattened_actions) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be |
||
|
||
|
||
def test_best_response_selection_no_indptr(): | ||
bimatrix = [[(3, 3), (3, 2)], | ||
[(2, 2), (5, 6)], | ||
[(0, 3), (6, 1)]] | ||
g = NormalFormGame(bimatrix) | ||
|
||
test_obj = _best_response_selection([1/3, 1/3, 1/3, 1/2,1/2], g) | ||
expected_output = np.array([0., 1., 0., 0., 1.]) | ||
|
||
assert_allclose(test_obj, expected_output) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be |
||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @oyamad I didn't know you could use
(
like this.