def create_grids(self): """ construct grids for states and shocks """ par = self.par par.a_grid = equilogspace(0,par.a_max,par.Na) par.e_grid, par.e_trans, par.e_ergodic = markov_rouwenhorst(par.rho,par.sigma_e,par.Ne)
def matrix(l, n, v_a): #parameter Na = 1000 Ne = 11 # number of states sol_shape = (Ne, Na) a = np.zeros(sol_shape) r = 0.03 w = 1.000 a_grid = equilogspace(0, 200, Na) rho = 0.97 # AR(1) parameter sigma_e = 0.25 # std. of persistent shock sigma = 2 beta = 0.96 e_grid, e_trans, e_ergodic, e_trans_cumsum, e_ergodic_cumsum = markov_rouwenhorst( rho, sigma_e, Ne) #calculation #step 3 A = np.kron(e_trans, np.identity(n)) B = (beta * A) @ v_a C = np.power(B, (-sigma)) a_extend = np.tile(a_grid, 11) m_endo = C + a_extend #samme opbygning som vektoren v_a #step 4 values = [] m = (1 + r) * a_grid[np.newaxis, :] + w * e_grid[:, np.newaxis] new_m_endo = np.array_split(m_endo, Ne) for k in range(Ne): linear_interp.interp_1d_vec(new_m_endo[k], a_grid, m[k], a[k]) for i_a in range(Na): a[k, i_a] = np.fmax(a[k, i_a], 0) values.append(m[k] - a[k]) #print(f'new_m_endo[0]:{new_m_endo[0]}') #print(new_m_endo[0]) c = np.concatenate(values) u = np.power(c, (1 - sigma)) / (1 - sigma) u[u == -np.inf] = -1000000000000000000000000000 print(f'c:{c}') print(f'u:{u}') #Create Q^ lis = [] Q = np.zeros(Ne * Na) #calculate for e in range(Ne): q = np.zeros((Na, Na)) #create each Q_i for k in range(Na): opt = a[e, k] if opt >= np.max(a_grid): q[k, Na - 1] = 1 #If opt equals the end point elif opt <= np.min(a_grid): q[k, 0] = 1 #If opt equals the start point else: a_high = np.min(np.nonzero(opt <= a_grid)) #create points a_low = np.max(np.nonzero(opt >= a_grid)) q[k, a_low] = (a_grid[a_low + 1] - opt) / (a_grid[a_low + 1] - a_grid[a_low]) q[k, a_high] = (opt - a_grid[a_high - 1]) / (a_grid[a_high] - a_grid[a_high - 1]) lis.append(q) Q = block_diag(*lis) #make block matrix from Q's omega = Q @ A new_v_a = np.linalg.inv(np.identity(11000) - beta * omega) @ u return new_v_a