random
Generate MarkovChain and DiscreteDP instances randomly.
- quantecon.markov.random.random_discrete_dp(num_states, num_actions, beta=None, k=None, scale=1, sparse=False, sa_pair=False, random_state=None)[source]
Generate a DiscreteDP randomly. The reward values are drawn from the normal distribution with mean 0 and standard deviation scale.
- Parameters:
- num_statesscalar(int)
Number of states.
- num_actionsscalar(int)
Number of actions.
- betascalar(float), optional(default=None)
Discount factor. Randomly chosen from [0, 1) if not specified.
- kscalar(int), optional(default=None)
Number of possible next states for each state-action pair. Equal to num_states if not specified.
- scalescalar(float), optional(default=1)
Standard deviation of the normal distribution for the reward values.
- sparsebool, optional(default=False)
Whether to store the transition probability array in sparse matrix form.
- sa_pairbool, optional(default=False)
Whether to represent the data in the state-action pairs formulation. (If sparse=True, automatically set True.)
- random_stateint or np.random.RandomState/Generator, optional
Random seed (integer) or np.random.RandomState or Generator instance to set the initial state of the random number generator for reproducibility. If None, a randomly initialized RandomState is used.
- Returns:
- ddpDiscreteDP
An instance of DiscreteDP.
- quantecon.markov.random.random_markov_chain(n, k=None, sparse=False, random_state=None)[source]
Return a randomly sampled MarkovChain instance with n states, where each state has k states with positive transition probability.
- Parameters:
- nscalar(int)
Number of states.
- kscalar(int), optional(default=None)
Number of states that may be reached from each state with positive probability. Set to n if not specified.
- sparsebool, optional(default=False)
Whether to store the transition probability matrix in sparse matrix form.
- random_stateint or np.random.RandomState/Generator, optional
Random seed (integer) or np.random.RandomState or Generator instance to set the initial state of the random number generator for reproducibility. If None, a randomly initialized RandomState is used.
- Returns:
- mcMarkovChain
Examples
>>> import numpy as np >>> import quantecon as qe >>> rng = np.random.default_rng(1234) >>> mc = qe.markov.random_markov_chain(3, random_state=rng) >>> mc.P array([[0.38019574, 0.59650403, 0.02330023], [0.26169242, 0.66155381, 0.07675377], [0.11809123, 0.20100583, 0.68090294]]) >>> rng = np.random.default_rng(1234) >>> mc = qe.markov.random_markov_chain(3, k=2, random_state=rng) >>> mc.P array([[0.97669977, 0. , 0.02330023], [0.38019574, 0. , 0.61980426], [0.92324623, 0.07675377, 0. ]])
- quantecon.markov.random.random_stochastic_matrix(n, k=None, sparse=False, format='csr', random_state=None)[source]
Return a randomly sampled n x n stochastic matrix with k nonzero entries for each row.
- Parameters:
- nscalar(int)
Number of states.
- kscalar(int), optional(default=None)
Number of nonzero entries in each row of the matrix. Set to n if not specified.
- sparsebool, optional(default=False)
Whether to generate the matrix in sparse matrix form.
- formatstr, optional(default=’csr’)
Sparse matrix format, str in {‘bsr’, ‘csr’, ‘csc’, ‘coo’, ‘lil’, ‘dia’, ‘dok’}. Relevant only when sparse=True.
- random_stateint or np.random.RandomState/Generator, optional
Random seed (integer) or np.random.RandomState or Generator instance to set the initial state of the random number generator for reproducibility. If None, a randomly initialized RandomState is used.
- Returns:
- Pnumpy ndarray or scipy sparse matrix (float, ndim=2)
Stochastic matrix.
See also
random_markov_chainReturn a random MarkovChain instance.