PSLmodels / Tax-Calculator

USA Federal Individual Income and Payroll Tax Microsimulation Model
https://taxcalc.pslmodels.org
Other
254 stars 153 forks source link

Revise GrowFactors constructor to handle custom growfactors files #2757

Closed martinholmer closed 2 months ago

martinholmer commented 2 months ago

Default is still to use baseline growfactors in the package's growfactors.csv file. Now users of the Python API can specify custom growth factors to be used by the Records object.

For an example of how to use this new Tax-Calculator feature, we this comment.

codecov[bot] commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.42%. Comparing base (e0234b6) to head (afb7df1). Report is 16 commits behind head on master.

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/PSLmodels/Tax-Calculator/pull/2757/graphs/tree.svg?width=650&height=150&src=pr&token=KqtTvRSNjQ&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PSLmodels)](https://app.codecov.io/gh/PSLmodels/Tax-Calculator/pull/2757?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PSLmodels) ```diff @@ Coverage Diff @@ ## master #2757 +/- ## ======================================= Coverage 99.42% 99.42% ======================================= Files 13 13 Lines 2605 2607 +2 ======================================= + Hits 2590 2592 +2 Misses 15 15 ``` | [Flag](https://app.codecov.io/gh/PSLmodels/Tax-Calculator/pull/2757/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PSLmodels) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/PSLmodels/Tax-Calculator/pull/2757/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PSLmodels) | `99.42% <100.00%> (+<0.01%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PSLmodels#carryforward-flags-in-the-pull-request-comment) to find out more. | [Files](https://app.codecov.io/gh/PSLmodels/Tax-Calculator/pull/2757?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PSLmodels) | Coverage Δ | | |---|---|---| | [taxcalc/\_\_init\_\_.py](https://app.codecov.io/gh/PSLmodels/Tax-Calculator/pull/2757?src=pr&el=tree&filepath=taxcalc%2F__init__.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PSLmodels#diff-dGF4Y2FsYy9fX2luaXRfXy5weQ==) | `100.00% <100.00%> (ø)` | | | [taxcalc/calculator.py](https://app.codecov.io/gh/PSLmodels/Tax-Calculator/pull/2757?src=pr&el=tree&filepath=taxcalc%2Fcalculator.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PSLmodels#diff-dGF4Y2FsYy9jYWxjdWxhdG9yLnB5) | `100.00% <ø> (ø)` | | | [taxcalc/growfactors.py](https://app.codecov.io/gh/PSLmodels/Tax-Calculator/pull/2757?src=pr&el=tree&filepath=taxcalc%2Fgrowfactors.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PSLmodels#diff-dGF4Y2FsYy9ncm93ZmFjdG9ycy5weQ==) | `100.00% <100.00%> (ø)` | | | [taxcalc/parameters.py](https://app.codecov.io/gh/PSLmodels/Tax-Calculator/pull/2757?src=pr&el=tree&filepath=taxcalc%2Fparameters.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PSLmodels#diff-dGF4Y2FsYy9wYXJhbWV0ZXJzLnB5) | `100.00% <ø> (ø)` | |
donboyd5 commented 2 months ago

Thanks for this, @martinholmer, it is really helpful.

martinholmer commented 1 month ago

The following Python script illustrates the use of the new feature added in PR #2757:

"""
This gf.py script illustrates use of GrowFactors constructor with custom
growfactors representing a 2020 spike in wage inflation, a feature that
is in Tax-Calculator pull request #2757 and will be included beginning
in the Tax-Calculator 4.0.0 release.

(taxcalc-dev) Tax-Calculator% diff mygf.csv taxcalc/growfactors.csv
11c11
< 2020,1.067957,1.167938,0.947032,1.00799,1.041121,1.116381,1.016054,...
---                                                  ^^
> 2020,1.067957,1.167938,0.947032,1.00799,1.041121,1.006381,1.016054,...

(taxcalc-dev) Tax-Calculator% python gf.py
2112.718 2367.885
"""

from taxcalc import GrowFactors, Policy, Records, Calculator

TAXYEAR = 2022

gf0 = GrowFactors()
gf1 = GrowFactors(growfactors_filename='../Tax-Calculator/mygf.csv')

recs0 = Records('puf.csv', start_year=Records.PUFCSV_YEAR, gfactors=gf0)
recs1 = Records('puf.csv', start_year=Records.PUFCSV_YEAR, gfactors=gf1)

calc0 = Calculator(policy=Policy(), records=recs0)
calc1 = Calculator(policy=Policy(), records=recs1)

calc0.advance_to_year(TAXYEAR)
calc1.advance_to_year(TAXYEAR)

calc0.calc_all()
calc1.calc_all()

itax0 = calc0.weighted_total('iitax')
itax1 = calc1.weighted_total('iitax')

assert itax1 > itax0
print(round(itax0*1e-9, 3), round(itax1*1e-9, 3))

Executing the above script produces these results:

(taxcalc-dev) Tax-Calculator% python gf.py
2112.718 2367.885