From 03c5ba8f922d3892cfdd19cbfbab70fb4dceb81d Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Thu, 12 Nov 2026 00:55:30 -0500 Subject: [PATCH 1/3] Add a school meal take-up input Preserve participation by default and gate annual net, free and reduced-price school meal benefits when users or datasets opt out. Co-Authored-By: Claude Opus 5.5 --- changelog.d/add-school-meals-take-up.added.md | 1 + .../test_school_meal_takeup_invariants.py | 180 ++++++++++++++++++ .../usda/school_meals/free_school_meals.yaml | 11 ++ .../reduced_price_school_meals.yaml | 11 ++ .../school_meals/school_meal_net_subsidy.yaml | 54 ++++++ .../usda/school_meals/free_school_meals.py | 1 + .../reduced_price_school_meals.py | 1 + .../school_meals/school_meal_net_subsidy.py | 1 + .../takes_up_school_meals_if_eligible.py | 16 ++ 9 files changed, 276 insertions(+) create mode 100644 changelog.d/add-school-meals-take-up.added.md create mode 100644 policyengine_us/tests/core/test_school_meal_takeup_invariants.py create mode 100644 policyengine_us/variables/gov/usda/school_meals/takes_up_school_meals_if_eligible.py diff --git a/changelog.d/add-school-meals-take-up.added.md b/changelog.d/add-school-meals-take-up.added.md new file mode 100644 index 00000000000..20ae27b001c --- /dev/null +++ b/changelog.d/add-school-meals-take-up.added.md @@ -0,0 +1 @@ +Added a school-meal take-up input with a true default to preserve existing benefit amounts. diff --git a/policyengine_us/tests/core/test_school_meal_takeup_invariants.py b/policyengine_us/tests/core/test_school_meal_takeup_invariants.py new file mode 100644 index 00000000000..ef70a0149cc --- /dev/null +++ b/policyengine_us/tests/core/test_school_meal_takeup_invariants.py @@ -0,0 +1,180 @@ +"""Check school-meal take-up across generated SPM units. + +Core applies defined_for masks after aggregating people into SPM units. +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): + # Preserve the annual arithmetic from main at 935b24ff4f022e0548876c0bd859195c517a23bb. + # Compute each meal separately to check the share-based benefit 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[breakfast] - breakfast_rates.PAID + lunch_value = lunch_rates[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 diff --git a/policyengine_us/tests/policy/baseline/gov/usda/school_meals/free_school_meals.yaml b/policyengine_us/tests/policy/baseline/gov/usda/school_meals/free_school_meals.yaml index 3ad51321b8b..c70b16c0e7b 100644 --- a/policyengine_us/tests/policy/baseline/gov/usda/school_meals/free_school_meals.yaml +++ b/policyengine_us/tests/policy/baseline/gov/usda/school_meals/free_school_meals.yaml @@ -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] diff --git a/policyengine_us/tests/policy/baseline/gov/usda/school_meals/reduced_price_school_meals.yaml b/policyengine_us/tests/policy/baseline/gov/usda/school_meals/reduced_price_school_meals.yaml index 97871fe0bbe..f59d6e01245 100644 --- a/policyengine_us/tests/policy/baseline/gov/usda/school_meals/reduced_price_school_meals.yaml +++ b/policyengine_us/tests/policy/baseline/gov/usda/school_meals/reduced_price_school_meals.yaml @@ -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] diff --git a/policyengine_us/tests/policy/baseline/gov/usda/school_meals/school_meal_net_subsidy.yaml b/policyengine_us/tests/policy/baseline/gov/usda/school_meals/school_meal_net_subsidy.yaml index 4dcc0042a29..3fb4488d9b2 100644 --- a/policyengine_us/tests/policy/baseline/gov/usda/school_meals/school_meal_net_subsidy.yaml +++ b/policyengine_us/tests/policy/baseline/gov/usda/school_meals/school_meal_net_subsidy.yaml @@ -150,3 +150,57 @@ 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. + # Declining participation gives zero in every tier. + 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. + # Declining participation gives zero in every tier. + 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] + # 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. + # 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] diff --git a/policyengine_us/variables/gov/usda/school_meals/free_school_meals.py b/policyengine_us/variables/gov/usda/school_meals/free_school_meals.py index edc77291533..7fd43469bd5 100644 --- a/policyengine_us/variables/gov/usda/school_meals/free_school_meals.py +++ b/policyengine_us/variables/gov/usda/school_meals/free_school_meals.py @@ -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 = ( diff --git a/policyengine_us/variables/gov/usda/school_meals/reduced_price_school_meals.py b/policyengine_us/variables/gov/usda/school_meals/reduced_price_school_meals.py index c6eef52021b..414e83495f1 100644 --- a/policyengine_us/variables/gov/usda/school_meals/reduced_price_school_meals.py +++ b/policyengine_us/variables/gov/usda/school_meals/reduced_price_school_meals.py @@ -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 = ( diff --git a/policyengine_us/variables/gov/usda/school_meals/school_meal_net_subsidy.py b/policyengine_us/variables/gov/usda/school_meals/school_meal_net_subsidy.py index c70f078b784..c453b644797 100644 --- a/policyengine_us/variables/gov/usda/school_meals/school_meal_net_subsidy.py +++ b/policyengine_us/variables/gov/usda/school_meals/school_meal_net_subsidy.py @@ -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" diff --git a/policyengine_us/variables/gov/usda/school_meals/takes_up_school_meals_if_eligible.py b/policyengine_us/variables/gov/usda/school_meals/takes_up_school_meals_if_eligible.py new file mode 100644 index 00000000000..808c4e17b7c --- /dev/null +++ b/policyengine_us/variables/gov/usda/school_meals/takes_up_school_meals_if_eligible.py @@ -0,0 +1,16 @@ +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." + ) + 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)" From 93d4e5c629de936be1926ceb93eaa6f1cff57f65 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Thu, 12 Nov 2026 01:43:30 -0500 Subject: [PATCH 2/3] Use scalar keys in the school meal invariant reference Convert decoded tiers to Python strings before scalar parameter lookups. Co-Authored-By: Claude Opus 5.5 --- .../tests/core/test_school_meal_takeup_invariants.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/policyengine_us/tests/core/test_school_meal_takeup_invariants.py b/policyengine_us/tests/core/test_school_meal_takeup_invariants.py index ef70a0149cc..d798026fde0 100644 --- a/policyengine_us/tests/core/test_school_meal_takeup_invariants.py +++ b/policyengine_us/tests/core/test_school_meal_takeup_invariants.py @@ -1,6 +1,6 @@ """Check school-meal take-up across generated SPM units. -Core applies defined_for masks after aggregating people into 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. @@ -80,8 +80,8 @@ def _main_amounts(rows, year, 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[breakfast] - breakfast_rates.PAID - lunch_value = lunch_rates[lunch] - lunch_rates.PAID + 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 From f33d52067e369f1424379fcff2ca2c933ef4ebe8 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Sat, 14 Nov 2026 22:45:21 -0500 Subject: [PATCH 3/3] Clarify school meal take-up documentation Distinguish paid-tier zeros from declined benefits and describe the participation input beyond formal declines. Remove the stale commit reference from the invariant test. Co-Authored-By: Claude Opus 5.5 --- .../tests/core/test_school_meal_takeup_invariants.py | 3 +-- .../gov/usda/school_meals/school_meal_net_subsidy.yaml | 10 ++++++---- .../school_meals/takes_up_school_meals_if_eligible.py | 4 +++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/policyengine_us/tests/core/test_school_meal_takeup_invariants.py b/policyengine_us/tests/core/test_school_meal_takeup_invariants.py index d798026fde0..538636c7bbd 100644 --- a/policyengine_us/tests/core/test_school_meal_takeup_invariants.py +++ b/policyengine_us/tests/core/test_school_meal_takeup_invariants.py @@ -72,8 +72,7 @@ def _situation(rows, year): def _main_amounts(rows, year, breakfast_tier, lunch_tier): - # Preserve the annual arithmetic from main at 935b24ff4f022e0548876c0bd859195c517a23bb. - # Compute each meal separately to check the share-based benefit split. + # 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): diff --git a/policyengine_us/tests/policy/baseline/gov/usda/school_meals/school_meal_net_subsidy.yaml b/policyengine_us/tests/policy/baseline/gov/usda/school_meals/school_meal_net_subsidy.yaml index 3fb4488d9b2..455f3e10e8f 100644 --- a/policyengine_us/tests/policy/baseline/gov/usda/school_meals/school_meal_net_subsidy.yaml +++ b/policyengine_us/tests/policy/baseline/gov/usda/school_meals/school_meal_net_subsidy.yaml @@ -164,7 +164,7 @@ 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. - # Declining participation gives zero in every tier. + # 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] @@ -182,7 +182,7 @@ 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. - # Declining participation gives zero in every tier. + # 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] @@ -198,8 +198,10 @@ takes_up_school_meals_if_eligible: [false, false, false] 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. + # 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] diff --git a/policyengine_us/variables/gov/usda/school_meals/takes_up_school_meals_if_eligible.py b/policyengine_us/variables/gov/usda/school_meals/takes_up_school_meals_if_eligible.py index 808c4e17b7c..f645077f972 100644 --- a/policyengine_us/variables/gov/usda/school_meals/takes_up_school_meals_if_eligible.py +++ b/policyengine_us/variables/gov/usda/school_meals/takes_up_school_meals_if_eligible.py @@ -11,6 +11,8 @@ class takes_up_school_meals_if_eligible(Variable): "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." + "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)"