# How to use the quantecon.game_theory.normal_form_game.NormalFormGame function in quantecon

## To help you get started, we’ve selected a few quantecon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

QuantEcon / QuantEcon.py / quantecon / game_theory / normal_form_game.py View on Github
``````if self.num_opponents == 0:
return payoff_array.max() > payoff_array[action] + tol

ind = np.ones(self.num_actions, dtype=bool)
ind[action] = False
D = payoff_array[ind]
D -= payoff_array[action]
if D.shape[0] == 0:  # num_actions == 1
return False
if self.num_opponents >= 2:
D.shape = (D.shape[0], np.prod(D.shape[1:]))

if method is None:
from .lemke_howson import lemke_howson
g_zero_sum = NormalFormGame([Player(D), Player(-D.T)])
NE = lemke_howson(g_zero_sum)
return NE[0] @ D @ NE[1] > tol
elif method in ['simplex', 'interior-point']:
from scipy.optimize import linprog
m, n = D.shape
A = np.empty((n+2, m+1))
A[:n, :m] = -D.T
A[:n, -1] = 1  # Slack variable
A[n, :m], A[n+1, :m] = 1, -1  # Equality constraint
A[n:, -1] = 0
b = np.empty(n+2)
b[:n] = 0
b[n], b[n+1] = 1, -1
c = np.zeros(m+1)
c[-1] = -1
res = linprog(c, A_ub=A, b_ub=b, method=method)``````
QuantEcon / QuantEcon.py / quantecon / game_theory / game_generators / bimatrix_generators.py View on Github
``````----------
.. [1] `Combinatorial number system
`_,
Wikipedia.

"""
m = scipy.special.comb(n, k, exact=True)
if m &gt; np.iinfo(np.intp).max:
raise ValueError('Maximum allowed size exceeded')

payoff_arrays = tuple(np.zeros(shape) for shape in [(n, m), (m, n)])
tourn = random_tournament_graph(n, random_state=random_state)
indices, indptr = tourn.csgraph.indices, tourn.csgraph.indptr
_populate_tournament_payoff_array0(payoff_arrays[0], k, indices, indptr)
_populate_tournament_payoff_array1(payoff_arrays[1], k)
g = NormalFormGame(
[Player(payoff_array) for payoff_array in payoff_arrays]
)
return g
``````
QuantEcon / QuantEcon.py / quantecon / game_theory / game_generators / bimatrix_generators.py View on Github
``````[ 0.58,  0.58,  0.58,  0.58,  0.58]])

"""
payoff_arrays = tuple(np.empty((n, n)) for i in range(2))
random_state = check_random_state(random_state)

scores = random_state.randint(1, steps+1, size=(2, n))
scores.cumsum(axis=1, out=scores)

costs = np.empty((2, n-1))
costs[:] = random_state.randint(1, steps+1, size=(2, n-1))
costs.cumsum(axis=1, out=costs)
costs[:] /= (n * steps)

_populate_ranking_payoff_arrays(payoff_arrays, scores, costs)
g = NormalFormGame(
[Player(payoff_array) for payoff_array in payoff_arrays]
)
return g
``````
QuantEcon / QuantEcon.py / quantecon / game_theory / game_generators / bimatrix_generators.py View on Github
``````[ 0.  ,  0.  ,  0.  ,  0.  ,  0.75,  0.  ,  0.  ],
[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.75,  0.  ],
[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.75]])
>>> g.players[1]
Player([[ 0.75,  0.5 ,  1.  ,  0.5 ,  0.5 ,  0.5 ,  0.5 ],
[ 1.  ,  0.75,  0.5 ,  0.5 ,  0.5 ,  0.5 ,  0.5 ],
[ 0.5 ,  1.  ,  0.75,  0.5 ,  0.5 ,  0.5 ,  0.5 ],
[ 0.  ,  0.  ,  0.  ,  0.  ,  0.75,  0.  ,  0.  ],
[ 0.  ,  0.  ,  0.  ,  0.75,  0.  ,  0.  ,  0.  ],
[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.75],
[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.75,  0.  ]])

"""
payoff_arrays = tuple(np.empty((4*k-1, 4*k-1)) for i in range(2))
_populate_sgc_payoff_arrays(payoff_arrays)
g = NormalFormGame(
[Player(payoff_array) for payoff_array in payoff_arrays]
)
return g
``````
QuantEcon / QuantEcon.py / quantecon / game_theory / normal_form_game.py View on Github
``````&gt;&gt;&gt; g2 = g1.delete_action(1, 0)
&gt;&gt;&gt; print(g2)
2-player NormalFormGame with payoff profile array:
[[[0, 1]],
[[3, 1]]]

"""
# Allow negative indexing
if -self.N &lt;= player_idx &lt; 0:
player_idx = player_idx + self.N

players_new = tuple(
player.delete_action(action, player_idx-i)
for i, player in enumerate(self.players)
)
return NormalFormGame(players_new)
``````
QuantEcon / QuantEcon.py / quantecon / game_theory / random.py View on Github
``````Returns
-------
g : NormalFormGame

"""
N = len(nums_actions)
if N == 0:
raise ValueError('nums_actions must be non-empty')

random_state = check_random_state(random_state)
players = [
Player(random_state.random_sample(nums_actions[i:]+nums_actions[:i]))
for i in range(N)
]
g = NormalFormGame(players)
return g
``````
QuantEcon / QuantEcon.py / quantecon / game_theory / random.py View on Github
``````N = len(nums_actions)
if N &lt;= 1:
raise ValueError('length of nums_actions must be at least 2')
if not (-1 / (N - 1) &lt;= rho &lt;= 1):
lb = '-1' if N == 2 else '-1/{0}'.format(N-1)
raise ValueError('rho must be in [{0}, 1]'.format(lb))

mean = np.zeros(N)
cov = np.empty((N, N))
cov.fill(rho)
cov[range(N), range(N)] = 1

random_state = check_random_state(random_state)
payoff_profile_array = \
random_state.multivariate_normal(mean, cov, nums_actions)
g = NormalFormGame(payoff_profile_array)
return g``````
QuantEcon / QuantEcon.py / quantecon / game_theory / game_generators / bimatrix_generators.py View on Github
``````>>> g.players[1]
Player([[-1.20042463, -1.39708658, -1.39708658, -1.39708658],
[-1.00376268, -1.20042463, -1.39708658, -1.39708658],
[-1.00376268, -1.00376268, -1.20042463, -1.39708658],
[-1.00376268, -1.00376268, -1.00376268, -1.20042463]])

"""
actions = simplex_grid(h, t)
n = actions.shape[0]
payoff_arrays = tuple(np.empty((n, n)) for i in range(2))
mean = np.array([mu, mu])
cov = np.array([[1, rho], [rho, 1]])
random_state = check_random_state(random_state)
values = random_state.multivariate_normal(mean, cov, h)
_populate_blotto_payoff_arrays(payoff_arrays, actions, values)
g = NormalFormGame(
[Player(payoff_array) for payoff_array in payoff_arrays]
)
return g
``````
QuantEcon / QuantEcon.py / quantecon / game_theory / game_generators / bimatrix_generators.py View on Github
``````is_suboptimal = payoff_arrays[1] &lt; maxes
nums_suboptimal = is_suboptimal.sum(axis=1)

while (nums_suboptimal==0).any():
payoff_arrays[1][:] = random_state.random_sample((n, n))
payoff_arrays[1].max(axis=0, out=maxes)
np.less(payoff_arrays[1], maxes, out=is_suboptimal)
is_suboptimal.sum(axis=1, out=nums_suboptimal)

for i in range(n):
one_ind = random_state.randint(n)
while not is_suboptimal[i, one_ind]:
one_ind = random_state.randint(n)
payoff_arrays[0][one_ind, i] = 1

g = NormalFormGame(
[Player(payoff_array) for payoff_array in payoff_arrays]
)
return g``````

## quantecon

Import the main names to top level.

MIT