Skip to content
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
1 change: 1 addition & 0 deletions changelog.d/read-only-simulation-branch-removal.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The marginal tax rates, `marginal_tax_rate_on_capital_gains`, `employer_ss_tax_income_tax_revenue` and `employer_medicare_tax_income_tax_revenue` now remove their temporary branch with `simulation.branches.pop(name, None)`, so they calculate on a simulation that keeps no branch it creates, such as policyengine-core's planned read-only simulations, instead of raising `KeyError`. Calculated in a branch with the name they give their own branch, they now raise `ValueError` before changing that branch's inputs or system.
198 changes: 198 additions & 0 deletions policyengine_us/tests/core/test_unkept_temporary_branches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
"""Formulas that remove a temporary branch calculate when it was never kept.

``marginal_tax_rate`` and its component rates (through
``marginal_earnings_change``), ``marginal_tax_rate_on_capital_gains``,
``employer_ss_tax_income_tax_revenue`` and
``employer_medicare_tax_income_tax_revenue`` each create a branch of the
simulation with ``get_branch``, calculate in it, then remove it from
``simulation.branches``. policyengine-core's read-only simulations
(``Simulation.make_read_only``, not yet released) give a new branch for a name
they have not registered and do not keep it in ``branches``, so
``del simulation.branches[name]`` raised ``KeyError`` on them.

The released core keeps every branch ``get_branch`` creates, so these tests
give chosen simulations a ``get_branch`` that removes each new branch from
``branches`` before returning it, as a read-only simulation keeps none. Each
formula must give the value it gives on a plain simulation of the same
household, after creating its branch on the simulation that keeps none.

Cost: each case builds two one-person simulations; the reform case also
applies a parametric reform to each, the largest part of the file's cost.
"""

import numpy as np
import pytest
from policyengine_core.reforms import Reform
from policyengine_core.simulations import Simulation as CoreSimulation

from policyengine_us import Simulation

YEAR = 2024


def one_person_household(**person_inputs):
"""A California household of one 40-year-old with ``person_inputs`` in YEAR."""
person = {"age": {YEAR: 40}}
person.update({name: {YEAR: value} for name, value in person_inputs.items()})
return {
"people": {"person": person},
"tax_units": {"tax_unit": {"members": ["person"]}},
"spm_units": {"spm_unit": {"members": ["person"]}},
"families": {"family": {"members": ["person"]}},
"marital_units": {"marital_unit": {"members": ["person"]}},
"households": {
"household": {"members": ["person"], "state_code": {YEAR: "CA"}}
},
}


# Including employer payroll taxes in income makes the employer revenue
# formulas branch; with it off they return zero without branching.
EMPLOYER_PAYROLL_TAX_IN_INCOME = {
"gov.contrib.crfb.tax_employer_payroll_tax.in_effect": {
"2020-01-01.2100-12-31": True
},
"gov.contrib.crfb.tax_employer_payroll_tax.percentage": {
"2020-01-01.2100-12-31": 1.0
},
}


@pytest.fixture
def stop_keeping_new_branches(monkeypatch):
"""Return a function that stops a simulation keeping branches it creates.

After ``stop_keeping_new_branches(simulation)``, ``simulation.get_branch``
with a name it has not registered still creates and returns a branch, but
removes it from ``simulation.branches`` first, as a read-only simulation
keeps no branch it creates. The branch itself keeps the branches it
creates, as a read-only simulation's branches do. The function returns the
list of names of branches the simulation created and did not keep, which
grows as it calculates.
"""
unkept_branch_names = []
simulations_keeping_no_new_branches = []
core_get_branch = CoreSimulation.get_branch

def get_branch_keeping_none(self, name="branch", clone_system=False):
creating = name != self.branch_name and name not in self.branches
branch = core_get_branch(self, name, clone_system)
keeps_none = any(
self is simulation for simulation in simulations_keeping_no_new_branches
)
if creating and keeps_none:
del self.branches[name]
unkept_branch_names.append(name)
return branch

monkeypatch.setattr(CoreSimulation, "get_branch", get_branch_keeping_none)

def stop_keeping(simulation):
simulations_keeping_no_new_branches.append(simulation)
return unkept_branch_names

return stop_keeping


def test_the_test_simulation_keeps_no_branch_it_creates(stop_keeping_new_branches):
simulation = Simulation(situation=one_person_household(employment_income=50_000))
registered_before = dict(simulation.branches)
unkept_branch_names = stop_keeping_new_branches(simulation)
first = simulation.get_branch("temporary")
second = simulation.get_branch("temporary")
assert first is not second
assert first.parent_branch is simulation
assert simulation.branches == registered_before
assert unkept_branch_names == ["temporary", "temporary"]
nested = first.get_branch("nested")
assert first.branches == {"nested": nested}


# Each case: the household, the parameter changes it is simulated under, and
# for each formula the name it gives the branch it creates.
CASES = [
pytest.param(
one_person_household(employment_income=50_000, long_term_capital_gains=20_000),
None,
{
"marginal_tax_rate": "mtr_for_adult_1",
"marginal_tax_rate_on_capital_gains": "adult_1_cg_rise",
},
id="current law",
),
pytest.param(
one_person_household(employment_income=50_000),
EMPLOYER_PAYROLL_TAX_IN_INCOME,
{
"employer_ss_tax_income_tax_revenue": "without_employer_ss",
"employer_medicare_tax_income_tax_revenue": "without_employer_medicare",
},
id="employer payroll tax in income",
),
]


def build_simulation(situation, parameter_changes):
if parameter_changes is None:
return Simulation(situation=situation)
reform = Reform.from_dict(parameter_changes, country_id="us")
return Simulation(situation=situation, reform=reform)


@pytest.mark.parametrize("situation, parameter_changes, branch_name_of_variable", CASES)
def test_formulas_give_their_plain_values_when_their_branches_are_not_kept(
stop_keeping_new_branches, situation, parameter_changes, branch_name_of_variable
):
plain = build_simulation(situation, parameter_changes)
plain_values = {
variable: plain.calculate(variable, YEAR)
for variable in branch_name_of_variable
}

simulation = build_simulation(situation, parameter_changes)
registered_before = dict(simulation.branches)
unkept_branch_names = stop_keeping_new_branches(simulation)
for variable, branch_name in branch_name_of_variable.items():
value = simulation.calculate(variable, YEAR)
# A zero would also come from a formula that returned before branching.
assert np.all(plain_values[variable] != 0), variable
np.testing.assert_array_equal(value, plain_values[variable], err_msg=variable)
assert branch_name in unkept_branch_names, variable
assert simulation.branches == registered_before


@pytest.mark.parametrize("situation, parameter_changes, branch_name_of_variable", CASES)
def test_formulas_refuse_to_calculate_in_a_branch_with_their_branch_name(
situation, parameter_changes, branch_name_of_variable
):
"""In a branch with the name a formula gives its branch, it changes nothing.

There ``get_branch`` returns the simulation being calculated, so raising
its inputs or neutralizing variables in its system would change that
simulation, and its result would be wrong.
"""
simulation = build_simulation(situation, parameter_changes)
for variable, branch_name in branch_name_of_variable.items():
branch = simulation.get_branch(branch_name)
system = branch.tax_benefit_system
neutralized_before = {
name
for name, definition in system.variables.items()
if definition.is_neutralized
}
inputs_before = {
name: branch.calculate(name, YEAR).copy()
for name in ("employment_income_before_lsr", "long_term_capital_gains")
}
with pytest.raises(ValueError, match=branch_name):
branch.calculate(variable, YEAR)
neutralized_after = {
name
for name, definition in system.variables.items()
if definition.is_neutralized
}
assert neutralized_after == neutralized_before, variable
for name, value in inputs_before.items():
np.testing.assert_array_equal(
branch.calculate(name, YEAR), value, err_msg=f"{variable}: {name}"
)
21 changes: 21 additions & 0 deletions policyengine_us/tools/branched_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ def get_stored_variables(simulation: Simulation) -> list:
return stored_variables


def get_branch_to_change(
simulation: Simulation, branch_name: str, clone_system: bool = False
) -> Simulation:
"""Return ``simulation.get_branch(branch_name)``, never ``simulation``.

For a formula that changes its branch's inputs or tax-benefit system.
``get_branch`` returns the simulation itself when asked for the
simulation's own branch name, so the formula would change the simulation
it is calculating and give a wrong result. Raise ``ValueError`` instead,
before anything changes.
"""
branch = simulation.get_branch(branch_name, clone_system=clone_system)
if branch is simulation:
raise ValueError(
f"This simulation is the branch {branch_name!r}, the name the"
" formula gives the branch it changes. Calculate it in a"
" simulation with another branch name."
)
return branch


class BranchedSimulation:
def __init__(self, population: Population):
self.simulation = population.simulation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,5 @@ def formula(person, period, parameters): # pragma: no cover
increase = household_net_income_higher_gains - household_net_income
mtr_values += where(mask, 1 - increase / where(mask, stored_rise, 1), 0)

del simulation.branches[branch_name]
simulation.branches.pop(branch_name, None)
return mtr_values
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from policyengine_us.model_api import *
from policyengine_us.tools.branched_simulation import get_branch_to_change


class employer_medicare_tax_income_tax_revenue(Variable):
Expand Down Expand Up @@ -37,7 +38,9 @@ def formula(tax_unit, period, parameters):
tax_full = tax_unit("income_tax", period)

# === Branch: Tax WITHOUT employer Medicare (neutralize source) ===
branch = sim.get_branch("without_employer_medicare", clone_system=True)
branch = get_branch_to_change(
sim, "without_employer_medicare", clone_system=True
)

# Neutralize the source variable - reform formula will pick up 0 for Medicare
branch.tax_benefit_system.neutralize_variable("employer_medicare_tax")
Expand All @@ -52,7 +55,7 @@ def formula(tax_unit, period, parameters):
pass

tax_without_medicare = branch.tax_unit("income_tax", period)
del sim.branches["without_employer_medicare"]
sim.branches.pop("without_employer_medicare", None)

# Medicare revenue = tax with full reform - tax without Medicare
return max_(tax_full - tax_without_medicare, 0)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from policyengine_us.model_api import *
from policyengine_us.tools.branched_simulation import get_branch_to_change


class employer_ss_tax_income_tax_revenue(Variable):
Expand Down Expand Up @@ -37,7 +38,7 @@ def formula(tax_unit, period, parameters):
tax_full = tax_unit("income_tax", period)

# === Branch: Tax WITHOUT employer SS (neutralize source) ===
branch = sim.get_branch("without_employer_ss", clone_system=True)
branch = get_branch_to_change(sim, "without_employer_ss", clone_system=True)

# Neutralize the source variable - reform formula will pick up 0 for SS
branch.tax_benefit_system.neutralize_variable("employer_social_security_tax")
Expand All @@ -52,7 +53,7 @@ def formula(tax_unit, period, parameters):
pass

tax_without_ss = branch.tax_unit("income_tax", period)
del sim.branches["without_employer_ss"]
sim.branches.pop("without_employer_ss", None)

# SS revenue = tax with full reform - tax without SS
return max_(tax_full - tax_without_ss, 0)
13 changes: 10 additions & 3 deletions policyengine_us/variables/household/marginal_tax_rate_helpers.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 make_period
from policyengine_us.tools.branched_simulation import get_branch_to_change
from policyengine_us.tools.period_branch import drop_inherited_values
from policyengine_us.variables.gov.simulation.behavioral_response_measurements import (
NEUTRALIZED_BEHAVIORAL_RESPONSE_VARIABLES,
Expand Down Expand Up @@ -83,11 +84,17 @@ def create_perturbed_branch(simulation, period, branch_name, increments):
Args:
simulation: The simulation to branch from.
period: The period whose inputs rise.
branch_name: Name of the branch; the caller deletes it when done.
branch_name: Name of the branch; when done, the caller removes it
from ``simulation.branches`` if the simulation kept it.
increments: Person-level increase for each raised variable.

Returns:
The branch simulation.

Raises:
ValueError: ``branch_name`` is the simulation's own branch name, so
``get_branch`` would return the simulation itself (see
``get_branch_to_change``).
"""
# US wrappers also register explicit set_input calls after construction,
# including identification and other inputs set on the measuring branch.
Expand All @@ -99,7 +106,7 @@ def create_perturbed_branch(simulation, period, branch_name, increments):
for variable in HELD_BEHAVIORAL_RESPONSE_VARIABLES
if not variables[variable].is_neutralized
}
branch = simulation.get_branch(branch_name)
branch = get_branch_to_change(simulation, branch_name)
for variable in variables:
if variable not in input_variables:
branch.delete_arrays(variable)
Expand Down Expand Up @@ -183,7 +190,7 @@ def marginal_earnings_change(person, period, parameters, measure, branch_prefix)
alt = measure(branch.person)
change += where(mask, (alt - base) / delta, 0)
measured |= mask
del simulation.branches[branch_name]
simulation.branches.pop(branch_name, None)
return change, measured


Expand Down
Loading