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/add-school-meals-take-up.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a school-meal take-up input with a true default to preserve existing benefit amounts.
179 changes: 179 additions & 0 deletions policyengine_us/tests/core/test_school_meal_takeup_invariants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
"""Check school-meal take-up across generated SPM units.

Core applies defined_for masks to SPM-unit calculations.
Hypothesis checks those masks across mixed group sizes and participation
settings, including omitted inputs, in one vectorized simulation per example.
YAML covers the fixed policy amounts.

Each simulation has private entities and calculation holders. The tests reuse
the policy system without changing its variables or parameters.
"""

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

from policyengine_us import Simulation
from policyengine_us.system import system

BENEFITS = (
"school_meal_net_subsidy",
"free_school_meals",
"reduced_price_school_meals",
)
TIERS = (
"school_meal_tier",
"school_meal_breakfast_tier",
"school_meal_lunch_tier",
)
TAKEUP = (None, True, False)
TOLERANCE = 0.01

households = st.tuples(
st.sampled_from(["AK", "HI", "CA", "FL", "NH", "NJ", "TX", "UT"]),
st.integers(min_value=0, max_value=500).map(lambda ratio: ratio / 100),
st.booleans(),
st.lists(st.integers(min_value=0, max_value=22), max_size=4).map(
lambda ages: (35, *ages)
),
)


def _situation(rows, year):
situation = {
name: {}
for name in (
"people",
"spm_units",
"households",
"tax_units",
"families",
"marital_units",
)
}
for row, (state, ratio, categorical, ages) in enumerate(rows):
for variant, takes_up in enumerate(TAKEUP):
unit = f"unit_{row}_{variant}"
members = []
for member, age in enumerate(ages):
person = f"person_{row}_{variant}_{member}"
members.append(person)
situation["people"][person] = {"age": {year: age}}
situation["marital_units"][person] = {"members": [person]}
for group in ("spm_units", "households", "tax_units", "families"):
situation[group][unit] = {"members": list(members)}
situation["households"][unit]["state_code"] = {year: state}
spm_unit = situation["spm_units"][unit]
spm_unit["school_meal_fpg_ratio"] = {year: ratio}
spm_unit["meets_school_meal_categorical_eligibility"] = {year: categorical}
if takes_up is not None:
spm_unit["takes_up_school_meals_if_eligible"] = {year: takes_up}
return situation


def _main_amounts(rows, year, breakfast_tier, lunch_tier):
# Recompute main's annual arithmetic per meal to check the share-based split.
p = system.parameters(year).gov.usda.school_meals
expected = {name: [] for name in BENEFITS}
for (state, _, _, ages), breakfast, lunch in zip(rows, breakfast_tier, lunch_tier):
state_group = state if state in ("AK", "HI") else "CONTIGUOUS_US"
breakfast_rates = p.amount.sbp[state_group]
lunch_rates = p.amount.nslp[state_group]
breakfast_value = breakfast_rates[str(breakfast)] - breakfast_rates.PAID
lunch_value = lunch_rates[str(lunch)] - lunch_rates.PAID
child_days = sum(5 <= age <= 17 for age in ages) * p.school_days
expected["school_meal_net_subsidy"].append(
(breakfast_value + lunch_value) * child_days
)
expected["free_school_meals"].append(
(
(breakfast_value if breakfast == "FREE" else 0)
+ (lunch_value if lunch == "FREE" else 0)
)
* child_days
)
expected["reduced_price_school_meals"].append(
(
(breakfast_value if breakfast == "REDUCED" else 0)
+ (lunch_value if lunch == "REDUCED" else 0)
)
* child_days
)
return expected


@settings(
max_examples=6,
deadline=None,
derandomize=True,
suppress_health_check=[HealthCheck.too_slow],
)
@given(
year=st.sampled_from([2020, 2024, 2025, 2026]),
drawn=st.lists(households, max_size=6),
)
def test_school_meal_takeup_preserves_defaults_and_masks_each_spm_unit(year, drawn):
rows = [
# New Hampshire covers the breakfast copay but keeps reduced-price lunch.
("NH", 1.8, False, (35, 10)),
("FL", 1.8, False, (35, 5, 17)),
("FL", 0.5, False, (35, 11)),
("CA", 4.0, False, (40,)),
("FL", 1.8, False, (35, 4, 18)),
("FL", 4.0, False, (35, 10)),
("AK", 0.5, False, (40, 6, 10)),
("HI", 1.8, False, (40, 8)),
*drawn,
]
simulation = Simulation(
tax_benefit_system=system,
situation=_situation(rows, year),
)
participation = simulation.calculate("takes_up_school_meals_if_eligible", year)
np.testing.assert_array_equal(
participation.reshape(-1, 3),
np.tile([True, True, False], (len(rows), 1)),
)

tiers = {}
for variable in TIERS:
values = simulation.calculate(variable, year).decode_to_str().reshape(-1, 3)
np.testing.assert_array_equal(values[:, 0], values[:, 1])
np.testing.assert_array_equal(values[:, 0], values[:, 2])
tiers[variable] = values[:, 0]
assert tiers["school_meal_breakfast_tier"][0] == "FREE"
assert tiers["school_meal_lunch_tier"][0] == "REDUCED"

for variable in ("school_meal_daily_subsidy", "school_meal_paid_daily_subsidy"):
values = simulation.calculate(variable, year).reshape(-1, 3)
np.testing.assert_array_equal(values[:, 0], values[:, 1])
np.testing.assert_array_equal(values[:, 0], values[:, 2])

expected = _main_amounts(
rows,
year,
tiers["school_meal_breakfast_tier"],
tiers["school_meal_lunch_tier"],
)
amounts = {}
for variable in BENEFITS:
values = simulation.calculate(variable, year).reshape(-1, 3)
np.testing.assert_array_equal(values[:, 0], values[:, 1])
np.testing.assert_allclose(
values[:, 0], expected[variable], rtol=0, atol=TOLERANCE
)
np.testing.assert_array_equal(values[:, 2], 0)
assert (values >= 0).all()
assert (values[:, 2] <= values[:, 1]).all()
# Adult-only units and children outside the K-12 age range get no meals.
np.testing.assert_array_equal(values[3:5], 0)
amounts[variable] = values

np.testing.assert_allclose(
amounts["free_school_meals"] + amounts["reduced_price_school_meals"],
amounts["school_meal_net_subsidy"],
rtol=0,
atol=TOLERANCE,
)
assert amounts["free_school_meals"][0, 0] > 0
assert amounts["reduced_price_school_meals"][0, 0] > 0
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,14 @@
is_in_k12_school: [true, true, true, true, true, true]
output:
free_school_meals: [621.00, 1_256.40, 621.00, 498.60, 1_008.00, 498.60]

- name: Take-up gates free school meals even with a supplied net subsidy.
period: 2025
input:
school_meal_breakfast_tier: [FREE, FREE]
school_meal_lunch_tier: [FREE, FREE]
school_meal_net_subsidy: [100, 100]
takes_up_school_meals_if_eligible: [true, false]
output:
# Both meals use the same tier, so its share of the net subsidy is one.
free_school_meals: [100, 0]
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,14 @@
is_in_k12_school: [true, true, true, true, true, true]
output:
reduced_price_school_meals: [1_184.40, 567.00, 0, 936.00, 444.60, 0]

- name: Take-up gates reduced price school meals even with a supplied net subsidy.
period: 2025
input:
school_meal_breakfast_tier: [REDUCED, REDUCED]
school_meal_lunch_tier: [REDUCED, REDUCED]
school_meal_net_subsidy: [100, 100]
takes_up_school_meals_if_eligible: [true, false]
output:
# Both meals use the same tier, so its share of the net subsidy is one.
reduced_price_school_meals: [100, 0]
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,59 @@
school_meal_net_subsidy: [0, 1_065.60, 3_196.80]
free_school_meals: [0, 748.80, 2_246.40]
reduced_price_school_meals: [0, 316.80, 950.40]

- name: School meal take-up defaults to participation.
period: 2025
absolute_error_margin: 0.01
input:
state_code: [FL, FL, FL]
school_meal_fpg_ratio: [0.5, 1.5, 3]
meets_school_meal_categorical_eligibility: [false, false, false]
is_in_k12_school: [true, true, true]
output:
takes_up_school_meals_if_eligible: [true, true, true]
school_meal_tier: [FREE, REDUCED, PAID]
# Free: (2.37 - 0.39 + 4.43 - 0.42) * 180 = 1,078.20.
# Reduced: (2.07 - 0.39 + 4.03 - 0.42) * 180 = 952.20.
# Paid: no subsidy above the paid rate, so 0.
school_meal_net_subsidy: [1078.2, 952.2, 0]
free_school_meals: [1078.2, 0, 0]
reduced_price_school_meals: [0, 952.2, 0]

- name: School meal take-up preserves benefits with explicit participation.
period: 2025
absolute_error_margin: 0.01
input:
state_code: [FL, FL, FL]
school_meal_fpg_ratio: [0.5, 1.5, 3]
meets_school_meal_categorical_eligibility: [false, false, false]
is_in_k12_school: [true, true, true]
takes_up_school_meals_if_eligible: [true, true, true]
output:
school_meal_tier: [FREE, REDUCED, PAID]
# Free: (2.37 - 0.39 + 4.43 - 0.42) * 180 = 1,078.20.
# Reduced: (2.07 - 0.39 + 4.03 - 0.42) * 180 = 952.20.
# Paid: no subsidy above the paid rate, so 0.
school_meal_net_subsidy: [1078.2, 952.2, 0]
free_school_meals: [1078.2, 0, 0]
reduced_price_school_meals: [0, 952.2, 0]

- name: School meal take-up removes benefits when the unit declines participation.
period: 2025
absolute_error_margin: 0.01
input:
state_code: [FL, FL, FL]
school_meal_fpg_ratio: [0.5, 1.5, 3]
meets_school_meal_categorical_eligibility: [false, false, false]
is_in_k12_school: [true, true, true]
takes_up_school_meals_if_eligible: [false, false, false]
output:
school_meal_tier: [FREE, REDUCED, PAID]
# Declining participation removes the free amount:
# (2.37 - 0.39 + 4.43 - 0.42) * 180 = 1,078.20.
# Declining participation removes the reduced-price amount:
# (2.07 - 0.39 + 4.03 - 0.42) * 180 = 952.20.
# Declining participation gives zero in every tier.
school_meal_net_subsidy: [0, 0, 0]
free_school_meals: [0, 0, 0]
reduced_price_school_meals: [0, 0, 0]
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class free_school_meals(Variable):
value_type = float
entity = SPMUnit
definition_period = YEAR
defined_for = "takes_up_school_meals_if_eligible"
label = "free school meals"
unit = USD
documentation = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class reduced_price_school_meals(Variable):
value_type = float
entity = SPMUnit
definition_period = YEAR
defined_for = "takes_up_school_meals_if_eligible"
label = "reduced price school meals"
unit = USD
documentation = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class school_meal_net_subsidy(Variable):
value_type = float
entity = SPMUnit
definition_period = YEAR
defined_for = "takes_up_school_meals_if_eligible"
label = "Free and reduced price school meals"
unit = USD
documentation = "Value of free and reduced price school meal subsidies"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from policyengine_us.model_api import *


class takes_up_school_meals_if_eligible(Variable):
value_type = bool
entity = SPMUnit
label = "Whether an eligible SPM unit takes up school meals"
definition_period = YEAR
default_value = True
documentation = (
"Whether this SPM unit participates in free or reduced-price school "
"breakfast and lunch when eligible. Applies to all K-12 children in "
"the unit. Defaults to true to preserve amounts when callers omit "
"this input. Callers can also set false to represent nonapplication "
"or children who do not eat the meals; the cited regulation addresses "
"formal declines."
)
reference = "https://www.ecfr.gov/current/title-7/subtitle-B/chapter-II/subchapter-A/part-245/section-245.6#p-245.6(c)(6)(iii)"
Loading