Ejemplo n.º 1
0
def test_tp_output_dump_table():
    df = output_tables.tp_output_dump_table(base_params,
                                            base_tpi,
                                            reform_params=reform_params,
                                            reform_tpi=reform_tpi)
    assert isinstance(df, pd.DataFrame)
Ejemplo n.º 2
0
def comp_output(base_params,
                base_ss,
                reform_params,
                reform_ss,
                time_path,
                base_tpi=None,
                reform_tpi=None,
                var='cssmat'):
    '''
    Function to create output for the COMP platform
    '''
    if time_path:
        table_title = 'Percentage Changes in Economic Aggregates Between'
        table_title += ' Baseline and Reform Policy'
        plot1_title = 'Pct Changes in Economic Aggregates Between'
        plot1_title += ' Baseline and Reform Policy'
        plot2_title = 'Pct Changes in Interest Rates and Wages'
        plot2_title += ' Between Baseline and Reform Policy'
        plot3_title = 'Differences in Fiscal Variables Relative to GDP'
        plot3_title += ' Between Baseline and Reform Policy'
        out_table = ot.tp_output_dump_table(base_params,
                                            base_tpi,
                                            reform_params,
                                            reform_tpi,
                                            table_format='csv')
        html_table = ot.macro_table(
            base_tpi,
            base_params,
            reform_tpi,
            reform_params,
            var_list=['Y', 'C', 'I_total', 'L', 'D', 'G', 'r', 'w'],
            output_type='pct_diff',
            num_years=10,
            include_SS=True,
            include_overall=True,
            start_year=base_params.start_year,
            table_format='html')
        fig1 = op.plot_aggregates(base_tpi,
                                  base_params,
                                  reform_tpi,
                                  reform_params,
                                  var_list=['Y', 'C', 'K', 'L'],
                                  plot_type='pct_diff',
                                  num_years_to_plot=50,
                                  start_year=base_params.start_year,
                                  vertical_line_years=[
                                      base_params.start_year + base_params.tG1,
                                      base_params.start_year + base_params.tG2
                                  ],
                                  plot_title=None,
                                  path=None)
        in_memory_file1 = io.BytesIO()
        fig1.savefig(in_memory_file1, format="png", bbox_inches="tight")
        in_memory_file1.seek(0)
        fig2 = op.plot_aggregates(base_tpi,
                                  base_params,
                                  reform_tpi,
                                  reform_params,
                                  var_list=['r_gov', 'w'],
                                  plot_type='pct_diff',
                                  num_years_to_plot=50,
                                  start_year=base_params.start_year,
                                  vertical_line_years=[
                                      base_params.start_year + base_params.tG1,
                                      base_params.start_year + base_params.tG2
                                  ],
                                  plot_title=None,
                                  path=None)
        in_memory_file2 = io.BytesIO()
        fig2.savefig(in_memory_file2, format="png", bbox_inches="tight")
        in_memory_file2.seek(0)
        fig3 = op.plot_gdp_ratio(base_tpi,
                                 base_params,
                                 reform_tpi,
                                 reform_params,
                                 var_list=['D', 'G', 'total_tax_revenue'],
                                 plot_type='diff',
                                 num_years_to_plot=50,
                                 start_year=base_params.start_year,
                                 vertical_line_years=[
                                     base_params.start_year + base_params.tG1,
                                     base_params.start_year + base_params.tG2
                                 ],
                                 plot_title=None,
                                 path=None)
        in_memory_file3 = io.BytesIO()
        fig3.savefig(in_memory_file3, format="png", bbox_inches="tight")
        in_memory_file3.seek(0)

        comp_dict = {
            "renderable": [{
                "media_type": "PNG",
                "title": plot1_title,
                "data": in_memory_file1.read()
            }, {
                "media_type": "PNG",
                "title": plot2_title,
                "data": in_memory_file2.read()
            }, {
                "media_type": "PNG",
                "title": plot3_title,
                "data": in_memory_file3.read()
            }, {
                "media_type": "table",
                "title": table_title,
                "data": html_table
            }],
            "downloadable": [{
                "media_type": "CSV",
                "title": table_title,
                "data": out_table.to_csv()
            }]
        }
    else:
        table_title = 'Percentage Changes in Economic Aggregates Between'
        table_title += ' Baseline and Reform Policy'
        plot_title = 'Percentage Changes in Consumption by Lifetime Income'
        plot_title += ' Percentile Group'
        out_table = ot.macro_table_SS(base_ss,
                                      reform_ss,
                                      var_list=[
                                          'Yss', 'Css', 'Iss_total', 'Gss',
                                          'total_tax_revenue', 'Lss', 'rss',
                                          'wss'
                                      ],
                                      table_format='csv')
        html_table = ot.macro_table_SS(base_ss,
                                       reform_ss,
                                       var_list=[
                                           'Yss', 'Css', 'Iss_total', 'Gss',
                                           'total_tax_revenue', 'Lss', 'rss',
                                           'wss'
                                       ],
                                       table_format='html')
        fig = op.ability_bar_ss(base_ss,
                                base_params,
                                reform_ss,
                                reform_params,
                                var=var)
        in_memory_file = io.BytesIO()
        fig.savefig(in_memory_file, format="png", bbox_inches="tight")
        in_memory_file.seek(0)

        comp_dict = {
            "renderable": [{
                "media_type": "PNG",
                "title": plot_title,
                "data": in_memory_file.read()
            }, {
                "media_type": "table",
                "title": table_title,
                "data": html_table
            }],
            "downloadable": [{
                "media_type": "CSV",
                "title": table_title,
                "data": out_table.to_csv()
            }]
        }

    return comp_dict
Ejemplo n.º 3
0
def main():
    # Define parameters to use for multiprocessing
    client = Client()
    num_workers = min(multiprocessing.cpu_count(), 12)
    print('Number of workers = ', num_workers)
    run_start_time = time.time()

    reform_url = ('biden-iitax-reforms.json')
    ref = Calculator.read_json_param_objects(reform_url, None)
    iit_reform = ref['policy']

    # Directories to save data
    CUR_DIR = os.path.dirname(os.path.realpath(__file__))
    base_dir = os.path.join(CUR_DIR, BASELINE_DIR)
    reform_dir = os.path.join(CUR_DIR, REFORM_DIR)
    '''
    ------------------------------------------------------------------------
    Run baseline policy first
    ------------------------------------------------------------------------
    '''
    og_spec_base = {
        'start_year': 2021,
        'tG1': 20,
        'tG2': 200,
        'initial_debt_ratio': 0.982,
        'debt_ratio_ss': 1.5,
        'tax_func_type': 'DEP',
        'age_specific': True
    }
    kwargs = {
        'output_base': base_dir,
        'baseline_dir': base_dir,
        'test': False,
        'time_path': True,
        'baseline': True,
        'og_spec': og_spec_base,
        'guid': '_biden',
        'run_micro': True,
        'tax_func_path': None,
        'data': 'cps',
        'client': client,
        'num_workers': num_workers
    }

    start_time = time.time()
    runner(**kwargs)
    print('run time = ', time.time() - start_time)
    '''
    ------------------------------------------------------------------------
    Run reform policy
    ------------------------------------------------------------------------
    '''
    # update the effective corporate income tax rate
    og_spec_reform = {
        'start_year': 2021,
        'tG1': 20,
        'tG2': 200,
        'initial_debt_ratio': 0.982,
        'debt_ratio_ss': 1.5,
        'tax_func_type': 'DEP',
        'age_specific': True,
        'cit_rate': [0.28]
    }
    kwargs = {
        'output_base': reform_dir,
        'baseline_dir': base_dir,
        'test': False,
        'time_path': True,
        'baseline': False,
        'og_spec': og_spec_reform,
        'guid': '_biden',
        'iit_reform': iit_reform,
        'run_micro': True,
        'tax_func_path': None,
        'data': 'cps',
        'client': client,
        'num_workers': num_workers
    }

    start_time = time.time()
    runner(**kwargs)
    print('run time = ', time.time() - start_time)

    # return ans - the percentage changes in macro aggregates and prices
    # due to policy changes from the baseline to the reform
    base_tpi = safe_read_pickle(os.path.join(base_dir, 'TPI', 'TPI_vars.pkl'))
    base_ss = safe_read_pickle(os.path.join(base_dir, 'SS', 'SS_vars.pkl'))
    base_params = safe_read_pickle(os.path.join(base_dir, 'model_params.pkl'))
    reform_tpi = safe_read_pickle(
        os.path.join(reform_dir, 'TPI', 'TPI_vars.pkl'))
    reform_ss = safe_read_pickle(os.path.join(reform_dir, 'SS', 'SS_vars.pkl'))
    reform_params = safe_read_pickle(
        os.path.join(reform_dir, 'model_params.pkl'))

    # Macro Agg Tables
    ans0 = ot.macro_table(base_tpi,
                          base_params,
                          reform_tpi=reform_tpi,
                          reform_params=reform_params,
                          var_list=['Y', 'C', 'K', 'L', 'r', 'w'],
                          output_type='pct_diff',
                          num_years=10,
                          start_year=2021)
    ans1 = ot.macro_table(base_tpi,
                          base_params,
                          reform_tpi=reform_tpi,
                          reform_params=reform_params,
                          var_list=['Y', 'C', 'K', 'L', 'r', 'w'],
                          output_type='pct_diff',
                          num_years=10,
                          start_year=2031)

    # Dynamic Revenue Feedback Table
    ans2 = ot.dynamic_revenue_decomposition(base_params,
                                            base_tpi,
                                            base_ss,
                                            reform_params,
                                            reform_tpi,
                                            reform_ss,
                                            num_years=10,
                                            include_SS=False,
                                            include_overall=True,
                                            start_year=2021,
                                            table_format=None,
                                            path=None)

    # Data for Macro Aggregates Graph
    ans3 = ot.macro_table(base_tpi,
                          base_params,
                          reform_tpi=reform_tpi,
                          reform_params=reform_params,
                          var_list=['Y', 'K', 'L', 'C'],
                          output_type='pct_diff',
                          num_years=60,
                          start_year=2021)

    # Data for Debt to GDP Graph
    ans4 = ot.macro_table(base_tpi,
                          base_params,
                          reform_tpi=reform_tpi,
                          reform_params=reform_params,
                          var_list=['D', 'Y'],
                          output_type='levels',
                          num_years=60,
                          start_year=2021)

    # Data Dump
    ans5 = ot.tp_output_dump_table(base_params,
                                   base_tpi,
                                   reform_params,
                                   reform_tpi,
                                   table_format=None,
                                   path=None)

    # create plots of output
    op.plot_all(base_dir, reform_dir, os.path.join(CUR_DIR, 'run_biden_plots'))

    print("total time was ", (time.time() - run_start_time))
    print('Percentage changes in aggregates:', ans)

    # save output tables to csv files
    ans0.to_csv('ogusa_biden_output_2021-30.csv')
    ans1.to_csv('ogusa_biden_output_2031-40.csv')
    ans2.to_csv('ogusa_biden_dynamic_revenue.csv')
    ans3.to_csv('ogusa_biden_macro_table.csv')
    ans4.to_csv('ogusa_biden_debt2gdp_2021-30.csv')
    ans5.to_csv('ogusa_biden_output_dump.csv')
    client.close()
Ejemplo n.º 4
0
def main():


    # Directories to save data
    CUR_DIR = os.path.dirname(os.path.realpath(__file__))
    base_dir = os.path.join(CUR_DIR, BASELINE_DIR)
    reform_dir = os.path.join(CUR_DIR, REFORM_DIR)


    # return ans - the percentage changes in macro aggregates and prices
    # due to policy changes from the baseline to the reform
    base_tpi = safe_read_pickle(
        os.path.join(base_dir, 'TPI', 'TPI_vars.pkl'))
    base_ss = safe_read_pickle(
        os.path.join(base_dir, 'SS', 'SS_vars.pkl'))
    base_params = safe_read_pickle(
        os.path.join(base_dir, 'model_params.pkl'))
    reform_tpi = safe_read_pickle(
        os.path.join(reform_dir, 'TPI', 'TPI_vars.pkl'))
    reform_ss = safe_read_pickle(
        os.path.join(reform_dir, 'SS', 'SS_vars.pkl'))
    reform_params = safe_read_pickle(
        os.path.join(reform_dir, 'model_params.pkl'))


    # Macro Agg Tables
    ans0 = ot.macro_table(
        base_tpi, base_params, reform_tpi=reform_tpi,
        reform_params=reform_params,
        var_list=['Y', 'C', 'K', 'L', 'r', 'w'], output_type='pct_diff',
        num_years=10, start_year=2021)
    ans1 = ot.macro_table(
        base_tpi, base_params, reform_tpi=reform_tpi,
        reform_params=reform_params,
        var_list=['Y', 'C', 'K', 'L', 'r', 'w'], output_type='pct_diff',
        num_years=10, start_year=2031)

    # Dynamic Revenue Feedback Table
    ans2 = ot.dynamic_revenue_decomposition(
        base_params, base_tpi, base_ss, reform_params, reform_tpi, reform_ss,
        num_years = 10, include_SS=False, include_overall=True,
        start_year=2021, table_format=None, path=None)

    # Data for Macro Aggregates Graph 
    ans3 = ot.macro_table(
        base_tpi, base_params, reform_tpi=reform_tpi,
        reform_params=reform_params,
        var_list=['Y', 'K', 'L', 'C'], output_type='pct_diff',
        num_years=60, start_year=2021)

    # Data for Debt to GDP Graph
    ans4 = ot.macro_table(
        base_tpi, base_params, reform_tpi=reform_tpi,
        reform_params=reform_params,
        var_list=['D','Y'], output_type='levels',
        num_years=60, start_year=2021)

    # Data Dump
    ans5 = ot.tp_output_dump_table(
        base_params, base_tpi, reform_params,
        reform_tpi, table_format=None, path=None)

    # create plots of output
    op.plot_all(base_dir, reform_dir,
                os.path.join(CUR_DIR, 'run_biden_plots'))

    # save output tables to csv files
    ans0.to_csv('ogusa_biden_output_2021-30.csv')
    ans1.to_csv('ogusa_biden_output_2031-40.csv')
    ans2.to_csv('ogusa_biden_dynamic_revenue.csv')
    ans3.to_csv('ogusa_biden_macro_table.csv')
    ans4.to_csv('ogusa_biden_debt2gdp_2021-30.csv')
    ans5.to_csv('ogusa_biden_output_dump.csv')
    client.close()