Beispiel #1
0
    def create_grids(self):
        """ construct grids for states and shocks """

        par = self.par

        # b. shocks

        # i. basic GuassHermite
        psi, psi_w = log_normal_gauss_hermite(sigma=par.sigma_psi, n=par.Npsi)
        xi, xi_w = log_normal_gauss_hermite(sigma=par.sigma_xi, n=par.Nxi)

        # ii. add low income shock to xi
        if par.pi > 0:

            # a. weights
            xi_w *= (1.0 - par.pi)
            xi_w = np.insert(xi_w, 0, par.pi)

            # b. values
            xi = (xi - par.mu * par.pi) / (1.0 - par.pi)
            xi = np.insert(xi, 0, par.mu)

        # iii. vectorize tensor product of shocks and total weight
        psi_vec, xi_vec = np.meshgrid(psi, xi, indexing='ij')
        psi_w_vec, xi_w_vec = np.meshgrid(psi_w, xi_w, indexing='ij')

        par.psi_vec = psi_vec.ravel()
        par.xi_vec = xi_vec.ravel()
        par.w = xi_w_vec.ravel() * psi_w_vec.ravel()

        assert 1 - np.sum(par.w) < 1e-8  # == summing to 1

        # iv. count number of shock nodes
        par.Nshocks = par.w.size

        # c. minimum a
        if par.borrowingfac == 0:

            par.a_min = np.zeros(par.T)  # never any borriwng

        else:

            # using formula from slides
            psi_min = np.min(par.psi_vec)
            xi_min = np.min(par.xi_vec)

            par.a_min = np.ones(par.T)
            for t in reversed(range(par.T - 1)):

                if t >= par.TR - 1:  # in retirement
                    Omega = 0
                elif t == par.TR - 2:  # next period is retirement
                    Omega = par.R**(-1) * par.G * par.L[t +
                                                        1] * psi_min * xi_min
                else:  # before retirement
                    Omega = par.R**(-1) * (np.fmin(Omega, par.borrowingfac) +
                                           xi_min) * par.G * par.L[t +
                                                                   1] * psi_min

                par.a_min[t] = -np.fmin(
                    Omega, par.borrowingfac) * par.G * par.L[t + 1] * psi_min

        # d. end-of-period assets and cash-on-hand
        par.grid_a = np.ones((par.T, par.Na))
        par.grid_m = np.ones((par.T, par.Nm))
        for t in range(par.T):
            par.grid_a[t, :] = nonlinspace(par.a_min[t] + 1e-6, par.a_max,
                                           par.Na, par.a_phi)
            par.grid_m[t, :] = nonlinspace(par.a_min[t] + 1e-6, par.m_max,
                                           par.Nm, par.m_phi)

        # e. conditions
        par.FHW = np.float(par.G / par.R)
        par.AI = np.float((par.R * par.beta)**(1 / par.rho))
        par.GI = np.float(par.AI * np.sum(par.w * par.psi_vec**(-1)) / par.G)
        par.RI = np.float(par.AI / par.R)
        par.WRI = np.float(par.pi**(1 / par.rho) * par.AI / par.R)
        par.FVA = np.float(
            par.beta * np.sum(par.w * (par.G * par.psi_vec)**(1 - par.rho)))

        # g. check for existance of solution
        self.check(do_print=False)
    def create_grids(self):
        """ construct grids for states and shocks """
        
        par = self.par

        # a. retirement
    
        # pre-decision states
        par.grid_m_ret = nonlinspace(par.eps,par.m_max_ret,par.Nm_ret,par.phi_m)
        par.Nmcon_ret = par.Nm_ret - par.Na_ret
        
        # post-decision states
        par.grid_a_ret = nonlinspace(0,par.a_max_ret,par.Na_ret,par.phi_m)
        
        # b. working: state space (m,n,k)    
        par.grid_m = nonlinspace(par.eps,par.m_max,par.Nm,par.phi_m)

        par.Nn = par.Nm
        par.n_max = par.m_max + par.n_add
        par.grid_n = nonlinspace(0,par.n_max,par.Nn,par.phi_n)

        par.grid_n_nd, par.grid_m_nd = np.meshgrid(par.grid_n,par.grid_m,indexing='ij')

        # c. working: w interpolant (and wa and wb and wq)
        par.Na_pd = np.int64(np.floor(par.pd_fac*par.Nm))
        par.a_max = par.m_max + par.a_add
        par.grid_a_pd = nonlinspace(0,par.a_max,par.Na_pd,par.phi_m)
    
        par.Nb_pd = np.int64(np.floor(par.pd_fac*par.Nn))
        par.b_max = par.n_max + par.b_add
        par.grid_b_pd = nonlinspace(0,par.b_max,par.Nb_pd,par.phi_n)
    
        par.grid_b_pd_nd, par.grid_a_pd_nd = np.meshgrid(par.grid_b_pd,par.grid_a_pd,indexing='ij')
        
        # d. working: egm (seperate grids for each segment)
        
        if par.solmethod == 'G2EGM':

            # i. dcon
            par.d_dcon = np.zeros((par.Na_pd,par.Nb_pd),dtype=np.float_,order='C')
                
            # ii. acon
            par.Nc_acon = np.int64(np.floor(par.Na_pd*par.acon_fac))
            par.Nb_acon = np.int64(np.floor(par.Nb_pd*par.acon_fac))
            par.grid_b_acon = nonlinspace(0,par.b_max,par.Nb_acon,par.phi_n)
            par.a_acon = np.zeros(par.grid_b_acon.shape)
            par.b_acon = par.grid_b_acon

            # iii. con
            par.Nc_con = np.int64(np.floor(par.Na_pd*par.con_fac))
            par.Nb_con = np.int64(np.floor(par.Nb_pd*par.con_fac))
            
            par.grid_c_con = nonlinspace(par.eps,par.m_max,par.Nc_con,par.phi_m)
            par.grid_b_con = nonlinspace(0,par.b_max,par.Nb_con,par.phi_n)

            par.b_con,par.c_con = np.meshgrid(par.grid_b_con,par.grid_c_con,indexing='ij')
            par.a_con = np.zeros(par.c_con.shape)
            par.d_con = np.zeros(par.c_con.shape)
        
        elif par.solmethod == 'NEGM':

            par.grid_l = par.grid_m

        # e. shocks
        assert (par.Neta == 1 and par.var_eta == 0) or (par.Neta > 1 and par.var_eta > 0)

        if par.Neta > 1:
            par.eta,par.w_eta = log_normal_gauss_hermite(np.sqrt(par.var_eta), par.Neta)
        else:
            par.eta = np.ones(1)
            par.w_eta = np.ones(1)

        # f. timings
        par.time_work = np.zeros(par.T)
        par.time_w = np.zeros(par.T)
        par.time_egm = np.zeros(par.T)
        par.time_vfi = np.zeros(par.T)