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/split-capital-loss-carryover-k1.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Split the capital loss carryover memo inputs into the person's own carryover and one from a terminated estate or trust (Schedule K-1 (Form 1041), box 11, codes C and D), add a short-term carryover memo, and add the short-term carryover back in Arizona property tax credit household income (ITR 12-1 item (7)).
5 changes: 5 additions & 0 deletions policyengine_us/data/economic_assumptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"foreign_tax_credit": DEFAULT_MICRODATA_UPRATING,
"interest_deduction": DEFAULT_MICRODATA_UPRATING,
"long_term_capital_gains": "calibration.gov.irs.soi.long_term_capital_gains",
# long_term_capital_loss_carryover and short_term_capital_loss_carryover
# add their own and estate-or-trust leaves. A dataset that carries either
# sum keeps the series its leaves follow.
"long_term_capital_loss_carryover": "calibration.gov.irs.soi.long_term_capital_gains",
"misc_deduction": DEFAULT_MICRODATA_UPRATING,
"partnership_income": "calibration.gov.irs.soi.partnership_s_corp_income",
"partnership_s_corp_income": "calibration.gov.irs.soi.partnership_s_corp_income",
Expand All @@ -41,6 +45,7 @@
"self_employment_income": "calibration.gov.irs.soi.self_employment_income",
"self_employed_health_insurance_ald": DEFAULT_MICRODATA_UPRATING,
"self_employed_pension_contribution_ald": DEFAULT_MICRODATA_UPRATING,
"short_term_capital_loss_carryover": "calibration.gov.irs.soi.short_term_capital_gains",
"social_security": "calibration.gov.irs.soi.social_security",
"s_corp_income": "calibration.gov.irs.soi.partnership_s_corp_income",
"spm_unit_weight": "calibration.gov.census.populations.total",
Expand Down
95 changes: 95 additions & 0 deletions policyengine_us/system.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import hashlib
from functools import lru_cache
from pathlib import Path

import numpy as np
from policyengine_core.taxbenefitsystems import TaxBenefitSystem
from policyengine_us.entities import *
from policyengine_us.parameters.gov.irs.uprating import (
Expand Down Expand Up @@ -217,6 +219,95 @@ def _backfill_state_code_from_str(simulation):
state_code_str.delete_arrays(known_period)


CAPITAL_LOSS_CARRYOVER_SUMS = {
"long_term_capital_loss_carryover": (
"own_long_term_capital_loss_carryover",
"estate_or_trust_long_term_capital_loss_carryover",
),
"short_term_capital_loss_carryover": (
"own_short_term_capital_loss_carryover",
"estate_or_trust_short_term_capital_loss_carryover",
),
}


def _file_capital_loss_carryover_sums(simulation):
"""File an entered capital loss carryover sum as the person's own carryover.

Each sum adds the person's own carryover and one from a terminated estate
or trust. Core does not uprate a variable that adds others, and a sum
entered for anyone in a simulation would otherwise be an input for
everyone in it. So, mirroring the ``employment_income`` ->
``employment_income_before_lsr`` moves, for each period a sum is entered
for and each person it is nonzero for, the person's own carryover becomes
the sum, which uprates to later periods as that leaf does, and their
estate or trust carryover becomes zero, even if it would otherwise uprate
from an earlier period. Everyone else keeps the leaf values they had for
that period, read before any period is filed, so a sum filed for one
period never feeds another's. The sum is then computed from its leaves.

Core clones a reform's baseline branch before this runs, so every branch
that exists at construction is filed the same way.
"""
for branch in [simulation, *simulation.branches.values()]:
_file_branch_capital_loss_carryover_sums(branch)


def _file_branch_capital_loss_carryover_sums(simulation):
filings = []
for total, (own, estate_or_trust) in CAPITAL_LOSS_CARRYOVER_SUMS.items():
holder = simulation.get_holder(total)
for known_period in holder.get_known_periods():
filings.append(
(
holder,
known_period,
holder.get_array(known_period),
{
leaf: _leaf_value(simulation, leaf, known_period)
for leaf in (own, estate_or_trust)
},
own,
estate_or_trust,
)
)
for holder, known_period, amount, current, own, estate_or_trust in filings:
entered = amount != 0
for leaf, value in [(own, amount), (estate_or_trust, 0)]:
if current[leaf] is None and leaf == estate_or_trust:
# Zero for everyone already.
continue
other = 0 if current[leaf] is None else current[leaf]
simulation.set_input(leaf, known_period, np.where(entered, value, other))
holder.delete_arrays(known_period)
if getattr(simulation, "input_variables", None) is not None:
names = [
name
for name in simulation.input_variables
if name not in CAPITAL_LOSS_CARRYOVER_SUMS
]
for leaves in CAPITAL_LOSS_CARRYOVER_SUMS.values():
for leaf in leaves:
known = simulation.get_holder(leaf).get_known_periods()
if known and leaf not in names:
names.append(leaf)
simulation.input_variables = names


def _leaf_value(simulation, leaf, period):
"""A carryover leaf's value for the period: its input for the period, or
what it uprates to from an earlier input, or what a formula a reform gave
it returns. None when it has none of those, so it is zero for everyone."""
holder = simulation.get_holder(leaf)
array = holder.get_array(period)
if array is not None:
return array
variable = simulation.tax_benefit_system.get_variable(leaf)
if holder.get_known_periods() or not variable.is_input_variable():
return np.asarray(simulation.calculate(leaf, period))
return None


class Simulation(SPMSimulationMixin, CoreSimulation):
"""
A simulation of the tax-benefit system for the United States,
Expand Down Expand Up @@ -296,6 +387,8 @@ def __init__(self, *args, **kwargs):
)
cg_holder.delete_arrays(known_period)

_file_capital_loss_carryover_sums(self)

# Geography backfill: state_code_str-only input -> state_code enum.
_backfill_state_code_from_str(self)

Expand Down Expand Up @@ -556,6 +649,8 @@ def __init__(self, *args, **kwargs):
)
cg_holder.delete_arrays(known_period)

_file_capital_loss_carryover_sums(self)

# Geography backfill: state_code_str-only input -> state_code enum.
# Datasets carry state_fips, so this only fires for situations that
# explicitly supply state_code_str.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
Port the specialty dependent cohorts from PR #9977's investment-interest
properties without its Form 4952 calculations. YAML covers policy examples
and regular tax; Hypothesis covers arbitrary vectorized populations, changes
to dependents' inputs, and redistribution between the head and spouse.
to dependents' inputs, and redistribution between the head and spouse. A
differential checks that entering long_term_capital_loss_carryover as its two
leaves, the person's own carryover and one from a terminated estate or trust
(Schedule K-1 (Form 1041), box 11, code D), gives the same worksheet lines as
entering their sum, as older callers and datasets do.

Each example constructs a small Core system containing the production
worksheet classes and their inputs, without reconstructing the full US
Expand Down Expand Up @@ -38,6 +42,9 @@
from policyengine_us.variables.household.income.person.capital_gains.collectibles_gain_or_loss import (
collectibles_gain_or_loss,
)
from policyengine_us.variables.household.income.person.capital_gains.estate_or_trust_long_term_capital_loss_carryover import (
estate_or_trust_long_term_capital_loss_carryover,
)
from policyengine_us.variables.household.income.person.capital_gains.long_term_capital_gains_on_collectibles import (
long_term_capital_gains_on_collectibles,
)
Expand All @@ -47,6 +54,9 @@
from policyengine_us.variables.household.income.person.capital_gains.long_term_capital_loss_carryover import (
long_term_capital_loss_carryover,
)
from policyengine_us.variables.household.income.person.capital_gains.own_long_term_capital_loss_carryover import (
own_long_term_capital_loss_carryover,
)
from policyengine_us.variables.household.income.person.capital_gains.section_1202_gain import (
section_1202_gain,
)
Expand All @@ -70,6 +80,11 @@
long_term_capital_gains_on_small_business_stock,
)
INPUTS = tuple(variable.__name__ for variable in INPUT_CLASSES)
# long_term_capital_loss_carryover adds these; an entered sum is used instead.
CARRYOVER_LEAVES = (
own_long_term_capital_loss_carryover,
estate_or_trust_long_term_capital_loss_carryover,
)
OUTPUTS = (
"rate_gains_less_losses",
"capital_gains_28_percent_rate_gain",
Expand All @@ -83,6 +98,7 @@ def _simulate(units, *, reverse_members=False):
system = TaxBenefitSystem(entities=[Person, TaxUnit])
system.add_variables(
*INPUT_CLASSES,
*CARRYOVER_LEAVES,
is_tax_unit_dependent,
unrecaptured_section_1250_gain,
unrecaptured_section_1250_gain_before_losses,
Expand Down Expand Up @@ -124,6 +140,10 @@ def _simulate(units, *, reverse_members=False):
for name in OUTPUTS
if name != "rate_gains_less_losses"
},
# Per person, in the order the units list them.
"long_term_capital_loss_carryover": simulation.calculate(
"long_term_capital_loss_carryover", PERIOD
),
}


Expand Down Expand Up @@ -262,3 +282,34 @@ def test_vectorized_worksheets_depend_only_on_non_dependents(units):
np.testing.assert_array_equal(
results[name][size : 2 * size], expected, err_msg=name
)


@settings(max_examples=60, deadline=None)
@given(units=UNITS, data=st.data())
def test_carryover_leaves_give_the_same_worksheets_as_their_sum(units, data):
"""Differential: each person's long-term carryover entered as its sum, or
split at random between the person's own carryover and one from a
terminated estate or trust, gives the same carryover and the same worksheet
lines. Dependents' carryovers are split too; the worksheets leave them out
either way. The two cohorts run in separate simulations: a sum entered for
anyone in a simulation is an input for everyone in it."""
as_leaves = deepcopy(units)
totals = []
for unit in as_leaves:
people = [unit["head"], *unit["dependents"]]
if unit["spouse"] is not None:
people.insert(1, unit["spouse"])
for person in people:
total = person.pop("long_term_capital_loss_carryover")
own = data.draw(st.integers(min_value=0, max_value=total))
person["own_long_term_capital_loss_carryover"] = own
person["estate_or_trust_long_term_capital_loss_carryover"] = total - own
totals.append(total)

summed, leaves = _simulate(units), _simulate(as_leaves)
for results in (summed, leaves):
np.testing.assert_array_equal(
results["long_term_capital_loss_carryover"], totals
)
for name in OUTPUTS:
np.testing.assert_array_equal(leaves[name], summed[name], err_msg=name)
Loading