Recipe 5: Redefining Expanded IncomeΒΆ

This is an advanced recipe that should be followed only after mastering the basic recipe. This recipe is almost exactly the same as Directly Comparing Two Reforms, so you might want to read that recipe first.

This recipe introduces a powerful technique for customizing the operation of Tax-Calculator. This calculator-customization technique is used in this recipe to redefine expanded income in a way that allows the redefined income measure to be used seamlessly with all the other (table and graph) methods of the Calculator class. The basic idea behind the calculator-customization technique is to derive a customized Calculator class from the Tax-Calculator Calculator class. This is a standard object-oriented programming technique.

Recipe that illustrates how to customize the taxcalc Calculator class so that it can seamlessly use an alternative definition of expanded income.

The technique for doing this customization is standard in object-oriented programming: a child class is derived from a parent class and then customized. The derived child class inherits all the data and methods of the parent class, but can be customized by adding new data and methods or by overriding inherited methods.

import pandas as pd
import taxcalc as tc


# override the ExpandIncome calcfunction that computes "market income"


@tc.iterate_jit(nopython=True)
def ExpandIncome(e00200, pencon_p, pencon_s, e00300, e00400, e00600,
                 e00700, e00800, e00900, e01100, e01200, e01400, e01500,
                 e02000, e02100, p22250, p23250, cmbtp, ptax_was,
                 expanded_income):
    """
    Calculates expanded_income as "market income" from component income types.
    """
    expanded_income = (
        e00200 +  # wage and salary income net of DC pension contributions
        pencon_p +  # tax-advantaged DC pension contributions for taxpayer
        pencon_s +  # tax-advantaged DC pension contributions for spouse
        e00300 +  # taxable interest income
        e00400 +  # non-taxable interest income
        e00600 +  # dividends
        e00700 +  # state and local income tax refunds
        e00800 +  # alimony received
        e00900 +  # Sch C business net income/loss
        e01100 +  # capital gain distributions not reported on Sch D
        e01200 +  # Form 4797 other net gain/loss
        e01400 +  # taxable IRA distributions
        e01500 +  # total pension & annuity income (including DB-plan benefits)
        e02000 +  # Sch E total rental, ..., partnership, S-corp income/loss
        e02100 +  # Sch F farm net income/loss
        p22250 +  # Sch D: net short-term capital gain/loss
        p23250 +  # Sch D: net long-term capital gain/loss
        cmbtp +  # other AMT taxable income items from Form 6251
        0.5 * ptax_was  # employer share of FICA taxes on wages/salaries
        # excluding:
        # ubi +  # total UBI benefit
        # benefit_value_total  # consumption value of all benefits received;
    )
    return expanded_income

# end overrided calcfunction used by customized Calculator class


class Calculator(tc.Calculator):
    """
    Customized Calculator class that inherits all tc.Calculator data and
    methods, overriding one method to get the desired customization.
    """
    def __init__(self, policy=None, records=None, verbose=False,
                 sync_years=True, consumption=None):
        # use same class constructor arguments as tc.Calculator class
        super().__init__(policy=policy, records=records,
                         verbose=verbose, sync_years=sync_years,
                         consumption=consumption)

    def calc_all(self, zero_out_calc_vars=False):
        """
        Call all tax-calculation functions for the current_year.
        """
        tc.BenefitPrograms(self)
        self._calc_one_year(zero_out_calc_vars)
        tc.BenefitSurtax(self)
        tc.BenefitLimitation(self)
        tc.FairShareTax(self.__policy, self.__records)
        tc.LumpSumTax(self.__policy, self.__records)
        ExpandIncome(self.__policy, self.__records)  # customized (see above)
        tc.AfterTaxIncome(self.__policy, self.__records)

# end of customized Calculator class definition


# top-level logic of program that uses customized Calculator class

# read an "old" reform file
# ("old" means the reform file is defined relative to pre-TCJA policy)
# specify reform dictionary for pre-TCJA policy
reform1 = tc.Policy.read_json_reform('github://PSLmodels:[email protected]/psl_examples/taxcalc/2017_law.json')

# specify reform dictionary for TCJA as passed by Congress in late 2017
reform2 = tc.Policy.read_json_reform('github://PSLmodels:[email protected]/psl_examples/taxcalc/TCJA.json')

# specify Policy object for pre-TCJA policy
bpolicy = tc.Policy()
bpolicy.implement_reform(reform1, print_warnings=False, raise_errors=False)
assert not bpolicy.parameter_errors

# specify Policy object for TCJA reform relative to pre-TCJA policy
rpolicy = tc.Policy()
rpolicy.implement_reform(reform1, print_warnings=False, raise_errors=False)
assert not rpolicy.parameter_errors
rpolicy.implement_reform(reform2, print_warnings=False, raise_errors=False)
assert not rpolicy.parameter_errors

# specify customized Calculator objects using bpolicy and rpolicy
recs = tc.Records.cps_constructor()
calc1 = Calculator(policy=bpolicy, records=recs)
calc2 = Calculator(policy=rpolicy, records=recs)

CYR = 2018

# calculate for specified CYR
calc1.advance_to_year(CYR)
calc1.calc_all()
calc2.advance_to_year(CYR)
calc2.calc_all()

# compare aggregate individual income tax revenue in CYR
iitax_rev1 = calc1.weighted_total('iitax')
iitax_rev2 = calc2.weighted_total('iitax')

# construct reform-vs-baseline difference table with results for income deciles
diff_table = calc1.difference_table(calc2, 'standard_income_bins', 'iitax')
assert isinstance(diff_table, pd.DataFrame)
diff_extract = pd.DataFrame()
dif_colnames = ['count', 'tax_cut', 'tax_inc',
                'tot_change', 'mean', 'pc_aftertaxinc']
ext_colnames = ['funits(#m)', 'taxfall(#m)', 'taxrise(#m)',
                'agg_diff($b)', 'mean_diff($)', 'aftertax_income_diff(%)']
for dname, ename in zip(dif_colnames, ext_colnames):
    diff_extract[ename] = diff_table[dname]

# print total revenue estimates for CYR
# (estimates in billons of dollars)
print('{}_REFORM1_iitax_rev($B)= {:.3f}'.format(CYR, iitax_rev1 * 1e-9))
print('{}_REFORM2_iitax_rev($B)= {:.3f}'.format(CYR, iitax_rev2 * 1e-9))
print('')

# print reform2-vs-reform1 difference table
title = 'Extract of {} income-tax difference table by expanded-income decile'
print(title.format(CYR))
print('(taxfall is count of funits with cut in income tax in reform 2 vs 1)')
print('(taxrise is count of funits with rise in income tax in reform 2 vs 1)')
print(diff_extract.to_string())
2018_REFORM1_iitax_rev($B)= 1361.859
2018_REFORM2_iitax_rev($B)= 1196.075

Extract of 2018 income-tax difference table by expanded-income decile
(taxfall is count of funits with cut in income tax in reform 2 vs 1)
(taxrise is count of funits with rise in income tax in reform 2 vs 1)
            funits(#m) taxfall(#m) taxrise(#m) agg_diff($b)  mean_diff($) aftertax_income_diff(%)
<$0K          0.129804         0.0         0.0          0.0           0.0                     0.0
=$0K         28.997572    0.003718    0.001407    -0.000259     -0.008926              -10.459826
$0-10K       32.861688     2.55197    1.399451    -0.201814     -6.141326                0.158635
$10-20K       21.68755   12.398236    3.606746    -1.691028    -77.972304                0.550945
$20-30K      18.705611   14.488926    2.698052    -4.134841   -221.048186                0.970103
$30-40K      16.861419   13.862656    2.451317    -7.486232   -443.985896                 1.47247
$40-50K      13.329158   11.319828     1.76968    -8.686528    -651.69367                1.737295
$50-75K      22.049099   19.613191     2.19565   -19.935975   -904.162779                1.821431
$75-100K      13.87195   12.584718    1.182932   -16.910393  -1219.035083                 1.77601
$100-200K    21.813959   19.737734    1.987578   -42.257864  -1937.193691                1.837309
$200-500K     7.482487    6.937591    0.541638   -41.071826  -5489.061086                2.652879
$500-1000K    0.967068    0.889137    0.077931   -12.340113 -12760.330566                2.715881
>$1000K       0.449118    0.372603    0.076515    -11.06684 -24641.257716                  1.9521
ALL         199.206484  114.760307   17.988898  -165.783714   -832.220471                1.888656