Skip to content

Commit

Permalink
Apply utility functions to rest of reforms, delete dc_ctc
Browse files Browse the repository at this point in the history
  • Loading branch information
leehengpan committed Oct 30, 2024
1 parent b8311f3 commit 1ce28f7
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 421 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from policyengine_us.model_api import *
from policyengine_us.reforms.utils import create_reform_two_threshold_check
import operator


def create_medicare_and_investment_tax_increase() -> Reform:
Expand Down Expand Up @@ -71,15 +73,19 @@ def apply(self):
def create_medicare_and_investment_tax_increase_reform(
parameters, period, bypass: bool = False
):
if bypass:
return create_medicare_and_investment_tax_increase()

p = parameters(period).gov.contrib.biden.budget_2025

if (p.medicare.rate > 0) | (p.net_investment_income.rate > 0):
return create_medicare_and_investment_tax_increase()
else:
return None
return create_reform_two_threshold_check(
parameters=parameters,
period=period,
parameter_path="gov.contrib.biden.budget_2025",
reform_function=create_medicare_and_investment_tax_increase,
comparison_parameter_path_1="medicare.rate",
comparison_parameter_path_2="net_investment_income.rate",
threshold_check_1=0,
threshold_check_2=0,
comparison_operator_1=operator.gt,
comparison_operator_2=operator.gt,
bypass=bypass,
)


medicare_and_investment_tax_increase = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from policyengine_us.model_api import *
from policyengine_us.reforms.utils import create_reform_threshold_check
import operator
import numpy as np


def create_increase_taxable_earnings_for_social_security() -> Reform:
Expand Down Expand Up @@ -29,15 +32,16 @@ def apply(self):
def create_increase_taxable_earnings_for_social_security_reform(
parameters, period, bypass: bool = False
):
if bypass:
return create_increase_taxable_earnings_for_social_security()

p = parameters(period).gov.contrib.cbo.payroll

if p.secondary_earnings_threshold < np.inf:
return create_increase_taxable_earnings_for_social_security()
else:
return None
return create_reform_threshold_check(
reform_function=create_increase_taxable_earnings_for_social_security,
parameters=parameters,
period=period,
parameter_path="gov.contrib.cbo.payroll",
comparison_parameter_path="secondary_earnings_threshold",
comparison_operator=operator.lt,
threshold_check=np.inf,
bypass=bypass,
)


increase_taxable_earnings_for_social_security = (
Expand Down
31 changes: 12 additions & 19 deletions policyengine_us/reforms/congress/delauro/american_family_act.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from policyengine_us.model_api import *
from policyengine_core.periods import period as period_
from policyengine_us.reforms.utils import create_reform_threshold_check
import operator


def create_american_family_act_with_baby_bonus() -> Reform:
Expand Down Expand Up @@ -35,24 +36,16 @@ def apply(self):
def create_american_family_act_with_baby_bonus_reform(
parameters, period, bypass: bool = False
):
if bypass:
return create_american_family_act_with_baby_bonus()

p = parameters.gov.contrib.congress.delauro.american_family_act

reform_active = False
current_period = period_(period)

for i in range(5):
if p(current_period).baby_bonus > 0:
reform_active = True
break
current_period = current_period.offset(1, "year")

if reform_active:
return create_american_family_act_with_baby_bonus()
else:
return None
return create_reform_threshold_check(
reform_function=create_american_family_act_with_baby_bonus,
parameters=parameters,
period=period,
parameter_path="gov.contrib.congress.delauro.american_family_act",
comparison_parameter_path="baby_bonus",
comparison_operator=operator.gt,
threshold_check=0,
bypass=bypass,
)


american_family_act = create_american_family_act_with_baby_bonus_reform(
Expand Down
6 changes: 0 additions & 6 deletions policyengine_us/reforms/reforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
from .biden.budget_2025 import create_capital_gains_tax_increase_reform
from .eitc import create_halve_joint_eitc_phase_out_rate_reform
from .states.ny.wftc import create_ny_working_families_tax_credit_reform
from .states.dc.dc_ctc import (
create_dc_ctc_reform,
)
from .harris.lift.middle_class_tax_credit import (
create_middle_class_tax_credit_reform,
)
Expand Down Expand Up @@ -92,8 +89,6 @@ def create_structural_reforms_from_parameters(parameters, period):
)
ny_wftc = create_ny_working_families_tax_credit_reform(parameters, period)

dc_ctc = create_dc_ctc_reform(parameters, period)

middle_class_tax_credit = create_middle_class_tax_credit_reform(
parameters, period
)
Expand Down Expand Up @@ -136,7 +131,6 @@ def create_structural_reforms_from_parameters(parameters, period):
capital_gains_tax_increase,
halve_joint_eitc_phase_out_rate,
ny_wftc,
dc_ctc,
middle_class_tax_credit,
rent_relief_tax_credit,
end_child_poverty_act,
Expand Down
3 changes: 0 additions & 3 deletions policyengine_us/reforms/states/dc/__init__.py

This file was deleted.

70 changes: 0 additions & 70 deletions policyengine_us/reforms/states/dc/dc_ctc.py

This file was deleted.

32 changes: 0 additions & 32 deletions policyengine_us/reforms/states/dc/dc_ctc_test_microsim.py

This file was deleted.

106 changes: 101 additions & 5 deletions policyengine_us/reforms/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
from policyengine_core.periods import period as period_
import numpy as np
import operator


def get_nested_value(base_param, path):
value = base_param
for part in path.split("."):
value = getattr(value, part)
return value


def create_reform_if_active(
Expand All @@ -25,15 +34,102 @@ def create_reform_if_active(
return reform_function()

current_period = period_(period)
path_parts = parameter_path.split(".")

p = parameters
for part in path_parts:
p = getattr(p, part)
p = get_nested_value(parameters, parameter_path)

for _ in range(5):
if getattr(p(current_period), active_parameter_path):
return reform_function()
current_period = current_period.offset(1, "year")

return None


def create_reform_threshold_check(
parameters,
period,
parameter_path: str,
comparison_parameter_path: str,
reform_function,
threshold_check: float = np.inf,
comparison_operator=operator.lt,
bypass: bool = False,
):
"""
Create a reform based on a parameter threshold check.
Args:
parameters: PolicyEngine parameters object
period: Time period for the reform
parameter_path: Dot-separated path to the parameter to check
comparison_parameter_path: Dot-separated path to the parameter to compare with threshold
reform_function: The specific reform creation function to call
bypass: If True, skip parameter checks and return reform
threshold_check: Value to compare parameter against
comparison_operator: Operator to use for comparison (default: less than)
"""
if bypass:
return reform_function()

# Navigate parameter tree using the path
p = get_nested_value(parameters, parameter_path)
current_period = period_(period)

for _ in range(5):
if comparison_operator(
getattr(p(current_period), comparison_parameter_path),
threshold_check,
):
return reform_function()
current_period = current_period.offset(1, "year")

return None


def create_reform_two_threshold_check(
parameters,
period,
parameter_path: str,
reform_function,
comparison_parameter_path_1: str,
comparison_parameter_path_2: str,
threshold_check_1: float = np.inf,
threshold_check_2: float = np.inf,
comparison_operator_1=operator.lt,
comparison_operator_2=operator.lt,
bypass: bool = False,
):
"""
Create a reform based on a parameter two-threshold check.
Args:
parameters: PolicyEngine parameters object
period: Time period for the reform
parameter_path: Dot-separated path to the parameter to check
reform_function: The specific reform creation function to call
comparison_parameter_path_1: Dot-separated path to the parameter to compare with threshold 1
comparison_parameter_path_2: Dot-separated path to the parameter to compare with threshold 2
threshold_check_1: Value to compare parameter against threshold 1
threshold_check_2: Value to compare parameter against threshold 2
comparison_operator_1: Operator to use for comparison (default: less than)
comparison_operator_2: Operator to use for comparison (default: less than)
bypass: If True, skip parameter checks and return reform
"""
if bypass:
return reform_function()

# Navigate parameter tree using the path
p = get_nested_value(parameters, parameter_path)
current_period = period_(period)

for _ in range(5):
param_at_period = p(current_period)
value1 = get_nested_value(param_at_period, comparison_parameter_path_1)
value2 = get_nested_value(param_at_period, comparison_parameter_path_2)

if comparison_operator_1(
value1, threshold_check_1
) or comparison_operator_2(value2, threshold_check_2):
return reform_function()
current_period = current_period.offset(1, "year")

return None
Loading

0 comments on commit 1ce28f7

Please sign in to comment.