コード例 #1
0
def LfEulerSys(bvec, *objs):
    '''
    Generates vector of all Euler errors for a given bvec, which errors
    characterize all optimal lifetime decisions

    Inputs:
        bvec       = [p,] vector, remaining lifetime savings decisions
                     where p is the number of remaining periods
        objs       = length 10 tuple, (p, S, beta, sigma, chi_b,
                     beg_wealth, nvec, rpath, wpath, BQpath)
        p          = integer in [2,S], remaining periods in life
        beta       = scalar in [0,1), discount factor
        sigma      = scalar > 0, coefficient of relative risk aversion
        beg_wealth = scalar, wealth at the beginning of first age
        nvec       = [p,] vector, remaining exogenous labor supply
        rpath      = [p,] vector, interest rates over remaining life
        wpath      = [p,] vector, wages rates over remaining life

    Functions called:
        get_cvec_lf
        c4ssf.get_b_errors

    Objects in function:
        bvec2        = [p, ] vector, remaining savings including initial
                       savings
        clf_params   = length 2 tuple, parameters for get_cvec_lf (p, S)
        cvec         = [p, ] vector, remaining lifetime consumption
                       levels implied by bvec2
        c_constr     = [p, ] boolean vector, =True if c_{s,t}<=0
        b_err_params = length 2 tuple, parameters to pass into
                       get_b_errors (beta, sigma)
        b_err_vec    = [p-1,] vector, Euler errors from lifetime
                       consumption vector

    Returns: b_err_vec
    '''
    p, S, beta, sigma, chi_b, beg_wealth, nvec, rpath, wpath, BQpath = \
        objs
    bvec2 = np.append(beg_wealth, bvec)
    clf_params = (p, S)
    cvec, c_constr = get_cvec_lf(clf_params, rpath, wpath, BQpath, nvec,
                     bvec2)
    b_err_params = (beta, sigma, chi_b, bvec[-1])
    bsp1_constr = np.zeros(p, dtype=bool)
    if bvec[-1] <= 0:
        bsp1_constr[-1] = True
    if p == 1 and bvec[-1] > 0:
        b_err_vec = chi_b * (bvec[-1] ** (-sigma)) - (cvec[-1] **
            (-sigma))
    elif p == 1 and bvec[-1] <= 0:
        b_err_vec = 9999.
    elif p > 1:
        b_err_vec = ssf.get_b_errors(b_err_params, rpath[1:], cvec,
                                       c_constr, bsp1_constr, diff=True)
    return b_err_vec
コード例 #2
0
def LfEulerSys(bvec, *objs):
    '''
    Generates vector of all Euler errors for a given bvec, which errors
    characterize all optimal lifetime decisions

    Inputs:
        bvec       = [p-1,] vector, remaining lifetime savings decisions
                     where p is the number of remaining periods
        objs       = length 9 tuple, (p, beta, sigma, beg_wealth, n,
                     rpath, wpath, pmpath, ppath)
        p          = integer in [2,S], remaining periods in life
        beta       = scalar in [0,1), discount factor
        sigma      = scalar > 0, coefficient of relative risk aversion
        beg_wealth = scalar, wealth at the beginning of first age
        n       = [p,] vector, remaining exogenous labor supply
        rpath      = [p,] vector, interest rates over remaining life
        wpath      = [p,] vector, wages rates over remaining life
        pcpath     = [I, p] matrix, remaining lifetime
                     consumption good prices
        ppath      = [p,] vector, remaining lifetime composite
                     goods prices
        ppath      = 

    Functions called:
        get_cvec_lf
        ssf.get_b_errors

    Objects in function:
        bvec2        = [p, ] vector, remaining savings including initial
                       savings
        cvec         = [p, ] vector, remaining lifetime consumption
                       levels implied by bvec2
        c_constr     = [p, ] boolean vector, =True if c_{s,t}<=0
        b_err_params = length 2 tuple, parameters to pass into
                       get_b_errors (beta, sigma)
        b_err_vec    = [p-1,] vector, Euler errors from lifetime
                       consumption vector

    Returns: b_err_vec
    '''
    (p, beta, sigma, beg_wealth, ci_tilde, n, rpath, wpath, pcpath,
     ppath) = objs
    bvec2 = np.append(beg_wealth, bvec)
    cvec, c_cstr = get_cvec_lf(ci_tilde, rpath, wpath, pcpath, ppath, n, bvec2)
    b_err_params = (beta, sigma)
    b_err_vec = ssf.get_b_errors(b_err_params,
                                 rpath[1:],
                                 cvec,
                                 c_cstr,
                                 diff=True)
    return b_err_vec
コード例 #3
0
ファイル: tpfuncs.py プロジェクト: jdclawson/dynamic
def LfEulerSys(bvec, *objs):
    """
    Generates vector of all Euler errors for a given bvec, which errors
    characterize all optimal lifetime decisions

    Inputs:
        bvec       = [p-1,] vector, remaining lifetime savings decisions
                     where p is the number of remaining periods
        objs       = length 9 tuple, (p, beta, sigma, beg_wealth, n,
                     rpath, wpath, pmpath, ppath)
        p          = integer in [2,S], remaining periods in life
        beta       = scalar in [0,1), discount factor
        sigma      = scalar > 0, coefficient of relative risk aversion
        beg_wealth = scalar, wealth at the beginning of first age
        n       = [p,] vector, remaining exogenous labor supply
        rpath      = [p,] vector, interest rates over remaining life
        wpath      = [p,] vector, wages rates over remaining life
        pcpath     = [I, p] matrix, remaining lifetime
                     consumption good prices
        ppath      = [p,] vector, remaining lifetime composite
                     goods prices
        ppath      = 

    Functions called:
        get_cvec_lf
        ssf.get_b_errors

    Objects in function:
        bvec2        = [p, ] vector, remaining savings including initial
                       savings
        cvec         = [p, ] vector, remaining lifetime consumption
                       levels implied by bvec2
        c_constr     = [p, ] boolean vector, =True if c_{s,t}<=0
        b_err_params = length 2 tuple, parameters to pass into
                       get_b_errors (beta, sigma)
        b_err_vec    = [p-1,] vector, Euler errors from lifetime
                       consumption vector

    Returns: b_err_vec
    """
    (p, beta, sigma, beg_wealth, ci_tilde, n, rpath, wpath, pcpath, ppath) = objs
    bvec2 = np.append(beg_wealth, bvec)
    cvec, c_cstr = get_cvec_lf(ci_tilde, rpath, wpath, pcpath, ppath, n, bvec2)
    b_err_params = (beta, sigma)
    b_err_vec = ssf.get_b_errors(b_err_params, rpath[1:], cvec, c_cstr, diff=True)
    return b_err_vec
コード例 #4
0
ファイル: tpfuncs.py プロジェクト: jdclawson/dynamic
def paths_life(params, beg_age, beg_wealth, ci_tilde, n, rpath, wpath, pcpath, ppath, b_init):
    """
    Solve for the remaining lifetime savings decisions of an individual
    who enters the model at age beg_age, with corresponding initial
    wealth beg_wealth.

    Inputs:
        params     = length 5 tuple, (S, alpha, beta, sigma, tp_tol)
        S          = integer in [3,80], number of periods an individual
                     lives
        alpha      = [I,S-beg_age+1], expenditure share on good for remaing lifetime
        beta       = scalar in [0,1), discount factor for each model
                     period
        sigma      = scalar > 0, coefficient of relative risk aversion
        tp_tol    = scalar > 0, tolerance level for fsolve's in TPI
        beg_age    = integer in [1,S-1], beginning age of remaining life
        beg_wealth = scalar, beginning wealth at beginning age
        n       = [S-beg_age+1,] vector, remaining exogenous labor
                     supplies
        rpath      = [S-beg_age+1,] vector, remaining lifetime interest
                     rates
        wpath      = [S-beg_age+1,] vector, remaining lifetime wages
        pcpath     = [I, S-beg_age+1] matrix, remaining lifetime
                     consumption good prices
        ppath      = [S-beg_age+1,] vector, remaining lifetime composite
                     goods prices
        b_init     = [S-beg_age,] vector, initial guess for remaining
                     lifetime savings

    Functions called:
        LfEulerSys
        get_cvec_lf
        c4ssf.get_b_errors

    Objects in function:
        p            = integer in [2,S], remaining periods in life
        b_guess      = [p-1,] vector, initial guess for lifetime savings
                       decisions
        eullf_objs   = length 9 tuple, objects to be passed in to
                       LfEulerSys: (p, beta, sigma, beg_wealth, n,
                       rpath, wpath, pmpath, ppath)
        bpath        = [p-1,] vector, optimal remaining lifetime savings
                       decisions
        cpath        = [p,] vector, optimal remaining lifetime
                       consumption decisions
        cipath       = [p,I] martrix, remaining lifetime consumption
                        decisions by consumption good
        c_constr     = [p,] boolean vector, =True if c_{p}<=0,
        b_err_params = length 2 tuple, parameters to pass into
                       c4ssf.get_b_errors (beta, sigma)
        b_err_vec    = [p-1,] vector, Euler errors associated with
                       optimal savings decisions

    Returns: bpath, cpath, cipath, b_err_vec
    """
    S, alpha, beta, sigma, tp_tol = params
    p = int(S - beg_age + 1)
    if beg_age == 1 and beg_wealth != 0:
        sys.exit("Beginning wealth is nonzero for age s=1.")
    if len(rpath) != p:
        # print len(rpath), S-beg_age+1
        sys.exit("Beginning age and length of rpath do not match.")
    if len(wpath) != p:
        sys.exit("Beginning age and length of wpath do not match.")
    if len(n) != p:
        sys.exit("Beginning age and length of n do not match.")
    b_guess = 1.01 * b_init
    eullf_objs = (p, beta, sigma, beg_wealth, ci_tilde, n, rpath, wpath, pcpath, ppath)
    bpath = opt.fsolve(LfEulerSys, b_guess, args=(eullf_objs), xtol=tp_tol)
    cpath, c_cstr = get_cvec_lf(ci_tilde, rpath, wpath, pcpath, ppath, n, np.append(beg_wealth, bpath))
    cipath, cm_cstr = get_cimat_lf(alpha[:, :p], ci_tilde, cpath, pcpath, ppath)
    b_err_params = (beta, sigma)
    b_err_vec = ssf.get_b_errors(b_err_params, rpath[1:], cpath, c_cstr, diff=True)
    return bpath, cpath, cipath, b_err_vec
コード例 #5
0
def paths_life(params, beg_age, beg_wealth, ci_tilde, n, rpath, wpath, pcpath,
               ppath, b_init):
    '''
    Solve for the remaining lifetime savings decisions of an individual
    who enters the model at age beg_age, with corresponding initial
    wealth beg_wealth.

    Inputs:
        params     = length 5 tuple, (S, alpha, beta, sigma, tp_tol)
        S          = integer in [3,80], number of periods an individual
                     lives
        alpha      = [I,S-beg_age+1], expenditure share on good for remaing lifetime
        beta       = scalar in [0,1), discount factor for each model
                     period
        sigma      = scalar > 0, coefficient of relative risk aversion
        tp_tol    = scalar > 0, tolerance level for fsolve's in TPI
        beg_age    = integer in [1,S-1], beginning age of remaining life
        beg_wealth = scalar, beginning wealth at beginning age
        n       = [S-beg_age+1,] vector, remaining exogenous labor
                     supplies
        rpath      = [S-beg_age+1,] vector, remaining lifetime interest
                     rates
        wpath      = [S-beg_age+1,] vector, remaining lifetime wages
        pcpath     = [I, S-beg_age+1] matrix, remaining lifetime
                     consumption good prices
        ppath      = [S-beg_age+1,] vector, remaining lifetime composite
                     goods prices
        b_init     = [S-beg_age,] vector, initial guess for remaining
                     lifetime savings

    Functions called:
        LfEulerSys
        get_cvec_lf
        c4ssf.get_b_errors

    Objects in function:
        p            = integer in [2,S], remaining periods in life
        b_guess      = [p-1,] vector, initial guess for lifetime savings
                       decisions
        eullf_objs   = length 9 tuple, objects to be passed in to
                       LfEulerSys: (p, beta, sigma, beg_wealth, n,
                       rpath, wpath, pmpath, ppath)
        bpath        = [p-1,] vector, optimal remaining lifetime savings
                       decisions
        cpath        = [p,] vector, optimal remaining lifetime
                       consumption decisions
        cipath       = [p,I] martrix, remaining lifetime consumption
                        decisions by consumption good
        c_constr     = [p,] boolean vector, =True if c_{p}<=0,
        b_err_params = length 2 tuple, parameters to pass into
                       c4ssf.get_b_errors (beta, sigma)
        b_err_vec    = [p-1,] vector, Euler errors associated with
                       optimal savings decisions

    Returns: bpath, cpath, cipath, b_err_vec
    '''
    S, alpha, beta, sigma, tp_tol = params
    p = int(S - beg_age + 1)
    if beg_age == 1 and beg_wealth != 0:
        sys.exit("Beginning wealth is nonzero for age s=1.")
    if len(rpath) != p:
        #print len(rpath), S-beg_age+1
        sys.exit("Beginning age and length of rpath do not match.")
    if len(wpath) != p:
        sys.exit("Beginning age and length of wpath do not match.")
    if len(n) != p:
        sys.exit("Beginning age and length of n do not match.")
    b_guess = 1.01 * b_init
    eullf_objs = (p, beta, sigma, beg_wealth, ci_tilde, n, rpath, wpath,
                  pcpath, ppath)
    bpath = opt.fsolve(LfEulerSys, b_guess, args=(eullf_objs), xtol=tp_tol)
    cpath, c_cstr = get_cvec_lf(ci_tilde, rpath, wpath, pcpath, ppath, n,
                                np.append(beg_wealth, bpath))
    cipath, cm_cstr = get_cimat_lf(alpha[:, :p], ci_tilde, cpath, pcpath,
                                   ppath)
    b_err_params = (beta, sigma)
    b_err_vec = ssf.get_b_errors(b_err_params,
                                 rpath[1:],
                                 cpath,
                                 c_cstr,
                                 diff=True)
    return bpath, cpath, cipath, b_err_vec
コード例 #6
0
def paths_life(params, beg_age, beg_wealth, nvec, rpath, wpath, BQpath,
               b_init):
    '''
    Solve for the remaining lifetime savings decisions of an individual
    who enters the model at age beg_age, with corresponding initial
    wealth beg_wealth.

    Inputs:
        params     = length 5 tuple, (S, beta, sigma, chi_b, TPI_tol)
        S          = integer in [3,80], number of periods an individual
                     lives
        beta       = scalar in [0,1), discount factor for each model
                     period
        sigma      = scalar > 0, coefficient of relative risk aversion
        chi_b      = scalar > 0, scale parameter on utility of bequests
        TPI_tol    = scalar > 0, tolerance level for fsolve's in TPI
        beg_age    = integer in [1,S-1], beginning age of remaining life
        beg_wealth = scalar, beginning wealth at beginning age
        nvec       = [S-beg_age+1,] vector, remaining exogenous labor
                     supplies
        rpath      = [S-beg_age+1,] vector, remaining lifetime interest
                     rates
        wpath      = [S-beg_age+1,] vector, remaining lifetime wages
        BQpath     = [S-beg_age+1,] vector, remaining lifetime total
                     bequests
        b_init     = [S-beg_age,] vector, initial guess for remaining
                     lifetime savings

    Functions called:
        LfEulerSys
        get_cvec_lf
        c4ssf.get_b_errors

    Objects in function:
        p            = integer in [1,S], remaining periods in life
        b_guess      = [p,] vector, initial guess for lifetime savings
                       decisions
        eullf_objs   = length 10 tuple, objects to be passed in to
                       LfEulerSys (p, S, beta, sigma, chi_b, beg_wealth,
                       nvec, rpath, wpath, BQpath)
        bpath        = [p,] vector, optimal remaining lifetime savings
                       decisions
        clf_params   = length 2 tuple, parameters for get_cvec_lf
        cpath        = [p,] vector, optimal remaining lifetime
                       consumption decisions
        c_constr     = [p,] boolean vector, =True if c_{p}<=0,
        b_err_params = length 2 tuple, parameters to pass into
                       c4ssf.get_b_errors (beta, sigma)
        rpath2       = [p+1,] vector, rpath with 0 on the end so we can
                       pass it in to get_b_errors for p=1 which does not
                       require an interest rate
        b_err_vec    = [p,] vector, Euler errors associated with
                       optimal savings decisions

    Returns: bpath, cpath, b_err_vec
    '''
    S, beta, sigma, chi_b, TPI_tol = params
    p = int(S - beg_age + 1)
    if beg_age == 1 and beg_wealth != 0:
        sys.exit("Beginning wealth is nonzero for age s=1.")
    if len(rpath) != p:
        #print len(rpath), S-beg_age+1
        sys.exit("Beginning age and length of rpath do not match.")
    if len(wpath) != p:
        sys.exit("Beginning age and length of wpath do not match.")
    if len(nvec) != p:
        sys.exit("Beginning age and length of nvec do not match.")
    b_guess = 1.01 * b_init
    eullf_objs = (p, S, beta, sigma, chi_b, beg_wealth, nvec, rpath,
                 wpath, BQpath)
    bpath = opt.fsolve(LfEulerSys, b_guess, args=(eullf_objs),
                       xtol=TPI_tol)
    clf_params = (p, S)
    cpath, c_constr = get_cvec_lf(clf_params, rpath, wpath, BQpath, nvec,
                                  np.append(beg_wealth, bpath))
    b_err_params = (beta, sigma, chi_b, bpath[-1])
    bsp1_constr = np.zeros(p, dtype=bool)
    if bpath[-1] <= 0:
        bsp1_constr[-1] = True
    if beg_age == S and bpath[-1] > 0:
        b_err_vec = chi_b * (bpath[-1] ** (-sigma)) - (cpath[-1] **
                    (-sigma))
    elif beg_age == S and bpath[-1] <=0:
        b_err_vec = 9999.
    elif beg_age < S:
        b_err_vec = ssf.get_b_errors(b_err_params, rpath[1:], cpath,
                                       c_constr, bsp1_constr, diff=True)
    return bpath, cpath, b_err_vec