Ejemplo n.º 1
0
def pol_ss(Pi,
           e_grid,
           a_grid,
           r,
           w,
           beta,
           eis,
           Va_seed=None,
           tol=1E-8,
           maxit=5000):
    """Find steady state policy functions."""
    if Va_seed is None:
        coh = (1 + r) * a_grid[np.newaxis, :] + w * e_grid[:, np.newaxis]
        Va = (1 + r) * (0.1 * coh)**(-1 / eis)
    else:
        Va = Va_seed

    # iterate until convergence of a policy by tol or reach max number of iterations
    a = np.empty_like(Va)
    for it in range(maxit):
        Va, anew, c = backward_iterate(Va, Pi, a_grid, e_grid, r, w, beta, eis)

        if it % 10 == 1 and utils.within_tolerance(a, anew, tol):
            break
        a = anew
    else:
        raise ValueError(f'No convergence after {maxit} backward iterations!')

    return Va, a, c
Ejemplo n.º 2
0
    def policy_ss(self, ssin, tol=1E-8, maxit=5000):
        """Find steady-state policies and backward variables through backward iteration until convergence.

        Parameters
        ----------
        ssin : dict
            all steady-state inputs to back_step_fun, including seed values for backward variables
        tol : [optional] float
            max diff between consecutive iterations of policy variables needed for convergence
        maxit : [optional] int
            maximum number of iterations, if 'tol' not reached by then, raise error

        Returns
        ----------
        sspol : dict
            all steady-state outputs of backward iteration, combined with inputs to backward iteration
        """

        original_ssin = ssin
        ssin = self.make_inputs(ssin)

        old = {}
        for it in range(maxit):
            try:
                # run and store results of backward iteration, which come as tuple, in dict
                sspol = {
                    k: v
                    for k, v in zip(self.all_outputs_order,
                                    self.back_step_fun(**ssin))
                }
            except KeyError as e:
                print(f'Missing input {e} to {self.back_step_fun.__name__}!')
                raise

            # only check convergence every 10 iterations for efficiency
            if it % 10 == 1 and all(
                    utils.within_tolerance(sspol[k], old[k], tol)
                    for k in self.policy):
                break

            # update 'old' for comparison during next iteration, prepare 'ssin' as input for next iteration
            old.update({k: sspol[k] for k in self.policy})
            ssin.update({k + '_p': sspol[k] for k in self.backward})
        else:
            raise ValueError(
                f'No convergence of policy functions after {maxit} backward iterations!'
            )

        # want to record inputs in ssin, but remove _p, add in hetinput inputs if there
        for k in self.inputs_p:
            ssin[k] = ssin[k + '_p']
            del ssin[k + '_p']
        if self.hetinput is not None:
            for k in self.hetinput_inputs:
                if k in original_ssin:
                    ssin[k] = original_ssin[k]
        return {**ssin, **sspol}
Ejemplo n.º 3
0
def pol_labor_ss(Pi,
                 a_grid,
                 e_grid,
                 T,
                 w,
                 r,
                 beta,
                 eis,
                 frisch,
                 vphi,
                 Va_seed=None,
                 tol=1E-8,
                 maxit=5000):
    if Va_seed is None:
        coh = (1 + r) * a_grid[
            np.newaxis, :] + w * e_grid[:, np.newaxis] + T[:, np.newaxis]
        Va = (1 + r) * (0.1 * coh)**(-1 / eis)
    else:
        Va = Va_seed

    # precompute constrained c and n which don't depend on Va
    fininc = (1 + r) * a_grid + T[:, np.newaxis] - a_grid[0]
    c_const, n_const = solve_cn(w * e_grid[:, np.newaxis], fininc, eis, frisch,
                                vphi, Va)

    # iterate until convergence of a policy by tol or reach max number of iterations
    a = np.empty_like(Va)
    for it in range(maxit):
        Va, anew, c, n, ns = backward_iterate_labor(Va,
                                                    Pi,
                                                    a_grid,
                                                    e_grid,
                                                    T,
                                                    w,
                                                    r,
                                                    beta,
                                                    eis,
                                                    frisch,
                                                    vphi,
                                                    c_const,
                                                    n_const,
                                                    ssflag=True)

        if it % 10 == 1 and utils.within_tolerance(a, anew, tol):
            break
        a = anew
    else:
        raise ValueError(f'No convergence after {maxit} backward iterations!')

    return Va, a, c, n, ns, c_const, n_const
Ejemplo n.º 4
0
            D = D_seed

        # obtain interpolated policy rule for each dimension of endogenous policy
        sspol_i = {}
        sspol_pi = {}
        for pol in self.policy:
            # use robust binary search-based method that only requires grids, not policies, to be monotonic
            sspol_i[pol], sspol_pi[pol] = utils.interpolate_coord_robust(grid[pol], sspol[pol])

        # iterate until convergence by tol, or maxit
        Pi_T = Pi.T.copy()
        for it in range(maxit):
            Dnew = self.forward_step(D, Pi_T, sspol_i, sspol_pi)

            # only check convergence every 10 iterations for efficiency
            if it % 10 == 0 and utils.within_tolerance(D, Dnew, tol):
                break
            D = Dnew
        else:
            print(f'No convergence after {maxit} forward iterations!')
        
        return D

    '''Part 4: components of jac(), corresponding to *4 steps of fake news algorithm* in paper
        - Step 1: backward_step_fakenews and backward_iteration_fakenews to get curlyYs and curlyDs
        - Step 2: forward_iteration_fakenews to get curlyPs
        - Step 3: build_F to get fake news matrix from curlyYs, curlyDs, curlyPs
        - Step 4: J_from_F to get Jacobian from fake news matrix
    '''

    def backward_step_fakenews(self, din_dict, output_list, ssin_dict, ssout_list,