<commit_before>
'''
This module should be organized as follows:

Main function:
chi_estimate() = returns chi_n, chi_b
    - calls: 
        wealth.get_wealth_data() - returns data moments on wealth distribution
        labor.labor_data_moments() - returns data moments on labor supply
        minstat() - returns min of statistical objective function
            model_moments() - returns model moments
                SS.run_SS() - return SS distributions

'''

'''
------------------------------------------------------------------------
Last updated: 4/11/2016

Uses a simulated method of moments to calibrate the chi_n adn chi_b 
parameters of OG-USA.

This py-file calls the following other file(s):
    wealth.get_wealth_data()
    labor.labor_data_moments()
    SS.run_SS

This py-file creates the following other file(s): None
------------------------------------------------------------------------
'''


def chi_estimate(income_tax_params, ss_parameters, iterative_params, chi_guesses, baseline_dir="./OUTPUT"):

    # unpack tuples of parameters
    J, S, T, BW, beta, sigma, alpha, Z, delta, ltilde, nu, g_y,\
                  g_n_ss, tau_payroll, tau_bq, rho, omega_SS, lambdas, e, retire, mean_income_data,\
                  h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = ss_params

    # Generate Wealth data moments
    wealth_moments = wealth.get_wealth_data(lambdas, J, flag_graphs, output_dir)

    # Generate labor data moments
    labor_moments = labor.labor_data_moments(flag_graphs, output_dir)

    # combine moments
    data_moments = list(wealth_moments.flatten()) + list(labor_moments.flatten())

    # call minimizer
    bnds = np.ones((S+J,2))*(1e-12, None) # Need (1e-12, None) pari J+S times
    #min_args = () data_moments
    min_args = data_moments
    est_output = opt.minimize(minstat, chi_guesses,
                    args=(min_args), method="L-BFGS-B", bounds=bnds,
                    tol=1e-15)
    chi_params = est_output.x
    objective_func_min = est_out.fun 

    return chi_params




def minstat(chi_guesses, *args):
    '''
    --------------------------------------------------------------------
    This function generates the weighted sum of squared differences
    between the model and data moments.


    RETURNS: wssqdev
    --------------------------------------------------------------------
    '''
    chi_params = chi_guesses
    data_moments, income_tax_params, ss_params, iterative_params, chi_params, baseline_dir = args
    ss_output = SS.run_SS(income_tax_params, ss_params, iterative_params, chi_params, True, baseline_dir)

        # output = {'Kss': Kss, 'bssmat': bssmat, 'Lss': Lss, 'Css':Css, 'nssmat': nssmat, 'Yss': Yss,
        #       'wss': wss, 'rss': rss, 'theta': theta, 'BQss': BQss, 'factor_ss': factor_ss,
        #       'bssmat_s': bssmat_s, 'cssmat': cssmat, 'bssmat_splus1': bssmat_splus1,
        #       'T_Hss': T_Hss, 'euler_savings': euler_savings,
        #       'euler_labor_leisure': euler_labor_leisure, 'chi_n': chi_n,
        #       'chi_b': chi_b}

    model_moments = calc_moments(ss_output)

    distance = ((model_moments - data_moments)**2).sum()
    return distance


def calc_moments(**ss_output):
    '''
    --------------------------------------------------------------------
    This function calculates moments from the SS output that correspond
    to the data moments used for estimation.

    RETURNS: model_moments
    --------------------------------------------------------------------
    '''

    # wealth moments

    # labor moments

    # combine moments
    model_moments = list(model_wealth_moments.flatten()) + list(model_labor_moments.flatten())


    return model_moments







    ***********************************************
    ***********************************************

    OLD CODE BELOW

    ***********************************************
    ***********************************************

if calibrate_model:
        global Nfeval, value_all, chi_params_all
        Nfeval = 1
        value_all = np.zeros((10000))
        chi_params_all = np.zeros((S+J,10000))
        outputs = {'solutions': solutions, 'chi_params': chi_params}
        ss_init_path = os.path.join(
            output_dir, "Saved_moments/SS_init_solutions.pkl")
        pickle.dump(outputs, open(ss_init_path, "wb"))
        function_to_minimize_X = lambda x: function_to_minimize(
            x, chi_params, income_tax_parameters, ss_parameters, iterative_params, omega_SS, rho, lambdas, tau_bq, e, output_dir)
        bnds = tuple([(1e-6, None)] * (S + J))
        # In order to scale all the parameters to estimate in the minimizer, we have the minimizer fit a vector of ones that
        # will be multiplied by the chi initial guesses inside the function.  Otherwise, if chi^b_j=1e5 for some j, and the
        # minimizer peturbs that value by 1e-8, the % difference will be extremely small, outside of the tolerance of the<commit_msg>small change to allow calibrate.py to compile<commit_after>
'''
This module should be organized as follows:

Main function:
chi_estimate() = returns chi_n, chi_b
    - calls: 
        wealth.get_wealth_data() - returns data moments on wealth distribution
        labor.labor_data_moments() - returns data moments on labor supply
        minstat() - returns min of statistical objective function
            model_moments() - returns model moments
                SS.run_SS() - return SS distributions

'''

'''
------------------------------------------------------------------------
Last updated: 4/11/2016

Uses a simulated method of moments to calibrate the chi_n adn chi_b 
parameters of OG-USA.

This py-file calls the following other file(s):
    wealth.get_wealth_data()
    labor.labor_data_moments()
    SS.run_SS

This py-file creates the following other file(s): None
------------------------------------------------------------------------
'''


def chi_estimate(income_tax_params, ss_parameters, iterative_params, chi_guesses, baseline_dir="./OUTPUT"):

    # unpack tuples of parameters
    J, S, T, BW, beta, sigma, alpha, Z, delta, ltilde, nu, g_y,\
                  g_n_ss, tau_payroll, tau_bq, rho, omega_SS, lambdas, e, retire, mean_income_data,\
                  h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = ss_params

    # Generate Wealth data moments
    wealth_moments = wealth.get_wealth_data(lambdas, J, flag_graphs, output_dir)

    # Generate labor data moments
    labor_moments = labor.labor_data_moments(flag_graphs, output_dir)

    # combine moments
    data_moments = list(wealth_moments.flatten()) + list(labor_moments.flatten())

    # call minimizer
    bnds = np.ones((S+J,2))*(1e-12, None) # Need (1e-12, None) pari J+S times
    #min_args = () data_moments
    min_args = data_moments
    est_output = opt.minimize(minstat, chi_guesses,
                    args=(min_args), method="L-BFGS-B", bounds=bnds,
                    tol=1e-15)
    chi_params = est_output.x
    objective_func_min = est_out.fun 

    return chi_params




def minstat(chi_guesses, *args):
    '''
    --------------------------------------------------------------------
    This function generates the weighted sum of squared differences
    between the model and data moments.


    RETURNS: wssqdev
    --------------------------------------------------------------------
    '''
    chi_params = chi_guesses
    data_moments, income_tax_params, ss_params, iterative_params, chi_params, baseline_dir = args
    ss_output = SS.run_SS(income_tax_params, ss_params, iterative_params, chi_params, True, baseline_dir)

        # output = {'Kss': Kss, 'bssmat': bssmat, 'Lss': Lss, 'Css':Css, 'nssmat': nssmat, 'Yss': Yss,
        #       'wss': wss, 'rss': rss, 'theta': theta, 'BQss': BQss, 'factor_ss': factor_ss,
        #       'bssmat_s': bssmat_s, 'cssmat': cssmat, 'bssmat_splus1': bssmat_splus1,
        #       'T_Hss': T_Hss, 'euler_savings': euler_savings,
        #       'euler_labor_leisure': euler_labor_leisure, 'chi_n': chi_n,
        #       'chi_b': chi_b}

    model_moments = calc_moments(ss_output)

    distance = ((model_moments - data_moments)**2).sum()
    return distance


def calc_moments(**ss_output):
    '''
    --------------------------------------------------------------------
    This function calculates moments from the SS output that correspond
    to the data moments used for estimation.

    RETURNS: model_moments
    --------------------------------------------------------------------
    '''

    # wealth moments

    # labor moments

    # combine moments
    model_moments = list(model_wealth_moments.flatten()) + list(model_labor_moments.flatten())


    return model_moments

#***********************************************
#***********************************************

#OLD CODE BELOW

#***********************************************
#***********************************************

calibrate_model = False

if calibrate_model:
        global Nfeval, value_all, chi_params_all
        Nfeval = 1
        value_all = np.zeros((10000))
        chi_params_all = np.zeros((S+J,10000))
        outputs = {'solutions': solutions, 'chi_params': chi_params}
        ss_init_path = os.path.join(
            output_dir, "Saved_moments/SS_init_solutions.pkl")
        pickle.dump(outputs, open(ss_init_path, "wb"))
        function_to_minimize_X = lambda x: function_to_minimize(
            x, chi_params, income_tax_parameters, ss_parameters, iterative_params, omega_SS, rho, lambdas, tau_bq, e, output_dir)
        bnds = tuple([(1e-6, None)] * (S + J))
        # In order to scale all the parameters to estimate in the minimizer, we have the minimizer fit a vector of ones that
        # will be multiplied by the chi initial guesses inside the function.  Otherwise, if chi^b_j=1e5 for some j, and the
        # minimizer peturbs that value by 1e-8, the % difference will be extremely small, outside of the tolerance of the
{'fraction_non_alphanumeric': 0.2063253012048193, 'fraction_numerical': 0.007429718875502008, 'mean_word_length': 2.3426174496644294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6404968', 'n_tokens_mistral': 3139, 'n_tokens_neox': 2915, 'n_words': 777}