def test_distribution_tables(cps_subsample):
    """
    Test distribution_tables method.
    """
    pol = Policy()
    recs = Records.cps_constructor(data=cps_subsample)
    calc1 = Calculator(policy=pol, records=recs)
    assert calc1.current_year == 2014
    calc1.calc_all()
    dt1, dt2 = calc1.distribution_tables(None, 'weighted_deciles')
    assert isinstance(dt1, pd.DataFrame)
    assert dt2 is None
    dt1, dt2 = calc1.distribution_tables(calc1, 'weighted_deciles')
    assert isinstance(dt1, pd.DataFrame)
    assert isinstance(dt2, pd.DataFrame)
    reform = {2014: {'_UBI_u18': [1000],
                     '_UBI_1820': [1000],
                     '_UBI_21': [1000]}}
    pol.implement_reform(reform)
    assert not pol.parameter_errors
    calc2 = Calculator(policy=pol, records=recs)
    calc2.calc_all()
    dt1, dt2 = calc1.distribution_tables(calc2, 'weighted_deciles')
    assert isinstance(dt1, pd.DataFrame)
    assert isinstance(dt2, pd.DataFrame)
def test_distribution_tables(cps_subsample):
    """
    Test distribution_tables method.
    """
    pol = Policy()
    recs = Records.cps_constructor(data=cps_subsample)
    calc1 = Calculator(policy=pol, records=recs)
    assert calc1.current_year == 2014
    calc1.calc_all()
    dt1, dt2 = calc1.distribution_tables(None, 'weighted_deciles')
    assert isinstance(dt1, pd.DataFrame)
    assert dt2 is None
    dt1, dt2 = calc1.distribution_tables(calc1, 'weighted_deciles')
    assert isinstance(dt1, pd.DataFrame)
    assert isinstance(dt2, pd.DataFrame)
    reform = {
        'UBI_u18': {
            2014: 1000
        },
        'UBI_1820': {
            2014: 1000
        },
        'UBI_21': {
            2014: 1000
        }
    }
    pol.implement_reform(reform)
    assert not pol.parameter_errors
    calc2 = Calculator(policy=pol, records=recs)
    calc2.calc_all()
    dt1, dt2 = calc1.distribution_tables(calc2, 'weighted_deciles')
    assert isinstance(dt1, pd.DataFrame)
    assert isinstance(dt2, pd.DataFrame)
Esempio n. 3
0
def test_distribution_tables(cps_subsample):
    pol = Policy()
    recs = Records.cps_constructor(data=cps_subsample)
    calc1 = Calculator(policy=pol, records=recs)
    assert calc1.current_year == 2014
    calc1.calc_all()
    dt1, dt2 = calc1.distribution_tables(None)
    assert isinstance(dt1, pd.DataFrame)
    assert dt2 is None
    dt1, dt2 = calc1.distribution_tables(calc1)
    assert isinstance(dt1, pd.DataFrame)
    assert isinstance(dt2, pd.DataFrame)
    reform = {
        2014: {
            '_UBI_u18': [1000],
            '_UBI_1820': [1000],
            '_UBI_21': [1000]
        }
    }
    pol.implement_reform(reform)
    assert not pol.reform_errors
    calc2 = Calculator(policy=pol, records=recs)
    calc2.calc_all()
    dt1, dt2 = calc1.distribution_tables(calc2)
    assert isinstance(dt1, pd.DataFrame)
    assert isinstance(dt2, pd.DataFrame)
Esempio n. 4
0
def test_dist_table_sum_row(cps_subsample):
    rec = Records.cps_constructor(data=cps_subsample)
    calc = Calculator(policy=Policy(), records=rec)
    calc.calc_all()
    # create three distribution tables and compare the ALL row contents
    tb1, _ = calc.distribution_tables(None, 'standard_income_bins')
    tb2, _ = calc.distribution_tables(None, 'soi_agi_bins')
    tb3, _ = calc.distribution_tables(None, 'weighted_deciles')
    tb4, _ = calc.distribution_tables(None,
                                      'weighted_deciles',
                                      pop_quantiles=True)
    assert np.allclose(tb1.loc['ALL'], tb2.loc['ALL'])
    assert np.allclose(tb1.loc['ALL'], tb3.loc['ALL'])
    # make sure population count is larger than filing-unit count
    assert tb4.at['ALL', 'count'] > tb1.at['ALL', 'count']
    # make sure population table has same ALL row values as filing-unit table
    for col in ['count', 'count_StandardDed', 'count_ItemDed', 'count_AMT']:
        tb4.at['ALL', col] = tb1.at['ALL', col]
    assert np.allclose(tb1.loc['ALL'], tb4.loc['ALL'])
    # make sure population table has same ALL tax liabilities as diagnostic tbl
    dgt = calc.diagnostic_table(1)
    assert np.allclose([tb4.at['ALL', 'iitax'], tb4.at['ALL', 'payrolltax']], [
        dgt.at['Ind Income Tax ($b)', calc.current_year],
        dgt.at['Payroll Taxes ($b)', calc.current_year]
    ])
Esempio n. 5
0
def test_dec_graph_plots(cps_subsample):
    pol = Policy()
    rec = Records.cps_constructor(data=cps_subsample)
    calc1 = Calculator(policy=pol, records=rec)
    year = 2020
    calc1.advance_to_year(year)
    reform = {
        year: {
            '_SS_Earnings_c': [9e99],  # OASDI FICA tax on all earnings
            '_FICA_ss_trt': [0.107484]  # lower rate to keep revenue unchanged

        }
    }
    pol.implement_reform(reform)
    calc2 = Calculator(policy=pol, records=rec)
    calc2.advance_to_year(year)
    assert calc1.current_year == calc2.current_year
    calc1.calc_all()
    calc2.calc_all()
    fig = calc1.decile_graph(calc2)
    assert fig
    dt1, dt2 = calc1.distribution_tables(calc2)
    dta = dec_graph_data(dt1, dt2, year,
                         include_zero_incomes=True,
                         include_negative_incomes=False)
    assert isinstance(dta, dict)
    dta = dec_graph_data(dt1, dt2, year,
                         include_zero_incomes=False,
                         include_negative_incomes=True)
    assert isinstance(dta, dict)
    dta = dec_graph_data(dt1, dt2, year,
                         include_zero_incomes=False,
                         include_negative_incomes=False)
    assert isinstance(dta, dict)
def test_dec_graph_plots(cps_subsample):
    pol = Policy()
    rec = Records.cps_constructor(data=cps_subsample)
    calc1 = Calculator(policy=pol, records=rec)
    year = 2020
    calc1.advance_to_year(year)
    reform = {
        'SS_Earnings_c': {year: 9e99},  # OASDI FICA tax on all earnings
        'FICA_ss_trt': {year: 0.107484}  # lower rate to keep revenue unchanged
    }
    pol.implement_reform(reform)
    calc2 = Calculator(policy=pol, records=rec)
    calc2.advance_to_year(year)
    assert calc1.current_year == calc2.current_year
    calc1.calc_all()
    calc2.calc_all()
    fig = calc1.decile_graph(calc2)
    assert fig
    dt1, dt2 = calc1.distribution_tables(calc2, 'weighted_deciles')
    dta = dec_graph_data(dt1, dt2, year,
                         include_zero_incomes=True,
                         include_negative_incomes=False)
    assert isinstance(dta, dict)
    dta = dec_graph_data(dt1, dt2, year,
                         include_zero_incomes=False,
                         include_negative_incomes=True)
    assert isinstance(dta, dict)
    dta = dec_graph_data(dt1, dt2, year,
                         include_zero_incomes=False,
                         include_negative_incomes=False)
    assert isinstance(dta, dict)
def test_dist_table_sum_row(cps_subsample):
    rec = Records.cps_constructor(data=cps_subsample)
    calc = Calculator(policy=Policy(), records=rec)
    calc.calc_all()
    # create three distribution tables and compare the ALL row contents
    tb1, _ = calc.distribution_tables(None, 'standard_income_bins')
    tb2, _ = calc.distribution_tables(None, 'soi_agi_bins')
    tb3, _ = calc.distribution_tables(None, 'weighted_deciles')
    tb4, _ = calc.distribution_tables(None, 'weighted_deciles',
                                      pop_quantiles=True)
    assert np.allclose(tb1.loc['ALL'], tb2.loc['ALL'])
    assert np.allclose(tb1.loc['ALL'], tb3.loc['ALL'])
    # make sure population count is larger than filing-unit count
    assert tb4.at['ALL', 'count'] > tb1.at['ALL', 'count']
    # make sure population table has same ALL row values as filing-unit table
    for col in ['count', 'count_StandardDed', 'count_ItemDed', 'count_AMT']:
        tb4.at['ALL', col] = tb1.at['ALL', col]
    assert np.allclose(tb1.loc['ALL'], tb4.loc['ALL'])
    # make sure population table has same ALL tax liabilities as diagnostic tbl
    dgt = calc.diagnostic_table(1)
    assert np.allclose([tb4.at['ALL', 'iitax'],
                        tb4.at['ALL', 'payrolltax']],
                       [dgt.at['Ind Income Tax ($b)', calc.current_year],
                        dgt.at['Payroll Taxes ($b)', calc.current_year]])
Esempio n. 8
0
def test_create_tables(cps_subsample):
    # pylint: disable=too-many-statements,too-many-branches
    # create a current-law Policy object and Calculator object calc1
    rec = Records.cps_constructor(data=cps_subsample)
    pol = Policy()
    calc1 = Calculator(policy=pol, records=rec)
    calc1.calc_all()
    # create a policy-reform Policy object and Calculator object calc2
    reform = {2013: {'_II_rt1': [0.15]}}
    pol.implement_reform(reform)
    calc2 = Calculator(policy=pol, records=rec)
    calc2.calc_all()

    test_failure = False

    # test creating various difference tables

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'standard_income_bins', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, np.nan, -0.16, -0.57, -0.72, -0.69, -0.82, -0.80, -0.75, -0.65,
        -0.23, -0.09, -0.06, -0.59
    ]
    if not np.allclose(
            diff[tabcol].values, expected, atol=0.005, rtol=0.0,
            equal_nan=True):
        test_failure = True
        print('diff xbin', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'weighted_deciles', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'tot_change'
    expected = [
        0, 0, 1219678, 15503037, 25922077, 35000592, 48336897, 62637728,
        79750078, 93136108, 116996252, 102458801, 580961247, 63156380,
        33664610, 5637811
    ]
    if not np.allclose(diff[tabcol].values, expected, atol=0.51, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'share_of_change'
    expected = [
        0.00, 0.00, 0.21, 2.67, 4.46, 6.02, 8.32, 10.78, 13.73, 16.03, 20.14,
        17.64, 100.00, 10.87, 5.79, 0.97
    ]
    if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, np.nan, -0.15, -0.62, -0.70, -0.73, -0.78, -0.80, -0.80, -0.74,
        -0.71, -0.30, -0.59, -0.55, -0.25, -0.06
    ]
    if not np.allclose(
            diff[tabcol].values, expected, atol=0.005, rtol=0.0,
            equal_nan=True):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, np.nan, -0.15, -0.62, -0.70, -0.73, -0.78, -0.80, -0.80, -0.74,
        -0.71, -0.30, -0.59, -0.55, -0.25, -0.06
    ]
    if not np.allclose(
            diff[tabcol].values, expected, atol=0.005, rtol=0.0,
            equal_nan=True):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    # test creating various distribution tables

    dist, _ = calc2.distribution_tables(None, 'weighted_deciles')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [
        0, 0, -53644343, -65258622, -57617119, 37391333, 200879230, 329784586,
        553827330, 1015854407, 1731283600, 7090603505, 10783103907, 1638192777,
        2213960052, 3238450675
    ]
    if not np.allclose(dist[tabcol].values, expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'num_returns_ItemDed'
    expected = [
        0, 0, 2561, 12610, 21936, 29172, 50890, 61563, 78247, 91823, 118523,
        128886, 596211, 63986, 51634, 13266
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'expanded_income'
    expected = [
        0, 0, 835224673, 2639667638, 3940559051, 5286856071, 6972849344,
        8881099529, 11467767759, 14761195525, 19832126806, 44213000235,
        118830346631, 14399218059, 16868648076, 12945134101
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'aftertax_income'
    expected = [
        0, 0, 818813684, 2466000535, 3671150517, 4790979126, 6173998985,
        7754183496, 9907604744, 12510477225, 16273592612, 33915377411,
        98282178334, 11345456373, 13400757263, 9169163776
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    dist, _ = calc2.distribution_tables(None, 'standard_income_bins')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [
        0, 0, -42244205, -76727831, -62581860, 53797887, 217016689, 723516183,
        1108097059, 3272479928, 2818979541, 950296405, 1820474110, 10783103907
    ]
    if not np.allclose(dist[tabcol], expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'num_returns_ItemDed'
    expected = [
        0, 0, 1202, 13614, 27272, 34407, 48265, 117225, 103319, 181885, 61014,
        5126, 2882, 596211
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    if test_failure:
        assert 1 == 2
Esempio n. 9
0
def test_create_distribution_tables(pit_fullsample, cit_crosssample):
    # pylint: disable=too-many-statements,too-many-branches
    # create a current-law Policy object and Calculator object calc1
    rec = Records(data=pit_fullsample)
    crec = CorpRecords(data=cit_crosssample)
    pol = Policy()
    calc1 = Calculator(policy=pol, records=rec, corprecords=crec)
    calc1.calc_all()
    # create a policy-reform Policy object and Calculator object calc2
    reform = {2017: {'_rate2': [0.06]}}
    pol.implement_reform(reform)
    calc2 = Calculator(policy=pol, records=rec, corprecords=crec)
    calc2.calc_all()

    test_failure = False

    # test creating various distribution tables

    dist, _ = calc2.distribution_tables(None, 'weighted_deciles')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'pitax'
    expected = [
        0.000, 0.000, 0.000, 0.000, 0.000, 1.962, 5.711, 14.602, 45.503,
        163.177, 397.795, 1018.520, 1647.270, 331.218, 384.399, 302.903
    ]
    if not np.allclose(dist[tabcol].values, expected):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.3f},'.format(val))

    tabcol = 'GTI'
    expected = [
        0.000, 0.000, 688.359, 893.687, 1107.005, 1332.670, 1605.580, 1824.545,
        2327.660, 2818.092, 3848.954, 6071.569, 22518.121, 2490.655, 2119.235,
        1461.678
    ]
    if not np.allclose(dist[tabcol].tolist(), expected):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.3f},'.format(val))

    dist, _ = calc2.distribution_tables(None, 'standard_income_bins')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'pitax'
    expected = [
        0.000, 0.000, 8.334, 279.113, 542.762, 401.310, 415.751, 0.000, 0.000,
        0.000, 0.000, 1647.270
    ]
    if not np.allclose(dist[tabcol], expected):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.3f},'.format(val))

    tabcol = 'GTI'
    expected = [
        0.000, 0.000, 5884.790, 7399.792, 4810.526, 2392.643, 2030.370, 0.000,
        0.000, 0.000, 0.000, 22518.121
    ]
    if not np.allclose(dist[tabcol].tolist(), expected):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.3f},'.format(val))
    """
Esempio n. 10
0
def xtest_create_tables(pit_subsample):
    # pylint: disable=too-many-statements,too-many-branches
    # create a current-law Policy object and Calculator object calc1
    rec = Records(data=pit_subsample)
    pol = Policy()
    calc1 = Calculator(policy=pol, records=rec)
    calc1.calc_all()
    # create a policy-reform Policy object and Calculator object calc2
    reform = {2017: {'_rate2': [0.06]}}
    pol.implement_reform(reform)
    calc2 = Calculator(policy=pol, records=rec)
    calc2.calc_all()

    test_failure = False

    # test creating various difference tables

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'standard_income_bins', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, np.nan, -0.24, -0.79, -0.81, -0.55, -0.77, -0.69, -0.70, -0.67,
        -0.27, -0.11, -0.06, -0.58
    ]
    if not np.allclose(
            diff[tabcol].values, expected, atol=0.005, rtol=0.0,
            equal_nan=True):
        test_failure = True
        print('diff xbin', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'weighted_deciles', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'tot_change'
    expected = [
        0, 0, 254095629, 2544151690, 2826173357, 2539574809, 4426339426,
        5178198524, 6277367974, 8069273960, 10572653961, 10269542170,
        52957371499, 6188055374, 3497187245, 584299551
    ]
    if not np.allclose(diff[tabcol].values, expected, atol=0.51, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'share_of_change'
    expected = [
        0.00, 0.00, 0.48, 4.80, 5.34, 4.80, 8.36, 9.78, 11.85, 15.24, 19.96,
        19.39, 100.00, 11.68, 6.60, 1.10
    ]
    if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, np.nan, -0.26, -0.96, -0.74, -0.52, -0.75, -0.71, -0.68, -0.71,
        -0.71, -0.34, -0.58, -0.61, -0.30, -0.07
    ]
    if not np.allclose(
            diff[tabcol].values, expected, atol=0.005, rtol=0.0,
            equal_nan=True):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    # test creating various distribution tables

    dist, _ = calc2.distribution_tables(None, 'weighted_deciles')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [
        0, 0, -1818680093, 1971755805, 5010676892, 6746034269, 17979713134,
        26281107130, 35678824858, 82705314943, 148818900147, 734130905355,
        1057504552440, 152370476198, 234667184101, 347093245055
    ]
    if not np.allclose(dist[tabcol].values, expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'num_returns_ItemDed'
    expected = [
        0, 0, 357019, 1523252, 2463769, 2571339, 4513934, 5278763, 6299826,
        7713038, 11001450, 13125085, 54847474, 6188702, 5498415, 1437967
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'expanded_income'
    expected = [
        0, 0, 105927980655, 294023675202, 417068113194, 528376852001,
        658853731628, 818158430558, 1037838578149, 1324689584778,
        1788101751565, 4047642990187, 11020681687917, 1286581399758,
        1511884268254, 1249177322176
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'aftertax_income'
    expected = [
        0, 0, 97735000432, 261911925318, 378049091828, 485619641327,
        586208937799, 722979233740, 919208533243, 1130705156084, 1473967098213,
        2994484618211, 9050869236196, 1002450732282, 1147596725957,
        844437159972
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    dist, _ = calc2.distribution_tables(None, 'standard_income_bins')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [
        0, 0, -544908512, -140959365, 3354006293, 9339571323, 18473567840,
        55206201916, 89276157367, 297973932010, 290155554334, 99528466942,
        194882962290, 1057504552440
    ]
    if not np.allclose(dist[tabcol], expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'num_returns_ItemDed'
    expected = [
        0, 0, 60455, 1107780, 2366845, 3460607, 4837397, 10090391, 8850784,
        17041135, 6187168, 532925, 311987, 54847474
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    if test_failure:
        assert 1 == 2
def test_create_tables(cps_subsample):
    # pylint: disable=too-many-statements,too-many-branches
    # create a current-law Policy object and Calculator object calc1
    rec = Records.cps_constructor(data=cps_subsample)
    pol = Policy()
    calc1 = Calculator(policy=pol, records=rec)
    calc1.calc_all()
    # create a policy-reform Policy object and Calculator object calc2
    reform = {'II_rt1': {2013: 0.15}}
    pol.implement_reform(reform)
    calc2 = Calculator(policy=pol, records=rec)
    calc2.calc_all()

    test_failure = False

    # test creating various difference tables

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'standard_income_bins', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'pc_aftertaxinc'
    expected = [np.nan,
                np.nan,
                -0.2,
                -0.8,
                -0.8,
                -0.5,
                -0.8,
                -0.7,
                -0.7,
                -0.7,
                -0.3,
                -0.1,
                -0.1,
                -0.6]
    if not np.allclose(diff[tabcol].values, expected,
                       atol=0.1, rtol=0.0, equal_nan=True):
        test_failure = True
        print('diff xbin', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'weighted_deciles', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'tot_change'
    expected = [0.0,
                0.0,
                0.3,
                2.5,
                2.8,
                2.5,
                4.4,
                5.2,
                6.3,
                8.1,
                10.6,
                10.3,
                53.0,
                6.2,
                3.5,
                0.6]
    if not np.allclose(diff[tabcol].values, expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'share_of_change'
    expected = [0.0,
                0.0,
                0.5,
                4.8,
                5.3,
                4.8,
                8.3,
                9.9,
                11.8,
                15.2,
                19.9,
                19.4,
                100.0,
                11.7,
                6.6,
                1.1]
    if not np.allclose(diff[tabcol].values, expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'pc_aftertaxinc'
    expected = [np.nan,
                np.nan,
                -0.3,
                -1.0,
                -0.7,
                -0.5,
                -0.7,
                -0.7,
                -0.7,
                -0.7,
                -0.7,
                -0.3,
                -0.6,
                -0.6,
                -0.3,
                -0.1]
    if not np.allclose(diff[tabcol].values, expected,
                       atol=0.1, rtol=0.0, equal_nan=True):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    # test creating various distribution tables

    dist, _ = calc2.distribution_tables(None, 'weighted_deciles')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [0.0,
                0.0,
                -1.8,
                2.0,
                5.0,
                6.7,
                18.0,
                26.1,
                35.2,
                82.7,
                148.8,
                734.1,
                1056.9,
                152.4,
                234.7,
                347.1]
    if not np.allclose(dist[tabcol].values, expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'count_ItemDed'
    expected = [0.0,
                0.0,
                0.4,
                1.5,
                2.5,
                2.6,
                4.5,
                5.3,
                6.3,
                7.7,
                11.0,
                13.1,
                54.9,
                6.2,
                5.5,
                1.4]
    if not np.allclose(dist[tabcol].tolist(), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'expanded_income'
    expected = [0.0,
                0.0,
                105.9,
                294.0,
                417.1,
                528.4,
                658.9,
                818.2,
                1037.8,
                1324.7,
                1788.1,
                4047.6,
                11020.7,
                1286.6,
                1511.9,
                1249.2]
    if not np.allclose(dist[tabcol].tolist(), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'aftertax_income'
    expected = [0.0,
                0.0,
                97.7,
                261.9,
                378.1,
                485.6,
                586.2,
                723.2,
                919.6,
                1130.7,
                1474.0,
                2994.5,
                9051.5,
                1002.5,
                1147.6,
                844.4]
    if not np.allclose(dist[tabcol].tolist(), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    dist, _ = calc2.distribution_tables(None, 'standard_income_bins')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [0.0,
                0.0,
                -0.5,
                -0.1,
                3.4,
                9.3,
                18.5,
                54.6,
                89.3,
                298.0,
                290.2,
                99.5,
                194.9,
                1056.9]
    if not np.allclose(dist[tabcol], expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'count_ItemDed'
    expected = [0.0,
                0.0,
                0.1,
                1.1,
                2.4,
                3.5,
                4.8,
                10.1,
                8.8,
                17.0,
                6.2,
                0.5,
                0.3,
                54.9]
    if not np.allclose(dist[tabcol].tolist(), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    if test_failure:
        assert 1 == 2
Esempio n. 12
0
def test_create_tables(cps_subsample):
    # pylint: disable=too-many-statements,too-many-branches
    # create a current-law Policy object and Calculator object calc1
    rec = Records.cps_constructor(data=cps_subsample)
    pol = Policy()
    calc1 = Calculator(policy=pol, records=rec)
    calc1.calc_all()
    # create a policy-reform Policy object and Calculator object calc2
    reform = {'II_rt1': {2013: 0.15}}
    pol.implement_reform(reform)
    calc2 = Calculator(policy=pol, records=rec)
    calc2.calc_all()

    test_failure = False

    # test creating various difference tables

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'standard_income_bins', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, np.nan, -0.2, -0.8, -0.8, -0.5, -0.8, -0.7, -0.7, -0.7, -0.3,
        -0.1, -0.1, -0.6
    ]
    if not np.allclose(
            diff[tabcol].values, expected, atol=0.1, rtol=0.0, equal_nan=True):
        test_failure = True
        print('diff xbin', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'weighted_deciles', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'tot_change'
    expected = [
        0.0, 0.0, 0.3, 2.5, 2.8, 2.5, 4.4, 5.2, 6.3, 8.1, 10.6, 10.3, 53.0,
        6.2, 3.5, 0.6
    ]
    if not np.allclose(diff[tabcol].values, expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'share_of_change'
    expected = [
        0.0, 0.0, 0.5, 4.8, 5.3, 4.8, 8.3, 9.9, 11.8, 15.2, 19.9, 19.4, 100.0,
        11.7, 6.6, 1.1
    ]
    if not np.allclose(diff[tabcol].values, expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, np.nan, -0.3, -1.0, -0.7, -0.5, -0.7, -0.7, -0.7, -0.7, -0.7,
        -0.3, -0.6, -0.6, -0.3, -0.1
    ]
    if not np.allclose(
            diff[tabcol].values, expected, atol=0.1, rtol=0.0, equal_nan=True):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    # test creating various distribution tables

    dist, _ = calc2.distribution_tables(None, 'weighted_deciles')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [
        0.0, 0.0, -1.8, 2.0, 5.0, 6.7, 18.0, 26.1, 35.2, 82.7, 148.8, 734.1,
        1056.9, 152.4, 234.7, 347.1
    ]
    if not np.allclose(dist[tabcol].values, expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'count_ItemDed'
    expected = [
        0.0, 0.0, 0.4, 1.5, 2.5, 2.6, 4.5, 5.3, 6.3, 7.7, 11.0, 13.1, 54.9,
        6.2, 5.5, 1.4
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'expanded_income'
    expected = [
        0.0, 0.0, 105.9, 294.0, 417.1, 528.4, 658.9, 818.2, 1037.8, 1324.7,
        1788.1, 4047.6, 11020.7, 1286.6, 1511.9, 1249.2
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'aftertax_income'
    expected = [
        0.0, 0.0, 97.7, 261.9, 378.1, 485.6, 586.2, 723.2, 919.6, 1130.7,
        1474.0, 2994.5, 9051.5, 1002.5, 1147.6, 844.4
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    dist, _ = calc2.distribution_tables(None, 'standard_income_bins')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [
        0.0, 0.0, -0.5, -0.1, 3.4, 9.3, 18.5, 54.6, 89.3, 298.0, 290.2, 99.5,
        194.9, 1056.9
    ]
    if not np.allclose(dist[tabcol], expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'count_ItemDed'
    expected = [
        0.0, 0.0, 0.1, 1.1, 2.4, 3.5, 4.8, 10.1, 8.8, 17.0, 6.2, 0.5, 0.3, 54.9
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    if test_failure:
        assert 1 == 2
Esempio n. 13
0
def test_create_tables(cps_subsample):
    # pylint: disable=too-many-statements,too-many-branches
    # create a current-law Policy object and Calculator object calc1
    rec = Records.cps_constructor(data=cps_subsample)
    pol = Policy()
    calc1 = Calculator(policy=pol, records=rec)
    calc1.calc_all()
    # create a policy-reform Policy object and Calculator object calc2
    reform = {2013: {'_II_rt1': [0.15]}}
    pol.implement_reform(reform)
    calc2 = Calculator(policy=pol, records=rec)
    calc2.calc_all()

    test_failure = False

    # test creating various difference tables

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'standard_income_bins', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, np.nan, -0.22, -0.77, -0.80, -0.56, -0.77, -0.69, -0.71, -0.67,
        -0.27, -0.11, -0.06, -0.58
    ]
    if not np.allclose(
            diff[tabcol].values, expected, atol=0.005, rtol=0.0,
            equal_nan=True):
        test_failure = True
        print('diff xbin', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'weighted_deciles', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'tot_change'
    expected = [
        0, 0, 241418460, 2474292614, 2770584237, 2535721686, 4444363117,
        5111483934, 6321945100, 8225913647, 10597074824, 10234573879,
        52957371499, 6137031947, 3513242382, 584299551
    ]
    if not np.allclose(diff[tabcol].values, expected, atol=0.51, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'share_of_change'
    expected = [
        0.00, 0.00, 0.46, 4.67, 5.23, 4.79, 8.39, 9.65, 11.94, 15.53, 20.01,
        19.33, 100.00, 11.59, 6.63, 1.10
    ]
    if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, np.nan, -0.25, -0.95, -0.73, -0.53, -0.75, -0.71, -0.68, -0.72,
        -0.71, -0.34, -0.58, -0.61, -0.30, -0.07
    ]
    if not np.allclose(
            diff[tabcol].values, expected, atol=0.005, rtol=0.0,
            equal_nan=True):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, np.nan, -0.25, -0.95, -0.73, -0.53, -0.75, -0.71, -0.68, -0.72,
        -0.71, -0.34, -0.58, -0.61, -0.30, -0.07
    ]
    if not np.allclose(
            diff[tabcol].values, expected, atol=0.005, rtol=0.0,
            equal_nan=True):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.2f},'.format(val))

    # test creating various distribution tables

    dist, _ = calc2.distribution_tables(None, 'weighted_deciles')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [
        0, 0, -1962728575, 1725493747, 4396953820, 6605728718, 16774691083,
        23860454276, 38350836962, 83963523110, 150930070726, 732859528574,
        1057504552440, 151607017873, 234865455600, 346387055100
    ]
    if not np.allclose(dist[tabcol].values, expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'num_returns_ItemDed'
    expected = [
        0, 0, 357019, 1448655, 2559613, 2513429, 4419624, 5275374, 6222375,
        7880642, 11147728, 13023015, 54847474, 6118072, 5478575, 1426368
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'expanded_income'
    expected = [
        0, 0, 105133510325, 290616204980, 413576297349, 517828725223,
        659857915773, 803218163892, 1042123266101, 1326558509787,
        1805622773921, 4048576203396, 11013111570748, 1281956155093,
        1515893182747, 1250726865556
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'aftertax_income'
    expected = [
        0, 0, 97194414402, 259232286042, 375389146785, 475719938941,
        588002960320, 710495874184, 921012587826, 1129166044052, 1488817328688,
        2998268537784, 9043299119027, 999335257953, 1151930187406, 847003092425
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    dist, _ = calc2.distribution_tables(None, 'standard_income_bins')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [
        0, 0, -544908512, -27720182, 2473153905, 9469043966, 17806661306,
        56292468689, 88558244888, 298427035609, 290639143539, 99528466942,
        194882962290, 1057504552440
    ]
    if not np.allclose(dist[tabcol], expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    tabcol = 'num_returns_ItemDed'
    expected = [
        0, 0, 60455, 1161281, 2323042, 3613216, 4759193, 10006287, 8785946,
        17093586, 6199555, 532925, 311987, 54847474
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.0f},'.format(val))

    if test_failure:
        assert 1 == 2
Esempio n. 14
0
def test_create_tables(cps_subsample):
    # pylint: disable=too-many-statements,too-many-branches
    # create a current-law Policy object and Calculator object calc1
    rec = Records.cps_constructor(data=cps_subsample)
    pol = Policy()
    calc1 = Calculator(policy=pol, records=rec)
    calc1.calc_all()
    # create a policy-reform Policy object and Calculator object calc2
    reform = {'II_rt1': {2013: 0.15}}
    pol.implement_reform(reform)
    calc2 = Calculator(policy=pol, records=rec)
    calc2.calc_all()

    test_failure = False

    # test creating various difference tables

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'standard_income_bins', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'pc_aftertaxinc'
    expected = [0.0,
                np.nan,
                -0.1,
                -0.5,
                -0.7,
                -0.7,
                -0.8,
                -0.7,
                -0.7,
                -0.7,
                -0.3,
                -0.1,
                -0.0,
                -0.6]
    if not np.allclose(diff[tabcol].values.astype('float'), expected,
                       atol=0.1, rtol=0.0, equal_nan=True):
        test_failure = True
        print('diff xbin', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'weighted_deciles', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'tot_change'
    expected = [0.0,
                0.0,
                0.0,
                0.6,
                2.9,
                3.5,
                4.4,
                6.1,
                6.5,
                8.7,
                12.0,
                13.3,
                58.0,
                7.7,
                4.8,
                0.8]
    if not np.allclose(diff[tabcol].values.astype('float'), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'share_of_change'
    expected = [0.0,
                0.0,
                0.0,
                1.0,
                5.0,
                6.0,
                7.6,
                10.6,
                11.1,
                15.1,
                20.7,
                22.9,
                100.0,
                13.2,
                8.3,
                1.4,]
    if not np.allclose(diff[tabcol].values.astype('float'), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'pc_aftertaxinc'
    expected = [np.nan,
                0.0,
                -0.0,
                -0.3,
                -0.8,
                -0.7,
                -0.7,
                -0.8,
                -0.7,
                -0.7,
                -0.7,
                -0.3,
                -0.6,
                -0.7,
                -0.4,
                -0.1]
    if not np.allclose(diff[tabcol].values.astype('float'), expected,
                       atol=0.1, rtol=0.0, equal_nan=True):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    # test creating various distribution tables

    dist, _ = calc2.distribution_tables(None, 'weighted_deciles')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [0.0,
                0.0,
                -0.4,
                -4.1,
                -5.9,
                8.0,
                16.9,
                29.0,
                27.0,
                71.4,
                153.4,
                910.1,
                1205.5,
                159.4,
                268.1,
                482.7]
    if not np.allclose(dist[tabcol].values.astype('float'), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'count_ItemDed'
    expected = [0.0,
                0.0,
                0.0,
                1.1,
                2.6,
                3.9,
                4.7,
                6.3,
                6.5,
                7.4,
                11.3,
                16.3,
                60.3,
                7.4,
                7.2,
                1.7]
    if not np.allclose(dist[tabcol].tolist(), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'expanded_income'
    expected = [0.0,
                -1.4,
                30.7,
                209.8,
                388.8,
                541.2,
                679.1,
                847.6,
                1097.1,
                1430.7,
                1978.3,
                5007.6,
                12209.4,
                1410.9,
                1765.5,
                1831.2]
    if not np.allclose(dist[tabcol].tolist(), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'aftertax_income'
    expected = [0.0,
                -1.4,
                29.0,
                195.5,
                363.0,
                491.0,
                612.2,
                747.1,
                980.6,
                1248.0,
                1630.2,
                3741.3,
                10036.6,
                1100.9,
                1339.0,
                1301.4]
    if not np.allclose(dist[tabcol].tolist(), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    dist, _ = calc2.distribution_tables(None, 'standard_income_bins')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [0.0,
                0.0,
                -1.3,
                -7.6,
                -1.2,
                20.7,
                26.3,
                47.2,
                95.5,
                321.9,
                324.0,
                64.8,
                315.2,
                1205.5]
    if not np.allclose(dist[tabcol].values.astype('float'), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'count_ItemDed'
    expected = [0.0,
                0.0,
                0.2,
                1.8,
                3.6,
                5.9,
                5.7,
                10.2,
                8.1,
                17.7,
                6.7,
                0.3,
                0.1,
                60.3]
    if not np.allclose(dist[tabcol].tolist(), expected,
                       atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    if test_failure:
        assert 1 == 2
def test_create_tables(cps_subsample):
    # pylint: disable=too-many-statements,too-many-branches
    # create a current-law Policy object and Calculator object calc1
    rec = Records.cps_constructor(data=cps_subsample)
    pol = Policy()
    calc1 = Calculator(policy=pol, records=rec)
    calc1.calc_all()
    # create a policy-reform Policy object and Calculator object calc2
    reform = {'II_rt1': {2013: 0.15}}
    pol.implement_reform(reform)
    calc2 = Calculator(policy=pol, records=rec)
    calc2.calc_all()

    test_failure = False

    # test creating various difference tables

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'standard_income_bins', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'pc_aftertaxinc'
    expected = [
        0.0, np.nan, -0.1, -0.5, -0.7, -0.7, -0.8, -0.7, -0.7, -0.7, -0.3,
        -0.1, -0.0, -0.6
    ]
    if not np.allclose(diff[tabcol].values.astype('float'),
                       expected,
                       atol=0.1,
                       rtol=0.0,
                       equal_nan=True):
        test_failure = True
        print('diff xbin', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES),
                                   calc2.dataframe(DIFF_VARIABLES),
                                   'weighted_deciles', 'combined')
    assert isinstance(diff, pd.DataFrame)
    tabcol = 'tot_change'
    expected = [
        0.0, 0.0, 0.0, 0.6, 2.7, 3.4, 4.2, 5.8, 6.3, 8.1, 11.5, 12.7, 55.2,
        7.2, 4.7, 0.8
    ]
    if not np.allclose(
            diff[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'share_of_change'
    expected = [
        0.0, 0.0, 0.0, 1.0, 4.9, 6.1, 7.6, 10.5, 11.4, 14.7, 20.7, 23.0, 100.0,
        13.0, 8.5, 1.5
    ]
    if not np.allclose(
            diff[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'pc_aftertaxinc'
    expected = [
        np.nan, 0.0, -0.0, -0.3, -0.8, -0.7, -0.7, -0.8, -0.7, -0.7, -0.7,
        -0.3, -0.6, -0.7, -0.4, -0.1
    ]
    if not np.allclose(diff[tabcol].values.astype('float'),
                       expected,
                       atol=0.1,
                       rtol=0.0,
                       equal_nan=True):
        test_failure = True
        print('diff xdec', tabcol)
        for val in diff[tabcol].values:
            print('{:.1f},'.format(val))

    # test creating various distribution tables

    dist, _ = calc2.distribution_tables(None, 'weighted_deciles')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [
        0.0, 0.0, -0.2, -3.5, -5.8, 9.0, 16.6, 28.6, 30.3, 70.2, 153.0, 893.7,
        1191.8, 145.6, 269.7, 478.4
    ]
    if not np.allclose(
            dist[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'count_ItemDed'
    expected = [
        0.0, 0.0, 0.0, 1.0, 2.9, 4.2, 4.5, 6.0, 6.0, 7.5, 11.3, 15.6, 59.0,
        6.7, 7.2, 1.8
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'expanded_income'
    expected = [
        0.0, -1.4, 28.1, 201.5, 377.6, 536.2, 662.2, 841.1, 1053.0, 1400.7,
        1923.1, 4956.7, 11978.6, 1374.6, 1743.5, 1838.6
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'aftertax_income'
    expected = [
        0.0, -1.4, 26.3, 187.5, 353.1, 483.4, 596.6, 743.0, 934.6, 1221.8,
        1579.5, 3721.8, 9846.3, 1096.0, 1317.8, 1308.0
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xdec', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    dist, _ = calc2.distribution_tables(None, 'standard_income_bins')
    assert isinstance(dist, pd.DataFrame)
    tabcol = 'iitax'
    expected = [
        0.0, 0.0, -1.2, -7.0, 0.2, 23.4, 26.9, 52.7, 95.5, 305.5, 321.1, 58.6,
        316.2, 1191.8
    ]
    if not np.allclose(
            dist[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    tabcol = 'count_ItemDed'
    expected = [
        0.0, 0.0, 0.2, 2.1, 3.5, 6.4, 5.8, 9.9, 7.8, 16.4, 6.5, 0.4, 0.1, 59.0
    ]
    if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0):
        test_failure = True
        print('dist xbin', tabcol)
        for val in dist[tabcol].values:
            print('{:.1f},'.format(val))

    if test_failure:
        assert 1 == 2