Example #1
0
 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)
Example #2
0
def test_atr_graph_data(records_2009):
    calc = Calculator(policy=Policy(), records=records_2009)
    with pytest.raises(ValueError):
        gdata = atr_graph_data(calc, calc, mars='bad')
    with pytest.raises(ValueError):
        gdata = atr_graph_data(calc, calc, mars=0)
    with pytest.raises(ValueError):
        gdata = atr_graph_data(calc, calc, mars=list())
    with pytest.raises(ValueError):
        gdata = atr_graph_data(calc, calc, atr_measure='badtax')
    with pytest.raises(ValueError):
        calcx = Calculator(policy=Policy(), records=records_2009)
        calcx.advance_to_year(2020)
        gdata = atr_graph_data(calcx, calc)
    gdata = atr_graph_data(calc, calc, mars=1, atr_measure='combined')
    gdata = atr_graph_data(calc, calc, atr_measure='itax')
    gdata = atr_graph_data(calc, calc, atr_measure='ptax')
    assert isinstance(gdata, dict)
Example #3
0
def test_atr_graph_data(cps_subsample):
    pol = Policy()
    rec = Records.cps_constructor(data=cps_subsample)
    calc = Calculator(policy=pol, records=rec)
    with pytest.raises(ValueError):
        atr_graph_data(calc, calc, mars='bad')
    with pytest.raises(ValueError):
        atr_graph_data(calc, calc, mars=0)
    with pytest.raises(ValueError):
        atr_graph_data(calc, calc, mars=list())
    with pytest.raises(ValueError):
        atr_graph_data(calc, calc, atr_measure='badtax')
    gdata = atr_graph_data(calc, calc, mars=1, atr_measure='combined')
    gdata = atr_graph_data(calc, calc, atr_measure='itax')
    gdata = atr_graph_data(calc, calc, atr_measure='ptax')
    assert isinstance(gdata, dict)
    with pytest.raises(ValueError):
        calcx = Calculator(policy=pol, records=rec)
        calcx.advance_to_year(2020)
        atr_graph_data(calcx, calc)
Example #4
0
def test_atr_graph_data(cps_subsample):
    pol = Policy()
    rec = Records.cps_constructor(data=cps_subsample, no_benefits=True)
    calc = Calculator(policy=pol, records=rec)
    year = calc.current_year
    with pytest.raises(ValueError):
        atr_graph_data(None, year, mars='bad')
    with pytest.raises(ValueError):
        atr_graph_data(None, year, mars=0)
    with pytest.raises(ValueError):
        atr_graph_data(None, year, mars=list())
    with pytest.raises(ValueError):
        atr_graph_data(None, year, atr_measure='badtax')
    calc.calc_all()
    vdf = calc.dataframe(['s006', 'MARS', 'expanded_income'])
    tax = 0.20 * np.ones_like(vdf['expanded_income'])
    vdf['tax1'] = tax
    vdf['tax2'] = tax
    gdata = atr_graph_data(vdf, year, mars=1, atr_measure='combined')
    gdata = atr_graph_data(vdf, year, atr_measure='itax')
    gdata = atr_graph_data(vdf, year, atr_measure='ptax')
    assert isinstance(gdata, dict)
def test_atr_graph_data(cps_subsample):
    pol = Policy()
    rec = Records.cps_constructor(data=cps_subsample)
    calc = Calculator(policy=pol, records=rec)
    year = calc.current_year
    with pytest.raises(ValueError):
        atr_graph_data(None, year, mars='bad')
    with pytest.raises(ValueError):
        atr_graph_data(None, year, mars=0)
    with pytest.raises(ValueError):
        atr_graph_data(None, year, mars=list())
    with pytest.raises(ValueError):
        atr_graph_data(None, year, atr_measure='badtax')
    calc.calc_all()
    vdf = calc.dataframe(['s006', 'MARS', 'expanded_income'])
    tax = 0.20 * np.ones_like(vdf['expanded_income'])
    vdf['tax1'] = tax
    vdf['tax2'] = tax
    gdata = atr_graph_data(vdf, year, mars=1, atr_measure='combined')
    gdata = atr_graph_data(vdf, year, atr_measure='itax')
    gdata = atr_graph_data(vdf, year, atr_measure='ptax')
    assert isinstance(gdata, dict)
Example #6
0
    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