Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the regular_tax_before_credits variable and add unit tests #5036

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- bump: minor
changes:
added:
- Refactor the regular_tax_before_credits variable and add unit tests.
15 changes: 15 additions & 0 deletions policyengine_us/parameters/gov/irs/capital_gains/in_effect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
description: Whether the additional lines apply to Schedule D Tax Worksheet.
values:
0000-01-01: false
2018-01-01: true
metadata:
unit: bool
period: year
label: Schedule D Tax Worksheet additional line in effect
reference:
- title: 2017 Instructions for Schedule D - line 19
href: https://www.irs.gov/pub/irs-prior/i1040sd--2017.pdf#page=15
- title: 2018 Instructions for Schedule D - line 18b
href: https://www.irs.gov/pub/irs-prior/i1040sd--2018.pdf#page=19
- title: 2019 Instructions for Schedule D - line 19
href: https://www.irs.gov/pub/irs-prior/i1040sd--2019.pdf#page=16
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
description: Taxable income thresholds and limits.
metadata:
unit: currency-USD
period: year
label: Taxable income thresholds
breakdown: filing_status
reference:
- title: 2018 Instructions for Schedule D - line 18b
href: https://www.irs.gov/pub/irs-prior/i1040sd--2018.pdf#page=19
- title: 2019 Instructions for Schedule D - line 19
href: https://www.irs.gov/pub/irs-prior/i1040sd--2019.pdf#page=16
- title: 2020 Instructions for Schedule D - line 19
href: https://www.irs.gov/pub/irs-prior/i1040sd--2020.pdf#page=16
- title: 2021 Instructions for Schedule D - line 19
href: https://www.irs.gov/pub/irs-prior/i1040sd--2021.pdf#page=16
- title: 2022 Instructions for Schedule D - line 19
href: https://www.irs.gov/pub/irs-prior/i1040sd--2022.pdf#page=16
- title: 2023 Instructions for Schedule D - line 19
href: https://www.irs.gov/pub/irs-prior/i1040sd--2023.pdf#page=16

SINGLE:
2018-01-01: 157_500
2019-01-01: 160_725
2020-01-01: 163_300
2021-01-01: 164_925
2022-01-01: 170_050
2023-01-01: 182_100
JOINT:
2018-01-01: 315_000
2019-01-01: 321_450
2020-01-01: 326_600
2021-01-01: 329_850
2022-01-01: 340_100
2023-01-01: 364_200
SEPARATE:
2018-01-01: 157_500
2019-01-01: 160_725
2020-01-01: 163_300
2021-01-01: 164_925
2022-01-01: 170_050
2023-01-01: 182_100
HEAD_OF_HOUSEHOLD:
2018-01-01: 157_500
2019-01-01: 160_700
2020-01-01: 163_300
2021-01-01: 164_900
2022-01-01: 170_050
2023-01-01: 182_100
SURVIVING_SPOUSE:
2018-01-01: 315_000
2019-01-01: 321_450
2020-01-01: 326_600
2021-01-01: 329_850
2022-01-01: 340_100
2023-01-01: 364_200
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
- name: non_sch_d_capital_gains > 0, investment_income_form_4952 > 0
period: 2019
input:
long_term_capital_gains: 2_000
qualified_dividend_income: 2_000
net_capital_gains: 1_000
non_sch_d_capital_gains: 3_000
investment_income_form_4952: 2_000
output: # 3000 - min(0, 2000)
adjusted_capital_gains_reduced_by_investment_income: 3_000

- name: non_sch_d_capital_gains > 0, investment_income_form_4952 < 0
period: 2019
input:
long_term_capital_gains: 2_000
qualified_dividend_income: 2_000
net_capital_gains: 1_000
non_sch_d_capital_gains: 3_000
investment_income_form_4952: -2_000
output: # 3000 - min(0, -2000)
adjusted_capital_gains_reduced_by_investment_income: 5_000

- name: non_sch_d_capital_gains < 0, investment_income_form_4952 < 0
period: 2019
input:
long_term_capital_gains: 2_000
qualified_dividend_income: 2_000
net_capital_gains: 1_000
non_sch_d_capital_gains: -1_000
investment_income_form_4952: -2_000
output: # min(2000 + 2000, 1000) - 1000 - min(0, -2000)
adjusted_capital_gains_reduced_by_investment_income: 2_000

- name: non_sch_d_capital_gains < 0, investment_income_form_4952 > 0
period: 2019
input:
long_term_capital_gains: 2_000
qualified_dividend_income: 2_000
net_capital_gains: 1_000
non_sch_d_capital_gains: -3_000
investment_income_form_4952: 2_000
output: # min(2000 + 2000, 1000) - 3000 - min(0, 2000)
adjusted_capital_gains_reduced_by_investment_income: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
- name: has_qdiv_or_ltcg
period: 2019
input:
reduced_qualified_dividends_by_adjusted_investment_interest: 1_000
adjusted_capital_gains_reduced_by_investment_income: 1_000
long_term_capital_gains: 2_000
qualified_dividend_income: 2_000
net_capital_gains: 1_000
non_sch_d_capital_gains: 1_000
has_qdiv_or_ltcg: true
output: # 1000 + 1000
computed_dividends_gains_whether_has_gains: 2_000

- name: no has_qdiv_or_ltcg, add_qdiv_or_ltcg > net_capital_gains
period: 2019
input:
reduced_qualified_dividends_by_adjusted_investment_interest: 1_000
adjusted_capital_gains_reduced_by_investment_income: 1_000
long_term_capital_gains: 2_000
qualified_dividend_income: 2_000
net_capital_gains: 1_000
non_sch_d_capital_gains: 2_000
has_qdiv_or_ltcg: false
output: # min(2000 + 2000, 1000) + 2000
computed_dividends_gains_whether_has_gains: 3_000

- name: no has_qdiv_or_ltcg, add_qdiv_or_ltcg < net_capital_gains
period: 2019
input:
reduced_qualified_dividends_by_adjusted_investment_interest: 1_000
adjusted_capital_gains_reduced_by_investment_income: 1_000
long_term_capital_gains: 2_000
qualified_dividend_income: 2_000
net_capital_gains: 5_000
non_sch_d_capital_gains: 2_000
has_qdiv_or_ltcg: false
output: # min(2000 + 2000, 5000) + 2000
computed_dividends_gains_whether_has_gains: 6_000

- name: no has_qdiv_or_ltcg, net_capital_gains < 0
period: 2019
input:
reduced_qualified_dividends_by_adjusted_investment_interest: 1_000
adjusted_capital_gains_reduced_by_investment_income: 1_000
long_term_capital_gains: 2_000
qualified_dividend_income: 2_000
net_capital_gains: -1_000
non_sch_d_capital_gains: 4_000
has_qdiv_or_ltcg: false
output: # max(0, min(2000 + 2000, -1000)) + 4000
computed_dividends_gains_whether_has_gains: 4_000
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
- name: has_qdiv_or_ltcg, taxable_income_minus_gains < taxable_income < capital_gains
period: 2017
input:
filing_status: SINGLE
taxable_income: 4_000
taxable_income_minus_gains: 3_000
computed_dividends_gains_whether_has_gains: 2_000
has_qdiv_or_ltcg: true
output: # max(min(37950, 4000, 3000), 4000 - 2000) * 1
computed_final_gains_after_all_adjustments: 3_000

- name: has_qdiv_or_ltcg, capital_gains < taxable_income_minus_gains < taxable_income
period: 2017
input:
filing_status: SINGLE
taxable_income: 50_000
taxable_income_minus_gains: 45_000
computed_dividends_gains_whether_has_gains: 20_000
has_qdiv_or_ltcg: true
output: # max(min(37950, 50000, 45000), 50000 - 20000) * 1
computed_final_gains_after_all_adjustments: 37_950

- name: has_qdiv_or_ltcg, capital_gains < taxable_income_minus_gains < taxable_income, taxable_income < computed_dividends_gains_whether_has_gains
period: 2017
input:
filing_status: SINGLE
taxable_income: 10_000
taxable_income_minus_gains: 5_000
computed_dividends_gains_whether_has_gains: 20_000
has_qdiv_or_ltcg: true
output: # max(min(37950, 10000, 5000), 10000 - 20000) * 1
computed_final_gains_after_all_adjustments: 5_000

- name: no has_qdiv_or_ltcg
period: 2017
input:
filing_status: SINGLE
taxable_income: 10_000
taxable_income_minus_gains: 5_000
computed_dividends_gains_whether_has_gains: 20_000
has_qdiv_or_ltcg: false
output: # max(min(37950, 10000, 5000), 10000 - 20000) * 0
computed_final_gains_after_all_adjustments: 0

- name: with additional steps
period: 2018
input:
filing_status: SINGLE
taxable_income: 200_000
taxable_income_minus_gains: 190_000
computed_dividends_gains_whether_has_gains: 50_000
has_qdiv_or_ltcg: true
output: # max(min(157500, 200000, 190000), 200000 - 50000) * 1
computed_final_gains_after_all_adjustments: 157_500
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
- name: has_qdiv_or_ltcg, added_1250_and_28_percent_rate_gains < adjusted_capital_gains_reduced_by_investment_income
period: 2019
input:
unrecaptured_section_1250_gain: 1_000
capital_gains_28_percent_rate_gain: 1_000
adjusted_capital_gains_reduced_by_investment_income: 3_000
computed_dividends_gains_whether_has_gains: 2_000
has_qdiv_or_ltcg: true
output: # (2000 - min(1000 + 1000, 3000)) * 1
computed_gains_after_1250_and_28_percent_rate_gains: 0

- name: has_qdiv_or_ltcg, added_1250_and_28_percent_rate_gains < adjusted_capital_gains_reduced_by_investment_income
period: 2019
input:
unrecaptured_section_1250_gain: 1_000
capital_gains_28_percent_rate_gain: 1_000
adjusted_capital_gains_reduced_by_investment_income: 1_000
computed_dividends_gains_whether_has_gains: 2_000
has_qdiv_or_ltcg: true
output: # (2000 - min(1000 + 1000, 1000)) * 1
computed_gains_after_1250_and_28_percent_rate_gains: 1_000

- name: no has_qdiv_or_ltcg
period: 2019
input:
unrecaptured_section_1250_gain: 1_000
capital_gains_28_percent_rate_gain: 1_000
adjusted_capital_gains_reduced_by_investment_income: 1_000
computed_dividends_gains_whether_has_gains: 2_000
has_qdiv_or_ltcg: false
output: # (2000 - min(1000 + 1000, 1000)) * 0
computed_gains_after_1250_and_28_percent_rate_gains: 0

- name: negative result
period: 2019
input:
unrecaptured_section_1250_gain: 2_000
capital_gains_28_percent_rate_gain: 1_000
adjusted_capital_gains_reduced_by_investment_income: 4_000
computed_dividends_gains_whether_has_gains: 2_000
has_qdiv_or_ltcg: true
output: # (2000 - min(2000 + 1000, 4000)) * 1
computed_gains_after_1250_and_28_percent_rate_gains: -1_000
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
- name: qualified_dividend_income larger than investment_income_form_4952
period: 2019
input:
qualified_dividend_income: 2_000
investment_income_form_4952: 1_000
output:
reduced_qualified_dividends_by_adjusted_investment_interest: 1_000

- name: qualified_dividend_income smaller than investment_income_form_4952
period: 2019
input:
qualified_dividend_income: 1_000
investment_income_form_4952: 2_000
output:
reduced_qualified_dividends_by_adjusted_investment_interest: 0

- name: investment_income_form_4952 smaller than 0
period: 2019
input:
qualified_dividend_income: 2_000
investment_income_form_4952: -2_000
output:
reduced_qualified_dividends_by_adjusted_investment_interest: 2_000
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- name: has_qdiv_or_ltcg, taxable_income > computed_gains_after_1250_and_28_percent_rate_gains
period: 2019
input:
taxable_income: 3_000
computed_gains_after_1250_and_28_percent_rate_gains: 1_000
has_qdiv_or_ltcg: true
output: # (3000 - 1000) * 1
taxable_income_minus_gains: 2_000

- name: has_qdiv_or_ltcg, taxable_income < computed_gains_after_1250_and_28_percent_rate_gains
period: 2019
input:
taxable_income: 3_000
computed_gains_after_1250_and_28_percent_rate_gains: 4_000
has_qdiv_or_ltcg: true
output: # (3000 - 4000) * 1
taxable_income_minus_gains: 0

- name: no has_qdiv_or_ltcg
period: 2019
input:
taxable_income: 3_000
computed_gains_after_1250_and_28_percent_rate_gains: 1_000
has_qdiv_or_ltcg: false
output: # (3000 - 1000) * 0
taxable_income_minus_gains: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from policyengine_us.model_api import *


class capital_gains_taxable_difference_after_gains_adjustments(Variable):
value_type = float
entity = TaxUnit
definition_period = YEAR
label = (
"Capital gains taxable difference after gains adjustments" # DWKS20
)
unit = USD
reference = (
"https://www.irs.gov/pub/irs-pdf/f1040sd.pdf",
"https://www.irs.gov/pub/irs-prior/i1040sd--2017.pdf#page=15",
"https://www.irs.gov/pub/irs-prior/i1040sd--2018.pdf#page=19",
"https://www.irs.gov/pub/irs-prior/i1040sd--2023.pdf#page=16",
)

def formula(tax_unit, period, parameters):
# Schedule D Tax Worksheet line 1
taxable_income = tax_unit("taxable_income", period) # dwks1
# Schedule D Tax Worksheet line 15
filing_status = tax_unit("filing_status", period)
capital_gains = parameters(period).gov.irs.capital_gains.brackets
# Schedule D Tax Worksheet line 16
min_capital_gains_and_taxable_income_1 = min_(
capital_gains.thresholds["1"][filing_status], taxable_income
) # dwks16
# Schedule D Tax Worksheet line 17
min_capital_gains_and_taxable_income_and_taxable_income_minus_gains = (
min_(
tax_unit("taxable_income_minus_gains", period),
min_capital_gains_and_taxable_income_1,
)
) # dwks17 = min_(tax_unit("dwks14", period), dwks16)
# Schedule D Tax Worksheet line 20 for 2017 and 2018, or line 22 for years after
return (
min_capital_gains_and_taxable_income_1
- min_capital_gains_and_taxable_income_and_taxable_income_minus_gains
) # dwks20 = dwks16 - dwks17
Loading
Loading