def test_ddp_to_sa_and_to_product(): n, m = 3, 2 R = np.array([[0, 1], [1, 0], [-np.inf, 1]]) Q = np.empty((n, m, n)) Q[:] = 1 / n Q[0, 0, 0] = 0 Q[0, 0, 1] = 2 / n beta = 0.95 sparse_R = np.array([0, 1, 1, 0, 1]) _Q = np.full((5, 3), 1 / 3) _Q[0, 0] = 0 _Q[0, 1] = 2 / n sparse_Q = sparse.coo_matrix(_Q) ddp = DiscreteDP(R, Q, beta) ddp_sa = ddp.to_sa_pair_form() ddp_sa2 = ddp_sa.to_sa_pair_form() ddp_sa3 = ddp.to_sa_pair_form(sparse=False) ddp2 = ddp_sa.to_product_form() ddp3 = ddp_sa2.to_product_form() ddp4 = ddp.to_product_form() # make sure conversion worked for ddp_s in [ddp_sa, ddp_sa2, ddp_sa3]: assert_allclose(ddp_s.R, sparse_R) # allcose doesn't work on sparse np.max(np.abs((sparse_Q - ddp_s.Q))) < 1e-15 assert_allclose(ddp_s.beta, beta) # these two will have probability 0 in state 2, action 0 b/c # of the infeasiability in R funky_Q = np.empty((n, m, n)) funky_Q[:] = 1 / n funky_Q[0, 0, 0] = 0 funky_Q[0, 0, 1] = 2 / n funky_Q[2, 0, :] = 0 for ddp_f in [ddp2, ddp3]: assert_allclose(ddp_f.R, ddp.R) assert_allclose(ddp_f.Q, funky_Q) assert_allclose(ddp_f.beta, ddp.beta) # this one is just the original one. assert_allclose(ddp4.R, ddp.R) assert_allclose(ddp4.Q, ddp.Q) assert_allclose(ddp4.beta, ddp.beta) for method in ["pi", "vi", "mpi"]: sol1 = ddp.solve(method=method) for ddp_other in [ddp_sa, ddp_sa2, ddp_sa3, ddp2, ddp3, ddp4]: sol2 = ddp_other.solve(method=method) for k in ["v", "sigma", "num_iter"]: assert_allclose(sol1[k], sol2[k])
def test_ddp_to_sa_and_to_product(): n, m = 3, 2 R = np.array([[0, 1], [1, 0], [-np.inf, 1]]) Q = np.empty((n, m, n)) Q[:] = 1/n Q[0, 0, 0] = 0 Q[0, 0, 1] = 2/n beta = 0.95 sparse_R = np.array([0, 1, 1, 0, 1]) _Q = np.full((5, 3), 1/3) _Q[0, 0] = 0 _Q[0, 1] = 2/n sparse_Q = sparse.coo_matrix(_Q) ddp = DiscreteDP(R, Q, beta) ddp_sa = ddp.to_sa_pair_form() ddp_sa2 = ddp_sa.to_sa_pair_form() ddp_sa3 = ddp.to_sa_pair_form(sparse=False) ddp2 = ddp_sa.to_product_form() ddp3 = ddp_sa2.to_product_form() ddp4 = ddp.to_product_form() # make sure conversion worked for ddp_s in [ddp_sa, ddp_sa2, ddp_sa3]: assert_allclose(ddp_s.R, sparse_R) # allcose doesn't work on sparse np.max(np.abs((sparse_Q - ddp_s.Q))) < 1e-15 assert_allclose(ddp_s.beta, beta) # these two will have probability 0 in state 2, action 0 b/c # of the infeasiability in R funky_Q = np.empty((n, m, n)) funky_Q[:] = 1/n funky_Q[0, 0, 0] = 0 funky_Q[0, 0, 1] = 2/n funky_Q[2, 0, :] = 0 for ddp_f in [ddp2, ddp3]: assert_allclose(ddp_f.R, ddp.R) assert_allclose(ddp_f.Q, funky_Q) assert_allclose(ddp_f.beta, ddp.beta) # this one is just the original one. assert_allclose(ddp4.R, ddp.R) assert_allclose(ddp4.Q, ddp.Q) assert_allclose(ddp4.beta, ddp.beta) for method in ["pi", "vi", "mpi"]: sol1 = ddp.solve(method=method) for ddp_other in [ddp_sa, ddp_sa2, ddp_sa3, ddp2, ddp3, ddp4]: sol2 = ddp_other.solve(method=method) for k in ["v", "sigma", "num_iter"]: assert_allclose(sol1[k], sol2[k])
def voter_dpp(self, etah, etal, tauy, pipar, prg, prb, Pm, pol_br, etam, beta, delta): R = self.rewardv(etam, beta) Q = self.populate_Q(etah, etal, tauy, pipar, prg, prb, Pm, pol_br) dpp = DiscreteDP(R=R, Q=Q, beta=delta, s_indices=self.state_indices, a_indices=self.action_indices) results = dpp.solve(method='policy_iteration') sol = DPPsol(results, R, Q) return (sol)
def prices_to_capital_stock(am, r): w = r_to_w(r) am.set_prices(r, w) aiyagari_ddp = DiscreteDP(am.R, am.Q, β) # Compute the optimal policy results = aiyagari_ddp.solve(method='policy_iteration') # Compute the stationary distribution stationary_probs = results.mc.stationary_distributions[0] # Extract the marginal distribution for assets asset_probs = asset_marginal(stationary_probs, am.a_size, am.z_size) # Return K return np.sum(asset_probs * am.a_vals)
def prices_to_capital_stock(am, r): """ Map prices to the induced level of capital stock. Parameters: ---------- am : Household An instance of an aiyagari_household.Household r : float The interest rate """ w = r_to_w(r) am.set_prices(r, w) aiyagari_ddp = DiscreteDP(am.R, am.Q, beta) # Compute the optimal policy results = aiyagari_ddp.solve(method='policy_iteration') # Compute the stationary distribution stationary_probs = results.mc.stationary_distributions[0] # Extract the marginal distribution for assets asset_probs = asset_marginal(stationary_probs, am.a_size, am.z_size) # Return K return np.sum(asset_probs * am.a_vals)
import matplotlib.pyplot as plt from aiyagari_household import Household from quantecon.markov import DiscreteDP # Example prices r = 0.03 w = 0.956 # Create an instance of Household am = Household(a_max=20, r=r, w=w) # Use the instance to build a discrete dynamic program am_ddp = DiscreteDP(am.R, am.Q, am.beta) # Solve using policy function iteration results = am_ddp.solve(method='policy_iteration') # Simplify names z_size, a_size = am.z_size, am.a_size z_vals, a_vals = am.z_vals, am.a_vals n = a_size * z_size # Get all optimal actions across the set of a indices with z fixed in each row a_star = np.empty((z_size, a_size)) for s_i in range(n): a_i = s_i // z_size z_i = s_i % z_size a_star[z_i, a_i] = a_vals[results.sigma[s_i]] fig, ax = plt.subplots(figsize=(9, 9)) ax.plot(a_vals, a_vals, 'k--')# 45 degrees
ax.plot(k_vals, r_vals, lw=2, alpha=0.6, color='b', label='supply of capital') ax.plot(k_vals, rd(k_vals), lw=2, alpha=0.6, color='r', label='demand of capital') ax.set_xlabel('capital stock') ax.set_ylabel('r') ax.legend(loc='upper right') plt.show() # Report the endogenous distribution of wealth. #STEP 1: Stationary distribution of wealth. am_ddp = DiscreteDP(am.R, am.Q, am.β) results = am_ddp.solve(method='policy_iteration') # Compute the stationary distribution stationary_probs = results.mc.stationary_distributions[0] # Extract the marginal distribution for assets asset_probs = asset_marginal(stationary_probs, am.a_size, am.z_size) #PLOT plt.hist(asset_probs, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid',