def test_xtr_graph_plot(cps_subsample): calc = Calculator(policy=Policy(), records=Records.cps_constructor(data=cps_subsample), behavior=Behavior()) mtr = 0.20 * np.ones_like(cps_subsample['e00200']) vdf = calc.dataframe(['s006', 'MARS', 'c00100']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr gdata = mtr_graph_data(vdf, calc.current_year, mtr_measure='ptax', income_measure='agi', dollar_weighting=False) gplot = xtr_graph_plot(gdata) assert gplot vdf = calc.dataframe(['s006', 'expanded_income']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr gdata = mtr_graph_data(vdf, calc.current_year, mtr_measure='itax', alt_e00200p_text='Taxpayer Earnings', income_measure='expanded_income', dollar_weighting=False) assert isinstance(gdata, dict)
def test_mtr_graph_data(records_2009): calc = Calculator(policy=Policy(), records=records_2009) with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, mars='bad', income_measure='agi', dollar_weighting=True) with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, mars=0, income_measure='expanded_income', dollar_weighting=True) with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, mars=list()) with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, mars='ALL', mtr_variable='e00200s') with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, mtr_measure='badtax') with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, income_measure='badincome') with pytest.raises(ValueError): calcx = Calculator(policy=Policy(), records=records_2009) calcx.advance_to_year(2020) gdata = mtr_graph_data(calcx, calc) gdata = mtr_graph_data(calc, calc, mars=1, mtr_wrt_full_compen=True, income_measure='wages', dollar_weighting=True) assert isinstance(gdata, dict)
def test_write_graph_file(cps_subsample): calc = Calculator(policy=Policy(), records=Records.cps_constructor(data=cps_subsample)) gdata = mtr_graph_data(calc, calc, mtr_measure='ptax', alt_e00200p_text='Taxpayer Earnings', income_measure='agi', dollar_weighting=False) gplot = xtr_graph_plot(gdata) assert gplot htmlfname = temporary_filename(suffix='.html') try: write_graph_file(gplot, htmlfname, 'title') except: # pylint: disable=bare-except if os.path.isfile(htmlfname): try: os.remove(htmlfname) except OSError: pass # sometimes we can't remove a generated temporary file assert 'write_graph_file()_ok' == 'no' # if try was successful, try to remove the file if os.path.isfile(htmlfname): try: os.remove(htmlfname) except OSError: pass # sometimes we can't remove a generated temporary file
def test_write_graph_file(cps_subsample): recs = Records.cps_constructor(data=cps_subsample, no_benefits=True) calc = Calculator(policy=Policy(), records=recs) mtr = 0.20 * np.ones_like(cps_subsample['e00200']) vdf = calc.dataframe(['s006', 'e00200', 'c00100']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr gdata = mtr_graph_data(vdf, calc.current_year, mtr_measure='ptax', alt_e00200p_text='Taxpayer Earnings', income_measure='agi', dollar_weighting=False) gplot = xtr_graph_plot(gdata) assert gplot htmlfname = temporary_filename(suffix='.html') try: write_graph_file(gplot, htmlfname, 'title') except Exception: # pylint: disable=broad-except if os.path.isfile(htmlfname): try: os.remove(htmlfname) except OSError: pass # sometimes we can't remove a generated temporary file assert 'write_graph_file()_ok' == 'no' # if try was successful, try to remove the file if os.path.isfile(htmlfname): try: os.remove(htmlfname) except OSError: pass # sometimes we can't remove a generated temporary file
def test_write_graph_file(cps_subsample): recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) mtr = 0.20 * np.ones_like(cps_subsample['e00200']) vdf = calc.dataframe(['s006', 'e00200', 'c00100']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr gdata = mtr_graph_data(vdf, calc.current_year, mtr_measure='ptax', alt_e00200p_text='Taxpayer Earnings', income_measure='agi', dollar_weighting=False) gplot = xtr_graph_plot(gdata) assert gplot htmlfname = temporary_filename(suffix='.html') try: write_graph_file(gplot, htmlfname, 'title') except Exception: # pylint: disable=broad-except if os.path.isfile(htmlfname): try: os.remove(htmlfname) except OSError: pass # sometimes we can't remove a generated temporary file assert 'write_graph_file()_ok' == 'no' # if try was successful, try to remove the file if os.path.isfile(htmlfname): try: os.remove(htmlfname) except OSError: pass # sometimes we can't remove a generated temporary file
def write_graph_files(self): """ Write graphs to HTML files. """ pos_wght_sum = self.calc.records.s006.sum() > 0. atr_fname = self._output_filename.replace('.csv', '-atr.html') atr_title = 'ATR by Income Percentile' if pos_wght_sum: atr_data = atr_graph_data(self.calc_clp, self.calc) atr_plot = xtr_graph_plot(atr_data) write_graph_file(atr_plot, atr_fname, atr_title) else: reason = 'No graph because sum of weights is not positive' TaxCalcIO.write_empty_graph_file(atr_fname, atr_title, reason) mtr_fname = self._output_filename.replace('.csv', '-mtr.html') mtr_title = 'MTR by Income Percentile' if pos_wght_sum: mtr_data = mtr_graph_data(self.calc_clp, self.calc, alt_e00200p_text='Taxpayer Earnings') mtr_plot = xtr_graph_plot(mtr_data) write_graph_file(mtr_plot, mtr_fname, mtr_title) else: reason = 'No graph because sum of weights is not positive' TaxCalcIO.write_empty_graph_file(mtr_fname, mtr_title, reason)
def test_mtr_graph_data(cps_subsample): calc = Calculator(policy=Policy(), records=Records.cps_constructor(data=cps_subsample)) with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, mars='bad', income_measure='agi', dollar_weighting=True) with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, mars=0, income_measure='expanded_income', dollar_weighting=True) with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, mars=list()) with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, mars='ALL', mtr_variable='e00200s') with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, mtr_measure='badtax') with pytest.raises(ValueError): gdata = mtr_graph_data(calc, calc, income_measure='badincome') gdata = mtr_graph_data(calc, calc, mars=1, mtr_wrt_full_compen=True, income_measure='wages', dollar_weighting=True) assert isinstance(gdata, dict)
def test_xtr_graph_plot(cps_subsample): calc = Calculator(policy=Policy(), records=Records.cps_constructor(data=cps_subsample), behavior=Behavior()) gdata = mtr_graph_data(calc, calc, mtr_measure='ptax', income_measure='agi', dollar_weighting=False) gplot = xtr_graph_plot(gdata) assert gplot gdata = mtr_graph_data(calc, calc, mtr_measure='itax', alt_e00200p_text='Taxpayer Earnings', income_measure='expanded_income', dollar_weighting=False) assert isinstance(gdata, dict)
def test_xtr_graph_plot(cps_subsample): recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) mtr = 0.20 * np.ones_like(cps_subsample['e00200']) vdf = calc.dataframe(['s006', 'MARS', 'c00100']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr gdata = mtr_graph_data(vdf, calc.current_year, mtr_measure='ptax', income_measure='agi', dollar_weighting=False) gplot = xtr_graph_plot(gdata) assert gplot vdf = calc.dataframe(['s006', 'expanded_income']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr gdata = mtr_graph_data(vdf, calc.current_year, mtr_measure='itax', alt_e00200p_text='Taxpayer Earnings', income_measure='expanded_income', dollar_weighting=False) assert isinstance(gdata, dict)
def test_mtr_graph_data(cps_subsample): recs = Records.cps_constructor(data=cps_subsample, no_benefits=True) calc = Calculator(policy=Policy(), records=recs) year = calc.current_year, with pytest.raises(ValueError): mtr_graph_data(None, year, mars='bad', income_measure='agi', dollar_weighting=True) with pytest.raises(ValueError): mtr_graph_data(None, year, mars=0, income_measure='expanded_income', dollar_weighting=True) with pytest.raises(ValueError): mtr_graph_data(None, year, mars=list()) with pytest.raises(ValueError): mtr_graph_data(None, year, mars='ALL', mtr_variable='e00200s') with pytest.raises(ValueError): mtr_graph_data(None, year, mtr_measure='badtax') with pytest.raises(ValueError): mtr_graph_data(None, year, income_measure='badincome') mtr = 0.20 * np.ones_like(cps_subsample['e00200']) vdf = calc.dataframe(['s006', 'MARS', 'e00200']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr vdf = vdf[vdf['MARS'] == 1] gdata = mtr_graph_data(vdf, year, mars=1, mtr_wrt_full_compen=True, income_measure='wages', dollar_weighting=True) assert isinstance(gdata, dict)
def test_mtr_graph_data(cps_subsample): recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) year = calc.current_year with pytest.raises(ValueError): mtr_graph_data(None, year, mars='bad', income_measure='agi', dollar_weighting=True) with pytest.raises(ValueError): mtr_graph_data(None, year, mars=0, income_measure='expanded_income', dollar_weighting=True) with pytest.raises(ValueError): mtr_graph_data(None, year, mars=list()) with pytest.raises(ValueError): mtr_graph_data(None, year, mars='ALL', mtr_variable='e00200s') with pytest.raises(ValueError): mtr_graph_data(None, year, mtr_measure='badtax') with pytest.raises(ValueError): mtr_graph_data(None, year, income_measure='badincome') mtr = 0.20 * np.ones_like(cps_subsample['e00200']) vdf = calc.dataframe(['s006', 'MARS', 'e00200']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr vdf = vdf[vdf['MARS'] == 1] gdata = mtr_graph_data(vdf, year, mars=1, mtr_wrt_full_compen=True, income_measure='wages', dollar_weighting=True) assert isinstance(gdata, dict)
def static_analysis(self, writing_output_file=False, output_graph=False, output_ceeu=False, output_dump=False): """ Conduct STATIC tax analysis for INPUT and write or return OUTPUT. Parameters ---------- writing_output_file: boolean output_graph: boolean whether or not to generate and show HTML graphs of average and marginal tax rates by income percentile output_ceeu: boolean whether or not to calculate and write to stdout standard certainty-equivalent expected-utility statistics output_dump: boolean whether or not to replace standard output with all input and calculated variables using their Tax-Calculator names Returns ------- Nothing """ # pylint: disable=too-many-arguments,too-many-locals,too-many-branches # conduct STATIC tax analysis if output_dump: (mtr_paytax, mtr_inctax, _) = self._calc.mtr(wrt_full_compensation=False) else: # do not need marginal tax rates self._calc.calc_all() # optionally conduct normative welfare analysis if output_ceeu: self._calc_clp.calc_all() cedict = ce_aftertax_income(self._calc_clp, self._calc, require_no_agg_tax_change=False) ceeu_results = TaxCalcIO.ceeu_output(cedict) else: ceeu_results = None # extract output if writing_output_file if writing_output_file: if output_dump: outdf = self.dump_output(mtr_inctax, mtr_paytax) else: outdf = self.standard_output() assert len(outdf.index) == self._calc.records.dim outdf.to_csv(self._output_filename, index=False, float_format='%.2f') # optionally write --graph output to HTML files if output_graph: atr_data = atr_graph_data(self._calc_clp, self._calc) atr_plot = xtr_graph_plot(atr_data) atr_fname = self._output_filename.replace('.csv', '-atr.html') atr_title = 'ATR by Income Percentile' write_graph_file(atr_plot, atr_fname, atr_title) mtr_data = mtr_graph_data(self._calc_clp, self._calc, alt_e00200p_text='Taxpayer Earnings') mtr_plot = xtr_graph_plot(mtr_data) mtr_fname = self._output_filename.replace('.csv', '-mtr.html') mtr_title = 'MTR by Income Percentile' write_graph_file(mtr_plot, mtr_fname, mtr_title) # optionally write --ceeu output to stdout if ceeu_results: print(ceeu_results) # pylint: disable=superfluous-parens