# How to use the quantecon.util.check_random_state 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 / random.py View on Github
``````(2000), 274-293.

"""
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 / normal_form_game.py View on Github
``````random_state : int or np.random.RandomState, optional
Random seed (integer) or np.random.RandomState instance to
set the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState
is used.

Returns
-------
scalar(int)
If `actions` is given, returns an integer representing a
pure action chosen randomly from `actions`; if not, an
action is chosen randomly from the player's all actions.

"""
random_state = check_random_state(random_state)

if actions is not None:
n = len(actions)
else:
n = self.num_actions

if n == 1:
idx = 0
else:
idx = random_state.randint(n)

if actions is not None:
return actions[idx]
else:
return idx
``````
QuantEcon / QuantEcon.py / quantecon / game_theory / random.py View on Github
``````nums_actions : tuple(int)
Tuple of the numbers of actions, one for each player.

random_state : int or np.random.RandomState, optional
Random seed (integer) or np.random.RandomState instance to set
the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState is
used.

Returns
-------
action_profile : Tuple(int)
Tuple of actions, one for each player.

"""
random_state = check_random_state(random_state)
action_profile = tuple(
[random_state.randint(num_actions) for num_actions in nums_actions]
)
return action_profile
``````
QuantEcon / QuantEcon.py / quantecon / discrete_rv.py View on Github
``````k : scalar(int), optional
Number of draws to be returned

random_state : int or np.random.RandomState, optional
Random seed (integer) or np.random.RandomState instance to set
the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState is
used.

Returns
-------
array_like(int)
An array of k independent draws from q

"""
random_state = check_random_state(random_state)

return self.Q.searchsorted(random_state.uniform(0, 1, size=k))
``````
QuantEcon / QuantEcon.py / quantecon / markov / core.py View on Github
``````Number of repetitions of simulation.

random_state : int or np.random.RandomState, optional
Random seed (integer) or np.random.RandomState instance to
set the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState
is used.

Returns
-------
X : ndarray(ndim=1 or 2)
Array containing the state values of the sample path(s). See

"""
random_state = check_random_state(random_state)
dim = 1  # Dimension of the returned array: 1 or 2

msg_out_of_range = 'index {init} is out of the state space'

try:
k = len(init)  # init is an array
dim = 2
init_states = np.asarray(init, dtype=int)
# Check init_states are in the state space
if (init_states &gt;= self.n).any() or (init_states &lt; -self.n).any():
idx = np.where(
(init_states &gt;= self.n) + (init_states &lt; -self.n)
)[0][0]
raise ValueError(msg_out_of_range.format(init=idx))
if num_reps is not None:
k *= num_reps``````
QuantEcon / QuantEcon.py / quantecon / game_theory / random.py View on Github
``````random_state : int or np.random.RandomState, optional
Random seed (integer) or np.random.RandomState instance to set
the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState is
used.

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 / quad.py View on Github
``````weights : np.ndarray(dtype=float)
Weights for quadrature nodes

Notes
-----
Based of original function ``qnwequi`` in CompEcon toolbox by
Miranda and Fackler

References
----------
Miranda, Mario J, and Paul L Fackler. Applied Computational
Economics and Finance, MIT Press, 2002.

"""
random_state = check_random_state(random_state)

if equidist_pp is None:
import sympy as sym
equidist_pp = np.sqrt(np.array(list(sym.primerange(0, 7920))))

n, a, b = list(map(np.atleast_1d, list(map(np.asarray, [n, a, b]))))

d = max(list(map(len, [n, a, b])))
n = np.prod(n)

if a.size == 1:
a = np.repeat(a, d)

if b.size == 1:
b = np.repeat(b, d)
``````
QuantEcon / QuantEcon.py / quantecon / game_theory / game_generators / bimatrix_generators.py View on Github
``````>>> g.players[0]
Player([[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
[ 0.82, -0.18, -0.18, -0.18, -0.18],
[ 0.8 ,  0.8 , -0.2 , -0.2 , -0.2 ],
[ 0.68,  0.68,  0.68, -0.32, -0.32],
[ 0.66,  0.66,  0.66,  0.66, -0.34]])
>>> g.players[1]
Player([[ 1.  ,  0.  ,  0.  ,  0.  ,  0.  ],
[ 0.8 ,  0.8 , -0.2 , -0.2 , -0.2 ],
[ 0.66,  0.66,  0.66, -0.34, -0.34],
[ 0.6 ,  0.6 ,  0.6 ,  0.6 , -0.4 ],
[ 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 / random / utilities.py View on Github
``````&gt;&gt;&gt; qe.random.sample_without_replacement(5, 3, num_trials=4,
...                                      random_state=1234)
array([[0, 2, 1],
[3, 4, 0],
[1, 3, 2],
[4, 1, 3]])

"""
if n &lt;= 0:
raise ValueError('n must be greater than 0')
if k &gt; n:
raise ValueError('k must be smaller than or equal to n')

size = k if num_trials is None else (num_trials, k)

random_state = check_random_state(random_state)
r = random_state.random_sample(size=size)
result = _sample_without_replacement(n, r)

return result
``````

## quantecon

Import the main names to top level.

MIT