Skip to content

Commit

Permalink
MAINT: TEST: Remove nose functions (#597)
Browse files Browse the repository at this point in the history
Replaced with `numpy.testing` functions
  • Loading branch information
oyamad committed Jan 12, 2022
1 parent 1eabdbf commit 92c0865
Showing 1 changed file with 23 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"""
import numpy as np
from scipy.special import comb
from numpy.testing import assert_array_equal
from nose.tools import eq_, ok_, raises
from numpy.testing import assert_array_equal, assert_, assert_raises
from quantecon.gridtools import num_compositions
from quantecon.game_theory import pure_nash_brute

Expand All @@ -15,19 +14,19 @@


class TestBlottoGame:
def setUp(self):
def setup(self):
self.h, self.t = 4, 3
rho = 0.5
self.g = blotto_game(self.h, self.t, rho)

def test_size(self):
n = num_compositions(self.h, self.t)
eq_(self.g.nums_actions, (n, n))
assert_(self.g.nums_actions == (n, n))

def test_constant_diagonal(self):
for i in range(self.g.N):
diag = self.g.payoff_arrays[i].diagonal()
ok_((diag == diag[0]).all())
assert_((diag == diag[0]).all())

def test_seed(self):
seed = 0
Expand All @@ -39,21 +38,23 @@ def test_seed(self):


class TestRankingGame:
def setUp(self):
def setup(self):
self.n = 100
self.g = ranking_game(self.n)

def test_size(self):
eq_(self.g.nums_actions, (self.n, self.n))
assert_(self.g.nums_actions == (self.n, self.n))

def test_weakly_decreasing_row_wise_payoffs(self):
for payoff_array in self.g.payoff_arrays:
ok_((payoff_array[:, 1:-1] >= payoff_array[:, 2:]).all())
assert_((payoff_array[:, 1:-1] >= payoff_array[:, 2:]).all())

def test_elements_first_row(self):
eq_(self.g.payoff_arrays[0][0, 0] + self.g.payoff_arrays[1][0, 0], 1.)
assert_(
self.g.payoff_arrays[0][0, 0] + self.g.payoff_arrays[1][0, 0] == 1.
)
for payoff_array in self.g.payoff_arrays:
ok_(np.isin(payoff_array[0, :], [0, 1, 0.5]).all())
assert_(np.isin(payoff_array[0, :], [0, 1, 0.5]).all())

def test_seed(self):
seed = 0
Expand Down Expand Up @@ -84,53 +85,52 @@ def test_sgc_game():


class TestTournamentGame:
def setUp(self):
def setup(self):
self.n = 5
self.k = 3
self.m = comb(self.n, self.k, exact=True)
self.g = tournament_game(self.n, self.k)

def test_size(self):
eq_(self.g.nums_actions, (self.n, self.m))
assert_(self.g.nums_actions == (self.n, self.m))

def test_payoff_values(self):
possible_values = [0, 1]
for payoff_array in self.g.payoff_arrays:
ok_(np.isin(payoff_array, possible_values).all())
assert_(np.isin(payoff_array, possible_values).all())

max_num_dominated_subsets = \
sum([comb(i, self.k, exact=True) for i in range(self.n)])
ok_(self.g.payoff_arrays[0].sum() <= max_num_dominated_subsets)
ok_((self.g.payoff_arrays[1].sum(axis=1) == self.k).all())
assert_(self.g.payoff_arrays[0].sum() <= max_num_dominated_subsets)
assert_((self.g.payoff_arrays[1].sum(axis=1) == self.k).all())

def test_seed(self):
seed = 0
g0 = tournament_game(self.n, self.k, random_state=seed)
g1 = tournament_game(self.n, self.k, random_state=seed)
assert_array_equal(g1.payoff_profile_array, g0.payoff_profile_array)

@raises(ValueError)
def test_raises_value_error_too_large_inputs(self):
n, k = 100, 50
tournament_game(n, k)
assert_raises(ValueError, tournament_game, n, k)


class TestUnitVectorGame:
def setUp(self):
def setup(self):
self.n = 100
self.g = unit_vector_game(self.n)

def test_size(self):
eq_(self.g.nums_actions, (self.n, self.n))
assert_(self.g.nums_actions == (self.n, self.n))

def test_payoff_values(self):
# Player 0
ok_((np.sum(self.g.players[0].payoff_array, axis=0) == 1).all())
assert_((np.sum(self.g.players[0].payoff_array, axis=0) == 1).all())

def test_avoid_pure_nash(self):
NEs = pure_nash_brute(unit_vector_game(self.n, avoid_pure_nash=True),
tol=0)
eq_(len(NEs), 0)
assert_(len(NEs) == 0)

def test_seed(self):
seed = 0
Expand All @@ -144,12 +144,11 @@ def test_redraw(self):
n = 2
g = unit_vector_game(n, avoid_pure_nash=True, random_state=seed)
NEs = pure_nash_brute(g, tol=0)
eq_(len(NEs), 0)
assert_(len(NEs) == 0)

@raises(ValueError)
def test_raises_value_error_avoid_pure_nash_n_1(self):
n = 1
unit_vector_game(n, avoid_pure_nash=True)
assert_raises(ValueError, unit_vector_game, n, avoid_pure_nash=True)


def test_payoff_range():
Expand All @@ -162,13 +161,3 @@ def test_payoff_range():
g = func(**kwargs)
for payoff_array in g.payoff_arrays:
assert_array_equal(np.clip(payoff_array, 0, 1), payoff_array)


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

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

0 comments on commit 92c0865

Please sign in to comment.