Ejemplo n.º 1
0
        def __init__(self, parameters):
            param = parameters

            # Cartesian grids
            self.W_Q_R\
                = UCGrid((param.A_min, param.A_max_WE, param.grid_size_A),
                         (param.Q_min, param.Q_max, param.grid_size_Q))

            self.X_cont_R\
                = UCGrid((param.A_min, param.A_max_R, param.grid_size_A),
                         (param.H_min, param.H_max, param.grid_size_H),
                         (param.Q_min, param.Q_max, param.grid_size_Q),
                         (param.M_min, param.M_max, param.grid_size_M))
            self.X_cont_RC\
                = UCGrid((param.A_min, param.A_max_R, param.grid_size_A),
                         (param.H_min, param.H_max, param.grid_size_H),
                         (param.Q_min, param.Q_max, param.grid_size_Q))
            self.X_QH_R\
                = UCGrid((param.Q_min, param.Q_max, param.grid_size_Q),
                         (param.M_min, param.M_max, param.grid_size_M),
                         (param.A_min, param.A_max_WE, param.grid_size_A))
            self.X_QH_W\
                = UCGrid((param.A_min_W, param.A_max_WW, param.grid_size_A),
                         (param.DC_min, param.DC_max, param.grid_size_DC))
            self.X_DCQ_W\
                = UCGrid((param.DC_min, param.DC_max, param.grid_size_DC),
                         (param.Q_min, param.Q_max, param.grid_size_Q),
                         (param.M_min, param.M_max, param.grid_size_M))
            self.X_cont_WAM\
                = UCGrid((param.A_min, param.A_max_W, param.grid_size_A),
                         (param.M_min, param.M_max, param.grid_size_M))
            self.X_cont_W\
                = UCGrid((param.A_min, param.A_max_W, param.grid_size_A),
                         (param.DC_min, param.DC_max, param.grid_size_DC),
                         (param.H_min, param.H_max, param.grid_size_H),
                         (param.Q_min, param.Q_max, param.grid_size_Q),
                         (param.M_min, param.M_max, param.grid_size_M))
            self.X_cont_W_bar\
                = UCGrid((param.A_min, param.A_max_W, param.grid_size_A),
                         (param.DC_min, param.DC_max, param.grid_size_DC),
                         (param.H_min, param.H_max, param.grid_size_H),
                         (param.M_min, param.M_max, param.grid_size_M))
            self.X_cont_W_hat\
                = UCGrid((param.A_min, param.A_max_W, param.grid_size_A),
                         (param.DC_min, param.DC_max, param.grid_size_DC))

            self.X_W_contgp = nodes(self.X_cont_W)
            self.X_RC_contgp = nodes(self.X_cont_RC)
            self.X_R_contgp = nodes(self.X_cont_R)
Ejemplo n.º 2
0
    def __init__(self,
                 p,
                 p_inv,
                 phi,
                 phi_prime,
                 beta=0.96,
                 delta_storage=.05,
                 delta_cap=.05,
                 grid_max_k=100,
                 grid_max_x=500,
                 mu_supply=0.5,
                 s_supply=0.5,
                 S_bar=100,
                 S_bar_flag=0,
                 grid_size=1000,
                 supply_shock_size=150,
                 demand_shocks=[1, 1.5],
                 demand_P=[1, 0],
                 solved_flag=0,
                 sol_func=np.array(0)):

        self.beta, self.delta_storage, self.delta_cap = beta, delta_storage, delta_cap
        self.p, self.p_inv = p, p_inv
        self.phi, self.phi_prime = phi, phi_prime
        self.demand_shocks, self.demand_P = demand_shocks, demand_P
        self.grid_size = grid_size
        self.s_supply = s_supply
        self.S_bar, self.S_bar_flag = S_bar, S_bar_flag
        # Set up grid for capital

        alpha = mu_supply * (((mu_supply * (1 - mu_supply)) / s_supply) - 1)
        beta = (1 - mu_supply) * (((mu_supply *
                                    (1 - mu_supply)) / s_supply) - 1)

        self.shocks = np.random.beta(
            a=alpha, b=beta, size=supply_shock_size)  # Store supply shocks
        self.gridI = ((1e-5, grid_max_k, grid_size), (1e-3, grid_max_x,
                                                      grid_size))
        self.grid = nodes(self.gridI)
Ejemplo n.º 3
0
def pfi(grid,
        model,
        init_pfunc=None,
        eps_max=1e-8,
        it_max=100,
        numba_jit=True,
        **pfiargs):
    """Somewhat generic policy function iteration

    This assumes the form 

        v_t = f(E_t x_{t+1}, v_{t-1})

    where f(.) is `func` and x_t = h(v_t) (where h(.) is `xfromv` can be directly retrieved from v_t. The returning function `p_func` is then the array repesentation of the solution v_t = g(v_{t-1}).

    In the future this should also allow for 

        y_t = f(E_t x_{t+1}, w_t, v_{t-1})

    with implied existing functions x_t = h_1(y_t), v_t = h_2(y_t) and w_t = h_3(y_t).

    Parameters
    ----------
    func : dynamic system as described above. Takes the argument `pars', `state`,  and `expect` (must be jitted)
    xfrom : see above (must be jitted)
    pars: list of parameters to func
    grid: for now only UCGrid from interpolate.py are supported and tested
    eps_max: float of maximum error tolerance

    Returns
    -------
    numpy array 
    """

    if numba_jit:
        pfi_func = pfi_jit
    else:
        pfi_func = pfi_raw

    flag = 0

    func = model.func
    xfromv = model.xfromv
    pars = np.ascontiguousarray(model.pars)
    args = np.ascontiguousarray(model.args)

    gp = nodes(grid)
    grid_shape = tuple(g_spec[2] for g_spec in grid)
    grid_shape = grid_shape + (len(grid), )

    if init_pfunc is None:
        init_pfunc = np.zeros(grid_shape)

    if init_pfunc.shape != grid_shape:
        init_pfunc = init_pfunc.reshape(grid_shape)

    p_func, it_cnt, eps = pfi_func(func, xfromv, pars, args, grid_shape, grid,
                                   gp, eps_max, it_max, init_pfunc, **pfiargs)
    if np.isnan(p_func).any():
        flag += 1
    if np.isnan(p_func).all():
        flag += 2
    if it_cnt >= it_max:
        flag += 4

    return p_func, it_cnt, flag
Ejemplo n.º 4
0
def pfi(grid,
        model=None,
        func=None,
        pars=None,
        xfromv=None,
        system_type=None,
        eps_max=1e-8,
        it_max=100,
        numba_jit=True):
    """Somewhat generic policy function iteration

    For now only deterministic solutions are supported. This assumes a form of

        v_t = f(E_t x_{t+1}, v_{t-1})

    where f(.) is `func` and x_t = h(v_t) (where h(.) is `xfromv` can be directly retrieved from v_t. The returning function `p_func` is then the array repesentation of the solution v_t = g(v_{t-1}).

    In the future this should also allow for 

        y_t = f(E_t x_{t+1}, w_t, v_{t-1})

    with implied existing functions x_t = h_1(y_t), v_t = h_2(y_t) and w_t = h_3(y_t).

    Parameters
    ----------
    func : dynamic system as described above. Takes the argument `pars', `state`,  and `expect` (must be jitted)
    xfrom : see above (must be jitted)
    pars: list of parameters to func
    grid: for now only UCGrid from interpolate.py are supported and tested
    system_type: str (eiter 'deterministic' or 'stochastic'
    eps_max: float of maximum error tolerance

    Returns
    -------
    numpy array 
    """

    if system_type is None:
        system_type = 'deterministic'

    if numba_jit:
        pfi_determinisic_func = pfi_determinisic_jit
    else:
        pfi_determinisic_func = pfi_determinisic

    flag = 0

    if model is not None:
        func = model.func
        xfromv = model.xfromv
        pars = np.ascontiguousarray(model.pars)
        args = np.ascontiguousarray(model.args)
    else:
        if func is None or pars is None or xfromv is None:
            SyntaxError(
                "If no model object is given, 'func', 'pars' and 'xfromv' must be provided."
            )
        pars = np.ascontiguousarray(pars)
        args = np.ascontiguousarray(args)

    gp = nodes(grid)
    grid_shape = tuple(g_spec[2] for g_spec in grid)
    grid_shape = grid_shape + (len(grid), )

    if system_type == 'deterministic':

        p_func, it_cnt, eps = pfi_determinisic_func(func, xfromv, pars, args,
                                                    grid_shape, grid, gp,
                                                    eps_max, it_max)
        if np.isnan(p_func).any():
            flag += 1
        if np.isnan(p_func).all():
            flag += 2
        if it_cnt >= it_max:
            flag += 4

    else:
        NotImplementedError('Only deterministic systems supported by now...')

    return p_func, flag