Skip to content
Merged
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/csfp-take-up-input.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an annual CSFP take-up input that defaults to true and lets callers decline payments without changing eligibility.
137 changes: 137 additions & 0 deletions policyengine_us/tests/core/test_csfp_takeup_invariants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
"""Check CSFP take-up across people and years in one vectorized simulation.

YAML covers fixed policy cases. With explicit flags for each year, Hypothesis
checks that changing one person's input changes only their payment for that
year. Each example
also compares default and all-true take-up with the payment before the gate and
recomputes eligibility after each change to detect any take-up dependency.
"""

import numpy as np
from hypothesis import HealthCheck, given, settings
from hypothesis import strategies as st

from policyengine_us import Simulation

YEARS = (2019, 2024, 2025, 2026)
BENEFIT = "commodity_supplemental_food_program"
ELIGIBILITY = "commodity_supplemental_food_program_eligible"
TAKEUP = "takes_up_csfp_if_eligible"
# Each SPM unit's poverty guideline; income equals the drawn ratio times it.
GUIDELINE = 10_000

people = st.tuples(
st.integers(0, 100),
st.sampled_from([0.0, 0.5, 1.0, 1.3, 1.5, 2.0, 3.0]),
st.tuples(*(st.booleans() for _ in YEARS)),
)


def _simulation(rows):
persons = {}
groups = {
name: {} for name in ("tax_units", "marital_units", "spm_units", "families")
}
for i, (age, ratio, _) in enumerate(rows):
person = f"person_{i}"
persons[person] = {
"age": {year: age for year in YEARS},
"tx_dta_csfp_income_eligible": {year: False for year in YEARS},
"ks_dcf_csfp_categorically_eligible": {
f"{year}-12": False for year in YEARS
},
}
for name, units in groups.items():
units[f"{name}_{i}"] = {"members": [person]}
# Eligibility divides csfp_countable_income by spm_unit_fpg, so a ratio
# of 1.3 gives 13,000 / 10,000 = 1.3 and the anchor's 100.0 gives
# 1,000,000 / 10,000 = 100.
spm_unit = groups["spm_units"][f"spm_units_{i}"]
spm_unit["csfp_countable_income"] = {year: ratio * GUIDELINE for year in YEARS}
spm_unit["spm_unit_fpg"] = {year: GUIDELINE for year in YEARS}
# Share a household so a household-wide take-up gate would fail.
household = {
"members": list(persons),
"state_code": {year: "CA" for year in YEARS},
}
for name in (
"ks_dcf_csfp_county_eligible",
"ma_dese_csfp_county_eligible",
"il_dhs_csfp_county_eligible",
):
household[name] = {year: True for year in YEARS}
return Simulation(
situation={
"people": persons,
"households": {"household": household},
**groups,
}
)


def _recalculate(simulation, year):
# Clear both outputs to recompute eligibility after changing take-up.
simulation.delete_arrays(BENEFIT, year)
simulation.delete_arrays(ELIGIBILITY, year)
eligibility = np.asarray(simulation.calculate(ELIGIBILITY, year)).copy()
benefit = np.asarray(simulation.calculate(BENEFIT, year)).copy()
return eligibility, benefit


@settings(
max_examples=6,
deadline=None,
derandomize=True,
suppress_health_check=[HealthCheck.too_slow, HealthCheck.data_too_large],
)
@given(
drawn=st.lists(people, min_size=4, max_size=12),
changed_year=st.sampled_from(YEARS),
)
def test_csfp_takeup_invariants(drawn, changed_year):
# Anchor an eligible person and two people who fail age or income.
rows = [
(70, 0.0, (False, True, False, True)),
(30, 0.0, (True, False, True, False)),
(70, 100.0, (True, True, True, True)),
*drawn,
]
simulation = _simulation(rows)
eligibility_before = {}
old_payments = {}
for year in YEARS:
amount = simulation.tax_benefit_system.parameters(year).gov.usda.csfp.amount
eligibility_before[year], default_benefit = _recalculate(simulation, year)
assert eligibility_before[year][0]
assert not eligibility_before[year][1:3].any()
old_payments[year] = eligibility_before[year] * amount
assert np.asarray(simulation.calculate(TAKEUP, year)).all()
np.testing.assert_array_equal(default_benefit, old_payments[year])

for year in YEARS:
amount = simulation.tax_benefit_system.parameters(year).gov.usda.csfp.amount
flags = np.array([row[2][YEARS.index(year)] for row in rows])
simulation.set_input(TAKEUP, year, flags)
eligible, benefit = _recalculate(simulation, year)
np.testing.assert_array_equal(eligible, eligibility_before[year])
assert (benefit >= 0).all()
assert (benefit <= amount).all()
assert (benefit[~flags | ~eligible] == 0).all()
np.testing.assert_array_equal(benefit, old_payments[year] * flags)

for year in YEARS:
simulation.set_input(TAKEUP, year, np.ones(len(rows), dtype=bool))
eligible, benefit = _recalculate(simulation, year)
np.testing.assert_array_equal(eligible, eligibility_before[year])
np.testing.assert_array_equal(benefit, old_payments[year])

flags = np.ones(len(rows), dtype=bool)
flags[0] = False
simulation.set_input(TAKEUP, changed_year, flags)
for year in YEARS:
eligible, benefit = _recalculate(simulation, year)
np.testing.assert_array_equal(eligible, eligibility_before[year])
expected = old_payments[year].copy()
if year == changed_year:
expected[0] = 0
np.testing.assert_array_equal(benefit, expected)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- name: Age ineligible
period: 2019
input:
takes_up_csfp_if_eligible: true
csfp_countable_income: 13_000
spm_unit_fpg: 10_000
age: 59
Expand All @@ -10,6 +11,7 @@
- name: Income ineligible
period: 2019
input:
takes_up_csfp_if_eligible: true
csfp_countable_income: 13_100
spm_unit_fpg: 10_000
age: 60
Expand All @@ -19,6 +21,7 @@
- name: Eligible
period: 2019
input:
takes_up_csfp_if_eligible: true
csfp_countable_income: 13_000
spm_unit_fpg: 10_000
age: 60
Expand All @@ -28,6 +31,7 @@
- name: Case 4, eligible in 2024.
period: 2024
input:
takes_up_csfp_if_eligible: true
csfp_countable_income: 13_000
spm_unit_fpg: 10_000
age: 60
Expand All @@ -37,6 +41,7 @@
- name: Case 5, eligible in 2025 with updated amount.
period: 2025
input:
takes_up_csfp_if_eligible: true
csfp_countable_income: 15_000
spm_unit_fpg: 10_000
age: 60
Expand All @@ -46,6 +51,7 @@
- name: Case 6, eligible in 2026 with updated amount.
period: 2026
input:
takes_up_csfp_if_eligible: true
csfp_countable_income: 15_000
spm_unit_fpg: 10_000
age: 60
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- name: Income integration test ineligible
period: 2019
input:
takes_up_csfp_if_eligible: true
employment_income: 3_248
self_employment_income: 3_248
rental_income: 3_248
Expand All @@ -13,6 +14,7 @@
- name: Income integration test eligible
period: 2019
input:
takes_up_csfp_if_eligible: true
employment_income: 3_247
self_employment_income: 3_247
rental_income: 3_247
Expand All @@ -25,6 +27,7 @@
- name: Child support received counts toward CSFP income, pushing over the limit
period: 2026
input:
takes_up_csfp_if_eligible: true
employment_income: 21_600
child_support_received: 3_000
age: 62
Expand All @@ -34,6 +37,7 @@
- name: Same earnings without child support stay under the limit
period: 2026
input:
takes_up_csfp_if_eligible: true
employment_income: 21_600
age: 62
output:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
- name: CSFP pays only eligible people who take it up
period: 2025
input:
commodity_supplemental_food_program_eligible: [true, true, false, false]
takes_up_csfp_if_eligible: [true, false, true, false]
output:
# $601 times eligibility times take-up gives [601, 0, 0, 0].
commodity_supplemental_food_program: [601, 0, 0, 0]

- name: CSFP preserves payments when take-up is omitted
period: 2025
input:
commodity_supplemental_food_program_eligible: true
output:
takes_up_csfp_if_eligible: true
# $601 times the default true take-up flag gives $601.
commodity_supplemental_food_program: 601

- name: Declining CSFP leaves eligibility unchanged
period: 2025
input:
age: 60
csfp_countable_income: 15_000
spm_unit_fpg: 10_000
takes_up_csfp_if_eligible: false
output:
# Age 60 meets the minimum of 60, and 15,000 / 10,000 = 1.5 meets the
# 2025 limit of 1.5.
commodity_supplemental_food_program_eligible: true
# $601 times false gives $0 despite eligibility.
commodity_supplemental_food_program: 0

- name: Taking up CSFP does not override age eligibility
period: 2025
input:
age: 59
csfp_countable_income: 15_000
spm_unit_fpg: 10_000
takes_up_csfp_if_eligible: true
output:
# 15,000 / 10,000 = 1.5 meets the 2025 limit of 1.5, and age 59 falls
# short of the minimum of 60.
commodity_supplemental_food_program_eligible: false
# An ineligible person receives $0 even when take-up equals true.
commodity_supplemental_food_program: 0
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class commodity_supplemental_food_program(Variable):
definition_period = YEAR
label = "Commodity Supplemental Food Program"
defined_for = "commodity_supplemental_food_program_eligible"
adds = ["gov.usda.csfp.amount"]

def formula(person, period, parameters):
amount = parameters(period).gov.usda.csfp.amount
takes_up = person("takes_up_csfp_if_eligible", period)
return amount * takes_up
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from policyengine_us.model_api import *


class takes_up_csfp_if_eligible(Variable):
value_type = bool
entity = Person
label = "Takes up the Commodity Supplemental Food Program if eligible"
documentation = (
"Whether this person receives CSFP when eligible. Set this annual input "
"during data construction or in a household calculation. It defaults "
"to true to preserve payments when omitted. Set it to false to decline "
"payments. The input does not determine eligibility or enforce the "
"caseload limits in 7 CFR 247.11."
)
reference = "https://www.govinfo.gov/content/pkg/CFR-2025-title7-vol4/pdf/CFR-2025-title7-vol4-sec247-11.pdf#page=1"
definition_period = YEAR
default_value = True
Loading