Skip to content

Commit 3029221

Browse files
authored
Merge pull request #477 from okuchap/random
FEAT: Add `random_pure_actions` and `random_mixed_actions`
2 parents 48b0678 + 6450f95 commit 3029221

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

quantecon/game_theory/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"""
66
from .normal_form_game import Player, NormalFormGame
77
from .normal_form_game import pure2mixed, best_response_2p
8-
from .random import random_game, covariance_game
8+
from .random import (
9+
random_game, covariance_game, random_pure_actions, random_mixed_actions
10+
)
911
from .pure_nash import pure_nash_brute, pure_nash_brute_gen
1012
from .support_enumeration import support_enumeration, support_enumeration_gen
1113
from .lemke_howson import lemke_howson

quantecon/game_theory/random.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
Generate random NormalFormGame instances.
33
44
"""
5+
56
import numpy as np
67

78
from .normal_form_game import Player, NormalFormGame
89
from ..util import check_random_state
10+
from ..random import probvec
911

1012

1113
def random_game(nums_actions, random_state=None):
@@ -92,3 +94,60 @@ def covariance_game(nums_actions, rho, random_state=None):
9294
random_state.multivariate_normal(mean, cov, nums_actions)
9395
g = NormalFormGame(payoff_profile_array)
9496
return g
97+
98+
99+
def random_pure_actions(nums_actions, random_state=None):
100+
"""
101+
Return a tuple of random pure actions (integers).
102+
103+
Parameters
104+
----------
105+
nums_actions : tuple(int)
106+
Tuple of the numbers of actions, one for each player.
107+
108+
random_state : int or np.random.RandomState, optional
109+
Random seed (integer) or np.random.RandomState instance to set
110+
the initial state of the random number generator for
111+
reproducibility. If None, a randomly initialized RandomState is
112+
used.
113+
114+
Returns
115+
-------
116+
action_profile : Tuple(int)
117+
Tuple of actions, one for each player.
118+
119+
"""
120+
random_state = check_random_state(random_state)
121+
action_profile = tuple(
122+
[random_state.randint(num_actions) for num_actions in nums_actions]
123+
)
124+
return action_profile
125+
126+
127+
def random_mixed_actions(nums_actions, random_state=None):
128+
"""
129+
Return a tuple of random mixed actions (vectors of floats).
130+
131+
Parameters
132+
----------
133+
nums_actions : tuple(int)
134+
Tuple of the numbers of actions, one for each player.
135+
136+
random_state : int or np.random.RandomState, optional
137+
Random seed (integer) or np.random.RandomState instance to set
138+
the initial state of the random number generator for
139+
reproducibility. If None, a randomly initialized RandomState is
140+
used.
141+
142+
Returns
143+
-------
144+
action_profile : tuple(ndarray(float, ndim=1))
145+
Tuple of mixed_actions, one for each player.
146+
147+
"""
148+
random_state = check_random_state(random_state)
149+
action_profile = tuple(
150+
[probvec(1, num_actions, random_state).ravel()
151+
for num_actions in nums_actions]
152+
)
153+
return action_profile

quantecon/game_theory/tests/test_random.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
"""
55
import numpy as np
66
from numpy.testing import assert_allclose, assert_raises
7-
from nose.tools import eq_
7+
from nose.tools import eq_, ok_
88

9-
from quantecon.game_theory import random_game, covariance_game
9+
from quantecon.game_theory import (
10+
random_game, covariance_game, random_pure_actions, random_mixed_actions
11+
)
1012

1113

1214
def test_random_game():
@@ -59,6 +61,25 @@ def test_covariance_game_value_error():
5961
assert_raises(ValueError, covariance_game, nums_actions, rho)
6062

6163

64+
def test_random_pure_actions():
65+
nums_actions = (2, 3, 4)
66+
N = len(nums_actions)
67+
seed = 1234
68+
action_profiles = [
69+
random_pure_actions(nums_actions, seed) for i in range(2)
70+
]
71+
for i in range(N):
72+
ok_(action_profiles[0][i] < nums_actions[i])
73+
eq_(action_profiles[0], action_profiles[1])
74+
75+
76+
def test_random_mixed_actions():
77+
nums_actions = (2, 3, 4)
78+
seed = 1234
79+
action_profile = random_mixed_actions(nums_actions, seed)
80+
eq_(tuple([len(action) for action in action_profile]), nums_actions)
81+
82+
6283
if __name__ == '__main__':
6384
import sys
6485
import nose

0 commit comments

Comments
 (0)