Пример #1
0
def function_to_minimize(chi_params_scalars, chi_params_init, params, weights_SS, rho_vec, lambdas, tau_bq, e):
    '''
    Parameters:
        chi_params_scalars = guesses for multipliers for chi parameters

    Returns:
        The max absolute deviation between the actual and simulated
            wealth moments
    '''
    J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params
    chi_params_init *= chi_params_scalars
    # print 'Print Chi_b: ', chi_params_init[:J]
    # print 'Scaling vals:', chi_params_scalars[:J]
    solutions_dict = pickle.load(open("OUTPUT/Saved_moments/SS_init_solutions.pkl", "r"))
    solutions = solutions_dict['solutions']

    b_guess = solutions[:S*J]
    n_guess = solutions[S*J:2*S*J]
    wguess, rguess, factorguess, T_Hguess = solutions[2*S*J:]
    solutions = SS_solver(b_guess.reshape(S, J), n_guess.reshape(S, J), wguess, rguess, T_Hguess, factorguess, chi_params_init[J:], chi_params_init[:J], params, iterative_params, tau_bq, rho, lambdas, weights_SS, e)

    b_new = solutions[:S*J]
    n_new = solutions[S*J:2*S*J]
    w_new, r_new, factor_new, T_H_new = solutions[2*S*J:]
    # Wealth Calibration Euler
    error5 = list(misc_funcs.check_wealth_calibration(b_new.reshape(S, J)[:-1, :], factor_new, params))
    # labor calibration euler
    lab_data_dict = pickle.load(open("OUTPUT/Saved_moments/labor_data_moments.pkl", "r"))
    labor_sim = (n_new.reshape(S, J)*lambdas.reshape(1, J)).sum(axis=1)
    error6 = list(misc_funcs.perc_dif_func(labor_sim, lab_data_dict['labor_dist_data']))
    # combine eulers
    output = np.array(error5 + error6)
    # Constraints
    eul_error = np.ones(J)
    for j in xrange(J):
        eul_error[j] = np.abs(Euler_equation_solver(np.append(b_new.reshape(S, J)[:, j], n_new.reshape(S, J)[:, j]), r_new, w_new, T_H_new, factor_new, j, params, chi_params_init[:J], chi_params_init[J:], tau_bq, rho, lambdas, weights_SS, e)).max()
    fsolve_no_converg = eul_error.max()
    if np.isnan(fsolve_no_converg):
        fsolve_no_converg = 1e6
    if fsolve_no_converg > 1e-4:
        output += 1e14
    else:
        var_names = ['solutions']
        dictionary = {}
        for key in var_names:
            dictionary[key] = locals()[key]
        pickle.dump(dictionary, open("OUTPUT/Saved_moments/SS_init_solutions.pkl", "w"))
    if (chi_params_init <= 0.0).any():
        output += 1e14
    weighting_mat = np.eye(2*J + S)
    scaling_val = 100.0
    value = np.dot(scaling_val * np.dot(output.reshape(1, 2*J+S), weighting_mat), scaling_val * output.reshape(2*J+S, 1))
    print 'Value of criterion function: ', value.sum()
    return value.sum()
Пример #2
0
def function_to_minimize(chi_guesses_init, params, weights_SS, rho_vec, lambdas, theta, tau_bq, e, wealth_data_array):
    '''
    Parameters:
        chi_guesses_init = guesses for chi_b
        other_guesses_init = guesses for the distribution of capital and labor
                            stock, and factor value

    Returns:
        The max absolute deviation between the actual and simulated
            wealth moments
    '''
    J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params
    print chi_guesses_init
    Steady_State_SS_X = lambda x: Steady_State_SS(x, chi_guesses_init, params, weights_SS, rho_vec, lambdas, theta, tau_bq, e)

    variables = pickle.load(open("OUTPUT/Saved_moments/minimization_solutions.pkl", "r"))
    for key in variables:
        globals()[key+'_pre'] = variables[key]
    solutions = opt.fsolve(Steady_State_SS_X, solutions_pre, xtol=1e-13)
    b_guess = solutions[0: S * J].reshape((S, J))
    # Wealth Calibration Euler
    error5 = list(misc_funcs.check_wealth_calibration(b_guess[:-1, :], solutions[-1], wealth_data_array, params))
    print error5
    # labor calibration euler
    labor_sim = ((solutions[S*J:2*S*J]).reshape(S, J)*lambdas.reshape(1, J)).sum(axis=1)
    error6 = list(misc_funcs.perc_dif_func(labor_sim, labor_dist_data))
    # combine eulers
    output = np.array(error5 + error6)
    # Constraints
    fsolve_no_converg = np.abs(Steady_State_SS_X(solutions)).max()
    if np.isnan(fsolve_no_converg):
        fsolve_no_converg = 1e6
    if fsolve_no_converg > 1e-4:
        output += 1e9
    else:
        var_names = ['solutions']
        dictionary = {}
        for key in var_names:
            dictionary[key] = locals()[key]
        pickle.dump(dictionary, open("OUTPUT/Saved_moments/minimization_solutions.pkl", "w"))
    if (chi_guesses_init <= 0.0).any():
        output += 1e9
    weighting_mat = np.eye(2*J + S)
    scaling_val = 100.0
    value = np.dot(scaling_val * np.dot(output.reshape(1, 2*J+S), weighting_mat), scaling_val * output.reshape(2*J+S, 1))
    print value.sum()
    return value.sum()
Пример #3
0
    BQnew = (1+rnew.reshape(T, 1))*(b_mat[:T] * omega_stationary[:T] * rho.reshape(1, S, 1)).sum(1)
    bmat_s = np.zeros((T, S, J))
    bmat_s[:, 1:, :] = b_mat[:T, :-1, :]
    T_H_new = np.array(list(tax.get_lump_sum(rnew.reshape(T, 1, 1), bmat_s, wnew.reshape(
        T, 1, 1), e.reshape(1, S, J), n_mat[:T], BQnew.reshape(T, 1, J), lambdas.reshape(
        1, 1, J), factor_ss, omega_stationary[:T], 'TPI', parameters, theta, tau_bq)) + [T_Hss]*S)

    winit[:T] = misc_funcs.convex_combo(wnew, winit[:T], parameters)
    rinit[:T] = misc_funcs.convex_combo(rnew, rinit[:T], parameters)
    BQinit[:T] = misc_funcs.convex_combo(BQnew, BQinit[:T], parameters)
    T_H_init[:T] = misc_funcs.convex_combo(T_H_new[:T], T_H_init[:T], parameters)
    guesses_b = misc_funcs.convex_combo(b_mat, guesses_b, parameters)
    guesses_n = misc_funcs.convex_combo(n_mat, guesses_n, parameters)

    TPIdist = np.array(list(misc_funcs.perc_dif_func(rnew, rinit[:T]))+list(misc_funcs.perc_dif_func(BQnew, BQinit[:T]).flatten())+list(
        misc_funcs.perc_dif_func(wnew, winit[:T]))+list(misc_funcs.perc_dif_func(T_H_new, T_H_init))).max()
    TPIdist_vec[TPIiter] = TPIdist
    # After T=10, if cycling occurs, drop the value of nu
    # wait til after T=10 or so, because sometimes there is a jump up
    # in the first couple iterations
    if TPIiter > 10:
        if TPIdist_vec[TPIiter] - TPIdist_vec[TPIiter-1] > 0:
            nu /= 2
            print 'New Value of nu:', nu
    TPIiter += 1
    print '\tIteration:', TPIiter
    print '\t\tDistance:', TPIdist

print 'Computing final solutions'
Пример #4
0
def function_to_minimize(chi_params_scalars, chi_params_init, params, weights_SS, rho_vec, lambdas, tau_bq, e):
    '''
    Inputs:
        chi_params_scalars = guesses for multipliers for chi parameters ((S+J)x1 array)
        chi_params_init = chi parameters that will be multiplied ((S+J)x1 array)
        params = list of parameters (list)
        weights_SS = steady state population weights (Sx1 array)
        rho_vec = mortality rates (Sx1 array)
        lambdas = ability weights (Jx1 array)
        tau_bq = bequest tax rates (Jx1 array)
        e = ability levels (Jx1 array)
    Output:
        The sum of absolute percent deviations between the actual and simulated wealth moments
    '''
    J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, g_n_ss, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params
    chi_params_init *= chi_params_scalars
    # print 'Print Chi_b: ', chi_params_init[:J]
    # print 'Scaling vals:', chi_params_scalars[:J]
    solutions_dict = pickle.load(open("OUTPUT/Saved_moments/SS_init_solutions.pkl", "r"))
    solutions = solutions_dict['solutions']

    b_guess = solutions[:S*J]
    n_guess = solutions[S*J:2*S*J]
    wguess, rguess, factorguess, T_Hguess = solutions[2*S*J:]
    solutions = SS_solver(b_guess.reshape(S, J), n_guess.reshape(S, J), wguess, rguess, T_Hguess, factorguess, chi_params_init[J:], chi_params_init[:J], params, iterative_params, tau_bq, rho, lambdas, weights_SS, e)

    b_new = solutions[:S*J]
    n_new = solutions[S*J:2*S*J]
    w_new, r_new, factor_new, T_H_new = solutions[2*S*J:]
    # Wealth Calibration Euler
    error5 = list(misc_funcs.check_wealth_calibration(b_new.reshape(S, J)[:-1, :], factor_new, params))
    # labor calibration euler
    lab_data_dict = pickle.load(open("OUTPUT/Saved_moments/labor_data_moments.pkl", "r"))
    labor_sim = (n_new.reshape(S, J)*lambdas.reshape(1, J)).sum(axis=1)
    error6 = list(misc_funcs.perc_dif_func(labor_sim, lab_data_dict['labor_dist_data']))
    # combine eulers
    output = np.array(error5 + error6)
    # Constraints
    eul_error = np.ones(J)
    for j in xrange(J):
        eul_error[j] = np.abs(Euler_equation_solver(np.append(b_new.reshape(S, J)[:, j], n_new.reshape(S, J)[:, j]), r_new, w_new, T_H_new, factor_new, j, params, chi_params_init[:J], chi_params_init[J:], tau_bq, rho, lambdas, weights_SS, e)).max()
    fsolve_no_converg = eul_error.max()
    if np.isnan(fsolve_no_converg):
        fsolve_no_converg = 1e6
    if fsolve_no_converg > 1e-4:
        # If the fsovle didn't converge (was NaN or above the tolerance), then tell the minimizer that this is a bad place to be
        # and don't save the solutions as initial guesses (since they might be gibberish)
        output += 1e14
    else:
        var_names = ['solutions']
        dictionary = {}
        for key in var_names:
            dictionary[key] = locals()[key]
        pickle.dump(dictionary, open("OUTPUT/Saved_moments/SS_init_solutions.pkl", "w"))
    if (chi_params_init <= 0.0).any():
        # In case the minimizer doesn't respect the bounds given
        output += 1e14
    # Use generalized method of moments to fit the chi's
    weighting_mat = np.eye(2*J + S)
    scaling_val = 100.0
    value = np.dot(scaling_val * np.dot(output.reshape(1, 2*J+S), weighting_mat), scaling_val * output.reshape(2*J+S, 1))
    print 'Value of criterion function: ', value.sum()
    return value.sum()
Пример #5
0
def SS_solver(b_guess_init, n_guess_init, wguess, rguess, T_Hguess, factorguess, chi_n, chi_b, params, iterative_params, tau_bq, rho, lambdas, weights, e):
    '''
    Solves for the steady state distribution of capital, labor, as well as w, r, T_H and the scaling factor, using an iterative method similar to TPI.
    Inputs:
        b_guess_init = guesses for b (SxJ array)
        n_guess_init = guesses for n (SxJ array)
        wguess = guess for wage rate (scalar)
        rguess = guess for rental rate (scalar)
        T_Hguess = guess for lump sum tax (scalar)
        factorguess = guess for scaling factor to dollars (scalar)
        chi_n = chi^n_s (Sx1 array)
        chi_b = chi^b_j (Jx1 array)
        params = list of parameters (list)
        iterative_params = list of parameters that determine the convergence of the while loop (list)
        tau_bq = bequest tax rate (Jx1 array)
        rho = mortality rates (Sx1 array)
        lambdas = ability weights (Jx1 array)
        weights = population weights (Sx1 array)
        e = ability levels (SxJ array)
    Outputs:
        solutions = steady state values of b, n, w, r, factor, T_H ((2*S*J+4)x1 array)
    '''
    J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, g_n_ss, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params
    maxiter, mindist_SS = iterative_params
    # Rename the inputs
    w = wguess
    r = rguess
    T_H = T_Hguess
    factor = factorguess
    bssmat = b_guess_init
    nssmat = n_guess_init

    dist = 10
    iteration = 0
    dist_vec = np.zeros(maxiter)
    
    while (dist > mindist_SS) and (iteration < maxiter):
        # Solve for the steady state levels of b and n, given w, r, T_H and factor
        for j in xrange(J):
            # Solve the euler equations
            guesses = np.append(bssmat[:, j], nssmat[:, j])
            solutions = opt.fsolve(Euler_equation_solver, guesses * .9, args=(r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e), xtol=1e-13)
            bssmat[:,j] = solutions[:S]
            nssmat[:,j] = solutions[S:]
            # print np.array(Euler_equation_solver(np.append(bssmat[:, j], nssmat[:, j]), r, w, T_H, factor, j, params, chi_b, chi_n, theta, tau_bq, rho, lambdas, e)).max()

        K = house.get_K(bssmat, weights.reshape(S, 1), lambdas.reshape(1, J), g_n_ss)
        L = firm.get_L(e, nssmat, weights.reshape(S, 1), lambdas.reshape(1, J))
        Y = firm.get_Y(K, L, params)
        new_r = firm.get_r(Y, K, params)
        new_w = firm.get_w(Y, L, params)
        b_s = np.array(list(np.zeros(J).reshape(1, J)) + list(bssmat[:-1, :]))
        average_income_model = ((new_r * b_s + new_w * e * nssmat) * weights.reshape(S, 1) * lambdas.reshape(1, J)).sum()
        new_factor = mean_income_data / average_income_model 
        new_BQ = house.get_BQ(new_r, bssmat, weights.reshape(S, 1), lambdas.reshape(1, J), rho.reshape(S, 1), g_n_ss)
        theta = tax.replacement_rate_vals(nssmat, new_w, new_factor, e, J, weights.reshape(S, 1), lambdas)
        new_T_H = tax.get_lump_sum(new_r, b_s, new_w, e, nssmat, new_BQ, lambdas.reshape(1, J), factor, weights.reshape(S, 1), 'SS', params, theta, tau_bq)

        r = misc_funcs.convex_combo(new_r, r, params)
        w = misc_funcs.convex_combo(new_w, w, params)
        factor = misc_funcs.convex_combo(new_factor, factor, params)
        T_H = misc_funcs.convex_combo(new_T_H, T_H, params)
        if T_H != 0:
            dist = np.array([misc_funcs.perc_dif_func(new_r, r)] + [misc_funcs.perc_dif_func(new_w, w)] + [misc_funcs.perc_dif_func(new_T_H, T_H)] + [misc_funcs.perc_dif_func(new_factor, factor)]).max()
        else:
            # If T_H is zero (if there are no taxes), a percent difference will throw NaN's, so we use an absoluate difference
            dist = np.array([misc_funcs.perc_dif_func(new_r, r)] + [misc_funcs.perc_dif_func(new_w, w)] + [abs(new_T_H - T_H)] + [misc_funcs.perc_dif_func(new_factor, factor)]).max()
        dist_vec[iteration] = dist
        # Similar to TPI: if the distance between iterations increases, then decrease the value of nu to prevent cycling
        if iteration > 10:
            if dist_vec[iteration] - dist_vec[iteration-1] > 0:
                nu /= 2.0
                print 'New value of nu:', nu
        iteration += 1
        print "Iteration: %02d" % iteration, " Distance: ", dist

    eul_errors = np.ones(J)
    b_mat = np.zeros((S, J))
    n_mat = np.zeros((S, J))
    # Given the final w, r, T_H and factor, solve for the SS b and n (if you don't do a final fsolve, there will be a slight mismatch, with high euler errors)
    for j in xrange(J):
        solutions1 = opt.fsolve(Euler_equation_solver, np.append(bssmat[:, j], nssmat[:, j])* .9, args=(r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e), xtol=1e-13)
        eul_errors[j] = np.array(Euler_equation_solver(solutions1, r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e)).max()
        b_mat[:, j] = solutions1[:S]
        n_mat[:, j] = solutions1[S:]
    print 'SS fsolve euler error:', eul_errors.max()
    solutions = np.append(b_mat.flatten(), n_mat.flatten())
    other_vars = np.array([w, r, factor, T_H])
    solutions = np.append(solutions, other_vars)
    return solutions
Пример #6
0
def SS_solver(b_guess_init, n_guess_init, wguess, rguess, T_Hguess, factorguess, chi_n, chi_b, params, iterative_params, tau_bq, rho, lambdas, weights, e):
    J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params
    maxiter, mindist_SS = iterative_params
    w = wguess
    r = rguess
    T_H = T_Hguess
    factor = factorguess
    bssmat = b_guess_init
    nssmat = n_guess_init

    dist = 10
    iteration = 0
    dist_vec = np.zeros(maxiter)

    w_step = .1
    r_step = .01
    w_down = True
    r_down = True
    
    while (dist > mindist_SS) and (iteration < maxiter):
        for j in xrange(J):
            # Solve the euler equations
            guesses = np.append(bssmat[:, j], nssmat[:, j])
            solutions = opt.fsolve(Euler_equation_solver, guesses * .9, args=(r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e), xtol=1e-13)
            bssmat[:,j] = solutions[:S]
            nssmat[:,j] = solutions[S:]
            # print np.array(Euler_equation_solver(np.append(bssmat[:, j], nssmat[:, j]), r, w, T_H, factor, j, params, chi_b, chi_n, theta, tau_bq, rho, lambdas, e)).max()

        # Update factor, T_H
        b_s = np.array(list(np.zeros(J).reshape(1, J)) + list(bssmat[:-1, :]))
        average_income_model = ((r * b_s + w * e * nssmat) * weights).sum()
        new_factor = mean_income_data / average_income_model 
        BQ = (1+r)*(bssmat * weights * rho.reshape(S, 1)).sum(0)
        theta = tax.replacement_rate_vals(nssmat, w, factor, e, J, weights)
        new_T_H = tax.get_lump_sum(r, b_s, w, e, nssmat, BQ, lambdas, new_factor, weights, 'SS', params, theta, tau_bq)

        # Update w, r
        B_supply = house.get_K(bssmat, weights)
        L_supply = firm.get_L(e, nssmat, weights)
        total_tax = tax.total_taxes(r, b_s, w, e, nssmat, BQ, lambdas, new_factor, new_T_H, None, 'SS', False, params, theta, tau_bq)
        c_mat = house.get_cons(r, b_s, w, e, nssmat, BQ, lambdas, bssmat, params, total_tax)
        C = (c_mat*weights).sum()
        Y = C / (1-(delta*alpha/(r+delta)))
        B_demand = alpha * Y / (r + delta)
        L_demand = (1-alpha) * Y / w
        if B_demand - B_supply > mindist_SS:
            if r_down:
                r_step /= 2.0
                r_down = False
            r += r_step
        else:
            if not(r_down):
                r_step /= 2.0
                r_down = True
            r -= r_step
        if L_demand - L_supply > mindist_SS:
            if w_down:
                w_step /=2.0
                w_down = False
            w += w_step
        else:
            if not(w_down):
                w_step /= 2.0
                w_down = True
            w -= w_step

        
        factor = misc_funcs.convex_combo(new_factor, factor, params)
        T_H = misc_funcs.convex_combo(new_T_H, T_H, params)
        dist = np.array([misc_funcs.perc_dif_func(new_T_H, T_H)] + [misc_funcs.perc_dif_func(new_factor, factor)] + [misc_funcs.perc_dif_func(B_demand, B_supply)] + [misc_funcs.perc_dif_func(L_demand, L_supply)]).max()
        dist_vec[iteration] = dist
        if iteration > 10:
            if dist_vec[iteration] - dist_vec[iteration-1] > 0:
                nu /= 2.0
                print 'New value of nu:', nu
        iteration += 1
        print "Iteration: %02d" % iteration, " Distance: ", dist

    eul_errors = np.ones(J)
    b_mat = np.zeros((S, J))
    n_mat = np.zeros((S, J))
    for j in xrange(J):
        solutions1 = opt.fsolve(Euler_equation_solver, np.append(bssmat[:, j], nssmat[:, j])* .9, args=(r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e), xtol=1e-13)
        eul_errors[j] = np.array(Euler_equation_solver(solutions1, r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e)).max()
        b_mat[:, j] = solutions1[:S]
        n_mat[:, j] = solutions1[S:]
    print 'SS fsolve euler error:', eul_errors.max()
    solutions = np.append(b_mat.flatten(), n_mat.flatten())
    other_vars = np.array([w, r, factor, T_H])
    solutions = np.append(solutions, other_vars)
    return solutions
Пример #7
0
                rnew.reshape(T, 1, 1), bmat_s, wnew.reshape(T, 1, 1),
                e.reshape(1, S, J), n_mat[:T], BQnew.reshape(T, 1, J),
                lambdas.reshape(1, 1, J), factor_ss, omega_stationary[:T].
                reshape(T, S, 1), 'TPI', parameters, theta, tau_bq)) +
        [T_Hss] * S)

    winit[:T] = misc_funcs.convex_combo(wnew, winit[:T], parameters)
    rinit[:T] = misc_funcs.convex_combo(rnew, rinit[:T], parameters)
    BQinit[:T] = misc_funcs.convex_combo(BQnew, BQinit[:T], parameters)
    T_H_init[:T] = misc_funcs.convex_combo(T_H_new[:T], T_H_init[:T],
                                           parameters)
    guesses_b = misc_funcs.convex_combo(b_mat, guesses_b, parameters)
    guesses_n = misc_funcs.convex_combo(n_mat, guesses_n, parameters)
    if T_H_init.all() != 0:
        TPIdist = np.array(
            list(misc_funcs.perc_dif_func(rnew, rinit[:T])) +
            list(misc_funcs.perc_dif_func(BQnew, BQinit[:T]).flatten()) +
            list(misc_funcs.perc_dif_func(wnew, winit[:T])) +
            list(misc_funcs.perc_dif_func(T_H_new, T_H_init))).max()
    else:
        TPIdist = np.array(
            list(misc_funcs.perc_dif_func(rnew, rinit[:T])) +
            list(misc_funcs.perc_dif_func(BQnew, BQinit[:T]).flatten()) +
            list(misc_funcs.perc_dif_func(wnew, winit[:T])) +
            list(np.abs(T_H_new, T_H_init))).max()
    TPIdist_vec[TPIiter] = TPIdist
    # After T=10, if cycling occurs, drop the value of nu
    # wait til after T=10 or so, because sometimes there is a jump up
    # in the first couple iterations
    if TPIiter > 10:
        if TPIdist_vec[TPIiter] - TPIdist_vec[TPIiter - 1] > 0:
Пример #8
0
     # b_mat[1, -1, j], n_mat[0, -1, j] = np.array(opt.fsolve(SS_TPI_firstdoughnutring, [b_mat[1, -2, j], n_mat[0, -2, j]],
     #     args=(winit[1], rinit[1], BQinit[1, j], T_H_init[1])))
 
 b_mat[0, :, :] = initial_b
 b_mat[1, -1, :]= b_mat[1, -2, :]
 n_mat[0, -1, :] = n_mat[0, -2, :]
 Knew = (omega_stationary[:T, :, :] * b_mat[:T, :, :]).sum(2).sum(1)
 Lnew = (omega_stationary[1:T+1, :, :] * e.reshape(
     1, S, J) * n_mat[:T, :, :]).sum(2).sum(1)
 BQnew = (1+rinit[:T].reshape(T, 1))*(b_mat[:T, :, :] * omega_stationary[:T, :, :] * rho.reshape(1, S, 1)).sum(1)
 Kinit = misc_funcs.convex_combo(Knew, Kinit[:T], parameters)
 Linit = misc_funcs.convex_combo(Lnew, Linit[:T], parameters)
 BQinit[:T] = misc_funcs.convex_combo(BQnew, BQinit[:T], parameters)
 guesses_b = misc_funcs.convex_combo(b_mat, guesses_b, parameters)
 guesses_n = misc_funcs.convex_combo(n_mat, guesses_n, parameters)
 TPIdist = np.array(list(misc_funcs.perc_dif_func(Knew, Kinit))+list(misc_funcs.perc_dif_func(BQnew, BQinit[:T]).flatten())+list(misc_funcs.perc_dif_func(Lnew, Linit))).max()
 TPIdist = np.array(list(np.abs(Knew - Kinit)) + list(np.abs(BQnew - BQinit[:T]).flatten()) + list(np.abs(Lnew - Linit))).max()
 TPIdist_vec[TPIiter] = TPIdist
 # After T=10, if cycling occurs, drop the value of nu
 # wait til after T=10 or so, because sometimes there is a jump up
 # in the first couple iterations
 if TPIiter > 10:
     if TPIdist_vec[TPIiter] - TPIdist_vec[TPIiter-1] > 0:
         nu /= 2
         print 'New Value of nu:', nu
 TPIiter += 1
 print '\tIteration:', TPIiter
 print '\t\tDistance:', TPIdist
 if (TPIiter < maxiter) and (TPIdist >= mindist_TPI):
     bmat_plus1 = np.zeros((T, S, J))
     bmat_plus1[:, 1:, :] = b_mat[:T, :-1, :]
Пример #9
0
def SS_solver(b_guess_init, n_guess_init, wguess, rguess, T_Hguess, factorguess, chi_n, chi_b, params, iterative_params, tau_bq, rho, lambdas, weights, e):
    J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params
    maxiter, mindist_SS = iterative_params
    w = wguess
    r = rguess
    T_H = T_Hguess
    factor = factorguess
    bssmat = b_guess_init
    nssmat = n_guess_init

    dist = 10
    iteration = 0
    dist_vec = np.zeros(maxiter)
    
    while (dist > mindist_SS) and (iteration < maxiter):
        for j in xrange(J):
            # Solve the euler equations
            guesses = np.append(bssmat[:, j], nssmat[:, j])
            solutions = opt.fsolve(Euler_equation_solver, guesses * .9, args=(r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e), xtol=1e-13)
            bssmat[:,j] = solutions[:S]
            nssmat[:,j] = solutions[S:]
            # print np.array(Euler_equation_solver(np.append(bssmat[:, j], nssmat[:, j]), r, w, T_H, factor, j, params, chi_b, chi_n, theta, tau_bq, rho, lambdas, e)).max()

        K = house.get_K(bssmat, weights)
        L = firm.get_L(e, nssmat, weights)
        Y = firm.get_Y(K, L, params)
        new_r = firm.get_r(Y, K, params)
        new_w = firm.get_w(Y, L, params)
        b_s = np.array(list(np.zeros(J).reshape(1, J)) + list(bssmat[:-1, :]))
        average_income_model = ((new_r * b_s + new_w * e * nssmat) * weights).sum()
        new_factor = mean_income_data / average_income_model 
        new_BQ = (1+new_r)*(bssmat * weights * rho.reshape(S, 1)).sum(0)
        theta = tax.replacement_rate_vals(nssmat, new_w, new_factor, e, J, weights)
        new_T_H = tax.get_lump_sum(new_r, b_s, new_w, e, nssmat, new_BQ, lambdas, factor, weights, 'SS', params, theta, tau_bq)

        r = misc_funcs.convex_combo(new_r, r, params)
        w = misc_funcs.convex_combo(new_w, w, params)
        factor = misc_funcs.convex_combo(new_factor, factor, params)
        T_H = misc_funcs.convex_combo(new_T_H, T_H, params)
        
        dist = np.array([misc_funcs.perc_dif_func(new_r, r)] + [misc_funcs.perc_dif_func(new_w, w)] + [misc_funcs.perc_dif_func(new_T_H, T_H)] + [misc_funcs.perc_dif_func(new_factor, factor)]).max()
        dist_vec[iteration] = dist
        if iteration > 10:
            if dist_vec[iteration] - dist_vec[iteration-1] > 0:
                nu /= 2.0
                print 'New value of nu:', nu
        iteration += 1
        print "Iteration: %02d" % iteration, " Distance: ", dist

    eul_errors = np.ones(J)
    b_mat = np.zeros((S, J))
    n_mat = np.zeros((S, J))
    for j in xrange(J):
        solutions1 = opt.fsolve(Euler_equation_solver, np.append(bssmat[:, j], nssmat[:, j])* .9, args=(r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e), xtol=1e-13)
        eul_errors[j] = np.array(Euler_equation_solver(solutions1, r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e)).max()
        b_mat[:, j] = solutions1[:S]
        n_mat[:, j] = solutions1[S:]
    print 'SS fsolve euler error:', eul_errors.max()
    solutions = np.append(b_mat.flatten(), n_mat.flatten())
    other_vars = np.array([w, r, factor, T_H])
    solutions = np.append(solutions, other_vars)
    return solutions