signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def _get_action_profile(x, indptr):
N = len(indptr) - <NUM_LIT:1><EOL>action_profile = tuple(x[indptr[i]:indptr[i+<NUM_LIT:1>]] for i in range(N))<EOL>return action_profile<EOL>
Obtain a tuple of mixed actions from a flattened action profile. Parameters ---------- x : array_like(float, ndim=1) Array of flattened mixed action profile of length equal to n_0 + ... + n_N-1, where `out[indptr[i]:indptr[i+1]]` contains player i's mixed action. indptr : array_like(int, ndim=1) Array...
f5062:m3
def _flatten_action_profile(action_profile, indptr):
N = len(indptr) - <NUM_LIT:1><EOL>out = np.empty(indptr[-<NUM_LIT:1>])<EOL>for i in range(N):<EOL><INDENT>if isinstance(action_profile[i], numbers.Integral): <EOL><INDENT>num_actions = indptr[i+<NUM_LIT:1>] - indptr[i]<EOL>mixed_action = pure2mixed(num_actions, action_profile[i])<EOL><DEDENT>else: <EOL><INDENT>mixed_...
Flatten the given action profile. Parameters ---------- action_profile : array_like(int or array_like(float, ndim=1)) Profile of actions of the N players, where each player i' action is a pure action (int) or a mixed action (array_like of floats of length n_i). indptr : array_like(int, ndim=1) Array o...
f5062:m4
def random_game(nums_actions, random_state=None):
N = len(nums_actions)<EOL>if N == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>random_state = check_random_state(random_state)<EOL>players = [<EOL>Player(random_state.random_sample(nums_actions[i:]+nums_actions[:i]))<EOL>for i in range(N)<EOL>]<EOL>g = NormalFormGame(players)<EOL>return g<EOL>
Return a random NormalFormGame instance where the payoffs are drawn independently from the uniform distribution on [0, 1). Parameters ---------- 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.ran...
f5064:m0
def covariance_game(nums_actions, rho, random_state=None):
N = len(nums_actions)<EOL>if N <= <NUM_LIT:1>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if not (-<NUM_LIT:1> / (N - <NUM_LIT:1>) <= rho <= <NUM_LIT:1>):<EOL><INDENT>lb = '<STR_LIT>' if N == <NUM_LIT:2> else '<STR_LIT>'.format(N-<NUM_LIT:1>)<EOL>raise ValueError('<STR_LIT>'.format(lb))<EOL><DEDENT>mean = np...
Return a random NormalFormGame instance where the payoff profiles are drawn independently from the standard multi-normal with the covariance of any pair of payoffs equal to `rho`, as studied in [1]_. Parameters ---------- nums_actions : tuple(int) Tuple of the numbers of actions, one for each player. rho : scalar...
f5064:m1
def random_pure_actions(nums_actions, random_state=None):
random_state = check_random_state(random_state)<EOL>action_profile = tuple(<EOL>[random_state.randint(num_actions) for num_actions in nums_actions]<EOL>)<EOL>return action_profile<EOL>
Return a tuple of random pure actions (integers). Parameters ---------- 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 numbe...
f5064:m2
def random_mixed_actions(nums_actions, random_state=None):
random_state = check_random_state(random_state)<EOL>action_profile = tuple(<EOL>[probvec(<NUM_LIT:1>, num_actions, random_state).ravel()<EOL>for num_actions in nums_actions]<EOL>)<EOL>return action_profile<EOL>
Return a tuple of random mixed actions (vectors of floats). Parameters ---------- 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 ra...
f5064:m3
def pure_nash_brute(g, tol=None):
return list(pure_nash_brute_gen(g, tol=tol))<EOL>
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 ------- NEs : list(tuple(int)) L...
f5065:m0
def pure_nash_brute_gen(g, tol=None):
for a in np.ndindex(*g.nums_actions):<EOL><INDENT>if g.is_nash(a, tol=tol):<EOL><INDENT>yield a<EOL><DEDENT><DEDENT>
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 ------ out : tuple(int) Tuple of Nash equilibrium pure action...
f5065:m1
def support_enumeration(g):
return list(support_enumeration_gen(g))<EOL>
Compute mixed-action Nash equilibria with equal support size for a 2-player normal form game by support enumeration. For a non-degenerate game input, these are all the Nash equilibria. The algorithm checks all the equal-size support pairs; if the players have the same number n of actions, there are 2n choose n minus 1...
f5066:m0
def support_enumeration_gen(g):
try:<EOL><INDENT>N = g.N<EOL><DEDENT>except:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if N != <NUM_LIT:2>:<EOL><INDENT>raise NotImplementedError('<STR_LIT>')<EOL><DEDENT>return _support_enumeration_gen(g.payoff_arrays)<EOL>
Generator version of `support_enumeration`. Parameters ---------- g : NormalFormGame NormalFormGame instance with 2 players. Yields ------- tuple(ndarray(float, ndim=1)) Tuple of Nash equilibrium mixed actions.
f5066:m1
@jit(nopython=True) <EOL>def _support_enumeration_gen(payoff_matrices):
nums_actions = payoff_matrices[<NUM_LIT:0>].shape<EOL>n_min = min(nums_actions)<EOL>for k in range(<NUM_LIT:1>, n_min+<NUM_LIT:1>):<EOL><INDENT>supps = (np.arange(<NUM_LIT:0>, k, <NUM_LIT:1>, np.int_), np.empty(k, np.int_))<EOL>actions = (np.empty(k+<NUM_LIT:1>), np.empty(k+<NUM_LIT:1>))<EOL>A = np.empty((k+<NUM_LIT:1>...
Main body of `support_enumeration_gen`. Parameters ---------- payoff_matrices : tuple(ndarray(float, ndim=2)) Tuple of payoff matrices, of shapes (m, n) and (n, m), respectively. Yields ------ out : tuple(ndarray(float, ndim=1)) Tuple of Nash equilibrium mixed actions, of lengths m and n, respectively...
f5066:m2
@jit(nopython=True, cache=True)<EOL>def _indiff_mixed_action(payoff_matrix, own_supp, opp_supp, A, out):
m = payoff_matrix.shape[<NUM_LIT:0>]<EOL>k = len(own_supp)<EOL>for i in range(k):<EOL><INDENT>for j in range(k):<EOL><INDENT>A[j, i] = payoff_matrix[own_supp[i], opp_supp[j]] <EOL><DEDENT><DEDENT>A[:-<NUM_LIT:1>, -<NUM_LIT:1>] = <NUM_LIT:1><EOL>A[-<NUM_LIT:1>, :-<NUM_LIT:1>] = -<NUM_LIT:1><EOL>A[-<NUM_LIT:1>, -<NUM_LI...
Given a player's payoff matrix `payoff_matrix`, an array `own_supp` of this player's actions, and an array `opp_supp` of the opponent's actions, each of length k, compute the opponent's mixed action whose support equals `opp_supp` and for which the player is indifferent among the actions in `own_supp`, if any such exis...
f5066:m3
def pure2mixed(num_actions, action):
mixed_action = np.zeros(num_actions)<EOL>mixed_action[action] = <NUM_LIT:1><EOL>return mixed_action<EOL>
Convert a pure action to the corresponding mixed action. Parameters ---------- num_actions : scalar(int) The number of the pure actions (= the length of a mixed action). action : scalar(int) The pure action to convert to the corresponding mixed action. Returns ------- ndarray(float, ndim=1) The mixed act...
f5067:m2
@jit(nopython=True, cache=True)<EOL>def best_response_2p(payoff_matrix, opponent_mixed_action, tol=<NUM_LIT>):
n, m = payoff_matrix.shape<EOL>payoff_max = -np.inf<EOL>payoff_vector = np.zeros(n)<EOL>for a in range(n):<EOL><INDENT>for b in range(m):<EOL><INDENT>payoff_vector[a] += payoff_matrix[a, b] * opponent_mixed_action[b]<EOL><DEDENT>if payoff_vector[a] > payoff_max:<EOL><INDENT>payoff_max = payoff_vector[a]<EOL><DEDENT><DE...
Numba-optimized version of `Player.best_response` compilied in nopython mode, specialized for 2-player games (where there is only one opponent). Return the best response action (with the smallest index if more than one) to `opponent_mixed_action` under `payoff_matrix`. Parameters ---------- payoff_matrix : ndarray(fl...
f5067:m3
def delete_action(self, action, player_idx=<NUM_LIT:0>):
payoff_array_new = np.delete(self.payoff_array, action, player_idx)<EOL>return Player(payoff_array_new)<EOL>
Return a new `Player` instance with the action(s) specified by `action` deleted from the action set of the player specified by `player_idx`. Deletion is not performed in place. Parameters ---------- action : scalar(int) or array_like(int) Integer or array like of integers representing the action(s) to be delet...
f5067:c0:m3
def payoff_vector(self, opponents_actions):
def reduce_last_player(payoff_array, action):<EOL><INDENT>"""<STR_LIT>"""<EOL>if isinstance(action, numbers.Integral): <EOL><INDENT>return payoff_array.take(action, axis=-<NUM_LIT:1>)<EOL><DEDENT>else: <EOL><INDENT>return payoff_array.dot(action)<EOL><DEDENT><DEDENT>if self.num_opponents == <NUM_LIT:1>:<EOL><INDENT>p...
Return an array of payoff values, one for each own action, given a profile of the opponents' actions. Parameters ---------- opponents_actions : see `best_response`. Returns ------- payoff_vector : ndarray(float, ndim=1) An array representing the player's payoff vector given the profile of the opponents' actio...
f5067:c0:m4
def is_best_response(self, own_action, opponents_actions, tol=None):
if tol is None:<EOL><INDENT>tol = self.tol<EOL><DEDENT>payoff_vector = self.payoff_vector(opponents_actions)<EOL>payoff_max = payoff_vector.max()<EOL>if isinstance(own_action, numbers.Integral):<EOL><INDENT>return payoff_vector[own_action] >= payoff_max - tol<EOL><DEDENT>else:<EOL><INDENT>return np.dot(own_action, payo...
Return True if `own_action` is a best response to `opponents_actions`. Parameters ---------- own_action : scalar(int) or array_like(float, ndim=1) An integer representing a pure action, or an array of floats representing a mixed action. opponents_actions : see `best_response` tol : scalar(float), optional(de...
f5067:c0:m5
def best_response(self, opponents_actions, tie_breaking='<STR_LIT>',<EOL>payoff_perturbation=None, tol=None, random_state=None):
if tol is None:<EOL><INDENT>tol = self.tol<EOL><DEDENT>payoff_vector = self.payoff_vector(opponents_actions)<EOL>if payoff_perturbation is not None:<EOL><INDENT>try:<EOL><INDENT>payoff_vector += payoff_perturbation<EOL><DEDENT>except TypeError: <EOL><INDENT>payoff_vector = payoff_vector + payoff_perturbation<EOL><DEDE...
Return the best response action(s) to `opponents_actions`. Parameters ---------- opponents_actions : scalar(int) or array_like A profile of N-1 opponents' actions, represented by either scalar(int), array_like(float), array_like(int), or array_like(array_like(float)). If N=2, then it must be a scalar o...
f5067:c0:m6
def random_choice(self, actions=None, random_state=None):
random_state = check_random_state(random_state)<EOL>if actions is not None:<EOL><INDENT>n = len(actions)<EOL><DEDENT>else:<EOL><INDENT>n = self.num_actions<EOL><DEDENT>if n == <NUM_LIT:1>:<EOL><INDENT>idx = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>idx = random_state.randint(n)<EOL><DEDENT>if actions is not None:<EOL><...
Return a pure action chosen randomly from `actions`. Parameters ---------- actions : array_like(int), optional(default=None) An array of integers representing pure actions. random_state : int or np.random.RandomState, optional Random seed (integer) or np.random.RandomState instance to set the initial stat...
f5067:c0:m7
def is_dominated(self, action, tol=None, method=None):
if tol is None:<EOL><INDENT>tol = self.tol<EOL><DEDENT>payoff_array = self.payoff_array<EOL>if self.num_opponents == <NUM_LIT:0>:<EOL><INDENT>return payoff_array.max() > payoff_array[action] + tol<EOL><DEDENT>ind = np.ones(self.num_actions, dtype=bool)<EOL>ind[action] = False<EOL>D = payoff_array[ind]<EOL>D -= payoff_a...
Determine whether `action` is strictly dominated by some mixed action. Parameters ---------- action : scalar(int) Integer representing a pure action. tol : scalar(float), optional(default=None) Tolerance level used in determining domination. If None, default to the value of the `tol` attribute. method : ...
f5067:c0:m8
def dominated_actions(self, tol=None, method=None):
out = []<EOL>for action in range(self.num_actions):<EOL><INDENT>if self.is_dominated(action, tol=tol, method=method):<EOL><INDENT>out.append(action)<EOL><DEDENT><DEDENT>return out<EOL>
Return a list of actions that are strictly dominated by some mixed actions. Parameters ---------- tol : scalar(float), optional(default=None) Tolerance level used in determining domination. If None, default to the value of the `tol` attribute. method : str, optional(default=None) If None, `lemke_howson` f...
f5067:c0:m9
def delete_action(self, player_idx, action):
<EOL>if -self.N <= player_idx < <NUM_LIT:0>:<EOL><INDENT>player_idx = player_idx + self.N<EOL><DEDENT>players_new = tuple(<EOL>player.delete_action(action, player_idx-i)<EOL>for i, player in enumerate(self.players)<EOL>)<EOL>return NormalFormGame(players_new)<EOL>
Return a new `NormalFormGame` instance with the action(s) specified by `action` deleted from the action set of the player specified by `player_idx`. Deletion is not performed in place. Parameters ---------- player_idx : scalar(int) Index of the player to delete action(s) for. action : scalar(int) or array_like(in...
f5067:c1:m6
def is_nash(self, action_profile, tol=None):
if self.N == <NUM_LIT:2>:<EOL><INDENT>for i, player in enumerate(self.players):<EOL><INDENT>own_action, opponent_action =action_profile[i], action_profile[<NUM_LIT:1>-i]<EOL>if not player.is_best_response(own_action, opponent_action,<EOL>tol):<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>elif self.N >= <NUM_LIT...
Return True if `action_profile` is a Nash equilibrium. Parameters ---------- action_profile : array_like(int or array_like(float)) An array of N objects, where each object must be an integer (pure action) or an array of floats (mixed action). tol : scalar(float) Tolerance level used in determining best re...
f5067:c1:m7
def update_values(self):
<EOL>Q, R, A, B, N, C = self.Q, self.R, self.A, self.B, self.N, self.C<EOL>P, d = self.P, self.d<EOL>S1 = Q + self.beta * dot(B.T, dot(P, B))<EOL>S2 = self.beta * dot(B.T, dot(P, A)) + N<EOL>S3 = self.beta * dot(A.T, dot(P, A))<EOL>self.F = solve(S1, S2)<EOL>new_P = R - dot(S2.T, self.F) + S3<EOL>new_d = self.beta * (d...
This method is for updating in the finite horizon case. It shifts the current value function .. math:: V_t(x) = x' P_t x + d_t and the optimal policy :math:`F_t` one step *back* in time, replacing the pair :math:`P_t` and :math:`d_t` with :math:`P_{t-1}` and :math:`d_{t-1}`, and :math:`F_t` with :math:`F_{t-1}...
f5068:c0:m3
def stationary_values(self, method='<STR_LIT>'):
<EOL>Q, R, A, B, N, C = self.Q, self.R, self.A, self.B, self.N, self.C<EOL>A0, B0 = np.sqrt(self.beta) * A, np.sqrt(self.beta) * B<EOL>P = solve_discrete_riccati(A0, B0, R, Q, N, method=method)<EOL>S1 = Q + self.beta * dot(B.T, dot(P, B))<EOL>S2 = self.beta * dot(B.T, dot(P, A)) + N<EOL>F = solve(S1, S2)<EOL>d = self.b...
Computes the matrix :math:`P` and scalar :math:`d` that represent the value function .. math:: V(x) = x' P x + d in the infinite horizon case. Also computes the control matrix :math:`F` from :math:`u = - Fx`. Computation is via the solution algorithm as specified by the `method` option (default to the doubling...
f5068:c0:m4
def compute_sequence(self, x0, ts_length=None, method='<STR_LIT>',<EOL>random_state=None):
<EOL>A, B, C = self.A, self.B, self.C<EOL>if self.T:<EOL><INDENT>T = self.T if not ts_length else min(ts_length, self.T)<EOL>self.P, self.d = self.Rf, <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>T = ts_length if ts_length else <NUM_LIT:100><EOL>self.stationary_values(method=method)<EOL><DEDENT>random_state = check_random...
Compute and return the optimal state and control sequences :math:`x_0, ..., x_T` and :math:`u_0,..., u_T` under the assumption that :math:`{w_t}` is iid and :math:`N(0, 1)`. Parameters ---------- x0 : array_like(float) The initial state, a vector of length n ts_length : scalar(int) Length of the simulation -...
f5068:c0:m5
@contextmanager<EOL>def capture(command, *args, **kwargs):
out, sys.stdout = sys.stdout, StringIO()<EOL>command(*args, **kwargs)<EOL>sys.stdout.seek(<NUM_LIT:0>)<EOL>yield sys.stdout.read()<EOL>sys.stdout = out<EOL>
A context manager to capture std out, so we can write tests that depend on messages that are printed to stdout References ---------- http://schinckel.net/2013/04/15/capture-and-test-sys.stdout-sys. stderr-in-unittest.testcase/ Examples -------- class FooTest(unittest.TestCase): def test_printed_msg(self): ...
f5070:m0
def get_data_dir():
this_dir = os.path.dirname(__file__)<EOL>data_dir = os.path.join(this_dir, "<STR_LIT:data>")<EOL>return data_dir<EOL>
Return directory where data is stored
f5070:m1
def get_h5_data_file():
data_dir = get_data_dir()<EOL>if not exists(data_dir):<EOL><INDENT>os.mkdir(data_dir)<EOL><DEDENT>data_file = join(data_dir, "<STR_LIT>")<EOL>return tables.open_file(data_file, "<STR_LIT:a>", "<STR_LIT>")<EOL>
return the data file used for holding test data. If the data directory or file do not exist, they are created. Notes ----- This should ideally be called from a context manage as so:: with get_h5_data_file() as f: # do stuff This way we know the file will be closed and cleaned up properly
f5070:m2
def get_h5_data_group(grp_name, parent="<STR_LIT:/>", f=get_h5_data_file()):
existed = True<EOL>try:<EOL><INDENT>group = f.getNode(parent + grp_name)<EOL><DEDENT>except:<EOL><INDENT>existed = False<EOL>msg = "<STR_LIT>".format(grp_name + "<STR_LIT>")<EOL>group = f.create_group(parent, grp_name, msg)<EOL><DEDENT>return existed, group<EOL>
Try to fetch the group named grp_name from the file f. If it doesn't yet exist, it is created Parameters ---------- grp_name : str A string specifying the name of the new group. This should be only the group name, not including any information about the group's parent (path) parent : str, optional(default...
f5070:m3
def write_array(f, grp, array, name):
atom = tables.Atom.from_dtype(array.dtype)<EOL>ds = f.createCArray(grp, name, atom, array.shape)<EOL>ds[:] = array<EOL>
stores array in into group grp of h5 file f under name name
f5070:m4
def max_abs_diff(a1, a2):
return np.max(np.abs(a1 - a2))<EOL>
return max absolute difference between two arrays
f5070:m5
def setUp(self):
<EOL>gam = <NUM_LIT:0><EOL>gamma = np.array([[gam], [<NUM_LIT:0>]])<EOL>phic = np.array([[<NUM_LIT:1>], [<NUM_LIT:0>]])<EOL>phig = np.array([[<NUM_LIT:0>], [<NUM_LIT:1>]])<EOL>phi1 = <NUM_LIT><EOL>phii = np.array([[<NUM_LIT:0>], [-phi1]])<EOL>deltak = np.array([[<NUM_LIT>]])<EOL>thetak = np.array([[<NUM_LIT:1>]])<EOL>b...
Given LQ control is tested we will test the transformation to alter the problem into a form suitable to solve using LQ
f5071:c0:m0
def solow_model(t, k, g, n, s, alpha, delta):
k_dot = s * k**alpha - (g + n + delta) * k<EOL>return k_dot<EOL>
Equation of motion for capital stock (per unit effective labor). Parameters ---------- t : float Time k : ndarray (float, shape=(1,)) Capital stock (per unit of effective labor) g : float Growth rate of technology. n : float Growth rate of the labor force. s : float Savings rate. Must satisfy `0 < ...
f5072:m0
def solow_jacobian(t, k, g, n, s, alpha, delta):
jac = s * alpha * k**(alpha - <NUM_LIT:1>) - (g + n + delta)<EOL>return jac<EOL>
Jacobian matrix for the Solow model. Parameters ---------- t : float Time k : ndarray (float, shape=(1,)) Capital stock (per unit of effective labor) g : float Growth rate of technology. n : float Growth rate of the labor force. s : float Savings rate. Must satisfy `0 < s < 1`. alpha : float El...
f5072:m1
def solow_steady_state(g, n, s, alpha, delta):
k_star = (s / (n + g + delta))**(<NUM_LIT:1> / (<NUM_LIT:1> - alpha))<EOL>return k_star<EOL>
Steady-state level of capital stock (per unit effective labor). Parameters ---------- g : float Growth rate of technology. n : float Growth rate of the labor force. s : float Savings rate. Must satisfy `0 < s < 1`. alpha : float Elasticity of output with respect to capital stock. Must satisfy :math...
f5072:m2
def solow_analytic_solution(t, k0, g, n, s, alpha, delta):
<EOL>lmbda = (n + g + delta) * (<NUM_LIT:1> - alpha)<EOL>k_t = (((s / (n + g + delta)) * (<NUM_LIT:1> - np.exp(-lmbda * t)) +<EOL>k0**(<NUM_LIT:1> - alpha) * np.exp(-lmbda * t))**(<NUM_LIT:1> / (<NUM_LIT:1> - alpha)))<EOL>analytic_traj = np.hstack((t[:, np.newaxis], k_t[:, np.newaxis]))<EOL>return analytic_traj<EOL>
Analytic solution for the path of capital stock (per unit effective labor). Parameters ---------- t : ndarray(float, shape=(1,)) Time k : ndarray (float, shape=(1,)) Capital stock (per unit of effective labor) g : float Growth rate of technology. n : float Growth rate of the labor force. s : float ...
f5072:m3
def _compute_fixed_length_solns(model, t0, k0):
<EOL>results = {}<EOL>for integrator in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>discrete_soln = model.solve(t0, k0, h=<NUM_LIT>, T=<NUM_LIT>,<EOL>integrator=integrator,<EOL>atol=<NUM_LIT>, rtol=<NUM_LIT>)<EOL>results[integrator] = discrete_soln<EOL><DEDENT>return results<EOL>
Returns a dictionary of fixed length solution trajectories.
f5072:m4
def _termination_condition(t, k, g, n, s, alpha, delta):
diff = k - solow_steady_state(g, n, s, alpha, delta)<EOL>return diff<EOL>
Terminate solver when we get close to steady state.
f5072:m5
def _compute_variable_length_solns(model, t0, k0, g, tol):
<EOL>results = {}<EOL>for integrator in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>discrete_soln = model.solve(t0, k0, h=<NUM_LIT>, g=g, tol=tol,<EOL>integrator=integrator,<EOL>atol=<NUM_LIT>, rtol=<NUM_LIT>)<EOL>results[integrator] = discrete_soln<EOL><DEDENT>return results<EOL>
Returns a dictionary of variable length solution trajectories.
f5072:m6
def list_of_array_equal(s, t):
eq_(len(s), len(t))<EOL>all(assert_array_equal(x, y) for x, y in zip(s, t))<EOL>
Compare two lists of ndarrays s, t: lists of numpy.ndarrays
f5074:m0
def setUp(self):
self.graphs = Graphs()<EOL>for graph_dict in self.graphs.graph_dicts:<EOL><INDENT>try:<EOL><INDENT>weighted = graph_dict['<STR_LIT>']<EOL><DEDENT>except:<EOL><INDENT>weighted = False<EOL><DEDENT>graph_dict['<STR_LIT:g>'] = DiGraph(graph_dict['<STR_LIT:A>'], weighted=weighted)<EOL><DEDENT>
Setup Digraph instances
f5074:c1:m0
def solve_discrete_lyapunov(A, B, max_it=<NUM_LIT:50>, method="<STR_LIT>"):
if method == "<STR_LIT>":<EOL><INDENT>A, B = list(map(np.atleast_2d, [A, B]))<EOL>alpha0 = A<EOL>gamma0 = B<EOL>diff = <NUM_LIT:5><EOL>n_its = <NUM_LIT:1><EOL>while diff > <NUM_LIT>:<EOL><INDENT>alpha1 = alpha0.dot(alpha0)<EOL>gamma1 = gamma0 + np.dot(alpha0.dot(gamma0), alpha0.conjugate().T)<EOL>diff = np.max(np.abs(g...
r""" Computes the solution to the discrete lyapunov equation .. math:: AXA' - X + B = 0 :math:`X` is computed by using a doubling algorithm. In particular, we iterate to convergence on :math:`X_j` with the following recursions for :math:`j = 1, 2, \dots` starting from :math:`X_0 = B`, :ma...
f5095:m0
def solve_discrete_riccati(A, B, Q, R, N=None, tolerance=<NUM_LIT>, max_iter=<NUM_LIT>,<EOL>method="<STR_LIT>"):
methods = ['<STR_LIT>', '<STR_LIT>']<EOL>if method not in methods:<EOL><INDENT>msg = "<STR_LIT>".format(*methods)<EOL>raise ValueError(msg)<EOL><DEDENT>error = tolerance + <NUM_LIT:1><EOL>fail_msg = "<STR_LIT>"<EOL>A, B, Q, R = np.atleast_2d(A, B, Q, R)<EOL>n, k = R.shape[<NUM_LIT:0>], Q.shape[<NUM_LIT:0>]<EOL>I = np.i...
Solves the discrete-time algebraic Riccati equation .. math:: X = A'XA - (N + B'XA)'(B'XB + R)^{-1}(N + B'XA) + Q Computation is via a modified structured doubling algorithm, an explanation of which can be found in the reference below, if `method="doubling"` (default), and via a QZ decomposition method by callin...
f5095:m1
def compute_steadystate(self, nnc=<NUM_LIT:2>):
zx = np.eye(self.A0.shape[<NUM_LIT:0>])-self.A0<EOL>self.zz = nullspace(zx)<EOL>self.zz /= self.zz[nnc]<EOL>self.css = self.Sc.dot(self.zz)<EOL>self.sss = self.Ss.dot(self.zz)<EOL>self.iss = self.Si.dot(self.zz)<EOL>self.dss = self.Sd.dot(self.zz)<EOL>self.bss = self.Sb.dot(self.zz)<EOL>self.kss = self.Sk.dot(self.zz)<...
Computes the non-stochastic steady-state of the economy. Parameters ---------- nnc : array_like(float) nnc is the location of the constant in the state vector x_t
f5096:c0:m1
def compute_sequence(self, x0, ts_length=None, Pay=None):
lq = LQ(self.Q, self.R, self.A, self.B,<EOL>self.C, N=self.W, beta=self.beta)<EOL>xp, up, wp = lq.compute_sequence(x0, ts_length)<EOL>self.h = self.Sh.dot(xp)<EOL>self.k = self.Sk.dot(xp)<EOL>self.i = self.Si.dot(xp)<EOL>self.b = self.Sb.dot(xp)<EOL>self.d = self.Sd.dot(xp)<EOL>self.c = self.Sc.dot(xp)<EOL>self.g = sel...
Simulate quantities and prices for the economy Parameters ---------- x0 : array_like(float) The initial state ts_length : scalar(int) Length of the simulation Pay : array_like(float) Vector to price an asset whose payout is Pay*xt
f5096:c0:m2
def irf(self, ts_length=<NUM_LIT:100>, shock=None):
if type(shock) != np.ndarray:<EOL><INDENT>shock = np.vstack((np.ones((<NUM_LIT:1>, <NUM_LIT:1>)), np.zeros((self.nw - <NUM_LIT:1>, <NUM_LIT:1>))))<EOL><DEDENT>self.c_irf = np.empty((ts_length, self.nc))<EOL>self.s_irf = np.empty((ts_length, self.nb))<EOL>self.i_irf = np.empty((ts_length, self.ni))<EOL>self.k_irf = np.e...
Create Impulse Response Functions Parameters ---------- ts_length : scalar(int) Number of periods to calculate IRF Shock : array_like(float) Vector of shocks to calculate IRF to. Default is first element of w
f5096:c0:m3
def canonical(self):
Ac1 = np.hstack((self.deltah, np.zeros((self.nh, self.nz))))<EOL>Ac2 = np.hstack((np.zeros((self.nz, self.nh)), self.a22))<EOL>Ac = np.vstack((Ac1, Ac2))<EOL>Bc = np.vstack((self.thetah, np.zeros((self.nz, self.nc))))<EOL>Cc = np.vstack((np.zeros((self.nh, self.nw)), self.c2))<EOL>Rc1 = np.hstack((self.llambda.T.dot(se...
Compute canonical preference representation Uses auxiliary problem of 9.4.2, with the preference shock process reintroduced Calculates pihat, llambdahat and ubhat for the equivalent canonical household technology
f5096:c0:m4
def cartesian(nodes, order='<STR_LIT:C>'):
nodes = [np.array(e) for e in nodes]<EOL>shapes = [e.shape[<NUM_LIT:0>] for e in nodes]<EOL>dtype = nodes[<NUM_LIT:0>].dtype<EOL>n = len(nodes)<EOL>l = np.prod(shapes)<EOL>out = np.zeros((l, n), dtype=dtype)<EOL>if order == '<STR_LIT:C>':<EOL><INDENT>repetitions = np.cumprod([<NUM_LIT:1>] + shapes[:-<NUM_LIT:1>])<EOL><...
Cartesian product of a list of arrays Parameters ---------- nodes : list(array_like(ndim=1)) order : str, optional(default='C') ('C' or 'F') order in which the product is enumerated Returns ------- out : ndarray(ndim=2) each line corresponds to one point of the product space
f5097:m0
def mlinspace(a, b, nums, order='<STR_LIT:C>'):
a = np.array(a, dtype='<STR_LIT>')<EOL>b = np.array(b, dtype='<STR_LIT>')<EOL>nums = np.array(nums, dtype='<STR_LIT>')<EOL>nodes = [np.linspace(a[i], b[i], nums[i]) for i in range(len(nums))]<EOL>return cartesian(nodes, order=order)<EOL>
Constructs a regular cartesian grid Parameters ---------- a : array_like(ndim=1) lower bounds in each dimension b : array_like(ndim=1) upper bounds in each dimension nums : array_like(ndim=1) number of nodes along each dimension order : str, optional(default='C') ('C' or 'F') order in which the prod...
f5097:m1
@njit<EOL>def _repeat_1d(x, K, out):
N = x.shape[<NUM_LIT:0>]<EOL>L = out.shape[<NUM_LIT:0>] // (K*N) <EOL>for n in range(N):<EOL><INDENT>val = x[n]<EOL>for k in range(K):<EOL><INDENT>for l in range(L):<EOL><INDENT>ind = k*N*L + n*L + l<EOL>out[ind] = val<EOL><DEDENT><DEDENT><DEDENT>
Repeats each element of a vector many times and repeats the whole result many times Parameters ---------- x : ndarray(ndim=1) vector to be repeated K : scalar(int) number of times each element of x is repeated (inner iterations) out : ndarray(ndim=1) placeholder for the result Returns ------- None
f5097:m2
@jit(nopython=True, cache=True)<EOL>def simplex_grid(m, n):
L = num_compositions_jit(m, n)<EOL>if L == <NUM_LIT:0>: <EOL><INDENT>raise ValueError(_msg_max_size_exceeded)<EOL><DEDENT>out = np.empty((L, m), dtype=np.int_)<EOL>x = np.zeros(m, dtype=np.int_)<EOL>x[m-<NUM_LIT:1>] = n<EOL>for j in range(m):<EOL><INDENT>out[<NUM_LIT:0>, j] = x[j]<EOL><DEDENT>h = m<EOL>for i in range(...
r""" Construct an array consisting of the integer points in the (m-1)-dimensional simplex :math:`\{x \mid x_0 + \cdots + x_{m-1} = n \}`, or equivalently, the m-part compositions of n, which are listed in lexicographic order. The total number of the points (hence the length of the output array) is L...
f5097:m3
def simplex_index(x, m, n):
if m == <NUM_LIT:1>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>decumsum = np.cumsum(x[-<NUM_LIT:1>:<NUM_LIT:0>:-<NUM_LIT:1>])[::-<NUM_LIT:1>]<EOL>idx = num_compositions(m, n) - <NUM_LIT:1><EOL>for i in range(m-<NUM_LIT:1>):<EOL><INDENT>if decumsum[i] == <NUM_LIT:0>:<EOL><INDENT>break<EOL><DEDENT>idx -= num_composition...
r""" Return the index of the point x in the lexicographic order of the integer points of the (m-1)-dimensional simplex :math:`\{x \mid x_0 + \cdots + x_{m-1} = n\}`. Parameters ---------- x : array_like(int, ndim=1) Integer point in the simplex, i.e., an array of m nonnegative i...
f5097:m4
def num_compositions(m, n):
<EOL>return scipy.special.comb(n+m-<NUM_LIT:1>, m-<NUM_LIT:1>, exact=True)<EOL>
The total number of m-part compositions of n, which is equal to (n+m-1) choose (m-1). Parameters ---------- m : scalar(int) Number of parts of composition. n : scalar(int) Integer to decompose. Returns ------- scalar(int) Total number of m-part compositions of n.
f5097:m5
@jit(nopython=True, cache=True)<EOL>def num_compositions_jit(m, n):
return comb_jit(n+m-<NUM_LIT:1>, m-<NUM_LIT:1>)<EOL>
Numba jit version of `num_compositions`. Return `0` if the outcome exceeds the maximum value of `np.intp`.
f5097:m6
def var_quadratic_sum(A, C, H, beta, x0):
<EOL>A, C, H = list(map(np.atleast_2d, (A, C, H)))<EOL>x0 = np.atleast_1d(x0)<EOL>Q = scipy.linalg.solve_discrete_lyapunov(sqrt(beta) * A.T, H)<EOL>cq = dot(dot(C.T, Q), C)<EOL>v = np.trace(cq) * beta / (<NUM_LIT:1> - beta)<EOL>q0 = dot(dot(x0.T, Q), x0) + v<EOL>return q0<EOL>
r""" Computes the expected discounted quadratic sum .. math:: q(x_0) = \mathbb{E} \Big[ \sum_{t=0}^{\infty} \beta^t x_t' H x_t \Big] Here :math:`{x_t}` is the VAR process :math:`x_{t+1} = A x_t + C w_t` with :math:`{x_t}` standard normal and :math:`x_0` the initial condition. Parameters ...
f5098:m0
def m_quadratic_sum(A, B, max_it=<NUM_LIT:50>):
gamma1 = solve_discrete_lyapunov(A, B, max_it)<EOL>return gamma1<EOL>
r""" Computes the quadratic sum .. math:: V = \sum_{j=0}^{\infty} A^j B A^{j'} V is computed by solving the corresponding discrete lyapunov equation using the doubling algorithm. See the documentation of `util.solve_discrete_lyapunov` for more information. Parameters ---------- ...
f5098:m1
def backward_induction(ddp, T, v_term=None):
n = ddp.num_states<EOL>vs = np.empty((T+<NUM_LIT:1>, n))<EOL>sigmas = np.empty((T, n), dtype=int)<EOL>if v_term is None:<EOL><INDENT>v_term = np.zeros(n)<EOL><DEDENT>vs[T, :] = v_term<EOL>for t in range(T, <NUM_LIT:0>, -<NUM_LIT:1>):<EOL><INDENT>ddp.bellman_operator(vs[t, :], Tv=vs[t-<NUM_LIT:1>, :], sigma=sigmas[t-<NU...
r""" Solve by backward induction a :math:`T`-period finite horizon discrete dynamic program with stationary reward and transition probability functions :math:`r` and :math:`q` and discount factor :math:`\beta \in [0, 1]`. The optimal value functions :math:`v^*_0, \ldots, v^*_T` and policy funct...
f5099:m0
@jit(nopython=True)<EOL>def _has_sorted_sa_indices(s_indices, a_indices):
L = len(s_indices)<EOL>for i in range(L-<NUM_LIT:1>):<EOL><INDENT>if s_indices[i] > s_indices[i+<NUM_LIT:1>]:<EOL><INDENT>return False<EOL><DEDENT>if s_indices[i] == s_indices[i+<NUM_LIT:1>]:<EOL><INDENT>if a_indices[i] >= a_indices[i+<NUM_LIT:1>]:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>return True<EOL>
Check whether `s_indices` and `a_indices` are sorted in lexicographic order. Parameters ---------- s_indices, a_indices : ndarray(ndim=1) Returns ------- bool Whether `s_indices` and `a_indices` are sorted.
f5099:m5
@jit(nopython=True)<EOL>def _generate_a_indptr(num_states, s_indices, out):
idx = <NUM_LIT:0><EOL>out[<NUM_LIT:0>] = <NUM_LIT:0><EOL>for s in range(num_states-<NUM_LIT:1>):<EOL><INDENT>while(s_indices[idx] == s):<EOL><INDENT>idx += <NUM_LIT:1><EOL><DEDENT>out[s+<NUM_LIT:1>] = idx<EOL><DEDENT>out[num_states] = len(s_indices)<EOL>
Generate `a_indptr`; stored in `out`. `s_indices` is assumed to be in sorted order. Parameters ---------- num_states : scalar(int) s_indices : ndarray(int, ndim=1) out : ndarray(int, ndim=1) Length must be num_states+1.
f5099:m6
def _check_action_feasibility(self):
<EOL>R_max = self.s_wise_max(self.R)<EOL>if (R_max == -np.inf).any():<EOL><INDENT>s = np.where(R_max == -np.inf)[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>raise ValueError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(s=s)<EOL>)<EOL><DEDENT>if self._sa_pair:<EOL><INDENT>diff = np.diff(self.a_indptr)<EOL>if (diff == <NUM_LIT:0>).any():<E...
Check that for every state, reward is finite for some action, and for the case sa_pair is True, that for every state, there is some action available.
f5099:c0:m1
def to_sa_pair_form(self, sparse=True):
if self._sa_pair:<EOL><INDENT>return self<EOL><DEDENT>else:<EOL><INDENT>s_ind, a_ind = np.where(self.R > - np.inf)<EOL>RL = self.R[s_ind, a_ind]<EOL>if sparse:<EOL><INDENT>QL = sp.csr_matrix(self.Q[s_ind, a_ind])<EOL><DEDENT>else:<EOL><INDENT>QL = self.Q[s_ind, a_ind]<EOL><DEDENT>return DiscreteDP(RL, QL, self.beta, s_...
Convert this instance of `DiscreteDP` to SA-pair form Parameters ---------- sparse : bool, optional(default=True) Should the `Q` matrix be stored as a sparse matrix? If true the CSR format is used Returns ------- ddp_sa : DiscreteDP The correspnoding DiscreteDP instance in SA-pair form Notes ----- If thi...
f5099:c0:m2
def to_product_form(self):
if self._sa_pair:<EOL><INDENT>ns = self.num_states<EOL>na = self.a_indices.max() + <NUM_LIT:1><EOL>R = np.full((ns, na), -np.inf)<EOL>R[self.s_indices, self.a_indices] = self.R<EOL>Q = np.zeros((ns, na, ns))<EOL>if self._sparse:<EOL><INDENT>_fill_dense_Q(self.s_indices, self.a_indices,<EOL>self.Q.toarray(), Q)<EOL><DED...
Convert this instance of `DiscreteDP` to the "product" form. The product form uses the version of the init method taking `R`, `Q` and `beta`. Parameters ---------- Returns ------- ddp_sa : DiscreteDP The correspnoding DiscreteDP instance in product form Notes ----- If this instance is already in product form th...
f5099:c0:m3
def RQ_sigma(self, sigma):
if self._sa_pair:<EOL><INDENT>sigma = np.asarray(sigma)<EOL>sigma_indices = np.empty(self.num_states, dtype=int)<EOL>_find_indices(self.a_indices, self.a_indptr, sigma,<EOL>out=sigma_indices)<EOL>R_sigma, Q_sigma = self.R[sigma_indices], self.Q[sigma_indices]<EOL><DEDENT>else:<EOL><INDENT>R_sigma = self.R[np.arange(sel...
Given a policy `sigma`, return the reward vector `R_sigma` and the transition probability matrix `Q_sigma`. Parameters ---------- sigma : array_like(int, ndim=1) Policy vector, of length n. Returns ------- R_sigma : ndarray(float, ndim=1) Reward vector for `sigma`, of length n. Q_sigma : ndarray(float, ndim=...
f5099:c0:m4
def bellman_operator(self, v, Tv=None, sigma=None):
vals = self.R + self.beta * self.Q.dot(v) <EOL>if Tv is None:<EOL><INDENT>Tv = np.empty(self.num_states)<EOL><DEDENT>self.s_wise_max(vals, out=Tv, out_argmax=sigma)<EOL>return Tv<EOL>
The Bellman operator, which computes and returns the updated value function `Tv` for a value function `v`. Parameters ---------- v : array_like(float, ndim=1) Value function vector, of length n. Tv : ndarray(float, ndim=1), optional(default=None) Optional output array for Tv. sigma : ndarray(int, ndim=1), op...
f5099:c0:m5
def T_sigma(self, sigma):
R_sigma, Q_sigma = self.RQ_sigma(sigma)<EOL>return lambda v: R_sigma + self.beta * Q_sigma.dot(v)<EOL>
Given a policy `sigma`, return the T_sigma operator. Parameters ---------- sigma : array_like(int, ndim=1) Policy vector, of length n. Returns ------- callable The T_sigma operator.
f5099:c0:m6
def compute_greedy(self, v, sigma=None):
if sigma is None:<EOL><INDENT>sigma = np.empty(self.num_states, dtype=int)<EOL><DEDENT>self.bellman_operator(v, sigma=sigma)<EOL>return sigma<EOL>
Compute the v-greedy policy. Parameters ---------- v : array_like(float, ndim=1) Value function vector, of length n. sigma : ndarray(int, ndim=1), optional(default=None) Optional output array for `sigma`. Returns ------- sigma : ndarray(int, ndim=1) v-greedy policy vector, of length n.
f5099:c0:m7
def evaluate_policy(self, sigma):
if self.beta == <NUM_LIT:1>:<EOL><INDENT>raise NotImplementedError(self._error_msg_no_discounting)<EOL><DEDENT>R_sigma, Q_sigma = self.RQ_sigma(sigma)<EOL>b = R_sigma<EOL>A = self._I - self.beta * Q_sigma<EOL>v_sigma = self._lineq_solve(A, b)<EOL>return v_sigma<EOL>
Compute the value of a policy. Parameters ---------- sigma : array_like(int, ndim=1) Policy vector, of length n. Returns ------- v_sigma : ndarray(float, ndim=1) Value vector of `sigma`, of length n.
f5099:c0:m8
def operator_iteration(self, T, v, max_iter, tol=None, *args, **kwargs):
<EOL>if max_iter <= <NUM_LIT:0>:<EOL><INDENT>return v, <NUM_LIT:0><EOL><DEDENT>for i in range(max_iter):<EOL><INDENT>new_v = T(v, *args, **kwargs)<EOL>if tol is not None and np.abs(new_v - v).max() < tol:<EOL><INDENT>v[:] = new_v<EOL>break<EOL><DEDENT>v[:] = new_v<EOL><DEDENT>num_iter = i + <NUM_LIT:1><EOL>return num_i...
Iteratively apply the operator `T` to `v`. Modify `v` in-place. Iteration is performed for at most a number `max_iter` of times. If `tol` is specified, it is terminated once the distance of `T(v)` from `v` (in the max norm) is less than `tol`. Parameters ---------- T : callable Operator that acts on `v`. v : ndar...
f5099:c0:m9
def solve(self, method='<STR_LIT>',<EOL>v_init=None, epsilon=None, max_iter=None, k=<NUM_LIT:20>):
if method in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>res = self.value_iteration(v_init=v_init,<EOL>epsilon=epsilon,<EOL>max_iter=max_iter)<EOL><DEDENT>elif method in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>res = self.policy_iteration(v_init=v_init,<EOL>max_iter=max_iter)<EOL><DEDENT>elif method in ['<STR_LIT>', '<STR_L...
Solve the dynamic programming problem. Parameters ---------- method : str, optinal(default='policy_iteration') Solution method, str in {'value_iteration', 'vi', 'policy_iteration', 'pi', 'modified_policy_iteration', 'mpi'}. v_init : array_like(float, ndim=1), optional(default=None) Initial value funct...
f5099:c0:m10
def value_iteration(self, v_init=None, epsilon=None, max_iter=None):
if self.beta == <NUM_LIT:1>:<EOL><INDENT>raise NotImplementedError(self._error_msg_no_discounting)<EOL><DEDENT>if max_iter is None:<EOL><INDENT>max_iter = self.max_iter<EOL><DEDENT>if epsilon is None:<EOL><INDENT>epsilon = self.epsilon<EOL><DEDENT>try:<EOL><INDENT>tol = epsilon * (<NUM_LIT:1>-self.beta) / (<NUM_LIT:2>*...
Solve the optimization problem by value iteration. See the `solve` method.
f5099:c0:m11
def policy_iteration(self, v_init=None, max_iter=None):
if self.beta == <NUM_LIT:1>:<EOL><INDENT>raise NotImplementedError(self._error_msg_no_discounting)<EOL><DEDENT>if max_iter is None:<EOL><INDENT>max_iter = self.max_iter<EOL><DEDENT>if v_init is None:<EOL><INDENT>v_init = self.s_wise_max(self.R)<EOL><DEDENT>sigma = self.compute_greedy(v_init)<EOL>new_sigma = np.empty(se...
Solve the optimization problem by policy iteration. See the `solve` method.
f5099:c0:m12
def modified_policy_iteration(self, v_init=None, epsilon=None,<EOL>max_iter=None, k=<NUM_LIT:20>):
if self.beta == <NUM_LIT:1>:<EOL><INDENT>raise NotImplementedError(self._error_msg_no_discounting)<EOL><DEDENT>if max_iter is None:<EOL><INDENT>max_iter = self.max_iter<EOL><DEDENT>if epsilon is None:<EOL><INDENT>epsilon = self.epsilon<EOL><DEDENT>def span(z):<EOL><INDENT>return z.max() - z.min()<EOL><DEDENT>def midran...
Solve the optimization problem by modified policy iteration. See the `solve` method.
f5099:c0:m13
def controlled_mc(self, sigma):
_, Q_sigma = self.RQ_sigma(sigma)<EOL>return MarkovChain(Q_sigma)<EOL>
Returns the controlled Markov chain for a given policy `sigma`. Parameters ---------- sigma : array_like(int, ndim=1) Policy vector, of length n. Returns ------- mc : MarkovChain Controlled Markov chain.
f5099:c0:m14
def KMR_Markov_matrix_sequential(N, p, epsilon):
P = np.zeros((N+<NUM_LIT:1>, N+<NUM_LIT:1>), dtype=float)<EOL>P[<NUM_LIT:0>, <NUM_LIT:0>], P[<NUM_LIT:0>, <NUM_LIT:1>] = <NUM_LIT:1> - epsilon * (<NUM_LIT:1>/<NUM_LIT:2>), epsilon * (<NUM_LIT:1>/<NUM_LIT:2>)<EOL>for n in range(<NUM_LIT:1>, N):<EOL><INDENT>P[n, n-<NUM_LIT:1>] =(n/N) * (epsilon * (<NUM_LIT:1>/<NUM_LIT:2>...
Generate the Markov matrix for the KMR model with *sequential* move Parameters ---------- N : int Number of players p : float Level of p-dominance of action 1, i.e., the value of p such that action 1 is the BR for (1-q, q) for any q > p, where q (1-q, resp.) is the prob that the opponent plays action ...
f5101:m0
def list_of_array_equal(s, t):
eq_(len(s), len(t))<EOL>all(assert_array_equal(x, y) for x, y in zip(s, t))<EOL>
Compare two lists of ndarrays s, t: lists of numpy.ndarrays
f5105:m0
def KMR_Markov_matrix_sequential(N, p, epsilon):
P = np.zeros((N+<NUM_LIT:1>, N+<NUM_LIT:1>), dtype=float)<EOL>P[<NUM_LIT:0>, <NUM_LIT:0>], P[<NUM_LIT:0>, <NUM_LIT:1>] = <NUM_LIT:1> - epsilon * (<NUM_LIT:1>/<NUM_LIT:2>), epsilon * (<NUM_LIT:1>/<NUM_LIT:2>)<EOL>for n in range(<NUM_LIT:1>, N):<EOL><INDENT>P[n, n-<NUM_LIT:1>] =(n/N) * (epsilon * (<NUM_LIT:1>/<NUM_LIT:2>...
Generate the Markov matrix for the KMR model with *sequential* move N: number of players p: level of p-dominance for action 1 = the value of p such that action 1 is the BR for (1-q, q) for any q > p, where q (1-q, resp.) is the prob that the opponent plays action 1 (0, resp.) epsilon: mutation probability Ref...
f5105:m1
def setUp(self):
self.P = KMR_Markov_matrix_sequential(self.N, self.p, self.epsilon)<EOL>self.mc = MarkovChain(self.P)<EOL>self.stationary = self.mc.stationary_distributions<EOL>stat_shape = self.stationary.shape<EOL>if len(stat_shape) == <NUM_LIT:1>:<EOL><INDENT>self.n_stat_dists = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>self.n_stat...
Setup a KMRMarkovMatrix and Compute Stationary Values
f5105:c0:m0
def rouwenhorst(n, ybar, sigma, rho):
<EOL>y_sd = sqrt(sigma**<NUM_LIT:2> / (<NUM_LIT:1> - rho**<NUM_LIT:2>))<EOL>p = (<NUM_LIT:1> + rho) / <NUM_LIT:2><EOL>q = p<EOL>psi = y_sd * np.sqrt(n - <NUM_LIT:1>)<EOL>ubar = psi<EOL>lbar = -ubar<EOL>bar = np.linspace(lbar, ubar, n)<EOL>def row_build_mat(n, p, q):<EOL><INDENT>"""<STR_LIT>"""<EOL>if n == <NUM_LIT:2>:<...
r""" Takes as inputs n, p, q, psi. It will then construct a markov chain that estimates an AR(1) process of: :math:`y_t = \bar{y} + \rho y_{t-1} + \varepsilon_t` where :math:`\varepsilon_t` is i.i.d. normal of mean 0, std dev of sigma The Rouwenhorst approximation uses the following recursive defin...
f5106:m0
def tauchen(rho, sigma_u, m=<NUM_LIT:3>, n=<NUM_LIT:7>):
<EOL>std_y = np.sqrt(sigma_u**<NUM_LIT:2> / (<NUM_LIT:1> - rho**<NUM_LIT:2>))<EOL>x_max = m * std_y<EOL>x_min = -x_max<EOL>x = np.linspace(x_min, x_max, n)<EOL>step = (x_max - x_min) / (n - <NUM_LIT:1>)<EOL>half_step = <NUM_LIT:0.5> * step<EOL>P = np.empty((n, n))<EOL>_fill_tauchen(x, P, n, rho, sigma_u, half_step)<EOL...
r""" Computes a Markov chain associated with a discretized version of the linear Gaussian AR(1) process .. math:: y_{t+1} = \rho y_t + u_{t+1} using Tauchen's method. Here :math:`{u_t}` is an i.i.d. Gaussian process with zero mean. Parameters ---------- rho : scalar(float) ...
f5106:m1
def gth_solve(A, overwrite=False, use_jit=True):
A1 = np.array(A, dtype=float, copy=not overwrite, order='<STR_LIT:C>')<EOL>if len(A1.shape) != <NUM_LIT:2> or A1.shape[<NUM_LIT:0>] != A1.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>n = A1.shape[<NUM_LIT:0>]<EOL>x = np.zeros(n)<EOL>if use_jit:<EOL><INDENT>_gth_solve_jit(A1, x)<EOL>return x...
r""" This routine computes the stationary distribution of an irreducible Markov transition matrix (stochastic matrix) or transition rate matrix (generator matrix) `A`. More generally, given a Metzler matrix (square matrix whose off-diagonal entries are all nonnegative) `A`, this routine solves ...
f5107:m0
@jit(nopython=True)<EOL>def _gth_solve_jit(A, out):
n = A.shape[<NUM_LIT:0>]<EOL>for k in range(n-<NUM_LIT:1>):<EOL><INDENT>scale = np.sum(A[k, k+<NUM_LIT:1>:n])<EOL>if scale <= <NUM_LIT:0>:<EOL><INDENT>n = k+<NUM_LIT:1><EOL>break<EOL><DEDENT>for i in range(k+<NUM_LIT:1>, n):<EOL><INDENT>A[i, k] /= scale<EOL>for j in range(k+<NUM_LIT:1>, n):<EOL><INDENT>A[i, j] += A[i, ...
JIT complied version of the main routine of gth_solve. Parameters ---------- A : numpy.ndarray(float, ndim=2) Stochastic matrix or generator matrix. Must be of shape n x n. Data will be overwritten. out : numpy.ndarray(float, ndim=1) Output array in which to place the stationary distribution of A.
f5107:m1
@jit(nopython=True)<EOL>def _generate_sample_paths(P_cdfs, init_states, random_values, out):
num_reps, ts_length = out.shape<EOL>for i in range(num_reps):<EOL><INDENT>out[i, <NUM_LIT:0>] = init_states[i]<EOL>for t in range(ts_length-<NUM_LIT:1>):<EOL><INDENT>out[i, t+<NUM_LIT:1>] = searchsorted(P_cdfs[out[i, t]], random_values[i, t])<EOL><DEDENT><DEDENT>
Generate num_reps sample paths of length ts_length, where num_reps = out.shape[0] and ts_length = out.shape[1]. Parameters ---------- P_cdfs : ndarray(float, ndim=2) Array containing as rows the CDFs of the state transition. init_states : array_like(int, ndim=1) Array containing the initial states. Its length...
f5109:m0
@jit(nopython=True)<EOL>def _generate_sample_paths_sparse(P_cdfs1d, indices, indptr, init_states,<EOL>random_values, out):
num_reps, ts_length = out.shape<EOL>for i in range(num_reps):<EOL><INDENT>out[i, <NUM_LIT:0>] = init_states[i]<EOL>for t in range(ts_length-<NUM_LIT:1>):<EOL><INDENT>k = searchsorted(P_cdfs1d[indptr[out[i, t]]:indptr[out[i, t]+<NUM_LIT:1>]],<EOL>random_values[i, t])<EOL>out[i, t+<NUM_LIT:1>] = indices[indptr[out[i, t]]...
For sparse matrix. Generate num_reps sample paths of length ts_length, where num_reps = out.shape[0] and ts_length = out.shape[1]. Parameters ---------- P_cdfs1d : ndarray(float, ndim=1) 1D array containing the CDFs of the state transition. indices : ndarray(int, ndim=1) CSR format index array. indptr : nda...
f5109:m1
def mc_compute_stationary(P):
return MarkovChain(P).stationary_distributions<EOL>
Computes stationary distributions of P, one for each recurrent class. Any stationary distribution is written as a convex combination of these distributions. Returns ------- stationary_dists : array_like(float, ndim=2) Array containing the stationary distributions as its rows.
f5109:m2
def mc_sample_path(P, init=<NUM_LIT:0>, sample_size=<NUM_LIT:1000>, random_state=None):
random_state = check_random_state(random_state)<EOL>if isinstance(init, numbers.Integral):<EOL><INDENT>X_0 = init<EOL><DEDENT>else:<EOL><INDENT>cdf0 = np.cumsum(init)<EOL>u_0 = random_state.random_sample()<EOL>X_0 = searchsorted(cdf0, u_0)<EOL><DEDENT>mc = MarkovChain(P)<EOL>return mc.simulate(ts_length=sample_size, in...
Generates one sample path from the Markov chain represented by (n x n) transition matrix P on state space S = {{0,...,n-1}}. Parameters ---------- P : array_like(float, ndim=2) A Markov transition matrix. init : array_like(float ndim=1) or scalar(int), optional(default=0) If init is an array_like, then it is ...
f5109:m3
def get_index(self, value):
if self.state_values is None:<EOL><INDENT>state_values_ndim = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>state_values_ndim = self.state_values.ndim<EOL><DEDENT>values = np.asarray(value)<EOL>if values.ndim <= state_values_ndim - <NUM_LIT:1>:<EOL><INDENT>return self._get_index(value)<EOL><DEDENT>elif values.ndim == state...
Return the index (or indices) of the given value (or values) in `state_values`. Parameters ---------- value Value(s) to get the index (indices) for. Returns ------- idx : int or ndarray(int) Index of `value` if `value` is a single state value; array of indices if `value` is an array_like of state values.
f5109:c0:m5
def _get_index(self, value):
error_msg = '<STR_LIT>'.format(value)<EOL>if self.state_values is None:<EOL><INDENT>if isinstance(value, numbers.Integral) and (<NUM_LIT:0> <= value < self.n):<EOL><INDENT>return value<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(error_msg)<EOL><DEDENT><DEDENT>if self.state_values.ndim == <NUM_LIT:1>:<EOL><INDENT>try...
Return the index of the given value in `state_values`. Parameters ---------- value Value to get the index for. Returns ------- idx : int Index of `value`.
f5109:c0:m6
def _compute_stationary(self):
if self.is_irreducible:<EOL><INDENT>if not self.is_sparse: <EOL><INDENT>stationary_dists = gth_solve(self.P).reshape(<NUM_LIT:1>, self.n)<EOL><DEDENT>else: <EOL><INDENT>stationary_dists =gth_solve(self.P.toarray(),<EOL>overwrite=True).reshape(<NUM_LIT:1>, self.n)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>rec_classes = se...
Store the stationary distributions in self._stationary_distributions.
f5109:c0:m19
def simulate_indices(self, ts_length, init=None, num_reps=None,<EOL>random_state=None):
random_state = check_random_state(random_state)<EOL>dim = <NUM_LIT:1> <EOL>msg_out_of_range = '<STR_LIT>'<EOL>try:<EOL><INDENT>k = len(init) <EOL>dim = <NUM_LIT:2><EOL>init_states = np.asarray(init, dtype=int)<EOL>if (init_states >= self.n).any() or (init_states < -self.n).any():<EOL><INDENT>idx = np.where(<EOL>(init...
Simulate time series of state transitions, where state indices are returned. Parameters ---------- ts_length : scalar(int) Length of each simulation. init : int or array_like(int, ndim=1), optional Initial state(s). If None, the initial state is randomly drawn. num_reps : scalar(int), optional(default=No...
f5109:c0:m23
def simulate(self, ts_length, init=None, num_reps=None, random_state=None):
if init is not None:<EOL><INDENT>init_idx = self.get_index(init)<EOL><DEDENT>else:<EOL><INDENT>init_idx = None<EOL><DEDENT>X = self.simulate_indices(ts_length, init=init_idx, num_reps=num_reps,<EOL>random_state=random_state)<EOL>if self.state_values is not None:<EOL><INDENT>X = self.state_values[X]<EOL><DEDENT>return X...
Simulate time series of state transitions, where the states are annotated with their values (if `state_values` is not None). Parameters ---------- ts_length : scalar(int) Length of each simulation. init : scalar or array_like, optional(default=None) Initial state values(s). If None, the initial state is r...
f5109:c0:m24
@jit(nopython=True)<EOL>def sa_indices(num_states, num_actions):
L = num_states * num_actions<EOL>dtype = np.int_<EOL>s_indices = np.empty(L, dtype=dtype)<EOL>a_indices = np.empty(L, dtype=dtype)<EOL>i = <NUM_LIT:0><EOL>for s in range(num_states):<EOL><INDENT>for a in range(num_actions):<EOL><INDENT>s_indices[i] = s<EOL>a_indices[i] = a<EOL>i += <NUM_LIT:1><EOL><DEDENT><DEDENT>retur...
Generate `s_indices` and `a_indices` for `DiscreteDP`, for the case where all the actions are feasible at every state. Parameters ---------- num_states : scalar(int) Number of states. num_actions : scalar(int) Number of actions. Returns ------- s_indices : ndarray(int, ndim=1) Array containing the state ...
f5110:m0
def random_markov_chain(n, k=None, sparse=False, random_state=None):
P = random_stochastic_matrix(n, k, sparse, format='<STR_LIT>',<EOL>random_state=random_state)<EOL>mc = MarkovChain(P)<EOL>return mc<EOL>
Return a randomly sampled MarkovChain instance with n states, where each state has k states with positive transition probability. Parameters ---------- n : scalar(int) Number of states. k : scalar(int), optional(default=None) Number of states that may be reached from each state with positive probability. ...
f5111:m0
def random_stochastic_matrix(n, k=None, sparse=False, format='<STR_LIT>',<EOL>random_state=None):
P = _random_stochastic_matrix(m=n, n=n, k=k, sparse=sparse, format=format,<EOL>random_state=random_state)<EOL>return P<EOL>
Return a randomly sampled n x n stochastic matrix with k nonzero entries for each row. Parameters ---------- n : scalar(int) Number of states. k : scalar(int), optional(default=None) Number of nonzero entries in each row of the matrix. Set to n if not specified. sparse : bool, optional(default=False) ...
f5111:m1
def _random_stochastic_matrix(m, n, k=None, sparse=False, format='<STR_LIT>',<EOL>random_state=None):
if k is None:<EOL><INDENT>k = n<EOL><DEDENT>probvecs = probvec(m, k, random_state=random_state)<EOL>if k == n:<EOL><INDENT>P = probvecs<EOL>if sparse:<EOL><INDENT>return scipy.sparse.coo_matrix(P).asformat(format)<EOL><DEDENT>else:<EOL><INDENT>return P<EOL><DEDENT><DEDENT>rows = np.repeat(np.arange(m), k)<EOL>cols =sam...
Generate a "non-square stochastic matrix" of shape (m, n), which contains as rows m probability vectors of length n with k nonzero entries. For other parameters, see `random_stochastic_matrix`.
f5111:m2
def random_discrete_dp(num_states, num_actions, beta=None,<EOL>k=None, scale=<NUM_LIT:1>, sparse=False, sa_pair=False,<EOL>random_state=None):
if sparse:<EOL><INDENT>sa_pair = True<EOL><DEDENT>L = num_states * num_actions<EOL>random_state = check_random_state(random_state)<EOL>R = scale * random_state.randn(L)<EOL>Q = _random_stochastic_matrix(L, num_states, k=k,<EOL>sparse=sparse, format='<STR_LIT>',<EOL>random_state=random_state)<EOL>if beta is None:<EOL><I...
Generate a DiscreteDP randomly. The reward values are drawn from the normal distribution with mean 0 and standard deviation `scale`. Parameters ---------- num_states : scalar(int) Number of states. num_actions : scalar(int) Number of actions. beta : scalar(float), optional(default=None) Discount factor. ...
f5111:m3
@njit<EOL>def func(x):
return (x**<NUM_LIT:3> - <NUM_LIT:1>)<EOL>
Function for testing on.
f5112:m0
@njit<EOL>def func_prime(x):
return (<NUM_LIT:3>*x**<NUM_LIT:2>)<EOL>
Derivative for func.
f5112:m1
@njit<EOL>def func_prime2(x):
return <NUM_LIT:6>*x<EOL>
Second order derivative for func.
f5112:m2
@njit<EOL>def func_two(x):
return np.sin(<NUM_LIT:4> * (x - <NUM_LIT:1>/<NUM_LIT:4>)) + x + x**<NUM_LIT:20> - <NUM_LIT:1><EOL>
Harder function for testing on.
f5112:m3