コード例 #1
0
def validate_inputs(meta_param_dict, adjustment, errors_warnings):
    '''
    Validates user inputs for parameters
    '''
    # Validate meta parameter inputs
    meta_params = MetaParams()
    meta_params.adjust(meta_param_dict, raise_errors=False)
    errors_warnings["Business Tax Parameters"]["errors"].update(
      meta_params.errors)
    # Validate CCC parameter inputs
    params = Specification()
    params.adjust(adjustment["Business Tax Parameters"],
                  raise_errors=False)
    errors_warnings["Business Tax Parameters"]["errors"].update(
        params.errors)
    # Validate TC parameter inputs
    iit_adj = cs2tc.convert_policy_adjustment(
        adjustment["Individual and Payroll Tax Parameters"])

    iit_params = Policy()
    iit_params.adjust(iit_adj, raise_errors=False, ignore_warnings=True)
    errors_warnings["Individual and Payroll Tax Parameters"][
        "errors"].update(iit_params.errors)

    return {"errors_warnings": errors_warnings}
コード例 #2
0
def run_model(meta_param_dict, adjustment):
    '''
    Initializes classes from CCC that compute the model under
    different policies.  Then calls function get output objects.
    '''
    # update MetaParams
    meta_params = MetaParams()
    meta_params.adjust(meta_param_dict)
    # Get data chosen by user
    if meta_params.data_source == "PUF":
        data = retrieve_puf(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    else:
        data = "cps"
    # Get TC params adjustments
    iit_mods = cs2tc.convert_policy_adjustment(
        adjustment["Individual and Payroll Tax Parameters"])
    filtered_ccc_params = {}
    # filter out CCC params that will not change between baeline and
    # reform runs (These are the Household Savings Behavior and
    # Economic Assumptions)
    constant_param_list = [
        'omega_scg', 'omega_lcg', 'omega_xcg', 'alpha_c_e_ft', 'alpha_c_e_td',
        'alpha_c_e_nt', 'alpha_c_d_ft', 'alpha_c_d_td', 'alpha_c_d_nt',
        'alpha_nc_d_ft', 'alpha_nc_d_td', 'alpha_nc_d_nt', 'alpha_h_d_ft',
        'alpha_h_d_td', 'alpha_h_d_nt', 'Y_td', 'Y_scg', 'Y_lcg', 'gamma',
        'E_c', 'inflation_rate', 'nominal_interest_rate'
    ]
    filtered_ccc_params = OrderedDict()
    for k, v in adjustment['Business Tax Parameters'].items():
        if k in constant_param_list:
            filtered_ccc_params[k] = v
    # Baseline CCC calculator
    params = Specification(year=meta_params.year,
                           call_tc=False,
                           iit_reform={},
                           data=data)
    params.update_specification(filtered_ccc_params)
    assets = Assets()
    dp = DepreciationParams()
    calc1 = Calculator(params, dp, assets)
    # Reform CCC calculator - includes TC adjustments
    params2 = Specification(year=meta_params.year,
                            call_tc=True,
                            iit_reform=iit_mods,
                            data=data)
    params2.update_specification(adjustment["Business Tax Parameters"])
    calc2 = Calculator(params2, dp, assets)
    comp_dict = comp_output(calc1, calc2)

    return comp_dict
コード例 #3
0
ファイル: functions.py プロジェクト: rickecon/Tax-Brain
def validate_inputs(meta_params_dict, adjustment, errors_warnings):
    """
    Function to validate COMP inputs
    """
    pol_params = cs2tc.convert_policy_adjustment(adjustment["policy"])
    policy_params = taxcalc.Policy()
    policy_params.adjust(pol_params, raise_errors=False, ignore_warnings=True)
    errors_warnings["policy"]["errors"].update(policy_params.errors)

    behavior_params = BehaviorParams()
    behavior_params.adjust(adjustment["behavior"], raise_errors=False)
    errors_warnings["behavior"]["errors"].update(behavior_params.errors)

    return {"errors_warnings": errors_warnings}
コード例 #4
0
def run_model(meta_param_dict, adjustment):
    """
    Initializes classes from OG-USA that compute the model under
    different policies.  Then calls function get output objects.
    """
    print("Meta_param_dict = ", meta_param_dict)
    print("adjustment dict = ", adjustment)

    meta_params = MetaParams()
    meta_params.adjust(meta_param_dict)
    if meta_params.data_source == "PUF":
        data = retrieve_puf(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
        # set name of cached baseline file in case use below
        cached_pickle = "TxFuncEst_baseline_PUF.pkl"
    else:
        data = "cps"
        # set name of cached baseline file in case use below
        cached_pickle = "TxFuncEst_baseline_CPS.pkl"
    # Get TC params adjustments
    iit_mods = convert_policy_adjustment(
        adjustment["Tax-Calculator Parameters"]
    )
    # Create output directory structure
    base_dir = os.path.join(CUR_DIR, BASELINE_DIR)
    reform_dir = os.path.join(CUR_DIR, REFORM_DIR)
    dirs = [base_dir, reform_dir]
    for _dir in dirs:
        utils.mkdirs(_dir)

    # Dask parmeters
    client = Client()
    num_workers = 5
    # TODO: Swap to these parameters when able to specify tax function
    # and model workers separately
    # num_workers_txf = 5
    # num_workers_mod = 6

    # whether to estimate tax functions from microdata
    run_micro = True
    time_path = meta_param_dict["time_path"][0]["value"]

    # filter out OG-USA params that will not change between baseline and
    # reform runs (these are the non-policy parameters)
    filtered_ogusa_params = {}
    constant_param_set = {
        "frisch",
        "beta_annual",
        "sigma",
        "g_y_annual",
        "gamma",
        "epsilon",
        "Z",
        "delta_annual",
        "small_open",
        "world_int_rate",
        "initial_debt_ratio",
        "initial_foreign_debt_ratio",
        "zeta_D",
        "zeta_K",
        "tG1",
        "tG2",
        "rho_G",
        "debt_ratio_ss",
        "budget_balance",
    }
    filtered_ogusa_params = OrderedDict()
    for k, v in adjustment["OG-USA Parameters"].items():
        if k in constant_param_set:
            filtered_ogusa_params[k] = v

    # Solve baseline model
    start_year = meta_param_dict["year"][0]["value"]
    if start_year == 2020:
        OGPATH = inspect.getfile(SS)
        OGDIR = os.path.dirname(OGPATH)
        tax_func_path = None  # os.path.join(OGDIR, 'data', 'tax_functions',
        #             cached_pickle)
        run_micro_baseline = True
    else:
        tax_func_path = None
        run_micro_baseline = True
    base_spec = {
        **{
            "start_year": start_year,
            "tax_func_type": "DEP",
            "age_specific": False,
        },
        **filtered_ogusa_params,
    }
    base_params = Specifications(
        output_base=base_dir,
        baseline_dir=base_dir,
        baseline=True,
        num_workers=num_workers,
    )
    base_params.update_specifications(
        json.load(
            open(
                os.path.join(
                    "..", "..", "ogusa", "ogusa_default_parameters.json"
                )
            )
        )
    )
    base_params.update_specifications(base_spec)
    BW = TC_LAST_YEAR - start_year + 1
    base_params.BW = BW
    # Will need to figure out how to handle default tax functions here
    # For now, estimating tax functions even for baseline
    c_base = Calibration(
        base_params,
        iit_reform={},
        estimate_tax_functions=True,
        data=data,
        client=client,
    )
    # update tax function parameters in Specifications Object
    d_base = c_base.get_dict()
    # additional parameters to change
    updated_txfunc_params = {
        "etr_params": d_base["etr_params"],
        "mtrx_params": d_base["mtrx_params"],
        "mtry_params": d_base["mtry_params"],
        "mean_income_data": d_base["mean_income_data"],
        "frac_tax_payroll": d_base["frac_tax_payroll"],
    }
    base_params.update_specifications(updated_txfunc_params)
    base_ss = SS.run_SS(base_params, client=client)
    utils.mkdirs(os.path.join(base_dir, "SS"))
    base_ss_dir = os.path.join(base_dir, "SS", "SS_vars.pkl")
    with open(base_ss_dir, "wb") as f:
        pickle.dump(base_ss, f)
    if time_path:
        base_tpi = TPI.run_TPI(base_params, client=client)
        tpi_dir = os.path.join(base_dir, "TPI", "TPI_vars.pkl")
        with open(tpi_dir, "wb") as f:
            pickle.dump(base_tpi, f)
    else:
        base_tpi = None

    # Solve reform model
    reform_spec = base_spec
    reform_spec.update(adjustment["OG-USA Parameters"])
    reform_params = Specifications(
        output_base=reform_dir,
        baseline_dir=base_dir,
        baseline=False,
        num_workers=num_workers,
    )
    reform_params.update_specifications(
        json.load(
            open(
                os.path.join(
                    "..", "..", "ogusa", "ogusa_default_parameters.json"
                )
            )
        )
    )
    reform_params.update_specifications(reform_spec)
    reform_params.BW = BW
    c_reform = Calibration(
        reform_params,
        iit_reform=iit_mods,
        estimate_tax_functions=True,
        data=data,
        client=client,
    )
    # update tax function parameters in Specifications Object
    d_reform = c_reform.get_dict()
    # additional parameters to change
    updated_txfunc_params = {
        "etr_params": d_reform["etr_params"],
        "mtrx_params": d_reform["mtrx_params"],
        "mtry_params": d_reform["mtry_params"],
        "mean_income_data": d_reform["mean_income_data"],
        "frac_tax_payroll": d_reform["frac_tax_payroll"],
    }
    reform_params.update_specifications(updated_txfunc_params)
    reform_ss = SS.run_SS(reform_params, client=client)
    utils.mkdirs(os.path.join(reform_dir, "SS"))
    reform_ss_dir = os.path.join(reform_dir, "SS", "SS_vars.pkl")
    with open(reform_ss_dir, "wb") as f:
        pickle.dump(reform_ss, f)
    if time_path:
        reform_tpi = TPI.run_TPI(reform_params, client=client)
    else:
        reform_tpi = None

    comp_dict = comp_output(
        base_params,
        base_ss,
        reform_params,
        reform_ss,
        time_path,
        base_tpi,
        reform_tpi,
    )

    # Shut down client and make sure all of its references are
    # cleaned up.
    client.close()
    del client

    return comp_dict
コード例 #5
0
from distributed import Client
from taxcalc import Policy
from collections import OrderedDict
from .helpers import retrieve_puf
from cs2tc import convert_policy_adjustment

AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID", "")
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY", "")
CUR_DIR = os.path.dirname(os.path.realpath(__file__))

# Get Tax-Calculator default parameters
TCPATH = inspect.getfile(Policy)
TCDIR = os.path.dirname(TCPATH)
with open(os.path.join(TCDIR, "policy_current_law.json"), "r") as f:
    pcl = json.loads(f.read())
RES = convert_policy_adjustment(pcl)


class TCParams(paramtools.Parameters):
    defaults = RES


class MetaParams(paramtools.Parameters):
    """
    Meta parameters class for COMP.  These parameters will be in a drop
    down menu on COMP.
    """

    array_first = True
    defaults = {
        "year": {
コード例 #6
0
def run_model(meta_params_dict, adjustment):
    """
    Runs TaxBrain
    """
    # update meta parameters
    meta_params = MetaParameters()
    meta_params.adjust(meta_params_dict)
    # convert COMP user inputs to format accepted by tax-calculator
    policy_mods = cs2tc.convert_policy_adjustment(adjustment["policy"])
    behavior_mods = cs2tc.convert_behavior_adjustment(adjustment["behavior"])
    user_mods = {"policy": policy_mods, "behavior": behavior_mods}
    start_year = int(meta_params.year)
    use_cps = meta_params.data_source == "CPS"
    if meta_params.data_source == "PUF":
        puf_df = retrieve_puf(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
        if puf_df is not None:
            if not isinstance(puf_df, pd.DataFrame):
                raise TypeError("'puf_df' must be a Pandas DataFrame.")
            fuzz = True
            use_cps = False
            sampling_frac = 0.05
            sampling_seed = 2222
            full_sample = puf_df
        else:
            # Access keys are not available. Default to the CPS.
            print("Defaulting to the CPS")
            meta_params.adjust({"data_source": "CPS"})
    if meta_params.data_source == "CPS":
        fuzz = False
        use_cps = True
        input_path = os.path.join(TCDIR, "cps.csv.gz")
        # full_sample = read_egg_csv(cpspath)  # pragma: no cover
        sampling_frac = 0.03
        sampling_seed = 180
        full_sample = pd.read_csv(input_path)

    if meta_params.use_full_sample:
        sample = full_sample
        end_year = min(start_year + 10, TaxBrain.LAST_BUDGET_YEAR)
    else:
        sample = full_sample.sample(frac=sampling_frac,
                                    random_state=sampling_seed)
        end_year = start_year

    tb = TaxBrain(start_year,
                  end_year,
                  microdata=sample,
                  use_cps=use_cps,
                  reform=policy_mods,
                  behavior=behavior_mods)
    tb.run()

    # Collect results for each year
    delayed_list = []
    for year in range(start_year, end_year + 1):
        print('delaying for', year)
        delay = delayed(nth_year_results)(tb, year, user_mods, fuzz)
        delayed_list.append(delay)
    results = compute(*delayed_list)

    # process results to get them ready for display
    # create aggregate plot
    agg_plot = aggregate_plot(tb)
    all_to_process = defaultdict(list)
    for result in results:
        for key, value in result.items():
            all_to_process[key] += value
    results, downloadable = postprocess(all_to_process)
    # create report output if it is not a run with no reforme
    if tb.params["policy"].keys():
        report_outputs = report(tb, clean=True)
        for name, data in report_outputs.items():
            if name.endswith(".md"):
                media_type = "Markdown"
            elif name.endswith(".pdf"):
                media_type = "PDF"
            downloadable.append({
                "media_type": media_type,
                "title": name,
                "data": data
            })
    agg_output, table_output = create_layout(results, start_year, end_year)

    comp_outputs = {
        "renderable": [agg_plot, agg_output, table_output],
        "downloadable": downloadable
    }
    return comp_outputs
コード例 #7
0
def run_model(meta_param_dict, adjustment):
    '''
    Initializes classes from OG-USA that compute the model under
    different policies.  Then calls function get output objects.
    '''
    print('Meta_param_dict = ', meta_param_dict)
    print('adjustment dict = ', adjustment)

    meta_params = MetaParams()
    meta_params.adjust(meta_param_dict)
    if meta_params.data_source == "PUF":
        data = retrieve_puf(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
        # set name of cached baseline file in case use below
        cached_pickle = 'TxFuncEst_baseline_PUF.pkl'
    else:
        data = "cps"
        # set name of cached baseline file in case use below
        cached_pickle = 'TxFuncEst_baseline_CPS.pkl'
    # Get TC params adjustments
    iit_mods = convert_policy_adjustment(
        adjustment["Tax-Calculator Parameters"])
    # Create output directory structure
    base_dir = os.path.join(CUR_DIR, BASELINE_DIR)
    reform_dir = os.path.join(CUR_DIR, REFORM_DIR)
    dirs = [base_dir, reform_dir]
    for _dir in dirs:
        utils.mkdirs(_dir)

    # Dask parmeters
    client = Client()
    num_workers = 5
    # TODO: Swap to these parameters when able to specify tax function
    # and model workers separately
    # num_workers_txf = 5
    # num_workers_mod = 6

    # whether to estimate tax functions from microdata
    run_micro = True
    time_path = meta_param_dict['time_path'][0]['value']

    # filter out OG-USA params that will not change between baseline and
    # reform runs (these are the non-policy parameters)
    filtered_ogusa_params = {}
    constant_param_set = {
        'frisch', 'beta_annual', 'sigma', 'g_y_annual', 'gamma', 'epsilon',
        'Z', 'delta_annual', 'small_open', 'world_int_rate',
        'initial_foreign_debt_ratio', 'zeta_D', 'zeta_K', 'tG1', 'tG2',
        'rho_G', 'debt_ratio_ss', 'budget_balance'
    }
    filtered_ogusa_params = OrderedDict()
    for k, v in adjustment['OG-USA Parameters'].items():
        if k in constant_param_set:
            filtered_ogusa_params[k] = v

    # Solve baseline model
    start_year = meta_param_dict['year'][0]['value']
    if start_year == 2020:
        OGPATH = inspect.getfile(SS)
        OGDIR = os.path.dirname(OGPATH)
        tax_func_path = None  #os.path.join(OGDIR, 'data', 'tax_functions',
        #             cached_pickle)
        run_micro_baseline = False
    else:
        tax_func_path = None
        run_micro_baseline = True
    base_spec = {
        **{
            'start_year': start_year,
            'tax_func_type': 'DEP',
            'age_specific': False
        },
        **filtered_ogusa_params
    }
    base_params = Specifications(run_micro=False,
                                 output_base=base_dir,
                                 baseline_dir=base_dir,
                                 test=False,
                                 time_path=False,
                                 baseline=True,
                                 iit_reform={},
                                 guid='',
                                 data=data,
                                 client=client,
                                 num_workers=num_workers)
    base_params.update_specifications(base_spec)
    base_params.get_tax_function_parameters(client,
                                            run_micro_baseline,
                                            tax_func_path=tax_func_path)
    base_ss = SS.run_SS(base_params, client=client)
    utils.mkdirs(os.path.join(base_dir, "SS"))
    base_ss_dir = os.path.join(base_dir, "SS", "SS_vars.pkl")
    with open(base_ss_dir, "wb") as f:
        pickle.dump(base_ss, f)
    if time_path:
        base_tpi = TPI.run_TPI(base_params, client=client)
        tpi_dir = os.path.join(base_dir, "TPI", "TPI_vars.pkl")
        with open(tpi_dir, "wb") as f:
            pickle.dump(base_tpi, f)
    else:
        base_tpi = None

    # Solve reform model
    reform_spec = base_spec
    reform_spec.update(adjustment["OG-USA Parameters"])
    reform_params = Specifications(run_micro=False,
                                   output_base=reform_dir,
                                   baseline_dir=base_dir,
                                   test=False,
                                   time_path=time_path,
                                   baseline=False,
                                   iit_reform=iit_mods,
                                   guid='',
                                   data=data,
                                   client=client,
                                   num_workers=num_workers)
    reform_params.update_specifications(reform_spec)
    reform_params.get_tax_function_parameters(client, run_micro)
    reform_ss = SS.run_SS(reform_params, client=client)
    utils.mkdirs(os.path.join(reform_dir, "SS"))
    reform_ss_dir = os.path.join(reform_dir, "SS", "SS_vars.pkl")
    with open(reform_ss_dir, "wb") as f:
        pickle.dump(reform_ss, f)
    if time_path:
        reform_tpi = TPI.run_TPI(reform_params, client=client)
    else:
        reform_tpi = None

    comp_dict = comp_output(base_params, base_ss, reform_params, reform_ss,
                            time_path, base_tpi, reform_tpi)

    # Shut down client and make sure all of its references are
    # cleaned up.
    client.close()
    del client

    return comp_dict