Skip to content

Commit

Permalink
Merge ec15121 into d73c959
Browse files Browse the repository at this point in the history
  • Loading branch information
okuchap committed Jan 12, 2018
2 parents d73c959 + ec15121 commit 4edb9ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 10 additions & 4 deletions quantecon/game_theory/pure_nash.py
Expand Up @@ -9,13 +9,16 @@
import numpy as np


def pure_nash_brute(g):
def pure_nash_brute(g, tol=None):
"""
Find all pure Nash equilibria of a normal form game by brute force.
Parameters
----------
g : NormalFormGame
tol : scalar(float), optional(default=None)
Tolerance level used in determining best responses. If None,
default to the value of the `tol` attribute of `g`.
Returns
-------
Expand Down Expand Up @@ -43,16 +46,19 @@ def pure_nash_brute(g):
[]
"""
return list(pure_nash_brute_gen(g))
return list(pure_nash_brute_gen(g, tol=tol))


def pure_nash_brute_gen(g):
def pure_nash_brute_gen(g, tol=None):
"""
Generator version of `pure_nash_brute`.
Parameters
----------
g : NormalFormGame
tol : scalar(float), optional(default=None)
Tolerance level used in determining best responses. If None,
default to the value of the `tol` attribute of `g`.
Yields
------
Expand All @@ -61,5 +67,5 @@ def pure_nash_brute_gen(g):
"""
for a in np.ndindex(*g.nums_actions):
if g.is_nash(a):
if g.is_nash(a, tol=tol):
yield a
14 changes: 14 additions & 0 deletions quantecon/game_theory/tests/test_pure_nash.py
Expand Up @@ -57,6 +57,20 @@ def test_brute_force(self):
for d in self.game_dicts:
eq_(sorted(pure_nash_brute(d['g'])), sorted(d['NEs']))

def test_tol(self):
# Prisoners' Dilemma game with one NE and one epsilon NE
epsilon = 1e-08

PD_bimatrix = [[(1, 1), (-2, 1 + epsilon)],
[(1 + epsilon, -2), (0, 0)]]

NEs = [(1, 1)]
epsilon_NEs = [(1, 1), (0, 0)]

g = NormalFormGame(PD_bimatrix)
for tol, answer in zip([0, epsilon], [NEs, epsilon_NEs]):
eq_(sorted(pure_nash_brute(g, tol=tol)), sorted(answer))


if __name__ == '__main__':
import sys
Expand Down

0 comments on commit 4edb9ff

Please sign in to comment.