Skip to content
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

pure_nash_brute: Add tol option #385

Merged
merged 1 commit into from
Jan 12, 2018
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
14 changes: 10 additions & 4 deletions quantecon/game_theory/pure_nash.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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