def __init__(self, input_filename, reform, exact_calculations, emulate_taxsim_2441_logic, output_records): """ SimpleTaxIO class constructor. """ # pylint: disable=too-many-arguments # check that input_filename is a string if not isinstance(input_filename, str): msg = 'SimpleTaxIO.ctor input_filename is not a string' raise ValueError(msg) # construct output_filename and delete old output file if it exists # ... construct reform extension to output_filename if reform is None: ref = '' self._using_reform_file = True else: # if reform is not None if isinstance(reform, str): if reform.endswith('.json'): ref = '-{}'.format(reform[:-5]) else: ref = '-{}'.format(reform) self._using_reform_file = True elif isinstance(reform, dict): ref = '' self._using_reform_file = False else: msg = 'SimpleTaxIO.ctor reform is neither None, str, nor dict' raise ValueError(msg) # ... construct whole output_filename self._using_input_file = True self._output_filename = '{}.out-simtax{}'.format(input_filename, ref) if os.path.isfile(self._output_filename): os.remove(self._output_filename) # pragma: no cover # check for existence of file named input_filename if not os.path.isfile(input_filename): msg = 'INPUT file named {} could not be found' raise ValueError(msg.format(input_filename)) # read input file contents into self._input dictionary self._read_input(input_filename) self.policy = Policy() # implement reform if reform is specified if reform: if self._using_reform_file: param_dict = Calculator.read_json_param_objects(reform, None) r_pol = param_dict['policy'] else: r_pol = reform self.policy.implement_reform(r_pol) # validate input variable values self._validate_input() self.calc = self._calc_object(exact_calculations, emulate_taxsim_2441_logic, output_records)
def reform_documentation(params, policy_dicts=None): """ Generate reform documentation. Parameters ---------- params: dict dictionary is structured like dict returned from the static Calculator method read_json_param_objects() policy_dicts : list of dict or None each dictionary in list is a params['policy'] dictionary representing second and subsequent elements of a compound reform; None implies no compound reform with the simple reform characterized in the params['policy'] dictionary Returns ------- doc: String the documentation for the policy reform specified in params """ # pylint: disable=too-many-statements,too-many-branches # nested function used only in reform_documentation def param_doc(years, change, base): """ Parameters ---------- years: list of change years change: dictionary of parameter changes base: Policy object with baseline values syear: parameter start assessment year Returns ------- doc: String """ # pylint: disable=too-many-locals # nested function used only in param_doc def lines(text, num_indent_spaces, max_line_length=77): """ Return list of text lines, each one of which is no longer than max_line_length, with the second and subsequent lines being indented by the number of specified num_indent_spaces; each line in the list ends with the '\n' character """ if len(text) < max_line_length: # all text fits on one line line = text + '\n' return [line] # all text does not fix on one line first_line = True line_list = list() words = text.split() while words: if first_line: line = '' first_line = False else: line = ' ' * num_indent_spaces while (words and (len(words[0]) + len(line)) < max_line_length): line += words.pop(0) + ' ' line = line[:-1] + '\n' line_list.append(line) return line_list # begin main logic of param_doc # pylint: disable=too-many-nested-blocks assert len(years) == len(change.keys()) assert isinstance(base, Policy) basex = copy.deepcopy(base) basevals = getattr(basex, '_vals', None) assert isinstance(basevals, dict) doc = '' for year in years: # write year basex.set_year(year) doc += '{}:\n'.format(year) # write info for each param in year for param in sorted(change[year].keys()): # ... write param:value line pval = change[year][param] if isinstance(pval, list): pval = pval[0] if basevals[param]['boolean_value']: if isinstance(pval, list): pval = [ True if item else False for item in pval ] else: pval = bool(pval) doc += ' {} : {}\n'.format(param, pval) # ... write optional param-index line if isinstance(pval, list): pval = basevals[param]['col_label'] pval = [str(item) for item in pval] doc += ' ' * (4 + len(param)) + '{}\n'.format(pval) # ... write name line if param.endswith('_cpi'): rootparam = param[:-4] name = '{} inflation indexing status'.format(rootparam) else: name = basevals[param]['long_name'] for line in lines('name: ' + name, 6): doc += ' ' + line # ... write optional desc line if not param.endswith('_cpi'): desc = basevals[param]['description'] for line in lines('desc: ' + desc, 6): doc += ' ' + line # ... write baseline_value line if param.endswith('_cpi'): rootparam = param[:-4] bval = basevals[rootparam].get('cpi_inflated', False) else: bval = getattr(basex, param[1:], None) if isinstance(bval, np.ndarray): bval = bval.tolist() if basevals[param]['boolean_value']: bval = [ True if item else False for item in bval ] elif basevals[param]['boolean_value']: bval = bool(bval) doc += ' baseline_value: {}\n'.format(bval) return doc # begin main logic of reform_documentation # create Policy object with pre-reform (i.e., baseline) values clp = Policy() # generate documentation text doc = 'REFORM DOCUMENTATION\n' doc += 'Policy Reform Parameter Values by Year:\n' years = sorted(params['policy'].keys()) if years: doc += param_doc(years, params['policy'], clp) else: doc += 'none: using current-law policy parameters\n' if policy_dicts is not None: assert isinstance(policy_dicts, list) base = clp base.implement_reform(params['policy']) assert not base.parameter_errors for policy_dict in policy_dicts: assert isinstance(policy_dict, dict) doc += 'Policy Reform Parameter Values by Year:\n' years = sorted(policy_dict.keys()) doc += param_doc(years, policy_dict, base) base.implement_reform(policy_dict) assert not base.parameter_errors return doc
def init(self, input_data, tax_year, reform, assump, growdiff_response, aging_input_data, exact_calculations): """ TaxCalcIO class post-constructor method that completes initialization. Parameters ---------- First four parameters are same as for TaxCalcIO constructor: input_data, tax_year, reform, assump. growdiff_response: Growdiff object or None growdiff_response Growdiff object is used only by the TaxCalcIO.growmodel_analysis method; must be None in all other cases. aging_input_data: boolean whether or not to extrapolate Records data from data year to tax_year. exact_calculations: boolean specifies whether or not exact tax calculations are done without any smoothing of "stair-step" provisions in the tax law. """ # pylint: disable=too-many-arguments,too-many-locals # pylint: disable=too-many-statements,too-many-branches self.errmsg = '' # get parameter dictionaries from --reform and --assump files paramdict = Calculator.read_json_param_objects(reform, assump) # create Behavior object beh = Behavior() beh.update_behavior(paramdict['behavior']) self.behavior_has_any_response = beh.has_any_response() # create gdiff_baseline object gdiff_baseline = Growdiff() gdiff_baseline.update_growdiff(paramdict['growdiff_baseline']) # create Growfactors clp object that incorporates gdiff_baseline gfactors_clp = Growfactors() gdiff_baseline.apply_to(gfactors_clp) # specify gdiff_response object if growdiff_response is None: gdiff_response = Growdiff() gdiff_response.update_growdiff(paramdict['growdiff_response']) elif isinstance(growdiff_response, Growdiff): gdiff_response = growdiff_response else: gdiff_response = None msg = 'TaxCalcIO.more_init: growdiff_response is neither None ' msg += 'nor a Growdiff object' self.errmsg += 'ERROR: {}\n'.format(msg) if gdiff_response is not None: some_gdiff_response = gdiff_response.has_any_response() if self.behavior_has_any_response and some_gdiff_response: msg = 'ASSUMP file cannot specify any "behavior" when using ' msg += 'GrowModel or when ASSUMP file has "growdiff_response"' self.errmsg += 'ERROR: {}\n'.format(msg) # create Growfactors ref object that has both gdiff objects applied gfactors_ref = Growfactors() gdiff_baseline.apply_to(gfactors_ref) if gdiff_response is not None: gdiff_response.apply_to(gfactors_ref) # create Policy objects if self.specified_reform: pol = Policy(gfactors=gfactors_ref) try: pol.implement_reform(paramdict['policy']) self.errmsg += pol.reform_errors except ValueError as valerr_msg: self.errmsg += valerr_msg.__str__() else: pol = Policy(gfactors=gfactors_clp) clp = Policy(gfactors=gfactors_clp) # check for valid tax_year value if tax_year < pol.start_year: msg = 'tax_year {} less than policy.start_year {}' msg = msg.format(tax_year, pol.start_year) self.errmsg += 'ERROR: {}\n'.format(msg) if tax_year > pol.end_year: msg = 'tax_year {} greater than policy.end_year {}' msg = msg.format(tax_year, pol.end_year) self.errmsg += 'ERROR: {}\n'.format(msg) # any errors imply cannot proceed with calculations if self.errmsg: return # set policy to tax_year pol.set_year(tax_year) clp.set_year(tax_year) # read input file contents into Records objects if aging_input_data: if self.cps_input_data: recs = Records.cps_constructor( gfactors=gfactors_ref, exact_calculations=exact_calculations) recs_clp = Records.cps_constructor( gfactors=gfactors_clp, exact_calculations=exact_calculations) else: # if not cps_input_data recs = Records(data=input_data, gfactors=gfactors_ref, exact_calculations=exact_calculations) recs_clp = Records(data=input_data, gfactors=gfactors_clp, exact_calculations=exact_calculations) else: # input_data are raw data that are not being aged recs = Records(data=input_data, gfactors=None, exact_calculations=exact_calculations, weights=None, adjust_ratios=None, start_year=tax_year) recs_clp = copy.deepcopy(recs) if tax_year < recs.data_year: msg = 'tax_year {} less than records.data_year {}' msg = msg.format(tax_year, recs.data_year) self.errmsg += 'ERROR: {}\n'.format(msg) # create Calculator objects con = Consumption() con.update_consumption(paramdict['consumption']) self.calc = Calculator(policy=pol, records=recs, verbose=True, consumption=con, behavior=beh, sync_years=aging_input_data) self.calc_clp = Calculator(policy=clp, records=recs_clp, verbose=False, consumption=con, sync_years=aging_input_data) # remember parameter dictionary for reform documentation self.param_dict = paramdict
def init(self, input_data, tax_year, baseline, reform, assump, growdiff_growmodel, aging_input_data, exact_calculations): """ TaxCalcIO class post-constructor method that completes initialization. Parameters ---------- First five are same as the first five of the TaxCalcIO constructor: input_data, tax_year, baseline, reform, assump. growdiff_growmodel: GrowDiff object or None growdiff_growmodel GrowDiff object is used only in the TaxCalcIO.growmodel_analysis method. aging_input_data: boolean whether or not to extrapolate Records data from data year to tax_year. exact_calculations: boolean specifies whether or not exact tax calculations are done without any smoothing of "stair-step" provisions in the tax law. """ # pylint: disable=too-many-arguments,too-many-locals # pylint: disable=too-many-statements,too-many-branches self.errmsg = '' # get policy parameter dictionary from --baseline file basedict = Calculator.read_json_param_objects(baseline, None) # get assumption sub-dictionaries paramdict = Calculator.read_json_param_objects(None, assump) # get policy parameter dictionaries from --reform file(s) policydicts = list() if self.specified_reform: reforms = reform.split('+') for ref in reforms: pdict = Calculator.read_json_param_objects(ref, None) policydicts.append(pdict['policy']) paramdict['policy'] = policydicts[0] # remember parameters for reform documentation self.param_dict = paramdict self.policy_dicts = policydicts # create Behavior object beh = Behavior() try: beh.update_behavior(paramdict['behavior']) except ValueError as valerr_msg: self.errmsg += valerr_msg.__str__() self.behavior_has_any_response = beh.has_any_response() # create gdiff_baseline object gdiff_baseline = GrowDiff() try: gdiff_baseline.update_growdiff(paramdict['growdiff_baseline']) except ValueError as valerr_msg: self.errmsg += valerr_msg.__str__() # create GrowFactors base object that incorporates gdiff_baseline gfactors_base = GrowFactors() gdiff_baseline.apply_to(gfactors_base) # specify gdiff_response object gdiff_response = GrowDiff() try: gdiff_response.update_growdiff(paramdict['growdiff_response']) except ValueError as valerr_msg: self.errmsg += valerr_msg.__str__() # create GrowFactors ref object that has all gdiff objects applied gfactors_ref = GrowFactors() gdiff_baseline.apply_to(gfactors_ref) gdiff_response.apply_to(gfactors_ref) if growdiff_growmodel: growdiff_growmodel.apply_to(gfactors_ref) # create Policy objects: # ... the baseline Policy object base = Policy(gfactors=gfactors_base) try: base.implement_reform(basedict['policy'], print_warnings=False, raise_errors=False) self.errmsg += base.parameter_errors except ValueError as valerr_msg: self.errmsg += valerr_msg.__str__() # ... the reform Policy object if self.specified_reform: pol = Policy(gfactors=gfactors_ref) for poldict in policydicts: try: pol.implement_reform(poldict, print_warnings=False, raise_errors=False) self.errmsg += pol.parameter_errors except ValueError as valerr_msg: self.errmsg += valerr_msg.__str__() else: pol = Policy(gfactors=gfactors_base) # create Consumption object con = Consumption() try: con.update_consumption(paramdict['consumption']) except ValueError as valerr_msg: self.errmsg += valerr_msg.__str__() # create GrowModel object self.growmodel = GrowModel() try: self.growmodel.update_growmodel(paramdict['growmodel']) except ValueError as valerr_msg: self.errmsg += valerr_msg.__str__() # check for valid tax_year value if tax_year < pol.start_year: msg = 'tax_year {} less than policy.start_year {}' msg = msg.format(tax_year, pol.start_year) self.errmsg += 'ERROR: {}\n'.format(msg) if tax_year > pol.end_year: msg = 'tax_year {} greater than policy.end_year {}' msg = msg.format(tax_year, pol.end_year) self.errmsg += 'ERROR: {}\n'.format(msg) # any errors imply cannot proceed with calculations if self.errmsg: return # set policy to tax_year pol.set_year(tax_year) base.set_year(tax_year) # read input file contents into Records objects if aging_input_data: if self.cps_input_data: recs = Records.cps_constructor( gfactors=gfactors_ref, exact_calculations=exact_calculations) recs_base = Records.cps_constructor( gfactors=gfactors_base, exact_calculations=exact_calculations) else: # if not cps_input_data but aging_input_data recs = Records(data=input_data, gfactors=gfactors_ref, exact_calculations=exact_calculations) recs_base = Records(data=input_data, gfactors=gfactors_base, exact_calculations=exact_calculations) else: # input_data are raw data that are not being aged recs = Records(data=input_data, gfactors=None, exact_calculations=exact_calculations, weights=None, adjust_ratios=None, start_year=tax_year) recs_base = copy.deepcopy(recs) if tax_year < recs.data_year: msg = 'tax_year {} less than records.data_year {}' msg = msg.format(tax_year, recs.data_year) self.errmsg += 'ERROR: {}\n'.format(msg) # create Calculator objects self.calc = Calculator(policy=pol, records=recs, verbose=True, consumption=con, behavior=beh, sync_years=aging_input_data) self.calc_base = Calculator(policy=base, records=recs_base, verbose=False, consumption=con, sync_years=aging_input_data)
def generate_policy_revenues(): from taxcalc.growfactors import GrowFactors from taxcalc.policy import Policy from taxcalc.records import Records from taxcalc.gstrecords import GSTRecords from taxcalc.corprecords import CorpRecords from taxcalc.parameters import ParametersBase from taxcalc.calculator import Calculator """ for num in range(1, num_reforms): block_selected_dict[num]['selected_item']= block_widget_dict[num][1].get() block_selected_dict[num]['selected_value']= block_widget_dict[num][3].get() block_selected_dict[num]['selected_year']= block_widget_dict[num][2].get() print(block_selected_dict) """ f = open('reform.json') block_selected_dict = json.load(f) print("block_selected_dict from json",block_selected_dict) #print(block_selected_dict) # create Records object containing pit.csv and pit_weights.csv input data #print("growfactors filename ", growfactors_filename) #recs = Records(data=data_filename, weights=weights_filename, gfactors=GrowFactors(growfactors_filename=growfactors_filename)) #recs = Records(data=data_filename, weights=weights_filename, gfactors=GrowFactors(growfactors_filename=growfactors_filename)) #recs.increment_year1(3.0) #grecs = GSTRecords() f = open('global_vars.json') vars = json.load(f) print("data_filename: ", vars['cit_data_filename']) print("weights_filename: ", vars['cit_weights_filename']) print("growfactors_filename: ", vars['GROWFACTORS_FILENAME']) print("policy_filename: ", vars['DEFAULTS_FILENAME']) # create CorpRecords object using cross-section data #crecs1 = CorpRecords(data='cit_cross.csv', weights='cit_cross_wgts1.csv') crecs1 = CorpRecords(data=vars['cit_data_filename'], weights=vars['cit_weights_filename'], gfactors=GrowFactors(growfactors_filename=vars['GROWFACTORS_FILENAME'])) #crecs1 = CorpRecords(data=vars['cit_weights_filename'], weights=vars['cit_weights_filename']) # Note: weights argument is optional assert isinstance(crecs1, CorpRecords) assert crecs1.current_year == 2017 # create Policy object containing current-law policy pol = Policy(DEFAULTS_FILENAME=vars['DEFAULTS_FILENAME']) # specify Calculator objects for current-law policy #calc1 = Calculator(policy=pol, records=recs, corprecords=crecs1, # gstrecords=grecs, verbose=False) calc1 = Calculator(policy=pol, corprecords=crecs1, verbose=False) #calc1.increment_year1(3.8) assert isinstance(calc1, Calculator) assert calc1.current_year == 2017 np.seterr(divide='ignore', invalid='ignore') pol2 = Policy(DEFAULTS_FILENAME=vars['DEFAULTS_FILENAME']) years, reform=read_reform_dict(block_selected_dict) print("reform dictionary: ",reform) #reform = Calculator.read_json_param_objects('app01_reform.json', None) pol2.implement_reform(reform['policy']) #calc2 = Calculator(policy=pol2, records=recs, corprecords=crecs1, # gstrecords=grecs, verbose=False) calc2 = Calculator(policy=pol2, corprecords=crecs1, verbose=False) pit_adjustment_factor={} revenue_dict_cit={} revenue_amount_dict = {} calc1.calc_all() for year in range(2019, 2024): cols = [] calc1.advance_to_year(year) calc2.advance_to_year(year) # NOTE: calc1 now contains a PRIVATE COPY of pol and a PRIVATE COPY of recs, # so we can continue to use pol and recs in this script without any # concern about side effects from Calculator method calls on calc1. # Produce DataFrame of results using the calculator # First run the calculator for the corporate income tax calc1.calc_all() print("***** Year ", year) weighted_citax1 = calc1.weighted_total_cit('citax') citax_collection_billions1 = weighted_citax1/10**9 citax_collection_str1 = '{0:.2f}'.format(citax_collection_billions1) print("The CIT Collection in billions is: ", citax_collection_billions1) # Produce DataFrame of results using cross-section calc2.calc_all() weighted_citax2 = calc2.weighted_total_cit('citax') citax_collection_billions2 = weighted_citax2/10**9 citax_collection_str2 = '{0:.2f}'.format(citax_collection_billions2) # This is the difference in the collection due to the reform # This amount will now be allocated to dividends of PIT citax_diff_collection_billions2 = (citax_collection_billions2-citax_collection_billions1) citax_diff_collection_str2 = '{0:.2f}'.format(citax_diff_collection_billions2) print("The CIT Collection after reform billions is: ", citax_collection_billions2) print("The difference in CIT Collection in billions is: ", citax_diff_collection_billions2) # Process of allocation of difference in CIT profits to PIT # in the form of Dividends # Dividends in this case is reported as Income from Other Sources # TOTAL_INCOME_OS in the PIT form # First get the unadjusted amounts # Now calculate the adjusted amounts # contribution to PIT Dividends proportion_change_dividend = (weighted_citax1 - weighted_citax2)/weighted_citax1 new_dividend_proportion_of_old = (1 + proportion_change_dividend) pit_adjustment_factor[year]=new_dividend_proportion_of_old # Store Results revenue_dict_cit[year]={} revenue_dict_cit[year]['current_law']=citax_collection_str1 revenue_dict_cit[year]['reform']=citax_collection_str2 revenue_dict_cit[year]['difference']=citax_diff_collection_str2 print(revenue_dict_cit) print("new_dividend_proportion_of_old ", pit_adjustment_factor) # now update pit.csv with this proportion # start a new round of simulation for pit recs = Records(data=vars['pit_data_filename'], weights=vars['pit_weights_filename'], gfactors=GrowFactors(growfactors_filename=vars['GROWFACTORS_FILENAME'])) # create Policy object containing current-law policy pol = Policy(DEFAULTS_FILENAME=vars['DEFAULTS_FILENAME']) # specify Calculator objects for current-law policy #calc1 = Calculator(policy=pol, records=recs, corprecords=crecs1, # gstrecords=grecs, verbose=False) calc1 = Calculator(policy=pol, records=recs, verbose=False) #calc1.increment_year1(3.8) assert isinstance(calc1, Calculator) assert calc1.current_year == 2017 np.seterr(divide='ignore', invalid='ignore') pol2 = Policy(DEFAULTS_FILENAME=vars['DEFAULTS_FILENAME']) #years, reform=read_reform_dict(block_selected_dict) #print("reform dictionary: ", reform) #reform = Calculator.read_json_param_objects('app01_reform.json', None) pol2.implement_reform(reform['policy']) #calc2 = Calculator(policy=pol2, records=recs, corprecords=crecs1, # gstrecords=grecs, verbose=False) calc2 = Calculator(policy=pol2, records=recs, verbose=False) total_revenue_text={} reform_revenue_text={} revenue_dict_pit={} revenue_amount_dict = {} num = 1 first_time = True i=1 j=0 #rows = [] window = tk.Toplevel() window.geometry("800x400+140+140") display_table(window, revenue_dict_cit, revenue_dict_pit, header=True) #for year in range(years[0], years[-1]+1): for year in range(2019, 2024): cols = [] calc1.advance_to_year(year) calc2.advance_to_year(year) # NOTE: calc1 now contains a PRIVATE COPY of pol and a PRIVATE COPY of recs, # so we can continue to use pol and recs in this script without any # concern about side effects from Calculator method calls on calc1. # Produce DataFrame of results using the calculator # First run the calculator for the corporate income tax calc1.calc_all() weighted_pitax1 = calc1.weighted_total_pit('pitax') pitax_collection_billions1 = weighted_pitax1/10**9 pitax_collection_str1 = '{0:.2f}'.format(pitax_collection_billions1) print('\n\n\n') print(f'TAX COLLECTION FOR THE YEAR - {year} \n') print("The PIT Collection in billions is: ", pitax_collection_billions1) #total_revenue_text[year] = "PIT COLLECTION UNDER CURRENT LAW FOR THE YEAR - " + str(year)+" : "+str(pitax_collection_str1)+" bill" # Produce DataFrame of results using cross-section calc2.calc_all() weighted_pitax2 = calc2.weighted_total_pit('pitax') pitax_collection_billions2 = weighted_pitax2/10**9 pitax_collection_str2 = '{0:.2f}'.format(pitax_collection_billions2) pitax_diff_collection_billions2 = (pitax_collection_billions2-pitax_collection_billions1) pitax_diff_collection_str2 = '{0:.2f}'.format(pitax_diff_collection_billions2) # Now calculate the adjusted amounts # contribution to PIT Dividends print("Total Income from Other Sources (bill) no adjustment is ", calc2.weighted_total_pit('TOTAL_INCOME_OS')/10**9 ) calc2.adjust_pit(pit_adjustment_factor[year]) print("Total Income from Other Sources (bill) after adjustment is ", calc2.weighted_total_pit('TOTAL_INCOME_OS')/10**9 ) calc2.calc_all() weighted_pitax3 = calc2.weighted_total_pit('pitax') pitax_collection_billions3 = weighted_pitax3/10**9 pitax_collection_str3 = '{0:.2f}'.format(pitax_collection_billions3) pitax_diff_collection_billions3 = (pitax_collection_billions3-pitax_collection_billions1) pitax_diff_collection_str3 = '{0:.2f}'.format(pitax_diff_collection_billions3) pitax_diff_collection_billions4 = (pitax_collection_billions3-pitax_collection_billions2) pitax_diff_collection_str4 = '{0:.2f}'.format(pitax_diff_collection_billions4) #save the results revenue_dict_pit[year]={} revenue_dict_pit[year]['current_law']=pitax_collection_str1 revenue_dict_pit[year]['reform']={} revenue_dict_pit[year]['reform']['unadjusted']=pitax_collection_str2 revenue_dict_pit[year]['reform']['adjusted']=pitax_collection_str3 revenue_dict_pit[year]['difference']=pitax_diff_collection_str3 print('\n\n\n') print(f'TAX COLLECTION FOR THE YEAR UNDER REFORM - {year} \n') print("The PIT Collection in billions is: ", pitax_collection_billions2) print("The difference in PIT Collection in billions is: ", pitax_diff_collection_billions2) print('****AFTER ADJUSTMENT \n\n\n') print('TAX COLLECTION FOR THE YEAR UNDER REFORM WITH ADJUSTMENT \n') print("The PIT Collection in billions after adjusting for the impact of CIT is: ", pitax_collection_billions3) print("The difference in PIT Collection in billions after adjusting for the impact of CIT is: ", pitax_diff_collection_billions3) print("The impact of adjustment is: ", pitax_diff_collection_billions4) display_table(window, revenue_dict_cit, revenue_dict_pit, year=year, row=i) i=i+1 #reverse the adjustment to obtain baseline calc2.adjust_pit(1/pit_adjustment_factor[year]) display_table(window, revenue_dict_cit, revenue_dict_pit, footer=i) """
def reform_documentation(params): """ Generate reform documentation. Parameters ---------- params: dict compound dictionary structured as dict returned from the static Calculator method read_json_param_objects() Returns ------- doc: String the documentation for the policy reform specified in params """ # pylint: disable=too-many-statements,too-many-branches # nested function used only in reform_documentation def param_doc(years, change, base): """ Parameters ---------- years: list of change years change: dictionary of parameter changes base: Policy or Growdiff object with baseline values syear: parameter start calendar year Returns ------- doc: String """ # nested function used only in param_doc def lines(text, num_indent_spaces, max_line_length=77): """ Return list of text lines, each one of which is no longer than max_line_length, with the second and subsequent lines being indented by the number of specified num_indent_spaces; each line in the list ends with the '\n' character """ if len(text) < max_line_length: # all text fits on one line line = text + '\n' return [line] # all text does not fix on one line first_line = True line_list = list() words = text.split() while words: if first_line: line = '' first_line = False else: line = ' ' * num_indent_spaces while (words and (len(words[0]) + len(line)) < max_line_length): line += words.pop(0) + ' ' line = line[:-1] + '\n' line_list.append(line) return line_list # begin main logic of param_doc # pylint: disable=too-many-nested-blocks assert len(years) == len(change.keys()) basevals = getattr(base, '_vals', None) assert isinstance(basevals, dict) doc = '' for year in years: # write year base.set_year(year) doc += '{}:\n'.format(year) # write info for each param in year for param in sorted(change[year].keys()): # ... write param:value line pval = change[year][param] if isinstance(pval, list): pval = pval[0] if basevals[param]['boolean_value']: if isinstance(pval, list): pval = [ True if item else False for item in pval ] else: pval = bool(pval) doc += ' {} : {}\n'.format(param, pval) # ... write optional param-index line if isinstance(pval, list): pval = basevals[param]['col_label'] pval = [str(item) for item in pval] doc += ' ' * (4 + len(param)) + '{}\n'.format(pval) # ... write name line if param.endswith('_cpi'): rootparam = param[:-4] name = '{} inflation indexing status'.format(rootparam) else: name = basevals[param]['long_name'] for line in lines('name: ' + name, 6): doc += ' ' + line # ... write optional desc line if not param.endswith('_cpi'): desc = basevals[param]['description'] for line in lines('desc: ' + desc, 6): doc += ' ' + line # ... write baseline_value line if isinstance(base, Policy): if param.endswith('_cpi'): rootparam = param[:-4] bval = basevals[rootparam].get( 'cpi_inflated', False) else: bval = getattr(base, param[1:], None) if isinstance(bval, np.ndarray): # pylint: disable=no-member bval = bval.tolist() if basevals[param]['boolean_value']: bval = [ True if item else False for item in bval ] elif basevals[param]['boolean_value']: bval = bool(bval) doc += ' baseline_value: {}\n'.format(bval) else: # if base is Growdiff object # all Growdiff parameters have zero as default value doc += ' baseline_value: 0.0\n' return doc # begin main logic of reform_documentation # create Policy object with pre-reform (i.e., baseline) values # ... create gdiff_baseline object gdb = Growdiff() gdb.update_growdiff(params['growdiff_baseline']) # ... create Growfactors clp object that incorporates gdiff_baseline gfactors_clp = Growfactors() gdb.apply_to(gfactors_clp) # ... create Policy object containing pre-reform parameter values clp = Policy(gfactors=gfactors_clp) # generate documentation text doc = 'REFORM DOCUMENTATION\n' doc += 'Baseline Growth-Difference Assumption Values by Year:\n' years = sorted(params['growdiff_baseline'].keys()) if years: doc += param_doc(years, params['growdiff_baseline'], gdb) else: doc += 'none: using default baseline growth assumptions\n' doc += 'Policy Reform Parameter Values by Year:\n' years = sorted(params['policy'].keys()) if years: doc += param_doc(years, params['policy'], clp) else: doc += 'none: using current-law policy parameters\n' return doc
def generate_revenues(): from taxcalc.growfactors import GrowFactors from taxcalc.policy import Policy from taxcalc.records import Records from taxcalc.gstrecords import GSTRecords from taxcalc.corprecords import CorpRecords from taxcalc.parameters import ParametersBase from taxcalc.calculator import Calculator """ for num in range(1, num_reforms): block_selected_dict[num]['selected_item']= block_widget_dict[num][1].get() block_selected_dict[num]['selected_value']= block_widget_dict[num][3].get() block_selected_dict[num]['selected_year']= block_widget_dict[num][2].get() print(block_selected_dict) """ f = open('reform.json') block_selected_dict = json.load(f) print("block_selected_dict from json", block_selected_dict) #print(block_selected_dict) # create Records object containing pit.csv and pit_weights.csv input data #print("growfactors filename ", growfactors_filename) #recs = Records(data=data_filename, weights=weights_filename, gfactors=GrowFactors(growfactors_filename=growfactors_filename)) #recs = Records(data=data_filename, weights=weights_filename, gfactors=GrowFactors(growfactors_filename=growfactors_filename)) #recs.increment_year1(3.0) #grecs = GSTRecords() f = open('global_vars.json') vars = json.load(f) print("data_filename: ", vars['cit_data_filename']) print("weights_filename: ", vars['cit_weights_filename']) print("growfactors_filename: ", vars['GROWFACTORS_FILENAME']) print("policy_filename: ", vars['DEFAULTS_FILENAME']) # create CorpRecords object using cross-section data #crecs1 = CorpRecords(data='cit_cross.csv', weights='cit_cross_wgts1.csv') crecs1 = CorpRecords( data=vars['cit_data_filename'], weights=vars['cit_weights_filename'], gfactors=GrowFactors( growfactors_filename=vars['GROWFACTORS_FILENAME'])) #crecs1 = CorpRecords(data=vars['cit_weights_filename'], weights=vars['cit_weights_filename']) # Note: weights argument is optional assert isinstance(crecs1, CorpRecords) assert crecs1.current_year == 2017 # create Policy object containing current-law policy pol = Policy(DEFAULTS_FILENAME=vars['DEFAULTS_FILENAME']) # specify Calculator objects for current-law policy #calc1 = Calculator(policy=pol, records=recs, corprecords=crecs1, # gstrecords=grecs, verbose=False) calc1 = Calculator(policy=pol, corprecords=crecs1, verbose=False) #calc1.increment_year1(3.8) assert isinstance(calc1, Calculator) assert calc1.current_year == 2017 np.seterr(divide='ignore', invalid='ignore') calc1.calc_all() revenue_dict_cit = {} for year in range(2019, 2024): cols = [] calc1.advance_to_year(year) # NOTE: calc1 now contains a PRIVATE COPY of pol and a PRIVATE COPY of recs, # so we can continue to use pol and recs in this script without any # concern about side effects from Calculator method calls on calc1. # Produce DataFrame of results using the calculator # First run the calculator for the corporate income tax calc1.calc_all() print("***** Year ", year) weighted_citax1 = calc1.weighted_total_cit('citax') citax_collection_billions1 = weighted_citax1 / 10**9 citax_collection_str1 = '{0:.2f}'.format(citax_collection_billions1) print("The CIT Collection in billions is: ", citax_collection_billions1) # Store Results revenue_dict_cit[year] = {} revenue_dict_cit[year]['current_law'] = citax_collection_str1 # start a new round of simulation for pit recs = Records(data=vars['pit_data_filename'], weights=vars['pit_weights_filename'], gfactors=GrowFactors( growfactors_filename=vars['GROWFACTORS_FILENAME'])) # create Policy object containing current-law policy pol = Policy(DEFAULTS_FILENAME=vars['DEFAULTS_FILENAME']) # specify Calculator objects for current-law policy #calc1 = Calculator(policy=pol, records=recs, corprecords=crecs1, # gstrecords=grecs, verbose=False) calc1 = Calculator(policy=pol, records=recs, verbose=False) #calc1.increment_year1(3.8) assert isinstance(calc1, Calculator) assert calc1.current_year == 2017 np.seterr(divide='ignore', invalid='ignore') total_revenue_text = {} reform_revenue_text = {} revenue_dict_pit = {} revenue_amount_dict = {} num = 1 first_time = True i = 1 j = 0 #rows = [] window = tk.Toplevel() window.geometry("800x400+140+140") display_table(window, revenue_dict_cit, revenue_dict_pit, header=True) #for year in range(years[0], years[-1]+1): for year in range(2019, 2024): cols = [] calc1.advance_to_year(year) # NOTE: calc1 now contains a PRIVATE COPY of pol and a PRIVATE COPY of recs, # so we can continue to use pol and recs in this script without any # concern about side effects from Calculator method calls on calc1. # Produce DataFrame of results using the calculator # First run the calculator for the corporate income tax calc1.calc_all() weighted_pitax1 = calc1.weighted_total_pit('pitax') pitax_collection_billions1 = weighted_pitax1 / 10**9 pitax_collection_str1 = '{0:.2f}'.format(pitax_collection_billions1) print('\n\n\n') print(f'TAX COLLECTION FOR THE YEAR - {year} \n') print("The PIT Collection in billions is: ", pitax_collection_billions1) #total_revenue_text[year] = "PIT COLLECTION UNDER CURRENT LAW FOR THE YEAR - " + str(year)+" : "+str(pitax_collection_str1)+" bill" #save the results revenue_dict_pit[year] = {} revenue_dict_pit[year]['current_law'] = pitax_collection_str1 display_table(window, revenue_dict_cit, revenue_dict_pit, year=year, row=i) i = i + 1 display_table(window, revenue_dict_cit, revenue_dict_pit, footer=i) """
def __init__( self, input_data, tax_year, reform, assump, growdiff_response, # =None in static analysis aging_input_data, exact_calculations): """ TaxCalcIO class constructor. """ # pylint: disable=too-many-arguments # pylint: disable=too-many-locals # pylint: disable=too-many-branches # pylint: disable=too-many-statements # check for existence of INPUT file if isinstance(input_data, six.string_types): # remove any leading directory path from INPUT filename fname = os.path.basename(input_data) # check if fname ends with ".csv" if fname.endswith('.csv'): inp = '{}-{}'.format(fname[:-4], str(tax_year)[2:]) else: msg = 'INPUT file named {} does not end in .csv' raise ValueError(msg.format(fname)) # check existence of INPUT file if not os.path.isfile(input_data): msg = 'INPUT file named {} could not be found' raise ValueError(msg.format(input_data)) elif isinstance(input_data, pd.DataFrame): inp = 'df-{}'.format(str(tax_year)[2:]) else: msg = 'INPUT is neither string nor Pandas DataFrame' raise ValueError(msg) # construct output_filename and delete old output file if it exists if reform is None: self._reform = False ref = '' elif isinstance(reform, six.string_types): self._reform = True # remove any leading directory path from REFORM filename fname = os.path.basename(reform) # check if fname ends with ".json" if fname.endswith('.json'): ref = '-{}'.format(fname[:-5]) else: msg = 'REFORM file named {} does not end in .json' raise ValueError(msg.format(fname)) else: msg = 'TaxCalcIO.ctor reform is neither None nor str' raise ValueError(msg) if assump is None: asm = '' elif isinstance(assump, six.string_types): # remove any leading directory path from ASSUMP filename fname = os.path.basename(assump) # check if fname ends with ".json" if fname.endswith('.json'): asm = '-{}'.format(fname[:-5]) else: msg = 'ASSUMP file named {} does not end in .json' raise ValueError(msg.format(fname)) else: msg = 'TaxCalcIO.ctor assump is neither None nor str' raise ValueError(msg) self._output_filename = '{}{}{}.csv'.format(inp, ref, asm) delete_file(self._output_filename) # get parameter dictionaries from --reform and --assump files param_dict = Calculator.read_json_param_files(reform, assump) # make sure no behavioral response is specified in --assump beh = Behavior() beh.update_behavior(param_dict['behavior']) if beh.has_any_response(): msg = '--assump ASSUMP cannot assume any "behavior"' raise ValueError(msg) # make sure no growdiff_response is specified in --assump gdiff_response = Growdiff() gdiff_response.update_growdiff(param_dict['growdiff_response']) if gdiff_response.has_any_response(): msg = '--assump ASSUMP cannot assume any "growdiff_response"' raise ValueError(msg) # create gdiff_baseline object gdiff_baseline = Growdiff() gdiff_baseline.update_growdiff(param_dict['growdiff_baseline']) # create Growfactors clp object that incorporates gdiff_baseline gfactors_clp = Growfactors() gdiff_baseline.apply_to(gfactors_clp) # specify gdiff_response object if growdiff_response is None: gdiff_response = Growdiff() elif isinstance(growdiff_response, Growdiff): gdiff_response = growdiff_response else: msg = 'TaxCalcIO.ctor growdiff_response is neither None nor {}' raise ValueError(msg.format('a Growdiff object')) # create Growfactors ref object that has both gdiff objects applied gfactors_ref = Growfactors() gdiff_baseline.apply_to(gfactors_ref) gdiff_response.apply_to(gfactors_ref) # create Policy object and implement reform if specified if self._reform: pol = Policy(gfactors=gfactors_ref) pol.implement_reform(param_dict['policy']) clp = Policy(gfactors=gfactors_clp) else: pol = Policy(gfactors=gfactors_clp) # check for valid tax_year value if tax_year < pol.start_year: msg = 'tax_year {} less than policy.start_year {}' raise ValueError(msg.format(tax_year, pol.start_year)) if tax_year > pol.end_year: msg = 'tax_year {} greater than policy.end_year {}' raise ValueError(msg.format(tax_year, pol.end_year)) # set policy to tax_year pol.set_year(tax_year) if self._reform: clp.set_year(tax_year) # read input file contents into Records object(s) if aging_input_data: if self._reform: recs = Records(data=input_data, gfactors=gfactors_ref, exact_calculations=exact_calculations) recs_clp = Records(data=input_data, gfactors=gfactors_clp, exact_calculations=exact_calculations) else: recs = Records(data=input_data, gfactors=gfactors_clp, exact_calculations=exact_calculations) else: # input_data are raw data that are not being aged recs = Records(data=input_data, exact_calculations=exact_calculations, gfactors=None, adjust_ratios=None, weights=None, start_year=tax_year) if self._reform: recs_clp = copy.deepcopy(recs) # create Calculator object(s) con = Consumption() con.update_consumption(param_dict['consumption']) self._calc = Calculator(policy=pol, records=recs, verbose=True, consumption=con, sync_years=aging_input_data) if self._reform: self._calc_clp = Calculator(policy=clp, records=recs_clp, verbose=False, consumption=con, sync_years=aging_input_data)